Skip to content

Commit 36947e6

Browse files
committed
Refactor SpotlessCheck to use that formatter so that we can ditch the clumsy cache.
1 parent a3cad7b commit 36947e6

File tree

2 files changed

+10
-57
lines changed

2 files changed

+10
-57
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.ByteArrayOutputStream;
1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
2122
import java.nio.file.Files;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
@@ -32,6 +33,7 @@
3233

3334
import com.diffplug.spotless.FileSignature;
3435
import com.diffplug.spotless.ThrowingEx;
36+
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
3537

3638
public abstract class SpotlessCheck extends SpotlessTaskService.ClientTask {
3739
public void performActionTest() throws IOException {
@@ -91,9 +93,14 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
9193
});
9294
if (!problemFiles.isEmpty()) {
9395
Collections.sort(problemFiles);
94-
String errMsg = errorMsgForCheck(problemFiles, "Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.");
95-
throw new GradleException(errMsg);
96-
96+
throw new GradleException(DiffMessageFormatter.builder()
97+
.runToFix("Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.")
98+
.formatterFolder(
99+
getProject().getRootDir().toPath(),
100+
getSpotlessOutDirectory().get().toPath(),
101+
StandardCharsets.UTF_8.name())
102+
.problemFiles(problemFiles)
103+
.getMessage());
97104
}
98105
}
99106
}

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

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
package com.diffplug.gradle.spotless;
1717

1818
import java.io.File;
19-
import java.io.IOException;
20-
import java.io.Serializable;
2119
import java.util.Collections;
2220
import java.util.HashMap;
23-
import java.util.List;
2421
import java.util.Map;
2522

2623
import org.gradle.api.DefaultTask;
@@ -31,10 +28,6 @@
3128

3229
import com.diffplug.common.base.Preconditions;
3330
import com.diffplug.common.base.Unhandled;
34-
import com.diffplug.common.collect.Maps;
35-
import com.diffplug.spotless.FileSignature;
36-
import com.diffplug.spotless.Formatter;
37-
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
3831

3932
/**
4033
* Allows the check and apply tasks to coordinate
@@ -47,8 +40,6 @@ public abstract class SpotlessTaskService implements BuildService<BuildServicePa
4740
private final Map<String, SpotlessTask> source = Collections.synchronizedMap(new HashMap<>());
4841

4942
public void registerSourceAlreadyRan(SpotlessTask task) {
50-
// if there's a registered source, we can wipe the cache
51-
cachePut(task.getOutputDirectory(), null, null);
5243
source.put(task.getPath(), task);
5344
}
5445

@@ -97,50 +88,5 @@ protected boolean sourceDidWork() {
9788
protected boolean applyHasRun() {
9889
return service().apply.containsKey(sourceTaskPath());
9990
}
100-
101-
protected String errorMsgForCheck(List<File> problemFiles, String runToFix) throws IOException {
102-
SpotlessTask task = service().source.get(sourceTaskPath());
103-
((SpotlessCheck) this).getSpotlessOutDirectory();
104-
if (task != null) {
105-
try (Formatter formatter = task.buildFormatter()) {
106-
String msg = DiffMessageFormatter.builder()
107-
.runToFix(runToFix)
108-
.formatter(formatter)
109-
.problemFiles(problemFiles)
110-
.getMessage();
111-
cachePut(FileSignature.signAsList(problemFiles), msg);
112-
return msg;
113-
}
114-
} else {
115-
return cacheGet(FileSignature.signAsList(problemFiles));
116-
}
117-
}
118-
119-
private void cachePut(FileSignature key, String msg) {
120-
SpotlessTaskService.cachePut(getSpotlessOutDirectory().get(), key, msg);
121-
}
122-
123-
private String cacheGet(FileSignature key) {
124-
Map.Entry<FileSignature, String> cached = SerializableMisc.fromFile(Map.Entry.class, getCacheFile(getSpotlessOutDirectory().get()));
125-
if (cached != null && cached.getKey().equals(key)) {
126-
return cached.getValue();
127-
} else {
128-
throw new IllegalStateException(getPath() + " is running but " + sourceTaskPath() + " was up-to-date and didn't run");
129-
}
130-
}
131-
}
132-
133-
private static void cachePut(File spotlessOut, FileSignature key, String msg) {
134-
File cacheFile = getCacheFile(spotlessOut);
135-
if (key == null) {
136-
cacheFile.delete();
137-
} else {
138-
SerializableMisc.toFile((Serializable) Maps.immutableEntry(key, msg), cacheFile);
139-
}
140-
}
141-
142-
private static File getCacheFile(File spotlessOut) {
143-
File parent = spotlessOut.getParentFile();
144-
return new File(parent, spotlessOut.getName() + "-errorMsg");
14591
}
14692
}

0 commit comments

Comments
 (0)