IntelliJ GSDL:使用可选参数定义方法

IntelliJ GSDL: Define method with optional arguments

我有 Jenkins 管道 shared library,它指定了一个 全局变量 foo,它提供了两种方法。 其中一个没有参数,另一个有一个可选参数:

/vars/foo.groovy

def getBarOne() {
    //...
}

def getBarTwo(String value = '') {
    //...
}

现在我想提供一个 IntellJ GSDL 文件,它支持对这两种方法有用的代码完成。 (我的 Jenkins 提供的 GSDL 只包含全局变量的定义,但不包含它的方法,所以我尝试添加它。)

pipeline.gsdl(詹金斯)

//The global script scope
def ctx = context(scope: scriptScope())
contributor(ctx) {
//...
property(name: 'foo', type: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable')
}
//..

pipeline.gsdl(被我拉皮条了)

//The global script scope
def ctx = context(scope: scriptScope())
contributor(ctx) {
//...
property(name: 'foo', type: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable')
}
def uservarCtx = context(ctype: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable')
contributor (uservarCtx) {
    method name: 'getBarOne', type: 'java.lang.String', params: [:]
    method name: 'getBarTwo', params: [value:'java.lang.String'], type: 'List<String>'
}
//..

到目前为止一切顺利。
然而,我的 Jenkinsfile 中的代码完成并不完全令人满意,因为它表明

对于getBarOne(),它同时建议.barOne.getBarOne();对于 getBarTwo(..) 仅建议 .getBarTwo(String value),尽管该参数是可选的。

我如何在 GDSL 文件中指定该参数是可选的,以便我得到所有三个(有效 groovy)选项的建议:barTwogetBarTwo()getBarTwo(String value)? (不幸的是 “GDSL AWESOMENESS” Series 没有帮助。)

要提供所有三个选项,必须在 GDSL 文件中指定两个方法签名。 一个带有(可选)参数,一个没有它:

pipeline.gdsl

//...
def uservarCtx = context(ctype: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable')
contributor (uservarCtx) {
    method name: 'getBarOne', type: 'java.lang.String', params: [:]
    method name: 'getBarTwo', params: [:], type: 'List<String>'     //<---
    method name: 'getBarTwo', params: [value:'java.lang.String'], type: 'List<String>'
}

自动完成建议:

奖励曲目:多个全局变量

因为我不仅有一个全局变量,还有两个,所以我也希望有自动完成功能来支持它。

这样做的诀窍是,为您的全局变量指定不同的类型:

pipeline.gsdl

//The global script scope
def ctx = context(scope: scriptScope())
contributor(ctx) {
//...
property(name: 'foo', type: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.Foo')
property(name: 'bar', type: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.Bar')
}
def varCtxFoo = context(ctype: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.Foo')
contributor (varCtxFoo) {
    //...
}
def varCtxBar = context(ctype: 'org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.Bar')
contributor (varCtxBar) {
    //...
}
//..

请注意带有类型定义的 UserDefinedGlobalVariable 类型的 .Foo.Bar 后缀。