diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java index 4fc8ef45b..0af26a795 100755 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/AbstractCacheableFormatter.java @@ -52,14 +52,8 @@ public Result formatFile(Path file) { Files.write(file, formattedCode.getBytes(configurationSource.getEncoding())); return Result.SUCCESS; - } catch (IOException e) { - log.warn(e); - return Result.FAIL; - } catch (MalformedTreeException e) { - log.warn(e); - return Result.FAIL; - } catch (BadLocationException e) { - log.warn(e); + } catch (Exception e) { + log.warn("Error formating: " + file.toAbsolutePath(), e); return Result.FAIL; } } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java index b8a3bb01e..db92ce424 100644 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/FormatterMojo.java @@ -22,9 +22,11 @@ import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.maven.plugin.AbstractMojo; @@ -34,13 +36,17 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.codehaus.plexus.util.AbstractScanner; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.MatchPatterns; import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.SelectorUtils; import org.codehaus.plexus.util.StringUtils; import org.eclipse.jdt.core.formatter.CodeFormatter; import org.xml.sax.SAXException; import com.google.common.base.Stopwatch; +import com.google.common.collect.Lists; import com.marvinformatics.formatter.java.JavaFormatter; import com.marvinformatics.formatter.javascript.JavascriptFormatter; import com.marvinformatics.formatter.model.ConfigReadException; @@ -221,7 +227,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { ResultCollector rc = new ResultCollector(); RecursiveWalk w = new RecursiveWalk(createJavaFormatter(), createJsFormatter(), rc, - pathsToScan.filter(file -> isValidDirectory(file)).map(file -> file.toPath())); + pathsToScan.filter(file -> isValidDirectory(file)).map(file -> file.toPath()), excludes()); ForkJoinPool p = new ForkJoinPool(8); p.invoke(w); @@ -230,6 +236,35 @@ public void execute() throws MojoExecutionException, MojoFailureException { report(watch, rc); } + private MatchPatterns excludes() { + List excludes; + if (this.excludes != null) + excludes = Lists.newArrayList(this.excludes); + else + excludes = Lists.newArrayList(); + excludes.addAll(Lists.newArrayList(AbstractScanner.DEFAULTEXCLUDES)); + + return MatchPatterns.from(new StreamIterable<>(excludes.stream().map(pattern -> normalize(pattern)))); + } + + private String normalize(String pattern) { + pattern = pattern.trim(); + + if (pattern.startsWith(SelectorUtils.REGEX_HANDLER_PREFIX)) { + if (File.separatorChar == '\\') + pattern = StringUtils.replace(pattern, "/", "\\\\"); + else + pattern = StringUtils.replace(pattern, "\\\\", "/"); + } else { + pattern = pattern.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar); + + if (pattern.endsWith(File.separator)) + pattern += "**"; + } + + return pattern; + } + protected void report(Stopwatch watch, ResultCollector rc) throws MojoFailureException, MojoExecutionException { Log log = getLog(); log.info("Successfully formatted: " + rc.successCount() + " file(s)"); diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java index 86881a9df..18a96ced0 100644 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/RecursiveWalk.java @@ -27,6 +27,8 @@ import java.util.concurrent.RecursiveAction; import java.util.stream.Stream; +import org.codehaus.plexus.util.MatchPatterns; + import com.marvinformatics.formatter.java.JavaFormatter; import com.marvinformatics.formatter.javascript.JavascriptFormatter; @@ -36,19 +38,21 @@ public class RecursiveWalk extends RecursiveAction { private final ThreadLocal javaFormatter; private final ResultCollector resultCollector; private final ThreadLocal jsFormatter; + private final MatchPatterns excludes; public RecursiveWalk(ThreadLocal javaFormatter, ThreadLocal jsFormatter, - ResultCollector resultCollector, Stream stream) { + ResultCollector resultCollector, Stream stream, MatchPatterns excludes) { super(); this.paths = stream; this.javaFormatter = javaFormatter; this.jsFormatter = jsFormatter; this.resultCollector = resultCollector; + this.excludes = excludes; } public RecursiveWalk(ThreadLocal javaFormatter, ThreadLocal jsFormatter, - ResultCollector resultCollector, Path path) { - this(javaFormatter, jsFormatter, resultCollector, Collections.singletonList(path).stream()); + ResultCollector resultCollector, Path path, MatchPatterns excludes) { + this(javaFormatter, jsFormatter, resultCollector, Collections.singletonList(path).stream(), excludes); } @Override @@ -59,8 +63,12 @@ protected void compute() { Files.walkFileTree(path, new SimpleFileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (matchExclusions(dir)) + return FileVisitResult.SKIP_SUBTREE; + if (!dir.equals(path)) { - RecursiveWalk w = new RecursiveWalk(javaFormatter, jsFormatter, resultCollector, dir); + RecursiveWalk w = new RecursiveWalk(javaFormatter, jsFormatter, resultCollector, dir, + excludes); w.fork(); walks.add(w); @@ -74,8 +82,13 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String name = file.getName(file.getNameCount() - 1).toString(); + if (matchExclusions(file)) + return FileVisitResult.CONTINUE; + if (name.endsWith(".java")) resultCollector.collect(javaFormatter.get().formatFile(file)); + if (name.endsWith(".js")) + resultCollector.collect(jsFormatter.get().formatFile(file)); return FileVisitResult.CONTINUE; } @@ -89,4 +102,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO w.join(); } } + + private boolean matchExclusions(Path path) { + return excludes.matches(path.toAbsolutePath().toString(), true); + } } diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/StreamIterable.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/StreamIterable.java new file mode 100644 index 000000000..14c0e997e --- /dev/null +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/StreamIterable.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com) + * + * 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.marvinformatics.formatter; + +import java.util.Iterator; +import java.util.stream.Stream; + +public class StreamIterable implements Iterable { + + private final Stream stream; + + public StreamIterable(Stream stream) { + this.stream = stream; + } + + @Override + public Iterator iterator() { + return stream.iterator(); + } +} \ No newline at end of file diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/java/JavaFormatter.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/java/JavaFormatter.java index 0d572ac16..58e946df2 100755 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/java/JavaFormatter.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/java/JavaFormatter.java @@ -50,10 +50,9 @@ public JavaFormatter(Map options, ConfigurationSource cfg) { public String doFormat(String code) throws IOException, BadLocationException { TextEdit te = formatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, configurationSource.lineEnding().getChars()); - if (te == null) { - log.debug("Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); - return null; - } + if (te == null) + throw new IllegalArgumentException( + "Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); IDocument doc = new Document(code); te.apply(doc); diff --git a/maven-plugin/src/main/java/com/marvinformatics/formatter/javascript/JavascriptFormatter.java b/maven-plugin/src/main/java/com/marvinformatics/formatter/javascript/JavascriptFormatter.java index 684cd0161..e8310b49f 100755 --- a/maven-plugin/src/main/java/com/marvinformatics/formatter/javascript/JavascriptFormatter.java +++ b/maven-plugin/src/main/java/com/marvinformatics/formatter/javascript/JavascriptFormatter.java @@ -43,10 +43,9 @@ public JavascriptFormatter(Map options, ConfigurationSource cfg) public String doFormat(String code) throws IOException, BadLocationException { TextEdit te = formatter.format(CodeFormatter.K_JAVASCRIPT_UNIT, code, 0, code.length(), 0, configurationSource.lineEnding().getChars()); - if (te == null) { - log.debug("Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); - return null; - } + if (te == null) + throw new IllegalArgumentException( + "Code cannot be formatted. Possible cause " + "is unmatched source/target/compliance version."); IDocument doc = new Document(code); te.apply(doc);