Changed Paths.computeRelativePath to use java.nio.file.Path standard method and moved to MorePaths.
Summary:
This replaces an imlementation of a method with a standard java 7 method Paths.relativize(). Also moves
the method to a new utility class.
Test Plan: ant clean test
diff --git a/src/com/facebook/buck/command/Project.java b/src/com/facebook/buck/command/Project.java
index fb6544c..bcdb904 100644
--- a/src/com/facebook/buck/command/Project.java
+++ b/src/com/facebook/buck/command/Project.java
@@ -47,7 +47,6 @@
import com.facebook.buck.util.Console;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.KeystoreProperties;
-import com.facebook.buck.util.Paths;
import com.facebook.buck.util.ProcessExecutor;
import com.facebook.buck.util.ProjectFilesystem;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -79,6 +78,7 @@
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -267,19 +267,10 @@
continue;
}
- Path pathToImlFile = java.nio.file.Paths.get(module.pathToImlFile);
- Path depPathToImlFile = java.nio.file.Paths.get(dep.pathToImlFile);
+ Path pathToImlFile = Paths.get(module.pathToImlFile);
+ Path depPathToImlFile = Paths.get(dep.pathToImlFile);
- String relativePath = Paths.computeRelativePath(
- pathToImlFile.getParent().toString(),
- depPathToImlFile.getParent().toString()
- );
-
- // Drop the trailing slash from the path, since that's what the Android tools appear to do
- // when generating project.properties.
- if (relativePath.endsWith("/")) {
- relativePath = relativePath.substring(0, relativePath.length() - 1);
- }
+ String relativePath = pathToImlFile.getParent().relativize(depPathToImlFile.getParent()).toString();
// This is probably a self-reference. Ignore it.
if (relativePath.isEmpty()) {
@@ -441,7 +432,7 @@
// so that it will not disturb our glob() rules.
// To specify the location of gen, Intellij requires the relative path from
// the base path of current build target.
- module.moduleGenPath = generateRelativeGenPath(basePathWithSlash);
+ module.moduleGenPath = generateRelativeGenPath(basePathWithSlash).toString();
DependentModule jdkDependency;
if (isAndroidRule) {
@@ -450,7 +441,7 @@
NdkLibrary ndkLibrary = (NdkLibrary) projectRule.getBuildable();
module.isAndroidLibraryProject = true;
module.keystorePath = null;
- module.nativeLibs = Paths.computeRelativePath(relativePath, ndkLibrary.getLibraryPath());
+ module.nativeLibs = Paths.get(relativePath).relativize(Paths.get(ndkLibrary.getLibraryPath())).toString();
} else if (projectRule instanceof AndroidResourceRule) {
AndroidResourceRule androidResourceRule = (AndroidResourceRule)projectRule;
module.resFolder = createRelativePath(androidResourceRule.getRes(), target);
@@ -467,8 +458,7 @@
// getKeystore() returns a path relative to the project root, but an IntelliJ module
// expects the path to the keystore to be relative to the module root.
- module.keystorePath = Paths.computeRelativePath(relativePath,
- keystoreProperties.getKeystore());
+ module.keystorePath = Paths.get(relativePath).relativize(Paths.get(keystoreProperties.getKeystore())).toString();
} else {
module.isAndroidLibraryProject = true;
module.keystorePath = null;
@@ -489,7 +479,7 @@
"indicating that it is relative to the root of the repository.",
rootPrefix);
manifestPath = manifestPath.substring(rootPrefix.length());
- String relativePathToManifest = Paths.computeRelativePath(basePathWithSlash, manifestPath);
+ String relativePathToManifest = Paths.get(basePathWithSlash).relativize(Paths.get(manifestPath)).toString();
// IntelliJ requires that the path start with a slash to indicate that it is relative to
// the module.
module.androidManifest = "/" + relativePathToManifest;
@@ -521,7 +511,7 @@
String annotationGenSrc = processingData.getGeneratedSourceFolderName();
if (annotationGenSrc != null) {
module.annotationGenPath =
- "/" + Paths.computeRelativePath(basePathWithSlash, annotationGenSrc);
+ "/" + Paths.get(basePathWithSlash).relativize(Paths.get(annotationGenSrc)).toString();
module.annotationGenIsForTest = !hasSourceFoldersForSrcRule;
}
}
@@ -568,14 +558,13 @@
*
* @return the relative path of gen from the base path of current module.
*/
- static String generateRelativeGenPath(String basePathOfModuleWithSlash) {
- return
- "/"
- + Paths.computeRelativePath(basePathOfModuleWithSlash, "")
- + ANDROID_GEN_DIR
- + "/"
- + Paths.computeRelativePath("", basePathOfModuleWithSlash)
- + "gen";
+ static Path generateRelativeGenPath(String basePathOfModuleWithSlash) {
+ return Paths.get(
+ "/",
+ Paths.get(basePathOfModuleWithSlash).relativize(Paths.get("")).toString(),
+ ANDROID_GEN_DIR,
+ Paths.get("").relativize(Paths.get(basePathOfModuleWithSlash)).toString(),
+ "gen");
}
private boolean addSourceFolders(Module module,
diff --git a/src/com/facebook/buck/util/Paths.java b/src/com/facebook/buck/util/Paths.java
index ee746f3..e71f6a9 100644
--- a/src/com/facebook/buck/util/Paths.java
+++ b/src/com/facebook/buck/util/Paths.java
@@ -16,8 +16,6 @@
package com.facebook.buck.util;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import java.io.File;
@@ -26,53 +24,6 @@
private Paths() {}
- /**
- * @param from must always refer to a directory (it should either be the empty string or end with
- * a slash).
- * @param to may refer to either a file or a directory
- */
- public static String computeRelativePath(String from, String to) {
- Preconditions.checkNotNull(from);
- Preconditions.checkArgument(from.isEmpty() || from.endsWith("/"),
- "Directory path must either be the empty string or end with a slash");
- Preconditions.checkNotNull(to);
-
- if (from.isEmpty()) {
- return to;
- }
-
- // Both from and to have the same string prefix through this character index.
- int samePrefixIndex = 0;
- while (true) {
- int slashIndex = from.indexOf('/', samePrefixIndex);
- if (slashIndex < 0) {
- break;
- }
-
- int indexIncludingSlash = slashIndex + 1;
- if (indexIncludingSlash > to.length()) {
- break;
- }
-
- String fromPathElement = from.substring(samePrefixIndex, indexIncludingSlash);
- String toPathElement = to.substring(samePrefixIndex, indexIncludingSlash);
- if (fromPathElement.equals(toPathElement)) {
- samePrefixIndex = indexIncludingSlash;
- } else {
- break;
- }
- }
-
- int directoryDepth = 0;
- for (int charIndex = samePrefixIndex + 1; charIndex < from.length(); charIndex++) {
- if (from.charAt(charIndex) == '/') {
- directoryDepth++;
- }
- }
-
- return Strings.repeat("../", directoryDepth) + to.substring(samePrefixIndex);
- }
-
public static Iterable<String> transformFileToAbsolutePath(Iterable<File> files) {
return Iterables.transform(files, Functions.FILE_TO_ABSOLUTE_PATH);
}
diff --git a/test/com/facebook/buck/command/ProjectTest.java b/test/com/facebook/buck/command/ProjectTest.java
index 225026c..5f465a7 100644
--- a/test/com/facebook/buck/command/ProjectTest.java
+++ b/test/com/facebook/buck/command/ProjectTest.java
@@ -60,6 +60,7 @@
import org.junit.Test;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -223,8 +224,8 @@
@Test
public void testGenerateRelativeGenPath() {
String basePathOfModuleWithSlash = "android_res/com/facebook/gifts/";
- String expectedRelativePathToGen =
- "/../../../../buck-out/android/android_res/com/facebook/gifts/gen";
+ Path expectedRelativePathToGen =
+ java.nio.file.Paths.get("/../../../../buck-out/android/android_res/com/facebook/gifts/gen");
assertEquals(
expectedRelativePathToGen, Project.generateRelativeGenPath(basePathOfModuleWithSlash));
}
diff --git a/test/com/facebook/buck/util/PathsTest.java b/test/com/facebook/buck/util/PathsTest.java
index d64d867..9b96aaa 100644
--- a/test/com/facebook/buck/util/PathsTest.java
+++ b/test/com/facebook/buck/util/PathsTest.java
@@ -25,25 +25,6 @@
public class PathsTest {
@Test
- public void testComputeRelativePath() {
- assertEquals("foo/bar", Paths.computeRelativePath("", "foo/bar"));
- assertEquals("foo/bar/", Paths.computeRelativePath("", "foo/bar/"));
-
- assertEquals("../baz/", Paths.computeRelativePath("foo/bar/", "foo/baz/"));
- assertEquals("../../../", Paths.computeRelativePath("a/b/c/d/e/", "a/b/"));
- assertEquals("c/d/e/", Paths.computeRelativePath("a/b/", "a/b/c/d/e/"));
- assertEquals("../../../1/2/3/", Paths.computeRelativePath("a/b/c/d/e/f/", "a/b/c/1/2/3/"));
-
- assertEquals("foo/bar", Paths.computeRelativePath("/prefix/who/cares/",
- "/prefix/who/cares/foo/bar"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testComputeRelativePathThrows() {
- Paths.computeRelativePath("project.properties", "foo/bar.txt");
- }
-
- @Test
public void testNormalizePathSeparator() {
assertEquals("C:/Windows/System32/drivers.dll",
Paths.normalizePathSeparator("C:\\Windows\\System32\\drivers.dll"));