Skip to content

Commit

Permalink
fix for issue JENKINS-12800
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Barros committed Apr 2, 2013
1 parent 197b212 commit 8b0e052
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/main/java/au/com/rayh/XCodeBuilder.java
Expand Up @@ -47,8 +47,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

/**
* @author Ray Hilton
Expand Down Expand Up @@ -553,11 +553,68 @@ else if (StringUtils.isEmpty(cfBundleVersion))
return true;
}

static List<String> splitXcodeBuildArguments(String xcodebuildArguments) {
String[] parts = xcodebuildArguments.split("(?<!\\\\)\\s+");
List<String> result = new ArrayList<String>(parts.length);
for(String arg : parts) {
result.add(arg.replaceAll("\\\\ ", " "));
public static List<String> splitXcodeBuildArguments(String xcodebuildArguments) {
if (xcodebuildArguments == null || xcodebuildArguments.length() == 0) {
return new ArrayList<String>(0);
}

final int normal = 0;
final int inQuote = 1;
final int inDoubleQuote = 2;
final int escapeChar = 3;
int state = normal;
final StringTokenizer tok = new StringTokenizer(xcodebuildArguments, "\"'\\ ", true);
final List<String> result = new ArrayList<String>();
final StringBuilder current = new StringBuilder();
boolean lastTokenHasBeenQuoted = false;
String nextTok;
while (tok.hasMoreTokens()) {
nextTok = tok.nextToken();
switch (state) {
case inQuote:
if ("\'".equals(nextTok)) {
lastTokenHasBeenQuoted = true;
state = normal;
} else {
current.append(nextTok);
}
break;
case inDoubleQuote:
if ("\"".equals(nextTok)) {
lastTokenHasBeenQuoted = true;
state = normal;
} else {
current.append(nextTok);
}
break;
case escapeChar:
current.append(nextTok);
state = normal;
break;
default:
if ("\'".equals(nextTok)) {
state = inQuote;
} else if ("\"".equals(nextTok)) {
state = inDoubleQuote;
} else if ("\\".equals(nextTok)) {
state = escapeChar;
} else if (" ".equals(nextTok) && !(current.length() > 0 && current.charAt(current.length() - 1) == '\\')) {
if (lastTokenHasBeenQuoted || current.length() != 0) {
result.add(current.toString());
current.setLength(0);
}
} else {
current.append(nextTok);
}
lastTokenHasBeenQuoted = false;
break;
}
}
if (lastTokenHasBeenQuoted || current.length() != 0) {
result.add(current.toString());
}
if (state == inQuote || state == inDoubleQuote) {
throw new IllegalArgumentException(String.format("Inconsistent quotes: %s", xcodebuildArguments));
}
return result;
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/au/com/rayh/XCodeBuilderTest.java
Expand Up @@ -59,5 +59,7 @@ public void shouldSplitXcodeBuildArgumentsWithEscapedSpaces() throws Exception {
XCodeBuilder.splitXcodeBuildArguments("CODE_SIGN_IDENTITY=iPhone\\ Developer:\\ Todd\\ Kirby"));
assertEquals(asList("A=B", "CODE_SIGN_IDENTITY=iPhone Developer: Todd Kirby"),
XCodeBuilder.splitXcodeBuildArguments("A=B CODE_SIGN_IDENTITY=iPhone\\ Developer:\\ Todd\\ Kirby"));
assertEquals(asList("A=B", "CODE_SIGN_IDENTITY=iPhone Distribution", "C=D"),
XCodeBuilder.splitXcodeBuildArguments("A=B CODE_SIGN_IDENTITY=\"iPhone Distribution\" C=D"));
}
}

0 comments on commit 8b0e052

Please sign in to comment.