1
我建立在Groovy一個DSL應該允許使用DSL內字符串插值字符串插值內變量:如何訪問一個Groovy DSL
def dsl = """
def projectName = "test"
project("${projectName}/123") {
task {
}
}
"""
不幸的是,使用下面的代碼時,解釋以上DSL,Groovy的解釋是不知道的projectName
(併產生MissingPropertyException):
class ProjectSpec {
def name
void task(closure) {
}
}
binding.project = { name, closure ->
def project = new ProjectSpec()
project.name = name
def code = closure.rehydrate(project, this, this)
code.resolveStrategy = Closure.DELEGATE_ONLY
code()
project
}
def shell = new GroovyShell(binding)
Script script = shell.parse(dsl)
project = script.run()
println project.name
我怎樣才能做到這一點?我想我必須推遲GString的評估。我必須爲此使用ASTTransformation嗎?
感謝,
邁克爾