Skip to content
Permalink
Browse files

eval properties. (JENKINS-7750)

  • Loading branch information...
kiy0taka committed May 22, 2013
1 parent fa62b9a commit 41bc755378429754a99b37e9357053c3b57a0b71
@@ -1,7 +1,6 @@
package com.g2one.hudson.grails;

import java.io.IOException;
import java.util.*;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
@@ -14,10 +13,8 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.console.ConsoleNote;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Hudson;
@@ -26,16 +23,13 @@
import hudson.util.ArgumentListBuilder;
import hudson.util.VariableResolver;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

@@ -247,20 +241,20 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
args.addKeyValuePairs("-D", build.getBuildVariables());
Map systemProperties = new HashMap();
if (grailsWorkDir != null && !"".equals(grailsWorkDir.trim())) {
systemProperties.put("grails.work.dir", evalTarget(env, grailsWorkDir.trim()));
systemProperties.put("grails.work.dir", eval(env, grailsWorkDir));
} else {
systemProperties.put("grails.work.dir", build.getWorkspace().toURI().getPath() + "/target");
}
if (projectWorkDir != null && !"".equals(projectWorkDir.trim())) {
systemProperties.put("grails.project.work.dir", evalTarget(env, projectWorkDir.trim()));
systemProperties.put("grails.project.work.dir", eval(env, projectWorkDir));
}
if (serverPort != null && !"".equals(serverPort.trim())) {
systemProperties.put("server.port", evalTarget(env, serverPort.trim()));
systemProperties.put("server.port", eval(env, serverPort));
}
if (systemProperties.size() > 0) {
args.addKeyValuePairs("-D", systemProperties);
}
args.addKeyValuePairsFromPropertyString("-D", properties, build.getBuildVariableResolver());
args.addKeyValuePairsFromPropertyString("-D", eval(env, properties), build.getBuildVariableResolver());

args.add(target);
addArgument("--non-interactive", nonInteractive, args, env, targetsAndArgs);
@@ -303,7 +297,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
protected void addArgument(String option, Boolean optionEnabled, ArgumentListBuilder args, EnvVars env, String[] targetsAndArgs) {
boolean foundArgument = false;
for (int i = 1; i < targetsAndArgs.length; i++) {
String arg = evalTarget(env, targetsAndArgs[i]);
String arg = eval(env, targetsAndArgs[i]);
if(option.equals(arg)) {
foundArgument = true;
}
@@ -335,18 +329,27 @@ private FilePath getBasePath(AbstractBuild<?, ?> build) {
* @return The target with evaluated environment vars
*/
@SuppressWarnings({"StaticMethodOnlyUsedInOneClass", "TypeMayBeWeakened"})
static String evalTarget(Map<String, String> env, String target) {
target = Util.replaceMacro(target, new VariableResolver.ByMap<String>(env));
Binding binding = new Binding();
binding.setVariable("env", env);
binding.setVariable("sys", System.getProperties());
GroovyShell shell = new GroovyShell(binding);
Object result = shell.evaluate("return \"" + target + "\"");
if (result == null) {
return target;
static String eval(Map<String, String> env, String target) {
List<String> result = new ArrayList<String>();
if (target == null) {
return null;
} else {
return result.toString().trim();
target = target.trim();
}
for (String s : target.split("\r?\n")) {
s = Util.replaceMacro(s, new VariableResolver.ByMap<String>(env));
Binding binding = new Binding();
binding.setVariable("env", env);
binding.setVariable("sys", System.getProperties());
GroovyShell shell = new GroovyShell(binding);
Object value = shell.evaluate("return \"" + s + "\"");
if (value == null) {
result.add(s);
} else {
result.add(value.toString().trim());
}
}
return StringUtils.join(result, "\n");
}

protected List<String[]> getTargetsToRun(EnvVars env) {
@@ -364,7 +367,7 @@ static String evalTarget(Map<String, String> env, String target) {
JSAPResult jsapResult = jsap.parse(targetsEval);
String[] targets = jsapResult.getStringArray("targets");
for (String targetAndArgs : targets) {
String[] pieces = evalTarget(env, targetAndArgs).split(" ");
String[] pieces = eval(env, targetAndArgs).split(" ");
targetsToRun.add(pieces);
}
} catch (Exception e) {
@@ -67,6 +67,33 @@ public void testExpandEnvironmentsInTargets() {
"-DdefaultTarget=test-app " + TMP_WORK_DIR +" test-app");
}

public void testExpandEnvironmentsInProperties() {
{
List<String> logs = run(newBuilderWithProperties("foo=FOO"));
assertEcho(logs, TMP_WORK_DIR + " -Dfoo=FOO test-app");
}
{
List<String> logs = run(newBuilderWithProperties("foo=FOO\nbar=BAR"));
assertEcho(logs, TMP_WORK_DIR + " -Dbar=BAR -Dfoo=FOO test-app");
}
{
List<String> logs = run(newBuilderWithProperties("foo=${env['BUILD_NUMBER']}"));
assertEcho(logs, TMP_WORK_DIR + " -Dfoo=1 test-app");
}
{
List<String> logs = run(newBuilderWithProperties("foo=FOO\nbar=${env['BUILD_NUMBER']}"));
assertEcho(logs, TMP_WORK_DIR + " -Dbar=1 -Dfoo=FOO test-app");
}
{
List<String> logs = run(newBuilderWithProperties("foo=${BUILD_NUMBER}"));
assertEcho(logs, TMP_WORK_DIR + " -Dfoo=1 test-app");
}
{
List<String> logs = run(newBuilderWithProperties("foo=FOO\nbar=${BUILD_NUMBER}"));
assertEcho(logs, TMP_WORK_DIR + " -Dbar=1 -Dfoo=FOO test-app");
}
}

public void testForceUpgrade() {
{
GrailsBuilder builder = newBuilderWithTargets("test-app");
@@ -235,6 +262,10 @@ private GrailsBuilder newBuilderWithTargets(String targets) {
return new GrailsBuilder(targets, "echo", "/tmp", null, null, null, null, false, false, true, false, false, false, false);
}

private GrailsBuilder newBuilderWithProperties(String properties) {
return new GrailsBuilder("test-app", "echo", "/tmp", null, null, null, properties, false, false, true, false, false, false, false);
}

private Map<String, String> env(String key, String value) {
Map<String, String> result = new HashMap<String, String>();
result.put(key, value);

0 comments on commit 41bc755

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