Skip to content
Permalink
Browse files

[JENKINS-26135]

Global variable should be usable as a function, just like in Groovy
Closure property can be called like a function.
  • Loading branch information...
kohsuke committed May 22, 2015
1 parent 90d045b commit 9cdadd25a63ea5f586f275741cd077f9ab2e7682
@@ -103,7 +103,8 @@ public void userDefinedGlobalVariable() throws Exception {
FileUtils.writeStringToFile(f,
"def hello(name) { node { sh \"echo Hello ${name}\" } } \n" +
"def foo(x) { this.x = x+'-set'; } \n" +
"def bar() { return x+'-get' } \n");
"def bar() { return x+'-get' } \n" +
"def call(a,b) { echo \"call($a,$b)\" }");

// simulate the effect of push
uvl.rebuild();
@@ -113,13 +114,15 @@ public void userDefinedGlobalVariable() throws Exception {
p.setDefinition(new CpsFlowDefinition(
"acme.hello('Workflow');" +
"acme.foo('seed');" +
"echo '['+acme.bar()+']'"));
"echo '['+acme.bar()+']';"+
"acme(1,2);"));

// build this workflow
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));

story.j.assertLogContains("Hello Workflow", b);
story.j.assertLogContains("[seed-set-get]", b);
story.j.assertLogContains("call(1,2)", b);
}
});
}
@@ -38,6 +38,7 @@
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.DefaultGroovyStaticMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.InvokerInvocationException;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*;
@@ -94,6 +95,20 @@ public CpsScript() throws IOException {
*/
@Override
public final Object invokeMethod(String name, Object args) {
// if global variables are defined by that name, try to call it.
// the 'call' convention comes from Closure
for (GlobalVariable v : GlobalVariable.ALL) {
if (v.getName().equals(name)) {
try {
Object o = v.getValue(this);
return InvokerHelper.getMetaClass(o).invokeMethod(o,"call",args);
} catch (Exception x) {
throw new InvokerInvocationException(x);
}
}
}

// otherwise try Step impls.
DSL dsl = (DSL) getBinding().getVariable(STEPS_VAR);
return dsl.invokeMethod(name,args);
}

0 comments on commit 9cdadd2

Please sign in to comment.
You can’t perform that action at this time.