I have 2 groovy scripts. Both are under /scripts
.
From script npm.groovy
I want to load script steps.groovy
and dynamically call a function based on a Map
that is provided.
npm.groovy
:
void execute(){
stepsApi = load("scripts/steps.groovy")
Map steps = [upload:[number: "1", type: "module"]]
steps.each { step, params ->
stepsApi[step](params) <- Is this line correct?
}
}
step.groovy
:
void upload(Map params){
// ... some code here
}
I wonder if npm.groovy
is correctly invoking the function from steps.groovy
. Also, as steps
is a Map, and I iterate over it, is params a Map? If not, what should the type be in upload
function?
Below is the error when the above is run:
groovy.lang.MissingPropertyException: No such property: upload for class: groovy.variant.npm.Script6
Found the solution. To dynamically call a function we can use the below:
void execute(){
stepsApi = load("scripts/steps.groovy")
Map steps = [upload:[number: "1", type: "module"]]
steps.each { step, params ->
stepsApi."$step"(params)
}
}