Skip to content

Commit

Permalink
SpotlessTaskImpl now generates a lint result for check and for `app…
Browse files Browse the repository at this point in the history
…ly` (the line numbers in a lint will change depending on whether the `apply` already ran or not).
  • Loading branch information
nedtwigg committed Jan 14, 2022
1 parent a25eded commit 89339cd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
18 changes: 18 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/DirtyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,23 @@ public DirtyState calculateDirtyState() {
public List<Lint> calculateLintAgainstRaw() {
return formatter.lint(raw, file);
}

public List<Lint> calculateLintAgainstDirtyState(DirtyState dirtyState) {
if (dirtyState.isClean() || dirtyState.didNotConverge()) {
return calculateLintAgainstRaw();
} else {
String canonical = new String(dirtyState.canonicalBytes(), formatter.getEncoding());
return formatter.lint(canonical, file);
}
}

/** If {@link #calculateLintAgainstRaw()} was already called, then you might be able to reuse that value. */
public List<Lint> calculateLintAgainstDirtyState(DirtyState dirtyState, List<Lint> lintsAgainstRaw) {
if (dirtyState.isClean() || dirtyState.didNotConverge()) {
return lintsAgainstRaw;
} else {
return calculateLintAgainstDirtyState(dirtyState);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,19 @@ public File getOutputDirectory() {
}

static final String CONTENT = "content";
static final String LINT = "lint";
static final String LINT_APPLY = "lint-apply";
static final String LINT_CHECK = "lint-check";

File contentDir() {
return new File(outputDirectory, CONTENT);
}

File lintDir() {
return new File(outputDirectory, LINT);
File lintApplyDir() {
return new File(outputDirectory, LINT_APPLY);
}

File lintCheckDir() {
return new File(outputDirectory, LINT_CHECK);
}

protected final LiveCache<List<FormatterStep>> steps = createLive("steps");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public void performAction(InputChanges inputs) throws Exception {
getLogger().info("Not incremental: removing prior outputs");
getFs().delete(d -> d.delete(outputDirectory));
Files.createDirectories(contentDir().toPath());
Files.createDirectories(lintDir().toPath());
Files.createDirectories(lintApplyDir().toPath());
Files.createDirectories(lintCheckDir().toPath());
}

try (Formatter formatter = buildFormatter()) {
Expand All @@ -90,14 +91,16 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
File output = getOutputFile(input);
getLogger().debug("Applying format to " + input + " and writing to " + output);
DirtyState dirtyState;
List<Lint> lints;
List<Lint> lintsCheck, lintsApply;
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
dirtyState = DirtyState.clean();
lints = Collections.emptyList();
lintsCheck = Collections.emptyList();
lintsApply = Collections.emptyList();
} else {
DirtyState.Calculation calculation = DirtyState.of(formatter, input);
dirtyState = calculation.calculateDirtyState();
lints = calculation.calculateLintAgainstRaw();
lintsCheck = calculation.calculateLintAgainstRaw();
lintsApply = calculation.calculateLintAgainstDirtyState(dirtyState, lintsCheck);
}
if (dirtyState.isClean()) {
// Remove previous output if it exists
Expand All @@ -115,25 +118,34 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
dirtyState.writeCanonicalTo(output);
}

File lint = getLintFile(input);
writeLints(lintsCheck, getLintCheckFile(input));
writeLints(lintsApply, getLintApplyFile(input));
}

private void writeLints(List<Lint> lints, File lintFile) throws IOException {
if (lints.isEmpty()) {
Files.deleteIfExists(lint.toPath());
Files.deleteIfExists(lintFile.toPath());
} else {
Lint.toFile(lints, lint);
Lint.toFile(lints, lintFile);
}
}

private void deletePreviousResult(File input) throws IOException {
delete(getOutputFile(input));
delete(getLintFile(input));
delete(getLintCheckFile(input));
delete(getLintApplyFile(input));
}

private File getOutputFile(File input) {
return new File(contentDir(), relativize(input));
}

private File getLintFile(File input) {
return new File(lintDir(), relativize(input));
private File getLintCheckFile(File input) {
return new File(lintCheckDir(), relativize(input));
}

private File getLintApplyFile(File input) {
return new File(lintApplyDir(), relativize(input));
}

private void delete(File file) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ static abstract class ClientTask extends DefaultTask {
abstract Property<File> getSpotlessOutDirectory();

File contentDir() {
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.CONTENT);
return new File(getSpotlessOutDirectory().get(), SpotlessTask.CONTENT);
}

File lintDir() {
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.LINT);
File lintApplyDir() {
return new File(getSpotlessOutDirectory().get(), SpotlessTask.LINT_APPLY);
}

File lintCheckDir() {
return new File(getSpotlessOutDirectory().get(), SpotlessTask.LINT_CHECK);
}

@Internal
Expand Down

0 comments on commit 89339cd

Please sign in to comment.