From 1072a8638789e9ad247db4ccb5ae0100ae41ec42 Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Sat, 31 May 2014 20:07:49 -0400 Subject: [PATCH] Improve globbing algorithm. Fixes #439 --- .../javascript/jscomp/CommandLineRunner.java | 78 ++++++++++++------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/src/com/google/javascript/jscomp/CommandLineRunner.java b/src/com/google/javascript/jscomp/CommandLineRunner.java index 7f848e55d65..ccb78ca056c 100644 --- a/src/com/google/javascript/jscomp/CommandLineRunner.java +++ b/src/com/google/javascript/jscomp/CommandLineRunner.java @@ -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; @@ -507,48 +509,72 @@ private void printUsage(PrintStream err) { */ List getJsFiles(PrintStream err) throws CmdLineException, IOException { final Set allJsInputs = new LinkedHashSet<>(); - final List matchedPaths = new ArrayList<>(); List 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() { - @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 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 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() { + @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 getSourceMapLocationMappings() { List locationMappings = Lists.newArrayListWithCapacity(sourceMapLocationMapping.size());