Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve globbing algorithm. Fixes #439 #454

Merged
merged 1 commit into from Jun 5, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 52 additions & 26 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -18,7 +18,9 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -507,48 +509,72 @@ private void printUsage(PrintStream err) {
*/
List<String> getJsFiles(PrintStream err) throws CmdLineException, IOException {
final Set<String> allJsInputs = new LinkedHashSet<>();
final List<String> matchedPaths = new ArrayList<>();
List<String> patterns = new ArrayList<>();
patterns.addAll(js);
patterns.addAll(arguments);
for (String pattern : patterns) {
if (!pattern.contains("*") && !pattern.startsWith("!")) {
allJsInputs.add(pattern);
File matchedFile = new File(pattern);
if (matchedFile.isDirectory()) {
matchPaths(new File(matchedFile, "**.js").toString(), allJsInputs);
} else {
allJsInputs.add(pattern);
}
} else {
FileSystem fs = FileSystems.getDefault();
final boolean remove = pattern.indexOf("!") == 0;
if (remove) pattern = pattern.substring(1);
final PathMatcher matcher = fs.getPathMatcher("glob:" + pattern);
java.nio.file.Files.walkFileTree(
fs.getPath("."), new SimpleFileVisitor<Path>() {
@Override public FileVisitResult visitFile(
Path p, BasicFileAttributes attrs) {
if (matcher.matches(p)) {
if (remove) {
allJsInputs.remove(p.toString());
} else {
allJsInputs.add(p.toString());
}
}
matchedPaths.add(p.toString());
return FileVisitResult.CONTINUE;
}
});
matchPaths(pattern, allJsInputs);
}
}

if (!patterns.isEmpty() && allJsInputs.isEmpty()) {
err.println("Paths attempted to match:");
for (String path : matchedPaths) {
err.println(path);
}

throw new CmdLineException("No inputs matched");
}

return new ArrayList<>(allJsInputs);
}

private void matchPaths(String pattern, final Set<String> allJsInputs)
throws IOException {
FileSystem fs = FileSystems.getDefault();
final boolean remove = pattern.indexOf("!") == 0;
if (remove) pattern = pattern.substring(1);

if (File.separator.equals("\\")) {
pattern = pattern.replace('\\', '/');
}

// Split the pattern into two pieces: the globbing part
// and the non-globbing prefix.
List<String> patternParts =
ImmutableList.copyOf(Splitter.on("/").split(pattern));
String prefix = ".";
for (int i = 0; i < patternParts.size(); i++) {
if (patternParts.get(i).contains("*")) {
if (i == 0) {
break;
} else {
prefix = Joiner.on("/").join(patternParts.subList(0, i));
pattern = Joiner.on("/").join(patternParts.subList(i, patternParts.size()));
}
}
}

final PathMatcher matcher = fs.getPathMatcher("glob:" + pattern);
java.nio.file.Files.walkFileTree(
fs.getPath(prefix), new SimpleFileVisitor<Path>() {
@Override public FileVisitResult visitFile(
Path p, BasicFileAttributes attrs) {
if (matcher.matches(p)) {
if (remove) {
allJsInputs.remove(p.toString());
} else {
allJsInputs.add(p.toString());
}
}
return FileVisitResult.CONTINUE;
}
});
}

List<SourceMap.LocationMapping> getSourceMapLocationMappings() {
List<SourceMap.LocationMapping> locationMappings =
Lists.newArrayListWithCapacity(sourceMapLocationMapping.size());
Expand Down