Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
[JENKINS-26135]
Browse files Browse the repository at this point in the history
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 9cdadd2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Expand Up @@ -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();
Expand All @@ -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);
}
});
}
Expand Down
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9cdadd2

Please sign in to comment.