[英]Groovy ConfigSlurper: how to modify a closure in a config?
I want to modify a grails BuildConfig.groovy: 我想修改grails BuildConfig.groovy:
grails.project.dependency.resolution = {
plugins {
build ":tomcat:7.0.50"
// plugins for the compile step
compile ":scaffolding:2.0.1"
compile ':cache:1.1.1'
// plugins needed at runtime but not for compilation
runtime ":hibernate:3.6.10.7" // or ":hibernate4:4.1.11.6"
runtime ":database-migration:1.3.8"
runtime ":jquery:1.10.2.2"
runtime ":resources:1.2.1"
}
}
Especially I want to add a plugin and modify another one. 特别是我想添加一个插件并修改另一个。
I tried it with ConfigSlurper
: 我尝试了
ConfigSlurper
:
def conf = new ConfigSlurper().parse(new File(buildConfig).toURL())
def plugins = conf.grails.project.dependency.resolution
println "found plugins: $plugins"
plugins.each {
println it
}
The access to conf.grails.project.dependency
works fine but conf.grails.project.dependency.resolution
is a closure and I don't know how to access or even modify this section. 对
conf.grails.project.dependency
的访问正常,但conf.grails.project.dependency.resolution
是一个闭包,我不知道如何访问或修改此部分。
I don't know grails enough to make some opinionated guess, but it seems to me this config file doesn't conform to ConfigSlurper
expected syntax . 我不了解grails足以做出一些有根据的猜测,但是在我看来,此配置文件不符合
ConfigSlurper
预期的语法 。 If what you want to parse isn't very long, you can try intercepting it yourself: 如果要解析的内容不是很长,可以尝试自己拦截它:
class PluginConfig {
def compileLibs = []
def runtimeLibs = []
def version
def build(version) { this.version = version }
def compile(lib) { compileLibs << lib }
def runtime(lib) { runtimeLibs << lib }
}
def conf = new ConfigSlurper().parse(new File("BuildConfig.groovy").toURL())
def plugins = conf.grails.project.dependency.resolution
def lib = new PluginConfig()
plugins.delegate = lib // magick!!
plugins()
assert lib.compileLibs == [":scaffolding:2.0.1", ':cache:1.1.1']
assert lib.runtimeLibs == [
":hibernate:3.6.10.7",
":database-migration:1.3.8",
":jquery:1.10.2.2",
":resources:1.2.1"
]
assert lib.version == ":tomcat:7.0.50"
No idea how to rewrite this to a file (easily) after the change, though. 不过,不知道如何在更改后将其重写为文件(轻松)。 Maybe using Grails own config parser might be a better idea;
也许使用Grails自己的配置解析器可能更好。 it must have a representation of the config when it parses the file.
解析文件时,它必须具有配置的表示形式。
As far as I know there is no perfect way for doing this: all available parsers/slurpers drop the comments of your configuration. 据我所知,没有完美的方法可以执行此操作:所有可用的解析器/提取器都会删除您的配置注释。 So even if you would modify the result from the config slurper and write it back, it wouldn't be what you are looking for.
因此,即使您要修改config slurper的结果并将其写回,也不是您要找的东西。
You also have t consider that people might use variables for version numbers and other unexpected stuff. 您还没有考虑到人们可能将变量用于版本号和其他意外内容。
So I guess the best way to modify the config is to use some regular expressions and hope that you users have a standard configuration... 因此,我猜想修改配置的最好方法是使用一些正则表达式,并希望您的用户具有标准配置...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.