Skip to content

Commit 6688b33

Browse files
committed
Add reviewDog property and pipe it to spotlessCheck
Signed-off-by: yongjunhong <[email protected]>
1 parent a94b357 commit 6688b33

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.diffplug.spotless.FileSignature;
3939
import com.diffplug.spotless.ThrowingEx;
4040
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
41+
import com.diffplug.spotless.extra.middleware.ReviewDogGenerator;
4142

4243
@DisableCachingByDefault(because = "not worth caching")
4344
public abstract class SpotlessCheck extends SpotlessTaskService.ClientTask {
@@ -47,6 +48,9 @@ public abstract class SpotlessCheck extends SpotlessTaskService.ClientTask {
4748
@Input
4849
public abstract Property<String> getRunToFixMessage();
4950

51+
@Input
52+
public abstract Property<Boolean> getReviewDog();
53+
5054
public void performActionTest() throws IOException {
5155
performAction(true);
5256
}
@@ -61,29 +65,50 @@ private void performAction(boolean isTest) throws IOException {
6165
ConfigurableFileTree lintsFiles = getConfigCacheWorkaround().fileTree().from(getSpotlessLintsDirectory().get());
6266
if (cleanFiles.isEmpty() && lintsFiles.isEmpty()) {
6367
getState().setDidWork(sourceDidWork());
64-
} else if (!isTest && applyHasRun()) {
68+
return;
69+
}
70+
if (!isTest && applyHasRun()) {
6571
// if our matching apply has already run, then we don't need to do anything
6672
getState().setDidWork(false);
67-
} else {
68-
List<File> unformattedFiles = getUncleanFiles(cleanFiles);
69-
if (!unformattedFiles.isEmpty()) {
70-
// if any files are unformatted, we show those
71-
throw new GradleException(DiffMessageFormatter.builder()
72-
.runToFix(getRunToFixMessage().get())
73-
.formatterFolder(
74-
getProjectDir().get().getAsFile().toPath(),
75-
getSpotlessCleanDirectory().get().toPath(),
76-
getEncoding().get())
77-
.problemFiles(unformattedFiles)
78-
.getMessage());
79-
} else {
80-
// We only show lints if there are no unformatted files.
81-
// This is because lint line numbers are relative to the
82-
// formatted content, and formatting often fixes lints.
83-
boolean detailed = false;
84-
throw new GradleException(super.allLintsErrorMsgDetailed(lintsFiles, detailed));
73+
return;
74+
}
75+
76+
List<File> unformattedFiles = getUncleanFiles(cleanFiles);
77+
if (!unformattedFiles.isEmpty()) {
78+
if (getReviewDog().get()) {
79+
for (File file : unformattedFiles) {
80+
String originalContent = new String(Files.readAllBytes(file.toPath()), getEncoding().get());
81+
File cleanFile = new File(getSpotlessCleanDirectory().get().getName(), getProjectDir().get().getAsFile().toPath().relativize(file.toPath()).toString());
82+
String formattedContent = new String(Files.readAllBytes(cleanFile.toPath()), getEncoding().get());
83+
84+
// TODO : send or save the output of ReviewDogGenerator.rdjsonlDiff
85+
ReviewDogGenerator.rdjsonlDiff(file.getPath(), originalContent, formattedContent);
86+
}
87+
}
88+
89+
throw new GradleException(DiffMessageFormatter.builder()
90+
.runToFix(getRunToFixMessage().get())
91+
.formatterFolder(
92+
getProjectDir().get().getAsFile().toPath(),
93+
getSpotlessCleanDirectory().get().toPath(),
94+
getEncoding().get())
95+
.problemFiles(unformattedFiles)
96+
.getMessage());
97+
}
98+
99+
if (getReviewDog().get()) {
100+
for (File file : lintsFiles.getFiles()) {
101+
String path = file.getPath();
102+
// TODO : send or save the output of ReviewDogGenerator.rdjsonlLints
103+
ReviewDogGenerator.rdjsonlLints(path, null, null);
85104
}
86105
}
106+
107+
// We only show lints if there are no unformatted files.
108+
// This is because lint line numbers are relative to the
109+
// formatted content, and formatting often fixes lints.
110+
boolean detailed = false;
111+
throw new GradleException(super.allLintsErrorMsgDetailed(lintsFiles, detailed));
87112
}
88113

89114
private @NotNull List<File> getUncleanFiles(ConfigurableFileTree cleanFiles) {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -267,6 +267,24 @@ public void setEnforceCheck(boolean enforceCheck) {
267267
this.enforceCheck = enforceCheck;
268268
}
269269

270+
boolean reviewDog = false;
271+
272+
/**
273+
* Returns {@code true} if ReviewDog output should be generated; {@code false} otherwise.
274+
*/
275+
public boolean isReviewDog() {
276+
return reviewDog;
277+
}
278+
279+
/**
280+
* Configures Spotless to generate ReviewDog output if {@code true}.
281+
* <p>
282+
* {@code false} by default.
283+
*/
284+
public void setReviewDog(boolean reviewDog) {
285+
this.reviewDog = reviewDog;
286+
}
287+
270288
@SuppressWarnings("unchecked")
271289
public <T extends FormatExtension> void format(String name, Class<T> clazz, Action<T> configure) {
272290
maybeCreate(name, clazz).lazyActions.add((Action<FormatExtension>) configure);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,6 +91,9 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
9191

9292
// if the user runs both, make sure that apply happens first,
9393
task.mustRunAfter(applyTask);
94+
95+
// if the user enables the review dog, spotlessCheck will return the review dog format output
96+
task.getReviewDog().set(this.reviewDog);
9497
});
9598
rootCheckTask.configure(task -> task.dependsOn(checkTask));
9699

0 commit comments

Comments
 (0)