Skip to content
Permalink
Browse files
Merge pull request #7 from tyrius02/master
[FIXED JENKINS-17876] Use QuotedStringTokenizer when constructing msbuild command-line arguments
  • Loading branch information
gboissinot committed Jun 25, 2013
2 parents dbf9ee7 + 9a35332 commit 0ae0e878b29ff22072286e65fc198f32a0cdb46c
Showing with 48 additions and 10 deletions.
  1. +1 −2 pom.xml
  2. +15 −8 src/main/java/hudson/plugins/msbuild/MsBuildBuilder.java
  3. +32 −0 src/test/java/hudson/plugins/msbuild/MsBuildBuilderTest.java
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.410</version>
<version>1.420</version>
</parent>

<artifactId>msbuild</artifactId>
@@ -52,7 +52,6 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

</project>


@@ -6,6 +6,7 @@
import hudson.tasks.Builder;
import hudson.tools.ToolInstallation;
import hudson.util.ArgumentListBuilder;
import hudson.util.QuotedStringTokenizer;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
@@ -72,7 +73,8 @@ public boolean getContinueOnBuildFailure() {
}

public MsBuildInstallation getMsBuild() {
for (MsBuildInstallation i : DESCRIPTOR.getInstallations()) {
DescriptorImpl descriptor = (DescriptorImpl)getDescriptor();
for (MsBuildInstallation i : descriptor.getInstallations()) {
if (msBuildName != null && i.getName().equals(msBuildName))
return i;
}
@@ -110,7 +112,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
args.add(pathToMsBuild);

if (ai.getDefaultArgs() != null) {
args.addTokenized(ai.getDefaultArgs());
args.add(tokenizeArgs(ai.getDefaultArgs()));
}
}

@@ -120,7 +122,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
normalizedArgs = Util.replaceMacro(normalizedArgs, build.getBuildVariables());

if (normalizedArgs.trim().length() > 0)
args.addTokenized(normalizedArgs);
args.add(tokenizeArgs(normalizedArgs));

//Build /P:key1=value1;key2=value2 ...
Map<String, String> variables = build.getBuildVariables();
@@ -174,20 +176,25 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

@Override
public Descriptor<Builder> getDescriptor() {
return DESCRIPTOR;
return (DescriptorImpl)super.getDescriptor();
}

/**
* Descriptor should be singleton.
* Tokenize a set of arguments, preserving quotes.
* @param args
* @return
*/
@Extension
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
static String[] tokenizeArgs(String args) {
QuotedStringTokenizer tokenizer = new QuotedStringTokenizer(args, " \t\n\r\f", false, true);
return tokenizer.toArray();
}

@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
@CopyOnWrite
private volatile MsBuildInstallation[] installations = new MsBuildInstallation[0];

DescriptorImpl() {
public DescriptorImpl() {
super(MsBuildBuilder.class);
load();
}
@@ -0,0 +1,32 @@
package hudson.plugins.msbuild;

import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author Jonathan Zimmerman
*/
public class MsBuildBuilderTest {

@Test
public void shouldRetainQuotedArguments() {
final String platform = "/p:Platform=\"Any CPU\"";

String[] tokenizedArgs = MsBuildBuilder.tokenizeArgs(platform);
assertNotNull(tokenizedArgs);
assertEquals(1, tokenizedArgs.length);
assertEquals(platform, tokenizedArgs[0]);
}

@Test
public void shouldSplitArguments() {
final String arguments = "/t:Build /p:Configuration=Debug";

String[] tokenizedArgs = MsBuildBuilder.tokenizeArgs(arguments);
assertNotNull(tokenizedArgs);
assertEquals(2, tokenizedArgs.length);
assertEquals("/t:Build", tokenizedArgs[0]);
assertEquals("/p:Configuration=Debug", tokenizedArgs[1]);
}
}

0 comments on commit 0ae0e87

Please sign in to comment.