Skip to content

Commit 89339cd

Browse files
committed
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).
1 parent a25eded commit 89339cd

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

lib/src/main/java/com/diffplug/spotless/DirtyState.java

+18
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,23 @@ public DirtyState calculateDirtyState() {
142142
public List<Lint> calculateLintAgainstRaw() {
143143
return formatter.lint(raw, file);
144144
}
145+
146+
public List<Lint> calculateLintAgainstDirtyState(DirtyState dirtyState) {
147+
if (dirtyState.isClean() || dirtyState.didNotConverge()) {
148+
return calculateLintAgainstRaw();
149+
} else {
150+
String canonical = new String(dirtyState.canonicalBytes(), formatter.getEncoding());
151+
return formatter.lint(canonical, file);
152+
}
153+
}
154+
155+
/** If {@link #calculateLintAgainstRaw()} was already called, then you might be able to reuse that value. */
156+
public List<Lint> calculateLintAgainstDirtyState(DirtyState dirtyState, List<Lint> lintsAgainstRaw) {
157+
if (dirtyState.isClean() || dirtyState.didNotConverge()) {
158+
return lintsAgainstRaw;
159+
} else {
160+
return calculateLintAgainstDirtyState(dirtyState);
161+
}
162+
}
145163
}
146164
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,19 @@ public File getOutputDirectory() {
155155
}
156156

157157
static final String CONTENT = "content";
158-
static final String LINT = "lint";
158+
static final String LINT_APPLY = "lint-apply";
159+
static final String LINT_CHECK = "lint-check";
159160

160161
File contentDir() {
161162
return new File(outputDirectory, CONTENT);
162163
}
163164

164-
File lintDir() {
165-
return new File(outputDirectory, LINT);
165+
File lintApplyDir() {
166+
return new File(outputDirectory, LINT_APPLY);
167+
}
168+
169+
File lintCheckDir() {
170+
return new File(outputDirectory, LINT_CHECK);
166171
}
167172

168173
protected final LiveCache<List<FormatterStep>> steps = createLive("steps");

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public void performAction(InputChanges inputs) throws Exception {
6868
getLogger().info("Not incremental: removing prior outputs");
6969
getFs().delete(d -> d.delete(outputDirectory));
7070
Files.createDirectories(contentDir().toPath());
71-
Files.createDirectories(lintDir().toPath());
71+
Files.createDirectories(lintApplyDir().toPath());
72+
Files.createDirectories(lintCheckDir().toPath());
7273
}
7374

7475
try (Formatter formatter = buildFormatter()) {
@@ -90,14 +91,16 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
9091
File output = getOutputFile(input);
9192
getLogger().debug("Applying format to " + input + " and writing to " + output);
9293
DirtyState dirtyState;
93-
List<Lint> lints;
94+
List<Lint> lintsCheck, lintsApply;
9495
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
9596
dirtyState = DirtyState.clean();
96-
lints = Collections.emptyList();
97+
lintsCheck = Collections.emptyList();
98+
lintsApply = Collections.emptyList();
9799
} else {
98100
DirtyState.Calculation calculation = DirtyState.of(formatter, input);
99101
dirtyState = calculation.calculateDirtyState();
100-
lints = calculation.calculateLintAgainstRaw();
102+
lintsCheck = calculation.calculateLintAgainstRaw();
103+
lintsApply = calculation.calculateLintAgainstDirtyState(dirtyState, lintsCheck);
101104
}
102105
if (dirtyState.isClean()) {
103106
// Remove previous output if it exists
@@ -115,25 +118,34 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
115118
dirtyState.writeCanonicalTo(output);
116119
}
117120

118-
File lint = getLintFile(input);
121+
writeLints(lintsCheck, getLintCheckFile(input));
122+
writeLints(lintsApply, getLintApplyFile(input));
123+
}
124+
125+
private void writeLints(List<Lint> lints, File lintFile) throws IOException {
119126
if (lints.isEmpty()) {
120-
Files.deleteIfExists(lint.toPath());
127+
Files.deleteIfExists(lintFile.toPath());
121128
} else {
122-
Lint.toFile(lints, lint);
129+
Lint.toFile(lints, lintFile);
123130
}
124131
}
125132

126133
private void deletePreviousResult(File input) throws IOException {
127134
delete(getOutputFile(input));
128-
delete(getLintFile(input));
135+
delete(getLintCheckFile(input));
136+
delete(getLintApplyFile(input));
129137
}
130138

131139
private File getOutputFile(File input) {
132140
return new File(contentDir(), relativize(input));
133141
}
134142

135-
private File getLintFile(File input) {
136-
return new File(lintDir(), relativize(input));
143+
private File getLintCheckFile(File input) {
144+
return new File(lintCheckDir(), relativize(input));
145+
}
146+
147+
private File getLintApplyFile(File input) {
148+
return new File(lintApplyDir(), relativize(input));
137149
}
138150

139151
private void delete(File file) throws IOException {

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ static abstract class ClientTask extends DefaultTask {
9696
abstract Property<File> getSpotlessOutDirectory();
9797

9898
File contentDir() {
99-
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.CONTENT);
99+
return new File(getSpotlessOutDirectory().get(), SpotlessTask.CONTENT);
100100
}
101101

102-
File lintDir() {
103-
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.LINT);
102+
File lintApplyDir() {
103+
return new File(getSpotlessOutDirectory().get(), SpotlessTask.LINT_APPLY);
104+
}
105+
106+
File lintCheckDir() {
107+
return new File(getSpotlessOutDirectory().get(), SpotlessTask.LINT_CHECK);
104108
}
105109

106110
@Internal

0 commit comments

Comments
 (0)