Skip to content

Commit fbbd84d

Browse files
committed
Normalizing path separators in command line. Closes #4.
1 parent 883ced3 commit fbbd84d

File tree

6 files changed

+90
-63
lines changed

6 files changed

+90
-63
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
apply plugin: "groovy"
66

77
group = 'com.github.jlouns'
8-
version = '0.3.0'
8+
version = '0.4.0'
99

1010
repositories {
1111
jcenter()
@@ -32,5 +32,5 @@ pluginBundle {
3232
}
3333

3434
task wrapper(type: Wrapper) {
35-
gradleVersion = '2.4'
35+
gradleVersion = '2.8'
3636
}

gradle/wrapper/gradle-wrapper.jar

2.57 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Thu Jun 18 23:29:45 EDT 2015
1+
#Wed Nov 04 00:09:34 EST 2015
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-bin.zip

gradlew

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ case "`uname`" in
4242
;;
4343
esac
4444

45-
# For Cygwin, ensure paths are in UNIX format before anything is touched.
46-
if $cygwin ; then
47-
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48-
fi
49-
5045
# Attempt to set APP_HOME
5146
# Resolve links: $0 may be a link
5247
PRG="$0"
@@ -61,9 +56,9 @@ while [ -h "$PRG" ] ; do
6156
fi
6257
done
6358
SAVED="`pwd`"
64-
cd "`dirname \"$PRG\"`/" >&-
59+
cd "`dirname \"$PRG\"`/" >/dev/null
6560
APP_HOME="`pwd -P`"
66-
cd "$SAVED" >&-
61+
cd "$SAVED" >/dev/null
6762

6863
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
6964

@@ -114,6 +109,7 @@ fi
114109
if $cygwin ; then
115110
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116111
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112+
JAVACMD=`cygpath --unix "$JAVACMD"`
117113

118114
# We build the pattern for arguments to be converted via cygpath
119115
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`

src/main/groovy/com/github/jlouns/gradle/cpe/tasks/CrossPlatformExec.groovy

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ class CrossPlatformExec extends AbstractExecTask {
3939
}
4040

4141
private String findCommand(String command) {
42+
command = normalizeCommandPaths(command);
4243
if (windows) {
4344
return windowsExtensions.findResult(command) { extension ->
44-
String commandFile = command + '.' + extension;
45+
Path commandFile = Paths.get(command + '.' + extension);
4546

46-
if (Files.isExecutable(Paths.get(commandFile))) {
47-
return commandFile;
48-
}
49-
50-
return null;
47+
return CrossPlatformExec.resolveCommandFromFile(commandFile);
5148
};
5249
} else {
5350
return unixExtensions.findResult(command) { extension ->
@@ -58,20 +55,28 @@ class CrossPlatformExec extends AbstractExecTask {
5855
commandFile = Paths.get(command);
5956
}
6057

61-
if (Files.isExecutable(commandFile)) {
62-
Path cwd = Paths.get('.').toAbsolutePath().normalize();
63-
String resolvedCommand = cwd.relativize(commandFile.toAbsolutePath().normalize());
58+
return CrossPlatformExec.resolveCommandFromFile(commandFile);
59+
};
60+
}
61+
}
6462

65-
if (!resolvedCommand.startsWith('.')) {
66-
resolvedCommand = './' + resolvedCommand;
67-
}
63+
private static String resolveCommandFromFile(Path commandFile) {
64+
if (!Files.isExecutable(commandFile)) {
65+
return null;
66+
}
6867

69-
return resolvedCommand;
70-
}
68+
Path cwd = Paths.get('.').toAbsolutePath().normalize();
7169

72-
return null;
73-
};
70+
String resolvedCommand = cwd.relativize(commandFile.toAbsolutePath().normalize());
71+
72+
if (!resolvedCommand.startsWith('.')) {
73+
resolvedCommand = '.' + File.separator + resolvedCommand;
7474
}
75+
76+
return resolvedCommand;
7577
}
7678

79+
private static String normalizeCommandPaths(String command) {
80+
return command.replaceAll('\\\\', '/').replaceAll('/', File.separator);
81+
}
7782
}

src/test/groovy/com/github/jlouns/gradle/cpe/tasks/CrossPlatformExecTest.groovy

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,38 @@ import java.nio.file.attribute.PosixFilePermission
1111

1212
class CrossPlatformExecTest extends GroovyTestCase {
1313

14+
private static String separator = File.separator;
15+
1416
private Project project;
17+
private Path testDir;
1518
private Path[] commandFiles;
1619

1720
@Override
1821
void setUp() {
1922
project = ProjectBuilder.builder().build()
2023

21-
project.apply plugin: 'com.github.jlouns.cpe'
24+
project.apply plugin: "com.github.jlouns.cpe"
25+
26+
testDir = Paths.get("testdir");
2227

2328
commandFiles = [
24-
Paths.get('one'),
25-
Paths.get('one.cmd'),
26-
Paths.get('two.bat'),
27-
Paths.get('two.sh'),
28-
Paths.get('three'),
29-
Paths.get('three.exe')
29+
Paths.get("one"),
30+
Paths.get("one.cmd"),
31+
Paths.get("two.bat"),
32+
Paths.get("two.sh"),
33+
Paths.get("three"),
34+
Paths.get("three.exe"),
35+
testDir.resolve("four.sh"),
36+
testDir.resolve("four.exe")
3037
];
3138

39+
Files.createDirectory(testDir);
40+
3241
commandFiles.each {
3342
Files.createFile(it)
3443
try {
35-
Files.setPosixFilePermissions(it, EnumSet.allOf(PosixFilePermission));
36-
} catch (Exception ex) {
44+
Files.setPosixFilePermissions(it, EnumSet.allOf(PosixFilePermission))
45+
} catch (Exception ignore) {
3746
// Ignore it, probably on windows
3847
}
3948
}
@@ -44,81 +53,98 @@ class CrossPlatformExecTest extends GroovyTestCase {
4453
commandFiles.each {
4554
Files.delete(it)
4655
}
56+
Files.delete(testDir);
4757
}
4858

4959
Task createTask(String executable) {
50-
Task task = project.task([ 'type': CrossPlatformExec ], 'testCpe') {
51-
commandLine executable, 'foo'
60+
Task task = project.task([ "type": CrossPlatformExec ], "testCpe") {
61+
commandLine executable, "foo"
5262
}
5363

5464
try {
5565
task.exec()
56-
} catch (Exception ex) {
57-
println(ex.stackTrace)
58-
// Ignore it
66+
} catch (Exception ignore) {
67+
ignore.printStackTrace()
5968
}
6069

6170
task
6271
}
6372

6473
static void asOs(String os) {
65-
System.setProperty('os.name', os)
74+
System.setProperty("os.name", os)
6675
}
6776

6877
void testFormsLinuxExecCallForCommand() {
69-
asOs('linux')
78+
asOs("linux")
7079

71-
Task task = createTask('echo')
80+
Task task = createTask("echo")
7281

73-
assertArrayEquals(['echo', 'foo'].toArray(), task.commandLine.toArray());
82+
assertArrayEquals(["echo", "foo"].toArray(), task.commandLine.toArray());
7483
}
7584

7685
void testFormsLinuxExecCallForFile() {
77-
asOs('linux')
86+
asOs("linux")
7887

79-
Task task = createTask('one')
88+
Task task = createTask("one")
8089

81-
assertArrayEquals(['./one', 'foo'].toArray(), task.commandLine.toArray());
90+
assertArrayEquals([".${separator}one", "foo"].toArray(), task.commandLine.toArray());
8291
}
8392

8493
void testFormsLinuxExecCallForShFile() {
85-
asOs('linux')
94+
asOs("linux")
8695

87-
Task task = createTask('two')
96+
Task task = createTask("two")
8897

89-
assertArrayEquals(['./two.sh', 'foo'].toArray(), task.commandLine.toArray());
98+
assertArrayEquals([".${separator}two.sh", "foo"].toArray(), task.commandLine.toArray());
9099
}
91100

92101
void testFormsWindowsExecCallForCommand() {
93-
asOs('windows')
102+
asOs("windows")
94103

95-
Task task = createTask('echo')
104+
Task task = createTask("echo")
96105

97-
assertArrayEquals(['cmd', '/c', 'echo', 'foo'].toArray(), task.commandLine.toArray());
106+
assertArrayEquals(["cmd", "/c", "echo", "foo"].toArray(), task.commandLine.toArray());
98107
}
99108

100109
void testFormsWindowsExecCallForCmdFile() {
101-
asOs('windows')
110+
asOs("windows")
102111

103-
Task task = createTask('one')
112+
Task task = createTask("one")
104113

105-
assertArrayEquals(['cmd', '/c', 'one.cmd', 'foo'].toArray(), task.commandLine.toArray());
114+
assertArrayEquals(["cmd", "/c", ".${separator}one.cmd", "foo"].toArray(), task.commandLine.toArray());
106115
}
107116

108117
void testFormsWindowsExecCallForBatFile() {
109-
asOs('windows')
118+
asOs("windows")
110119

111-
Task task = createTask('two')
120+
Task task = createTask("two")
112121

113-
assertArrayEquals(['cmd', '/c', 'two.bat', 'foo'].toArray(), task.commandLine.toArray());
122+
assertArrayEquals(["cmd", "/c", ".${separator}two.bat", "foo"].toArray(), task.commandLine.toArray());
114123
}
115124

116125
void testFormsWindowsExecCallForExeFile() {
117-
asOs('windows')
126+
asOs("windows")
127+
128+
Task task = createTask("three")
129+
130+
assertArrayEquals(["cmd", "/c", ".${separator}three.exe", "foo"].toArray(), task.commandLine.toArray());
131+
}
132+
133+
void testFixesPathSeparatorsOnLinux() {
134+
asOs("linux")
135+
136+
Task task = createTask(".\\testdir\\four")
137+
138+
assertArrayEquals([".${separator}testdir${separator}four.sh", "foo"].toArray(), task.commandLine.toArray());
139+
}
140+
141+
void testFixesPathSeparatorsOnWindows() {
142+
asOs("windows")
118143

119-
Task task = createTask('three')
144+
Task task = createTask("./testdir/four")
120145

121-
assertArrayEquals(['cmd', '/c', 'three.exe', 'foo'].toArray(), task.commandLine.toArray());
146+
assertArrayEquals(["cmd", "/c", ".${separator}testdir${separator}four.exe", "foo"].toArray(),
147+
task.commandLine.toArray());
122148
}
123149

124150
}

0 commit comments

Comments
 (0)