Skip to content

Commit

Permalink
fix bug: the scripts output is not displayed/rendered in the Scriptle…
Browse files Browse the repository at this point in the history
…r UI: Scriptler > Run Script
  • Loading branch information
hayato1980 committed Jun 8, 2012
1 parent e00d375 commit d44120b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
for (Parameter parameter : parameters) {
expandedParams.add(new Parameter(parameter.getName(), TokenMacro.expandAll(build, listener, parameter.getValue())));
}
PrintWriter pw = new PrintWriter(listener.getLogger());
final Object output = launcher.getChannel().call(new GroovyScript(script.script, expandedParams.toArray(new Parameter[expandedParams.size()]), true, pw));
final Object output = launcher.getChannel().call(new GroovyScript(script.script, expandedParams.toArray(new Parameter[expandedParams.size()]), true, listener.getLogger()));
if (output instanceof Boolean && Boolean.FALSE.equals(output)) {
isOk = false;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.jenkinsci.plugins.scriptler.util;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.remoting.DelegatingCallable;

import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

Expand All @@ -19,16 +21,16 @@ public class GroovyScript implements DelegatingCallable<Object, RuntimeException
private final String script;
private final Parameter[] parameters;
private final boolean failWithException;
private final PrintWriter pw;
private final PrintStream ps;
private transient ClassLoader cl;

private static final String PW_PARAM_VARIABLE = "out";

public GroovyScript(String script, Parameter[] parameters, boolean failWithException, PrintWriter pw) {
public GroovyScript(String script, Parameter[] parameters, boolean failWithException, PrintStream ps) {
this.script = script;
this.parameters = parameters;
this.failWithException = failWithException;
this.pw = pw;
this.ps = ps;
cl = getClassLoader();
}

Expand All @@ -41,6 +43,7 @@ public Object call() throws RuntimeException {
if (cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}
PrintWriter pw = new PrintWriter(ps);
GroovyShell shell = new GroovyShell(cl);

for (Parameter param : parameters) {
Expand All @@ -51,7 +54,7 @@ public Object call() throws RuntimeException {
shell.setVariable(paramName, param.getValue());
}
}
shell.setVariable(PW_PARAM_VARIABLE, pw);
shell.setVariable(PW_PARAM_VARIABLE, ps);
try {
Object output = shell.evaluate(script);
if (output != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import hudson.model.Computer;
import hudson.model.Hudson;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -88,16 +86,15 @@ public static String runScript(String[] slaves, String scriptTxt, Parameter[] pa
public static String runScript(String node, String scriptTxt, Parameter[] parameters) throws IOException, ServletException {

Object output = "[no output]";
ByteArrayOutputStream sos = new ByteArrayOutputStream();
if (node != null && scriptTxt != null) {

try {

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
PrintStream ps = new PrintStream(sos);
Computer comp = Hudson.getInstance().getComputer(node);
if (comp == null && "(master)".equals(node)) {
// output = RemotingDiagnostics.executeGroovy(scriptTxt, MasterComputer.localChannel);
output = MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false, pw));
output = MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false, ps));
} else if (comp == null) {
output = Messages.node_not_found(node) + "\n";
} else {
Expand All @@ -106,15 +103,15 @@ public static String runScript(String node, String scriptTxt, Parameter[] parame
}

else {
output = comp.getChannel().call(new GroovyScript(scriptTxt, parameters, false, pw));
output = comp.getChannel().call(new GroovyScript(scriptTxt, parameters, false, ps));
}
}

} catch (InterruptedException e) {
throw new ServletException(e);
}
}
return output.toString();
return sos.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import static org.junit.Assert.assertEquals;

import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.jenkinsci.plugins.scriptler.config.Parameter;
import org.junit.Test;
Expand All @@ -11,37 +12,46 @@ public class GroovyScriptTest {

@Test
public void scriptReturnFalse() {
GroovyScript gs = new GroovyScript("return false", new Parameter[0], true, new PrintWriter(System.out)) {
ByteArrayOutputStream sos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(sos);
GroovyScript gs = new GroovyScript("return false", new Parameter[0], true, ps) {
@Override
public ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
};
Object result = gs.call();
assertEquals("", sos.toString());
assertEquals(false, result);
}

@Test
public void scriptReturnTrue() {
GroovyScript gs = new GroovyScript("return true", new Parameter[0], true, new PrintWriter(System.out)) {
ByteArrayOutputStream sos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(sos);
GroovyScript gs = new GroovyScript("return true", new Parameter[0], true, System.out) {
@Override
public ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
};
Object result = gs.call();
assertEquals("", sos.toString());
assertEquals(true, result);
}

@Test
public void helloWorld() {
GroovyScript gs = new GroovyScript("out.print(\"HelloWorld\")", new Parameter[0], true, new PrintWriter(System.out)) {
ByteArrayOutputStream sos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(sos);
GroovyScript gs = new GroovyScript("out.print(\"HelloWorld\")", new Parameter[0], true, ps) {
@Override
public ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
};
Object result = gs.call();
assertEquals("HelloWorld", sos.toString());
assertEquals("", result);
}
}

0 comments on commit d44120b

Please sign in to comment.