Skip to content

Commit

Permalink
JENKINS-12769 Provide location of gradlew script, which required movi…
Browse files Browse the repository at this point in the history
…ng code into

radioBlocks which dont have inline, so I had to move to a POJO to store
them. Uses macro replacement on wrapper location.
  • Loading branch information
quidryan authored and Justin Ryan committed Sep 5, 2012
1 parent a3dc7bc commit ffddd0b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 51 deletions.
89 changes: 51 additions & 38 deletions src/main/java/hudson/plugins/gradle/Gradle.java
Expand Up @@ -22,26 +22,41 @@
*/
public class Gradle extends Builder implements DryRun {

private final String description;
private final String switches;
private final String tasks;
private final String rootBuildScriptDir;
private final String buildFile;
private final String gradleName;
private final boolean useWrapper;
private final boolean makeExecutable;
private final String wrapperScript;

// Artifact of how Jelly/Stapler puts conditional variables in blocks, which NEED to map to a sub-Object.
// The alternative would have been to mess with DescriptorImpl.getInstance
public static class UsingWrapper {
@DataBoundConstructor
public UsingWrapper(String value, String gradleName, String wrapperScript, Boolean makeExecutable) {
this.gradleName = gradleName;
this.wrapperScript = wrapperScript;
this.makeExecutable = makeExecutable;
}

String gradleName;
String wrapperScript;
Boolean makeExecutable;
}

@DataBoundConstructor
public Gradle(String description, String switches, String tasks, String rootBuildScriptDir, String buildFile,
String gradleName, boolean useWrapper, boolean makeExecutable) {
this.description = description;
public Gradle(String switches, String tasks, String rootBuildScriptDir, String buildFile,
UsingWrapper usingWrapper) {
this.switches = switches;
this.tasks = tasks;
this.gradleName = gradleName;
this.rootBuildScriptDir = rootBuildScriptDir;
this.buildFile = buildFile;
this.useWrapper = useWrapper;
this.makeExecutable = makeExecutable;
this.useWrapper = usingWrapper != null && usingWrapper.wrapperScript!=null;
this.gradleName = usingWrapper==null?null:usingWrapper.gradleName; // May be null
this.wrapperScript = usingWrapper==null?null:usingWrapper.wrapperScript; // May be null
this.makeExecutable = usingWrapper==null?null:Boolean.TRUE.equals(usingWrapper.makeExecutable); // May be null
}


Expand All @@ -65,11 +80,6 @@ public String getTasks() {
return tasks;
}

@SuppressWarnings("unused")
public String getDescription() {
return description;
}

@SuppressWarnings("unused")
public boolean isUseWrapper() {
return useWrapper;
Expand All @@ -85,6 +95,11 @@ public boolean isMakeExecutable() {
return makeExecutable;
}

@SuppressWarnings("unused")
public String getWrapperScript() {
return wrapperScript;
}

public GradleInstallation getGradle() {
for (GradleInstallation i : getDescriptor().getInstallations()) {
if (gradleName != null && i.getName().equals(gradleName)) {
Expand Down Expand Up @@ -145,32 +160,46 @@ private boolean performTask(boolean dryRun, AbstractBuild<?, ?> build, Launcher
//Build arguments
ArgumentListBuilder args = new ArgumentListBuilder();
GradleInstallation ai = getGradle();
String exe;
if (ai == null) {
if (useWrapper) {
String execName = (launcher.isUnix()) ? GradleInstallation.UNIX_GRADLE_WRAPPER_COMMAND : GradleInstallation.WINDOWS_GRADLE_WRAPPER_COMMAND;
if( wrapperScript != null && wrapperScript.trim().length() != 0) {
// Override with provided relative path to gradlew
String wrapperScriptNormalized = wrapperScript.trim().replaceAll("[\t\r\n]+", "");
wrapperScriptNormalized = Util.replaceMacro(wrapperScriptNormalized.trim(), env);
wrapperScriptNormalized = Util.replaceMacro(wrapperScriptNormalized, build.getBuildVariableResolver());
execName = wrapperScriptNormalized;
}

FilePath gradleWrapperFile = new FilePath(build.getModuleRoot(), execName);
if( !gradleWrapperFile.exists() ) {
listener.fatalError("Unable to find Gradle Wrapper");
return false;
}
if (makeExecutable) {
gradleWrapperFile.chmod(0744);
gradleWrapperFile.chmod(0755);
}
args.add(gradleWrapperFile.getRemote());
exe = gradleWrapperFile.getRemote();
} else {
args.add(launcher.isUnix() ? GradleInstallation.UNIX_GRADLE_COMMAND : GradleInstallation.WINDOWS_GRADLE_COMMAND);
exe = launcher.isUnix() ? GradleInstallation.UNIX_GRADLE_COMMAND : GradleInstallation.WINDOWS_GRADLE_COMMAND;
}
} else {
ai = ai.forNode(Computer.currentComputer().getNode(), listener);
ai = ai.forEnvironment(env);
String exe;
if (useWrapper) {
if (useWrapper) { // Can not happen, the Gradle installation is disabled if the useWrapper is checked
exe = ai.getWrapperExecutable(build);
} else {
exe = ai.getExecutable(launcher);
}
if (exe == null) {
gradleLogger.error("Can't retrieve the Gradle executable.");
return false;
}
args.add(exe);
}

if (exe == null) {
listener.fatalError("ERROR");
return false;
}
args.add(exe);

args.addKeyValuePairs("-D", fixParameters(build.getBuildVariables()));
args.addTokenized(normalizedSwitches);
args.addTokenized(normalizedTasks);
Expand Down Expand Up @@ -310,21 +339,5 @@ public void setInstallations(GradleInstallation... installations) {
this.installations = installations;
save();
}

@Override
public Gradle newInstance(StaplerRequest request, JSONObject formData) throws FormException {

// "flatten" formData for useWrapper radioBlocks
JSONObject useWrapper = formData.getJSONObject("useWrapper");
boolean wrapper = useWrapper.getBoolean("value");
useWrapper.remove("value");
for (String key : (Set<String>) useWrapper.keySet()) {
formData.put(key, useWrapper.get(key));
}
formData.put("useWrapper", wrapper);

return (Gradle) request.bindJSON(clazz, formData);
}
}

}
26 changes: 13 additions & 13 deletions src/main/resources/hudson/plugins/gradle/Gradle/config.jelly
@@ -1,25 +1,25 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<f:radioBlock name="useWrapper" checked="${!instance.useWrapper}" value="false" title="${%Invoke Gradle}">
<f:entry title="${%Gradle Version}" field="gradleName">
<select class="setting-input">
<f:radioBlock name="usingWrapper" value="true" title="${%Use Gradle Wrapper}" checked="${instance.useWrapper}">
<f:entry title="${%Wrapper location}" field="wrapperScript"> <!-- Optional -->
<f:textbox/>
</f:entry>
<f:entry title="${%Make gradlew executable}" field="makeExecutable">
<f:checkbox />
</f:entry>
</f:radioBlock>

<f:radioBlock name="usingWrapper" value="false" title="${%Invoke Gradle}" checked="${!instance.useWrapper}">
<f:entry title="${%Gradle Version}">
<select class="setting-input" name="gradle.gradleName">
<option>(Default)</option>
<j:forEach var="inst" items="${descriptor.installations}">
<f:option selected="${inst.name==instance.gradle.name}">${inst.name}</f:option>
</j:forEach>
</select>
</f:entry>
</f:radioBlock>
<f:radioBlock name="useWrapper" checked="${instance.useWrapper}" value="true" title="${%Use Gradle Wrapper}">
<f:entry title="${%Make gradlew executable}" field="makeExecutable">
<f:checkbox />
</f:entry>
</f:radioBlock>

<f:entry title="${%Build step description}" field="description">
<f:expandableTextbox/>
</f:entry>

<f:entry title="${%Switches}" field="switches">
<f:expandableTextbox/>
Expand All @@ -43,4 +43,4 @@
<f:textbox/>
</f:entry>

</j:jelly>
</j:jelly>
@@ -0,0 +1,5 @@
<div>
If your workspace has the gradlew somewhere other than the module root directory, specify the path
to gradlew (relative to workspace) here,
such as parent/gradlew instead of just ${workspace}/parent/gradlew. If left empty, defaults to ${workspace}/gradlew.
</div>

0 comments on commit ffddd0b

Please sign in to comment.