Groovy: 如何从另一个闭包调用顶级作用域中的闭包
Groovy: how to call closure in top scope from another closure
我试图将使用 Jenkins Job DSL 插件的代码分解成可重用的部分,我怀疑我的问题是 Groovy 的通用问题,而不是 Jenkins 特定的。例如,我想重用这个块的部分内容:
freeStyleJob() {
//generic stuff
name "something"
description "something else"
//custom stuff
scm {
svn {
//etc....
}
}
}
通过将名称和描述放在实用方法中(显然我想做的不仅仅是在现实生活中)。但是,我找不到为当前范围创建闭包的正确语法。这是我认为它应该看起来的样子:
def jobCommonItems() {
return {
//generic stuff
name "something"
description "something else"
}
}
freeStyleJob() {
jobCommonItems().call()
//custom stuff
scm {
svn {
//etc....
}
}
}
(也许在某处有一个 closure.delegate = 这个)
但是,这不适用于闭包。它适用于方法,如下所示:https://dzone.com/articles/groovy-closures-owner-delegate
为了说明,这里是一个显示可能语法的三种组合的测试:
String myString = "Top Level: string"
def myMethod() {
println "Top Level: Method"
}
def myClosure = { println "Top Level: Class"}
class MyClass1 {
String myString = "Class1: String"
def myMethod() {
println "Class1: Method"
}
def myClosure = { println "Class1: Closure"}
}
class MyClass2 {
String myString = "Class2: String"
def myMethod() {
println "Class2: Method"
}
def myClosure = { println "Class2: Closure"}
}
class MyClass {
def closure = {
println "In-Class generated closure begins, delegate is ${delegate}"
myMethod()
myClosure()
println myString
}
}
def closure = new MyClass().closure
closure.delegate = new MyClass1()
closure()
closure = new MyClass().closure
closure.delegate = new MyClass2()
closure()
// This fails - it can find the top level method, but not closure or string
closure.delegate = this
closure()
def methodMissing(String methodName, args) {
println "Method not found in class ${this} by name ${methodName}"
}
我得到一个错误,闭包不在主要 class 中(即测试 test.groovy):在 class test@60611244 中找不到方法,名称为 myClosure
我尝试将委托更改为 "this",我尝试更改查找策略等。我可能遗漏了一些基本的东西。
似乎一种解决方案是像这样反转关系,并将 "this" 作为上下文传递以查找 DSL 顶级闭包。
class Utils {
static def makeMeABasicJob(def context) {
context.freeStyleJob() {
//generic stuff
name "something"
description "something else"
}
}
}
def job1 = Utils.makeMeABasicJob(this) //Passing the groovy file class as the resolution context
job1.with({
//custom stuff
scm {
svn {
//etc....
}
}
})
这样的怎么样?
def jobCommonItems(job) {
job.name = "something"
job.description = "something else"
}
freeStyleJob() {
jobCommonItems(this)
//custom stuff
scm {
svn {
//etc....
}
}
}
作业工厂方法类似于 freeStyleJob
return 对象,可用于使用 with
方法应用更多配置。该方法需要一个闭包参数,该参数与传递给 freeStyleJob
方法的闭包具有相同的属性。
def basicConfiguration() {
return {
description('foo')
scm {
// whatever
}
}
}
def myJob = freeStyleJob('example') {
publishers {
// more config
}
}
myJob.with basicConfiguration()
脚本本身是 DslFactory
的一个实例,它是包含例如freeStyleJob
方法。您可以将该对象传递给 类 或方法以使用 freeStyleJob
.
def myJobFactory(def dslFactory, def jobName) {
dslFactory.freeStyleJob(jobName) {
description('foo')
}
}
def myJob = myJobFactory(this, 'example')
然后您可以使用 myJob
对象通过 with
应用进一步的配置。
当试图从我的 Jenkins DSL groovy 脚本中提取通用逻辑时,我最初使用将 job 对象传递给通用静态助手的样式,并且然后做 job.with{...}
类似于 daspilker 解决方案。
但是我发现使用委托更直接一些。
例如:
freeStyleJob('name') {
jobDesc(delegate, 'something')
jobSCM(delegate, 'git@gitlab.com:MyGroup/MyProject.git', 'master')
}
def jobDesc(def context, def descText) {
context.description(descText)
}
def jobSCM(def context, def repoURL, def branchName)
{
context.scm {
git {
remote {
url(repoURL)
}
branch('refs/heads/' + branchName)
}
}
}
jobDesc 和 jobSCM 方法可以作为静态助手移动到单独的实用程序 class,然后您将导入您的 DSL groovy 脚本。
我试图将使用 Jenkins Job DSL 插件的代码分解成可重用的部分,我怀疑我的问题是 Groovy 的通用问题,而不是 Jenkins 特定的。例如,我想重用这个块的部分内容:
freeStyleJob() {
//generic stuff
name "something"
description "something else"
//custom stuff
scm {
svn {
//etc....
}
}
}
通过将名称和描述放在实用方法中(显然我想做的不仅仅是在现实生活中)。但是,我找不到为当前范围创建闭包的正确语法。这是我认为它应该看起来的样子:
def jobCommonItems() {
return {
//generic stuff
name "something"
description "something else"
}
}
freeStyleJob() {
jobCommonItems().call()
//custom stuff
scm {
svn {
//etc....
}
}
}
(也许在某处有一个 closure.delegate = 这个)
但是,这不适用于闭包。它适用于方法,如下所示:https://dzone.com/articles/groovy-closures-owner-delegate
为了说明,这里是一个显示可能语法的三种组合的测试:
String myString = "Top Level: string"
def myMethod() {
println "Top Level: Method"
}
def myClosure = { println "Top Level: Class"}
class MyClass1 {
String myString = "Class1: String"
def myMethod() {
println "Class1: Method"
}
def myClosure = { println "Class1: Closure"}
}
class MyClass2 {
String myString = "Class2: String"
def myMethod() {
println "Class2: Method"
}
def myClosure = { println "Class2: Closure"}
}
class MyClass {
def closure = {
println "In-Class generated closure begins, delegate is ${delegate}"
myMethod()
myClosure()
println myString
}
}
def closure = new MyClass().closure
closure.delegate = new MyClass1()
closure()
closure = new MyClass().closure
closure.delegate = new MyClass2()
closure()
// This fails - it can find the top level method, but not closure or string
closure.delegate = this
closure()
def methodMissing(String methodName, args) {
println "Method not found in class ${this} by name ${methodName}"
}
我得到一个错误,闭包不在主要 class 中(即测试 test.groovy):在 class test@60611244 中找不到方法,名称为 myClosure
我尝试将委托更改为 "this",我尝试更改查找策略等。我可能遗漏了一些基本的东西。
似乎一种解决方案是像这样反转关系,并将 "this" 作为上下文传递以查找 DSL 顶级闭包。
class Utils {
static def makeMeABasicJob(def context) {
context.freeStyleJob() {
//generic stuff
name "something"
description "something else"
}
}
}
def job1 = Utils.makeMeABasicJob(this) //Passing the groovy file class as the resolution context
job1.with({
//custom stuff
scm {
svn {
//etc....
}
}
})
这样的怎么样?
def jobCommonItems(job) {
job.name = "something"
job.description = "something else"
}
freeStyleJob() {
jobCommonItems(this)
//custom stuff
scm {
svn {
//etc....
}
}
}
作业工厂方法类似于 freeStyleJob
return 对象,可用于使用 with
方法应用更多配置。该方法需要一个闭包参数,该参数与传递给 freeStyleJob
方法的闭包具有相同的属性。
def basicConfiguration() {
return {
description('foo')
scm {
// whatever
}
}
}
def myJob = freeStyleJob('example') {
publishers {
// more config
}
}
myJob.with basicConfiguration()
脚本本身是 DslFactory
的一个实例,它是包含例如freeStyleJob
方法。您可以将该对象传递给 类 或方法以使用 freeStyleJob
.
def myJobFactory(def dslFactory, def jobName) {
dslFactory.freeStyleJob(jobName) {
description('foo')
}
}
def myJob = myJobFactory(this, 'example')
然后您可以使用 myJob
对象通过 with
应用进一步的配置。
当试图从我的 Jenkins DSL groovy 脚本中提取通用逻辑时,我最初使用将 job 对象传递给通用静态助手的样式,并且然后做 job.with{...}
类似于 daspilker 解决方案。
但是我发现使用委托更直接一些。 例如:
freeStyleJob('name') {
jobDesc(delegate, 'something')
jobSCM(delegate, 'git@gitlab.com:MyGroup/MyProject.git', 'master')
}
def jobDesc(def context, def descText) {
context.description(descText)
}
def jobSCM(def context, def repoURL, def branchName)
{
context.scm {
git {
remote {
url(repoURL)
}
branch('refs/heads/' + branchName)
}
}
}
jobDesc 和 jobSCM 方法可以作为静态助手移动到单独的实用程序 class,然后您将导入您的 DSL groovy 脚本。