OK, now we get that out of the way, let's look at the following example
fun = load scripts/fun.groovyand in fun.groovy, we have
fun.play
now if we want to add a variable so we can have different kind of "fun"
def play() {
sh "echo play"
}
return this
i was hoping i can load "fun.groovy" twice,
def type
def play() {
sh "echo playing $type"
}
def setType(string _type) {
type = _type
}
return this
fun = load scripts/fun.groovyi ended up getting
fun.setType('basketball')
more_fun = load scripts/fun.groovy
more_fun.setType('ping pong')
fun.play
more_fun.play
play ping pongapparently there's only one instance loaded. to get around that, i then try
play ping pong
class FUN {so we can now do this
def type
def dsl
def play() {
dsl.sh "echo play $type"
}
FUN(_dsl, string _type) {
type = _type
dsl = _dsl
}
}
def getFun(dsl,type) {
return new FUN(dsl,type)
}
return this
fun = load scripts/fun.groovynow we'll get
basketball = fun.getFun(this,'basketball')
pingpong = fun.getFun(this,'ping pong')
basketball.play
pingpong.play
play basketballnote that i've pass in "this" from the pipeline to the FUN class. The reason is the FUN class is no longer in the pipeline scope. To use any features/functions from the pipeline, we'll have to reference the pipeline object.
play ping pong
No comments:
Post a Comment