Skip to content

Commit

Permalink
Fix Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
ehrmann committed May 12, 2015
1 parent 84b3416 commit e22a241
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
Binary file not shown.
57 changes: 52 additions & 5 deletions src/main/java/com/nsegment/thrift/ThriftTask.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.io.OutputStream;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ThriftTask extends MatchingTask {
Expand Down Expand Up @@ -85,6 +87,35 @@ public class ThriftTask extends MatchingTask {
protected Path includePath = null;
protected File destDir = null;

protected final PathMapper<String, String> pathMapper;

public ThriftTask() {
String os = System.getProperty("os.name");
if (os != null && os.toLowerCase().startsWith("windows")) {
/*
This mapper does three things:
* Replaces '\' with '/'
* Prepends a '/' to absolute paths to make nestedvm happy
* make the drive name lower case (see UnixRuntime.java:1383)
*/
this.pathMapper = new PathMapper<String, String>() {
private final Pattern DRIVE_PATTERN = Pattern.compile("^([a-zA-Z])(:.*)$");
public String map(String path) {
path = path.replace(File.separatorChar, '/');

Matcher matcher = DRIVE_PATTERN.matcher(path);
if (matcher.matches()) {
path = "/" + matcher.group(1).toLowerCase() + matcher.group(2);
}

return path;
}
};
} else {
this.pathMapper = identityMapper(String.class);
}
}

@Override
public void execute() throws BuildException {
ArrayList<String> parameters = new ArrayList<String>();
Expand Down Expand Up @@ -126,7 +157,8 @@ public void execute() throws BuildException {

try {
parameters.add("-I");
parameters.add(file.getCanonicalPath().replace(File.separatorChar, '/'));
String include = this.pathMapper.map(file.getCanonicalPath());
parameters.add(include);
} catch (IOException e) {
throw new BuildException("Error resolving path element '" + resource.getName() + "'", e);
}
Expand All @@ -149,16 +181,18 @@ public void execute() throws BuildException {
}

try {
String destDir = this.destDir.getCanonicalPath().replace(File.separatorChar, '/');
String destDir = this.pathMapper.map((this.destDir.getCanonicalPath()));
parameters.add("-o");
parameters.add(destDir);
} catch (IOException e) {
throw new BuildException("Error accessing destDir '" + destDir.getPath() + "'", e);
}
} else {
try {

String outputDir = this.pathMapper.map(this.getProject().getBaseDir().getCanonicalPath());
parameters.add("-o");
parameters.add(this.getProject().getBaseDir().getCanonicalPath().replace(File.separatorChar, '/'));
parameters.add(outputDir);
} catch (IOException e) {
throw new BuildException("Error accessing destDir '" + this.getProject().getBaseDir() + "'", e);
}
Expand Down Expand Up @@ -284,15 +318,16 @@ public void execute() throws BuildException {

String resolvedSrcDir;
try {
resolvedSrcDir = srcDirFile.getCanonicalPath().replace(File.separatorChar, '/');
resolvedSrcDir = this.pathMapper.map(srcDirFile.getCanonicalPath());
} catch (IOException e) {
throw new BuildException("Failed to resolve source directory '" + srcDir + "'", e);
}

// FIXME: what are strings retunred by getIncludedFile absolute?
DirectoryScanner ds = this.getDirectoryScanner(srcDirFile);
String[] files = ds.getIncludedFiles();
for (String file : files) {
file = file.replace(File.separatorChar, '/');
file = this.pathMapper.map(file);
filesToProcess.add((resolvedSrcDir + "/" + file).replaceAll("/+", "/"));
}
}
Expand Down Expand Up @@ -634,4 +669,16 @@ public void write(int b) {
public void write(byte[] b) throws IOException {
}
}

protected interface PathMapper <D, S> {
D map(S value);
}

protected static <I> PathMapper<I, I> identityMapper(Class<I> clazz) {
return new PathMapper<I, I>() {
public I map(I value) {
return value;
}
};
}
}
8 changes: 4 additions & 4 deletions src/test/java/com/nsegment/thrift/ThriftTaskTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void testSimple() throws IOException {
for (File file : FileUtils.listFiles(folder.getRoot(), null, true)) {
String fileStr = file.getCanonicalPath();
assertTrue(fileStr.startsWith(prefix));
fileSet.add(fileStr.substring(prefix.length()));
fileSet.add(fileStr.substring(prefix.length()).replace(File.separatorChar, '/'));
}

assertEquals(new TreeSet<String>(Arrays.asList(new String[]{
Expand Down Expand Up @@ -158,7 +158,7 @@ public void testRelativePaths0() throws IOException {
for (File file : FileUtils.listFiles(artifacts, null, true)) {
String fileStr = file.getCanonicalPath();
assertTrue(fileStr.startsWith(prefix));
fileSet.add(fileStr.substring(prefix.length()));
fileSet.add(fileStr.substring(prefix.length()).replace(File.separatorChar, '/'));
}

assertEquals(new TreeSet<String>(Arrays.asList(new String[]{
Expand Down Expand Up @@ -201,7 +201,7 @@ public void testRelativePaths1() throws IOException {
for (File file : FileUtils.listFiles(artifacts, null, true)) {
String fileStr = file.getCanonicalPath();
assertTrue(fileStr.startsWith(prefix));
fileSet.add(fileStr.substring(prefix.length()));
fileSet.add(fileStr.substring(prefix.length()).replace(File.separatorChar, '/'));
}

assertEquals(new TreeSet<String>(Arrays.asList(new String[]{
Expand Down Expand Up @@ -240,7 +240,7 @@ public void testRelativePaths2() throws IOException {
for (File file : FileUtils.listFiles(this.folder.getRoot(), null, true)) {
String fileStr = file.getCanonicalPath();
assertTrue(fileStr.startsWith(prefix));
fileSet.add(fileStr.substring(prefix.length()));
fileSet.add(fileStr.substring(prefix.length()).replace(File.separatorChar, '/'));
}

assertEquals(new TreeSet<String>(Arrays.asList(new String[]{
Expand Down
25 changes: 22 additions & 3 deletions src/test/java/com/nsegment/thrift/ThriftcTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,31 @@ public void testJsonRoundTrip() throws IOException {
this.getClass().getResourceAsStream("struct.thrift"),
new File(folder.getRoot(), "struct.thrift"));

String[] args = {
String outputDir = folder.getRoot().getCanonicalPath().replace(File.separatorChar, '/');
String src = folder.getRoot().getCanonicalPath().replace(File.separatorChar, '/') + "/struct.thrift";

String os = System.getProperty("os.name");
if (os != null && os.toLowerCase().startsWith("windows")) {
if (outputDir.matches("^[A-Za-z]:.*")) {
outputDir = new StringBuilder(outputDir)
.replace(0, 1, outputDir.substring(0, 1).toLowerCase())
.insert(0, "/")
.toString();
}
if (src.matches("^[A-Za-z]:.*")) {
src = new StringBuilder(src)
.replace(0, 1, src.substring(0, 1).toLowerCase())
.insert(0, "/")
.toString();
}
}

String[] args = new String [] {
"-o",
folder.getRoot().getCanonicalPath().replace(File.separatorChar, '/'),
outputDir,
"--gen",
"js",
folder.getRoot().getCanonicalPath().replace(File.separatorChar, '/') + "/struct.thrift",
src,
};

Thriftc thrift = new Thriftc();
Expand Down

0 comments on commit e22a241

Please sign in to comment.