Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Restoring excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Feb 2, 2017
1 parent e0d4e4e commit 0865326
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -230,6 +236,35 @@ public void execute() throws MojoExecutionException, MojoFailureException {
report(watch, rc);
}

private MatchPatterns excludes() {
List<String> 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)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,19 +38,21 @@ public class RecursiveWalk extends RecursiveAction {
private final ThreadLocal<JavaFormatter> javaFormatter;
private final ResultCollector resultCollector;
private final ThreadLocal<JavascriptFormatter> jsFormatter;
private final MatchPatterns excludes;

public RecursiveWalk(ThreadLocal<JavaFormatter> javaFormatter, ThreadLocal<JavascriptFormatter> jsFormatter,
ResultCollector resultCollector, Stream<Path> stream) {
ResultCollector resultCollector, Stream<Path> stream, MatchPatterns excludes) {
super();
this.paths = stream;
this.javaFormatter = javaFormatter;
this.jsFormatter = jsFormatter;
this.resultCollector = resultCollector;
this.excludes = excludes;
}

public RecursiveWalk(ThreadLocal<JavaFormatter> javaFormatter, ThreadLocal<JavascriptFormatter> 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
Expand All @@ -59,8 +63,12 @@ protected void compute() {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@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);

Expand All @@ -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;
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<T> implements Iterable<T> {

private final Stream<T> stream;

public StreamIterable(Stream<T> stream) {
this.stream = stream;
}

@Override
public Iterator<T> iterator() {
return stream.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public JavaFormatter(Map<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public JavascriptFormatter(Map<String, String> 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);
Expand Down

0 comments on commit 0865326

Please sign in to comment.