From 89339cdc624a2f2921685462af78da568d5acab9 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Jan 2022 22:55:30 -0800 Subject: [PATCH] SpotlessTaskImpl now generates a lint result for `check` and for `apply` (the line numbers in a lint will change depending on whether the `apply` already ran or not). --- .../com/diffplug/spotless/DirtyState.java | 18 +++++++++++ .../gradle/spotless/SpotlessTask.java | 11 +++++-- .../gradle/spotless/SpotlessTaskImpl.java | 32 +++++++++++++------ .../gradle/spotless/SpotlessTaskService.java | 10 ++++-- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/DirtyState.java b/lib/src/main/java/com/diffplug/spotless/DirtyState.java index 90c96dc5e5..d5aab740cd 100644 --- a/lib/src/main/java/com/diffplug/spotless/DirtyState.java +++ b/lib/src/main/java/com/diffplug/spotless/DirtyState.java @@ -142,5 +142,23 @@ public DirtyState calculateDirtyState() { public List calculateLintAgainstRaw() { return formatter.lint(raw, file); } + + public List 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 calculateLintAgainstDirtyState(DirtyState dirtyState, List lintsAgainstRaw) { + if (dirtyState.isClean() || dirtyState.didNotConverge()) { + return lintsAgainstRaw; + } else { + return calculateLintAgainstDirtyState(dirtyState); + } + } } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java index 545ebe3f61..e99ea766d0 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java @@ -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> steps = createLive("steps"); diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java index a37a7306be..0e68b8edcc 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java @@ -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()) { @@ -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 lints; + List 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 @@ -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 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 { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java index 562dd85790..f8926f674f 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java @@ -96,11 +96,15 @@ static abstract class ClientTask extends DefaultTask { abstract Property 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