Skip to content

Commit

Permalink
Normalizing path separators in command line. Closes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlouns committed Nov 4, 2015
1 parent 883ced3 commit fbbd84d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 63 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
apply plugin: "groovy"

group = 'com.github.jlouns'
version = '0.3.0'
version = '0.4.0'

repositories {
jcenter()
Expand All @@ -32,5 +32,5 @@ pluginBundle {
}

task wrapper(type: Wrapper) {
gradleVersion = '2.4'
gradleVersion = '2.8'
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jun 18 23:29:45 EDT 2015
#Wed Nov 04 00:09:34 EST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-bin.zip
10 changes: 3 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ case "`uname`" in
;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched.
if $cygwin ; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
Expand All @@ -61,9 +56,9 @@ while [ -h "$PRG" ] ; do
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >&-
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >&-
cd "$SAVED" >/dev/null

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

Expand Down Expand Up @@ -114,6 +109,7 @@ fi
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ class CrossPlatformExec extends AbstractExecTask {
}

private String findCommand(String command) {
command = normalizeCommandPaths(command);
if (windows) {
return windowsExtensions.findResult(command) { extension ->
String commandFile = command + '.' + extension;
Path commandFile = Paths.get(command + '.' + extension);

if (Files.isExecutable(Paths.get(commandFile))) {
return commandFile;
}

return null;
return CrossPlatformExec.resolveCommandFromFile(commandFile);
};
} else {
return unixExtensions.findResult(command) { extension ->
Expand All @@ -58,20 +55,28 @@ class CrossPlatformExec extends AbstractExecTask {
commandFile = Paths.get(command);
}

if (Files.isExecutable(commandFile)) {
Path cwd = Paths.get('.').toAbsolutePath().normalize();
String resolvedCommand = cwd.relativize(commandFile.toAbsolutePath().normalize());
return CrossPlatformExec.resolveCommandFromFile(commandFile);
};
}
}

if (!resolvedCommand.startsWith('.')) {
resolvedCommand = './' + resolvedCommand;
}
private static String resolveCommandFromFile(Path commandFile) {
if (!Files.isExecutable(commandFile)) {
return null;
}

return resolvedCommand;
}
Path cwd = Paths.get('.').toAbsolutePath().normalize();

return null;
};
String resolvedCommand = cwd.relativize(commandFile.toAbsolutePath().normalize());

if (!resolvedCommand.startsWith('.')) {
resolvedCommand = '.' + File.separator + resolvedCommand;
}

return resolvedCommand;
}

private static String normalizeCommandPaths(String command) {
return command.replaceAll('\\\\', '/').replaceAll('/', File.separator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,38 @@ import java.nio.file.attribute.PosixFilePermission

class CrossPlatformExecTest extends GroovyTestCase {

private static String separator = File.separator;

private Project project;
private Path testDir;
private Path[] commandFiles;

@Override
void setUp() {
project = ProjectBuilder.builder().build()

project.apply plugin: 'com.github.jlouns.cpe'
project.apply plugin: "com.github.jlouns.cpe"

testDir = Paths.get("testdir");

commandFiles = [
Paths.get('one'),
Paths.get('one.cmd'),
Paths.get('two.bat'),
Paths.get('two.sh'),
Paths.get('three'),
Paths.get('three.exe')
Paths.get("one"),
Paths.get("one.cmd"),
Paths.get("two.bat"),
Paths.get("two.sh"),
Paths.get("three"),
Paths.get("three.exe"),
testDir.resolve("four.sh"),
testDir.resolve("four.exe")
];

Files.createDirectory(testDir);

commandFiles.each {
Files.createFile(it)
try {
Files.setPosixFilePermissions(it, EnumSet.allOf(PosixFilePermission));
} catch (Exception ex) {
Files.setPosixFilePermissions(it, EnumSet.allOf(PosixFilePermission))
} catch (Exception ignore) {
// Ignore it, probably on windows
}
}
Expand All @@ -44,81 +53,98 @@ class CrossPlatformExecTest extends GroovyTestCase {
commandFiles.each {
Files.delete(it)
}
Files.delete(testDir);
}

Task createTask(String executable) {
Task task = project.task([ 'type': CrossPlatformExec ], 'testCpe') {
commandLine executable, 'foo'
Task task = project.task([ "type": CrossPlatformExec ], "testCpe") {
commandLine executable, "foo"
}

try {
task.exec()
} catch (Exception ex) {
println(ex.stackTrace)
// Ignore it
} catch (Exception ignore) {
ignore.printStackTrace()
}

task
}

static void asOs(String os) {
System.setProperty('os.name', os)
System.setProperty("os.name", os)
}

void testFormsLinuxExecCallForCommand() {
asOs('linux')
asOs("linux")

Task task = createTask('echo')
Task task = createTask("echo")

assertArrayEquals(['echo', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals(["echo", "foo"].toArray(), task.commandLine.toArray());
}

void testFormsLinuxExecCallForFile() {
asOs('linux')
asOs("linux")

Task task = createTask('one')
Task task = createTask("one")

assertArrayEquals(['./one', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals([".${separator}one", "foo"].toArray(), task.commandLine.toArray());
}

void testFormsLinuxExecCallForShFile() {
asOs('linux')
asOs("linux")

Task task = createTask('two')
Task task = createTask("two")

assertArrayEquals(['./two.sh', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals([".${separator}two.sh", "foo"].toArray(), task.commandLine.toArray());
}

void testFormsWindowsExecCallForCommand() {
asOs('windows')
asOs("windows")

Task task = createTask('echo')
Task task = createTask("echo")

assertArrayEquals(['cmd', '/c', 'echo', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals(["cmd", "/c", "echo", "foo"].toArray(), task.commandLine.toArray());
}

void testFormsWindowsExecCallForCmdFile() {
asOs('windows')
asOs("windows")

Task task = createTask('one')
Task task = createTask("one")

assertArrayEquals(['cmd', '/c', 'one.cmd', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals(["cmd", "/c", ".${separator}one.cmd", "foo"].toArray(), task.commandLine.toArray());
}

void testFormsWindowsExecCallForBatFile() {
asOs('windows')
asOs("windows")

Task task = createTask('two')
Task task = createTask("two")

assertArrayEquals(['cmd', '/c', 'two.bat', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals(["cmd", "/c", ".${separator}two.bat", "foo"].toArray(), task.commandLine.toArray());
}

void testFormsWindowsExecCallForExeFile() {
asOs('windows')
asOs("windows")

Task task = createTask("three")

assertArrayEquals(["cmd", "/c", ".${separator}three.exe", "foo"].toArray(), task.commandLine.toArray());
}

void testFixesPathSeparatorsOnLinux() {
asOs("linux")

Task task = createTask(".\\testdir\\four")

assertArrayEquals([".${separator}testdir${separator}four.sh", "foo"].toArray(), task.commandLine.toArray());
}

void testFixesPathSeparatorsOnWindows() {
asOs("windows")

Task task = createTask('three')
Task task = createTask("./testdir/four")

assertArrayEquals(['cmd', '/c', 'three.exe', 'foo'].toArray(), task.commandLine.toArray());
assertArrayEquals(["cmd", "/c", ".${separator}testdir${separator}four.exe", "foo"].toArray(),
task.commandLine.toArray());
}

}

0 comments on commit fbbd84d

Please sign in to comment.