如何将Closure解析为ConfigSlurper

时间:2013-03-14 11:08:30

标签: groovy

背景。我正在使用gradle脚本,并希望使用ConfigSlurper内联一些默认配置。我已经能够从文件中解析配置,但似乎无法使内联配置起作用。

我想做的是这样的事情:

myScript = {
   some {
     random {
        stuff = "You can access"
     }
   }
}

groovy.lang.Script script = new groovy.lang.Script() {
   @Override
   Object run() {
      theScript.call()
   }
}

ConfigSlurper slurper = new ConfigSlurper()
slurper.setBinding(["theScript": myScript])
ConfigObject parse = slurper.parse(script)
assert parse.some.random.stuff == "You can access"

这不起作用。它说

Caught: groovy.lang.MissingMethodException: No signature of method: scriptStuff.some() is applicable for argument types: (scriptStuff$_run_closure1_closure2) values: [scriptStuff$_run_closure1_closure2@2aca5165]
Possible solutions: dump(), use([Ljava.lang.Object;), sleep(long), wait(), run(), run()
groovy.lang.MissingMethodException: No signature of method: scriptStuff.some() is applicable for argument types: (scriptStuff$_run_closure1_closure2) values: [scriptStuff$_run_closure1_closure2@2aca5165]
Possible solutions: dump(), use([Ljava.lang.Object;), sleep(long), wait(), run(), run()
    at scriptStuff$_run_closure1.doCall(scriptStuff.groovy:2)
    at scriptStuff$_run_closure1.doCall(scriptStuff.groovy)
    at scriptStuff$1.run(scriptStuff.groovy:12)
    at scriptStuff.run(scriptStuff.groovy:18)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

(scriptStuff指我运行粘贴脚本的文件:scriptStuff.groovy)

所以我不确定这是正确的方向。实际上如果你可以直接将封闭物传递给Slurper会更好。 但是Slurper只接受脚本,脚本文件和字符串。 另一个方向是将clusure转换为String。这可能吗?

任何解决方案都会很好。

更新 只是想详细说明一下我的用例。 我正在为我的gradle脚本创建一个插件。作为一个用户,我想写这样的东西:

projectConfiguration {
   //this one is easy to parse with ConfigSlurper
   confFile = "some/path/to/configuration.groovy"

   //this is difficult to parse
   inlineConf = {
      some {
         inlined {
            configuration = "Cool!"
         }
      }
   }
}

所以尽管我可以使用字符串表示法解析它,但上面的符号看起来很多,更像是gradle脚本的其余部分。 另一方面,我不介意解析clojure的代码丑陋。它只是插件中的用户。重要的是面向零件的用户是干净的。

2 个答案:

答案 0 :(得分:3)

使用Closure解决策略将DELEGATE_FIRST上的代理人设置为脚本:

class ClosureScript extends Script {
    Closure closure
    def run() {
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        closure.delegate = this
        closure.call()
    }
}
script = new ClosureScript(closure: myScript)

请参阅Creating a Groovy ConfigObject from a Closure

答案 1 :(得分:1)

您可以使用ConfigSlurper来解析字符串,如下所示:

def myScript = '''
   some {
     random {
        stuff = "You can access"
     }
   }
'''
def config = new ConfigSlurper().parse(myScript)
assert config.some.random.stuff == "You can access"

如果你真的想要传递一个脚本,那么你必须扩展脚本,因为它是一个抽象类。例如:

class MyConfiguration extends Script {  
    public Object run() { 
       some {
          random {
             stuff = "You can access"
          }
       }
    }
}

def config = new ConfigSlurper().parse(new MyConfiguration())
assert config.some.random.stuff=="You can access"