How to loop through a hash map to create sub directories for software installation


(Dimitar Vassilev) #1

Good morning,
I’m working on a Gradle task that parses an YAML file and generates lists of commands for software to be installed in separate sub directories.
The YAML structure is as follows
toolgroups:
toolgroup1:
software:
version:

I have the following code that handles the main directory structure

    import org.gradle.api.GradleException
    import org.gradle.api.tasks.TaskAction
    import org.yaml.snakeyaml.DumperOptions
    import org.yaml.snakeyaml.Yaml
    import org.gradle.api.DefaultTask
    import java.nio.file.Files
    import java.nio.file.Path

toolsYmlFile = project.file(".pkg/tools_${os.name}.yml")
pkgHome = project.file(".pkgdir/${os.name}")
pkgdataFile = project.file("${pkgHome}/.pkgdata")


yaml.load(toolsFile).toolgroups.each() {
          toolGroup ->
            def groupName = toolGroup.key
            def  pkgGroupHome = pkgHome.toString() + "/" + groupName
            File pkgHome = new File (pkgHome.toString(), groupName.toString())
            File pkgdataFile = new File ("${pkgHome}/.pkgdata")

              println "toolGroup: "+groupName
              println groupName
            if ( toolGroup.value.tools == null) throw new Exception("YAML toolgroups are missing!")
            toolGroup.value.tools.each() {
              tool ->
                 println "\ttool:"+tool.key
                 def toolName = tool.key
                 def toolVersion = tool.value.version
                 println "\ttoolName:"+toolName + "\ttoolVersion:"+toolVersion

                 if ( toolVersion == null) throw new Exception("Software cannot be installed due to missing version.")
                  if(pkgdataFile.exists()) {
                       commandList << "rm -f $(pkgdataFile)"
                       } else {
                           commandList << "mkdir -p ${pkgdataFile}"
                         }
                       commandList << ("installcmd " + toolVersion.toString() + " " + toolName.toString())
                       println commandList
                    }
                  }
                  this.command = new pkgToolsCommand(commandList, pkgHome, project.projectDir)

Can someone guide me what is the proper way to alter the pkgGroupHome variable so it iterates over each key and it does not stop on the first one?
Many thanks!


(Dimitar Vassilev) #2

Hello the above code wil work.

if(pkgdataFile.exists()) {
   commandList << "rm -f $(pkgdataFile)"
       } else {
             commandList << "mkdir -p ${pkgdataFile}"
   }

the problem I was facing is that the array of commands is overwritten each time and the job scheduler has different default shells on the Linux and the Unix build machines.
Hope it helps.