3838import com .diffplug .spotless .FileSignature ;
3939import com .diffplug .spotless .ThrowingEx ;
4040import com .diffplug .spotless .extra .integration .DiffMessageFormatter ;
41+ import com .diffplug .spotless .extra .middleware .ReviewDogGenerator ;
4142
4243@ DisableCachingByDefault (because = "not worth caching" )
4344public 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 ) {
0 commit comments