19
19
20
20
import java .io .File ;
21
21
import java .io .IOException ;
22
+ import java .util .ArrayList ;
22
23
import java .util .Arrays ;
23
24
import java .util .Collections ;
24
25
import java .util .HashSet ;
30
31
import java .util .regex .Pattern ;
31
32
import java .util .stream .Collectors ;
32
33
import java .util .stream .Stream ;
34
+ import java .util .stream .StreamSupport ;
33
35
34
36
import org .apache .maven .plugin .AbstractMojo ;
35
37
import org .apache .maven .plugin .MojoExecutionException ;
38
40
import org .codehaus .plexus .resource .ResourceManager ;
39
41
import org .codehaus .plexus .resource .loader .FileResourceLoader ;
40
42
import org .codehaus .plexus .util .FileUtils ;
43
+ import org .codehaus .plexus .util .MatchPatterns ;
41
44
import org .eclipse .aether .RepositorySystem ;
42
45
import org .eclipse .aether .RepositorySystemSession ;
43
46
import org .eclipse .aether .repository .RemoteRepository ;
44
47
45
- import com .diffplug .common .collect .Iterables ;
46
48
import com .diffplug .spotless .Formatter ;
47
49
import com .diffplug .spotless .LineEnding ;
48
50
import com .diffplug .spotless .Provisioner ;
@@ -137,38 +139,23 @@ public final void execute() throws MojoExecutionException {
137
139
}
138
140
139
141
private void execute (FormatterFactory formatterFactory ) throws MojoExecutionException {
140
- List <File > files = collectFiles (formatterFactory );
141
142
FormatterConfig config = getFormatterConfig ();
142
- Optional <String > ratchetFrom = formatterFactory .ratchetFrom (config );
143
- Iterable <File > toFormat ;
144
- if (!ratchetFrom .isPresent ()) {
145
- toFormat = files ;
146
- } else {
147
- toFormat = Iterables .filter (files , GitRatchetMaven .instance ().isGitDirty (baseDir , ratchetFrom .get ()));
148
- }
143
+ List <File > files = collectFiles (formatterFactory , config );
144
+
149
145
try (Formatter formatter = formatterFactory .newFormatter (files , config )) {
150
- process (toFormat , formatter );
146
+ process (files , formatter );
151
147
}
152
148
}
153
149
154
- private List <File > collectFiles (FormatterFactory formatterFactory ) throws MojoExecutionException {
155
- Set <String > configuredIncludes = formatterFactory .includes ();
156
- Set <String > configuredExcludes = formatterFactory .excludes ();
157
-
158
- Set <String > includes = configuredIncludes .isEmpty () ? formatterFactory .defaultIncludes () : configuredIncludes ;
159
- if (includes .isEmpty ()) {
160
- throw new MojoExecutionException ("You must specify some files to include, such as '<includes><include>src/**</include></includes>'" );
161
- }
162
-
163
- Set <String > excludes = new HashSet <>(FileUtils .getDefaultExcludesAsList ());
164
- excludes .add (withTrailingSeparator (buildDir .toString ()));
165
- excludes .addAll (configuredExcludes );
166
-
167
- String includesString = String .join ("," , includes );
168
- String excludesString = String .join ("," , excludes );
169
-
150
+ private List <File > collectFiles (FormatterFactory formatterFactory , FormatterConfig config ) throws MojoExecutionException {
151
+ Optional <String > ratchetFrom = formatterFactory .ratchetFrom (config );
170
152
try {
171
- final List <File > files = FileUtils .getFiles (baseDir , includesString , excludesString );
153
+ final List <File > files ;
154
+ if (ratchetFrom .isPresent ()) {
155
+ files = collectFilesFromGit (formatterFactory , ratchetFrom .get ());
156
+ } else {
157
+ files = collectFilesFromFormatterFactory (formatterFactory );
158
+ }
172
159
if (filePatterns == null || filePatterns .isEmpty ()) {
173
160
return files ;
174
161
}
@@ -189,10 +176,68 @@ private List<File> collectFiles(FormatterFactory formatterFactory) throws MojoEx
189
176
}
190
177
}
191
178
179
+ private List <File > collectFilesFromGit (FormatterFactory formatterFactory , String ratchetFrom ) throws MojoExecutionException {
180
+ MatchPatterns includePatterns = MatchPatterns .from (
181
+ withNormalizedFileSeparators (getIncludes (formatterFactory )));
182
+ MatchPatterns excludePatterns = MatchPatterns .from (
183
+ withNormalizedFileSeparators (getExcludes (formatterFactory )));
184
+
185
+ Iterable <String > dirtyFiles ;
186
+ try {
187
+ dirtyFiles = GitRatchetMaven
188
+ .instance ().getDirtyFiles (baseDir , ratchetFrom );
189
+ } catch (IOException e ) {
190
+ throw new MojoExecutionException ("Unable to scan file tree rooted at " + baseDir , e );
191
+ }
192
+
193
+ List <File > result = new ArrayList <>();
194
+ for (String file : withNormalizedFileSeparators (dirtyFiles )) {
195
+ if (includePatterns .matches (file , true )) {
196
+ if (!excludePatterns .matches (file , true )) {
197
+ result .add (new File (baseDir .getPath (), file ));
198
+ }
199
+ }
200
+ }
201
+ return result ;
202
+ }
203
+
204
+ private List <File > collectFilesFromFormatterFactory (FormatterFactory formatterFactory )
205
+ throws MojoExecutionException , IOException {
206
+ String includesString = String .join ("," , getIncludes (formatterFactory ));
207
+ String excludesString = String .join ("," , getExcludes (formatterFactory ));
208
+
209
+ return FileUtils .getFiles (baseDir , includesString , excludesString );
210
+ }
211
+
212
+ private Iterable <String > withNormalizedFileSeparators (Iterable <String > patterns ) {
213
+ return StreamSupport .stream (patterns .spliterator (), true )
214
+ .map (pattern -> pattern .replace ('/' , File .separatorChar ))
215
+ .map (pattern -> pattern .replace ('\\' , File .separatorChar ))
216
+ .collect (Collectors .toSet ());
217
+ }
218
+
192
219
private static String withTrailingSeparator (String path ) {
193
220
return path .endsWith (File .separator ) ? path : path + File .separator ;
194
221
}
195
222
223
+ private Set <String > getIncludes (FormatterFactory formatterFactory ) throws MojoExecutionException {
224
+ Set <String > configuredIncludes = formatterFactory .includes ();
225
+ Set <String > includes = configuredIncludes .isEmpty () ? formatterFactory .defaultIncludes () : configuredIncludes ;
226
+ if (includes .isEmpty ()) {
227
+ throw new MojoExecutionException ("You must specify some files to include, such as '<includes><include>src/**</include></includes>'" );
228
+ }
229
+ return includes ;
230
+ }
231
+
232
+ private Set <String > getExcludes (FormatterFactory formatterFactory ) {
233
+ Set <String > configuredExcludes = formatterFactory .excludes ();
234
+
235
+ Set <String > excludes = new HashSet <>(FileUtils .getDefaultExcludesAsList ());
236
+ excludes .add (withTrailingSeparator (buildDir .toString ()));
237
+ excludes .addAll (configuredExcludes );
238
+ return excludes ;
239
+ }
240
+
196
241
private FormatterConfig getFormatterConfig () {
197
242
ArtifactResolver resolver = new ArtifactResolver (repositorySystem , repositorySystemSession , repositories , getLog ());
198
243
Provisioner provisioner = MavenProvisioner .create (resolver );
0 commit comments