20
20
import java .nio .file .Files ;
21
21
import java .nio .file .Path ;
22
22
import java .nio .file .StandardCopyOption ;
23
+ import java .util .Collections ;
24
+ import java .util .List ;
23
25
24
26
import javax .annotation .Nullable ;
25
27
import javax .inject .Inject ;
38
40
import com .diffplug .common .base .StringPrinter ;
39
41
import com .diffplug .spotless .DirtyState ;
40
42
import com .diffplug .spotless .Formatter ;
43
+ import com .diffplug .spotless .Lint ;
41
44
import com .diffplug .spotless .extra .GitRatchet ;
42
45
43
46
@ CacheableTask
@@ -64,7 +67,8 @@ public void performAction(InputChanges inputs) throws Exception {
64
67
if (!inputs .isIncremental ()) {
65
68
getLogger ().info ("Not incremental: removing prior outputs" );
66
69
getFs ().delete (d -> d .delete (outputDirectory ));
67
- Files .createDirectories (outputDirectory .toPath ());
70
+ Files .createDirectories (contentDir ().toPath ());
71
+ Files .createDirectories (lintDir ().toPath ());
68
72
}
69
73
70
74
try (Formatter formatter = buildFormatter ()) {
@@ -86,10 +90,14 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
86
90
File output = getOutputFile (input );
87
91
getLogger ().debug ("Applying format to " + input + " and writing to " + output );
88
92
DirtyState dirtyState ;
93
+ List <Lint > lints ;
89
94
if (ratchet != null && ratchet .isClean (getProjectDir ().get ().getAsFile (), getRootTreeSha (), input )) {
90
95
dirtyState = DirtyState .clean ();
96
+ lints = Collections .emptyList ();
91
97
} else {
92
- dirtyState = DirtyState .of (formatter , input ).calculateDirtyState ();
98
+ DirtyState .Calculation calculation = DirtyState .of (formatter , input );
99
+ dirtyState = calculation .calculateDirtyState ();
100
+ lints = calculation .calculateLintAgainstRaw ();
93
101
}
94
102
if (dirtyState .isClean ()) {
95
103
// Remove previous output if it exists
@@ -106,27 +114,46 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
106
114
Files .copy (input .toPath (), output .toPath (), StandardCopyOption .REPLACE_EXISTING , StandardCopyOption .COPY_ATTRIBUTES );
107
115
dirtyState .writeCanonicalTo (output );
108
116
}
117
+
118
+ File lint = getLintFile (input );
119
+ if (lints .isEmpty ()) {
120
+ Files .deleteIfExists (lint .toPath ());
121
+ } else {
122
+ Lint .toFile (lints , lint );
123
+ }
109
124
}
110
125
111
126
private void deletePreviousResult (File input ) throws IOException {
112
- File output = getOutputFile (input );
113
- if (output .isDirectory ()) {
114
- getFs ().delete (d -> d .delete (output ));
127
+ delete (getOutputFile (input ));
128
+ delete (getLintFile (input ));
129
+ }
130
+
131
+ private File getOutputFile (File input ) {
132
+ return new File (contentDir (), relativize (input ));
133
+ }
134
+
135
+ private File getLintFile (File input ) {
136
+ return new File (lintDir (), relativize (input ));
137
+ }
138
+
139
+ private void delete (File file ) throws IOException {
140
+ if (file .isDirectory ()) {
141
+ getFs ().delete (d -> d .delete (file ));
115
142
} else {
116
- Files .deleteIfExists (output .toPath ());
143
+ Files .deleteIfExists (file .toPath ());
117
144
}
118
145
}
119
146
120
- private File getOutputFile (File input ) {
147
+ private String relativize (File input ) {
121
148
File projectDir = getProjectDir ().get ().getAsFile ();
122
149
String outputFileName = FormatExtension .relativize (projectDir , input );
123
- if (outputFileName == null ) {
124
- throw new IllegalArgumentException (StringPrinter .buildString (printer -> {
125
- printer .println ("Spotless error! All target files must be within the project dir." );
126
- printer .println (" project dir: " + projectDir .getAbsolutePath ());
127
- printer .println (" target: " + input .getAbsolutePath ());
128
- }));
150
+ if (outputFileName != null ) {
151
+ return outputFileName ;
129
152
}
130
- return new File (outputDirectory , outputFileName );
153
+ throw new IllegalArgumentException (StringPrinter .buildString (printer -> {
154
+ printer .println ("Spotless error! All target files must be within the project dir." );
155
+ printer .println (" project dir: " + projectDir .getAbsolutePath ());
156
+ printer .println (" target: " + input .getAbsolutePath ());
157
+ }));
131
158
}
132
159
}
0 commit comments