2016-12-22 0:42 GMT+02:00 John Wagenleitner <[email protected]>:
> On Wed, Dec 21, 2016 at 11:58 AM, Dimitar Vassilev < > [email protected]> wrote: > >> Good evening/morning/day, >> Quick one how can I parse a YAML file with groovy and snakeyaml >> >> I have a nested hash YAML like: >> >> pkgcolls: >> pkgcol1: >> software:Foo >> version:baz >> pkgcol2: >> software:baz >> version:Foo >> >> In Ruby I can do something like in the irb/file >> require 'yaml' >> thing = YAML.load_file('some.yml') >> puts thing.inspect >> >> How can I do similar thing in groovy using >> >> import org.yaml.snakeyaml.DumperOptions >> import org.yaml.snakeyaml.Yaml >> >> and the yaml.load(object) function? >> I would like to loop through the YAML structure and figure out which kind >> of loop to use. >> I'm interested in dealing directly with the pkgcol(1-N) attributes >> directly if possible. >> Thanks >> > > > Something like the following could be run in the GroovyConsole > > ``` > @Grab(group='org.yaml', module='snakeyaml', version='1.17') > import org.yaml.snakeyaml.* > > String yamlText = ''' > pkgcolls: > pkgcol1: > software:Foo > version:baz > pkgcol2: > software:baz > version:Foo > ''' > > Yaml yaml = new Yaml() > def result = yaml.load(yamlText) > // or from a file > // new File('/tmp/test.yml').withReader('UTF-8') { reader -> > // def result = yaml.load(reader) > // .... > // } > > result.pkgcolls.each { > assert it.key == 'pkgcol1' || it.key == 'pkgcol2' > } > > println result.inspect() > ``` > Thanks John, I got a hang of it. My next question is how one can expand a statically defined directory variable with values from the loop so that subdirs are created? I need to install my software in certain locations. The main directory is called pkgHome. I would like to have a separate subdirectory for each software collection. In the current state only the first statement is caught and only 1 directory is created. If you or anyone else can give a hand on this one, will be appreciated. I can donate the amount of 2-3 beers via Paypal or to a software project of your choice. The real structure I have is toolgroups: toolgroup1: tools: software: version toolgroup2: tools: software: version My sample code is below ---------------------------- ``````` /definitions of the placeholders toolsYmlFile = project.file(".pkg/tools_${os.name}.yml") pkgHome = project.file(".pkgdir/${os.name}") pkgdataFile = project.file("${pkgHome}/.pkgdata") def commandList = [] ```````` /* * * yaml parsing */ 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 << ("install " + toolVersion.toString() + " " + toolName.toString()) println commandList } } this.command = new pkgToolsCommand(commandList, pkgHome, project.projectDir) ```````` ----------------------
