15
15
*/
16
16
package com .diffplug .gradle .spotless ;
17
17
18
- import static java .nio .charset .StandardCharsets .UTF_8 ;
19
18
import static java .util .Collections .singleton ;
20
19
21
20
import java .io .File ;
21
+ import java .io .IOException ;
22
+ import java .nio .charset .StandardCharsets ;
22
23
import java .nio .file .Files ;
23
24
import java .nio .file .Path ;
24
25
import java .nio .file .Paths ;
26
+ import java .util .ArrayList ;
27
+ import java .util .Collections ;
25
28
import java .util .List ;
26
29
import java .util .Set ;
27
30
import java .util .stream .Stream ;
37
40
import org .eclipse .aether .RepositorySystemSession ;
38
41
import org .eclipse .aether .repository .RemoteRepository ;
39
42
43
+ import com .diffplug .spotless .Formatter ;
40
44
import com .diffplug .spotless .FormatterStep ;
45
+ import com .diffplug .spotless .LineEnding ;
41
46
import com .diffplug .spotless .Provisioner ;
47
+ import com .diffplug .spotless .ThrowingEx ;
42
48
import com .diffplug .spotless .extra .java .EclipseFormatterStep ;
43
49
44
50
@ Mojo (name = "spotless" )
@@ -62,31 +68,43 @@ public class SpotlessMojo extends AbstractMojo {
62
68
@ Override
63
69
public void execute () throws MojoExecutionException , MojoFailureException {
64
70
ArtifactResolver resolver = new ArtifactResolver (repositorySystem , repositorySystemSession , repositories );
65
-
66
71
Provisioner provisioner = MavenProvisioner .create (resolver );
72
+
73
+ // create the eclipse step
67
74
Set <File > settingFiles = singleton (new File (eclipseFormatFile ));
68
- FormatterStep step = EclipseFormatterStep .create (settingFiles , provisioner );
75
+ FormatterStep step = EclipseFormatterStep .create (EclipseFormatterStep .defaultVersion (),
76
+ settingFiles , provisioner );
69
77
78
+ // collect all the files that are going to be formatted
79
+ File rootDir = project .getFile ();
80
+ List <File > toFormat = new ArrayList <>();
70
81
for (String compileSourceRoot : project .getCompileSourceRoots ()) {
71
82
Path root = Paths .get (compileSourceRoot );
72
83
try (Stream <Path > entries = Files .walk (root )) {
73
84
entries .filter (Files ::isRegularFile )
74
85
.filter (file -> file .getFileName ().toString ().endsWith (".java" ))
75
- .forEach (file -> format (file , step ));
86
+ .map (Path ::toFile )
87
+ .forEach (toFormat ::add );
76
88
} catch (Exception e ) {
77
89
throw new MojoExecutionException ("Unable to walk the file tree" , e );
78
90
}
79
91
}
80
- }
81
92
82
- private void format (Path file , FormatterStep step ) {
93
+ // create a formatter
94
+ Formatter formatter = Formatter .builder ()
95
+ .lineEndingsPolicy (LineEnding .GIT_ATTRIBUTES .createPolicy (rootDir , () -> toFormat ))
96
+ .encoding (StandardCharsets .UTF_8 )
97
+ .rootDir (rootDir .toPath ())
98
+ .steps (Collections .singletonList (step ))
99
+ .build ();
100
+
101
+ // use the formatter to format all the files
83
102
try {
84
- getLog ().info ("Formatting " + file );
85
- String contents = new String (Files .readAllBytes (file ), UTF_8 );
86
- String formatted = step .format (contents , file .toFile ());
87
- Files .write (file , formatted .getBytes (UTF_8 ));
88
- } catch (Exception e ) {
89
- throw new RuntimeException ("Unable to format " + file , e );
103
+ for (File file : toFormat ) {
104
+ formatter .applyTo (file );
105
+ }
106
+ } catch (IOException e ) {
107
+ throw ThrowingEx .asRuntime (e );
90
108
}
91
109
}
92
110
}
0 commit comments