Skip to content

Commit

Permalink
Maven spotless:check will now throw an error if all of the files are …
Browse files Browse the repository at this point in the history
…as formatted as they can be, except for lints.
  • Loading branch information
nedtwigg committed Jan 14, 2022
1 parent cac4f70 commit d4ec76b
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import com.diffplug.spotless.DirtyState;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.Lint;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
import com.diffplug.spotless.maven.incremental.UpToDateChecker;

Expand All @@ -39,6 +41,7 @@ public class SpotlessCheckMojo extends AbstractSpotlessMojo {
@Override
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
List<File> problemFiles = new ArrayList<>();
TreeMap<File, List<Lint>> allLints = new TreeMap<>();
for (File file : files) {
if (upToDateChecker.isUpToDate(file.toPath())) {
if (getLog().isDebugEnabled()) {
Expand All @@ -48,10 +51,17 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
}

try {
DirtyState dirtyState = DirtyState.of(formatter, file).calculateDirtyState();
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
DirtyState.Calculation calculation = DirtyState.of(formatter, file);
DirtyState dirtyState = calculation.calculateDirtyState();
List<Lint> lints = calculation.calculateLintAgainstDirtyState(dirtyState);

if (!lints.isEmpty()) {
allLints.put(file, lints);
}
if (!dirtyState.isClean()) {
problemFiles.add(file);
} else {
}
if (lints.isEmpty() && dirtyState.isClean()) {
upToDateChecker.setUpToDate(file.toPath());
}
} catch (IOException e) {
Expand All @@ -66,5 +76,13 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
.problemFiles(problemFiles)
.getMessage());
}
if (!allLints.isEmpty()) {
allLints.forEach((file, lints) -> {
for (Lint lint : lints) {
System.err.println(file.getAbsolutePath() + ":" + lint.toString());
}
});
throw new MojoExecutionException("'mvn spotless:apply' cannot fix these violations.");
}
}
}

0 comments on commit d4ec76b

Please sign in to comment.