Removed Paths.normalizePathSeparator and Paths.containsBackslash.
Summary:
This removes two methods from com.facebook.buck.util.Paths class, replacing
their usages with methods form MorePaths
Test Plan: ant clean test
diff --git a/src/com/facebook/buck/cli/TargetsCommandOptions.java b/src/com/facebook/buck/cli/TargetsCommandOptions.java
index e46150e..7420a61 100644
--- a/src/com/facebook/buck/cli/TargetsCommandOptions.java
+++ b/src/com/facebook/buck/cli/TargetsCommandOptions.java
@@ -18,7 +18,7 @@
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.parser.NoSuchBuildTargetException;
-import com.facebook.buck.util.Paths;
+import com.facebook.buck.util.MorePaths;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
@@ -79,8 +79,8 @@
// Ignore files that aren't under project root.
if (canonicalFullPath.startsWith(projectRootCanonicalFullPathWithEndSlash)) {
builder.add(
- Paths.normalizePathSeparator(canonicalFullPath.substring(
- projectRootCanonicalFullPathWithEndSlash.length())));
+ MorePaths.newPathInstance(canonicalFullPath.substring(
+ projectRootCanonicalFullPathWithEndSlash.length())).toString());
}
}
return builder.build();
diff --git a/src/com/facebook/buck/java/DefaultJavaLibraryRule.java b/src/com/facebook/buck/java/DefaultJavaLibraryRule.java
index 33567f5..cdd64ab 100644
--- a/src/com/facebook/buck/java/DefaultJavaLibraryRule.java
+++ b/src/com/facebook/buck/java/DefaultJavaLibraryRule.java
@@ -52,7 +52,7 @@
import com.facebook.buck.step.fs.MakeCleanDirectoryStep;
import com.facebook.buck.step.fs.MkdirAndSymlinkFileStep;
import com.facebook.buck.util.BuckConstant;
-import com.facebook.buck.util.Paths;
+import com.facebook.buck.util.MorePaths;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Optional;
@@ -74,6 +74,8 @@
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@@ -715,9 +717,9 @@
//
// Therefore, some path-wrangling is required to produce the correct string.
- String resource = Paths.normalizePathSeparator(rawResource.resolve(context).toString());
- String javaPackageAsPath = javaPackageFinder.findJavaPackageFolderForPath(resource);
- String relativeSymlinkPath;
+ Path resource = MorePaths.separatorsToUnix(rawResource.resolve(context));
+ String javaPackageAsPath = javaPackageFinder.findJavaPackageFolderForPath(resource.toString());
+ Path relativeSymlinkPath;
if (resource.startsWith(BuckConstant.BUCK_OUTPUT_DIRECTORY) ||
@@ -726,23 +728,23 @@
resource.startsWith(BuckConstant.ANNOTATION_DIR)) {
// Handle the case where we depend on the output of another BuildRule. In that case, just
// grab the output and put in the same package as this target would be in.
- relativeSymlinkPath = String.format(
- "%s/%s", targetPackageDir, rawResource.resolve(context).getFileName());
+ relativeSymlinkPath = Paths.get(String.format(
+ "%s/%s", targetPackageDir, rawResource.resolve(context).getFileName()));
} else if ("".equals(javaPackageAsPath)) {
// In this case, the project root is acting as the default package, so the resource path
// works fine.
relativeSymlinkPath = resource;
} else {
- int lastIndex = resource.lastIndexOf(javaPackageAsPath);
+ int lastIndex = resource.toString().lastIndexOf(javaPackageAsPath);
Preconditions.checkState(lastIndex >= 0,
"Resource path %s must contain %s",
resource,
javaPackageAsPath);
- relativeSymlinkPath = resource.substring(lastIndex);
+ relativeSymlinkPath = Paths.get(resource.toString().substring(lastIndex));
}
- String target = outputDirectory + '/' + relativeSymlinkPath;
- MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(resource, target);
+ String target = Paths.get(outputDirectory).resolve(relativeSymlinkPath).toString();
+ MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(resource.toString(), target);
commands.add(link);
}
}
diff --git a/src/com/facebook/buck/model/BuildTarget.java b/src/com/facebook/buck/model/BuildTarget.java
index 8096a9d..8a4632c 100644
--- a/src/com/facebook/buck/model/BuildTarget.java
+++ b/src/com/facebook/buck/model/BuildTarget.java
@@ -17,7 +17,6 @@
package com.facebook.buck.model;
import com.facebook.buck.util.BuckConstant;
-import com.facebook.buck.util.Paths;
import com.facebook.buck.util.ProjectFilesystem;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -45,10 +44,8 @@
"baseName must start with // but was %s",
baseName);
- // On Windows, basePath may contain backslashes, which are not permitted by BuildTarget.
- if (Paths.containsBackslash(baseName)) {
- baseName = Paths.normalizePathSeparator(baseName);
- }
+ // On Windows, baseName may contain backslashes, which are not permitted by BuildTarget.
+ baseName = baseName.replace("\\", "/");
this.baseName = baseName;
this.shortName = Preconditions.checkNotNull(shortName);
diff --git a/src/com/facebook/buck/util/DirectoryTraversers.java b/src/com/facebook/buck/util/DirectoryTraversers.java
index 52cc3b4..5098dc7 100644
--- a/src/com/facebook/buck/util/DirectoryTraversers.java
+++ b/src/com/facebook/buck/util/DirectoryTraversers.java
@@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
+import java.nio.file.Paths;
public class DirectoryTraversers {
@@ -46,7 +47,7 @@
traverser.traverse(new DirectoryTraversal(new File(pathToDirectory)) {
@Override
public void visit(File file, String relativePath) {
- allFiles.add(java.nio.file.Paths.get(pathToDirectory, relativePath).toString());
+ allFiles.add(Paths.get(pathToDirectory, relativePath).toString());
}
});
diff --git a/src/com/facebook/buck/util/MorePaths.java b/src/com/facebook/buck/util/MorePaths.java
index 056f3e4..dff2c96 100644
--- a/src/com/facebook/buck/util/MorePaths.java
+++ b/src/com/facebook/buck/util/MorePaths.java
@@ -42,7 +42,10 @@
* @return The path using UNIX path separators.
*/
public static Path separatorsToUnix(String path) {
- return Paths.get(path.replace(File.separator, "/")).normalize();
+ if (!File.separator.equals("/")) {
+ path = path.replace(File.separator, "/");
+ }
+ return Paths.get(path).normalize();
}
/**
diff --git a/src/com/facebook/buck/util/Paths.java b/src/com/facebook/buck/util/Paths.java
deleted file mode 100644
index 3a685e4..0000000
--- a/src/com/facebook/buck/util/Paths.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright 2012-present Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License. You may obtain
- * a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- */
-
-package com.facebook.buck.util;
-
-public class Paths {
-
- private Paths() {}
-
- /**
- * Returns normalized path. On Windows \ will be replaced with /.
- * @return Normalized path
- */
- public static String normalizePathSeparator(String path) {
- return path.replace("\\", "/");
- }
-
- /** @return true if the specified path contains a backslash character. */
- public static boolean containsBackslash(String path) {
- return path.indexOf('\\') >= 0;
- }
-}
diff --git a/src/com/facebook/buck/util/ProjectFilesystem.java b/src/com/facebook/buck/util/ProjectFilesystem.java
index 4cb1590..7197341 100644
--- a/src/com/facebook/buck/util/ProjectFilesystem.java
+++ b/src/com/facebook/buck/util/ProjectFilesystem.java
@@ -34,6 +34,7 @@
import java.nio.file.FileVisitor;
import java.nio.file.LinkOption;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
@@ -235,7 +236,7 @@
* returned. Otherwise, an {@link Optional} with the first line of the file will be returned.
*/
public Optional<String> readFirstLine(String pathRelativeToProjectRoot) {
- return readFirstLine(java.nio.file.Paths.get(pathRelativeToProjectRoot));
+ return readFirstLine(Paths.get(pathRelativeToProjectRoot));
}
/**
diff --git a/test/com/facebook/buck/command/ProjectTest.java b/test/com/facebook/buck/command/ProjectTest.java
index 5f465a7..78e1e61 100644
--- a/test/com/facebook/buck/command/ProjectTest.java
+++ b/test/com/facebook/buck/command/ProjectTest.java
@@ -47,7 +47,6 @@
import com.facebook.buck.testutil.BuckTestConstant;
import com.facebook.buck.testutil.RuleMap;
import com.facebook.buck.util.HumanReadableException;
-import com.facebook.buck.util.Paths;
import com.facebook.buck.util.ProjectFilesystem;
import com.google.common.base.Function;
import com.google.common.base.Optional;
@@ -838,7 +837,7 @@
assertEquals("You must define a project_config() in example/child/BUCK containing " +
"//example/parent:ex1. The project_config() in //example/child:config transitively " +
"depends on it.",
- Paths.normalizePathSeparator(e.getHumanReadableErrorMessage()));
+ e.getHumanReadableErrorMessage().replace("\\", "/"));
}
}
}
diff --git a/test/com/facebook/buck/rules/BuildContextTest.java b/test/com/facebook/buck/rules/BuildContextTest.java
index a27cdff..4c8dec5 100644
--- a/test/com/facebook/buck/rules/BuildContextTest.java
+++ b/test/com/facebook/buck/rules/BuildContextTest.java
@@ -23,6 +23,7 @@
import com.facebook.buck.testutil.TestConsole;
import com.facebook.buck.util.AndroidPlatformTarget;
import com.facebook.buck.util.HumanReadableException;
+import com.facebook.buck.util.MorePaths;
import com.facebook.buck.util.ProjectFilesystem;
import com.facebook.buck.util.Verbosity;
import com.google.common.base.Optional;
@@ -64,8 +65,7 @@
BuildContext context = builder.build();
Supplier<String> androidBootclasspathSupplier = context.getAndroidBootclasspathSupplier();
- String androidBootclasspath = com.facebook.buck.util.Paths.normalizePathSeparator(
- androidBootclasspathSupplier.get());
+ String androidBootclasspath = MorePaths.newPathInstance(androidBootclasspathSupplier.get()).toString();
assertEquals(
"add-ons/addon-google_apis-google-15/libs/effects.jar:" +
"add-ons/addon-google_apis-google-15/libs/maps.jar:" +
diff --git a/test/com/facebook/buck/rules/NonCheckingBuildRuleFactoryParams.java b/test/com/facebook/buck/rules/NonCheckingBuildRuleFactoryParams.java
index ae22f29..38095f4 100644
--- a/test/com/facebook/buck/rules/NonCheckingBuildRuleFactoryParams.java
+++ b/test/com/facebook/buck/rules/NonCheckingBuildRuleFactoryParams.java
@@ -19,7 +19,7 @@
import com.facebook.buck.model.BuildFileTree;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.parser.BuildTargetParser;
-import com.facebook.buck.util.Paths;
+import com.facebook.buck.util.MorePaths;
import com.facebook.buck.util.ProjectFilesystem;
import com.google.common.collect.ImmutableSet;
@@ -60,7 +60,7 @@
@Override
public String getBasePathOfAncestorTarget(String filePath) {
// Always assume the file is local to the target.
- return Paths.normalizePathSeparator(new File(filePath).getParent());
+ return MorePaths.newPathInstance(filePath).getParent().toString();
}
}
diff --git a/test/com/facebook/buck/util/PathsTest.java b/test/com/facebook/buck/util/PathsTest.java
deleted file mode 100644
index 9b96aaa..0000000
--- a/test/com/facebook/buck/util/PathsTest.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright 2012-present Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License. You may obtain
- * a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- */
-
-package com.facebook.buck.util;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import org.junit.Test;
-
-public class PathsTest {
-
- @Test
- public void testNormalizePathSeparator() {
- assertEquals("C:/Windows/System32/drivers.dll",
- Paths.normalizePathSeparator("C:\\Windows\\System32\\drivers.dll"));
- }
-
- @Test
- public void testContainsBackslash() {
- assertTrue(Paths.containsBackslash("C:\\Windows\\System32\\drivers.dll"));
- assertFalse(Paths.containsBackslash("C:/Windows"));
- assertFalse(Paths.containsBackslash("/usr/bin"));
- }
-}