|
16 | 16 | package com.diffplug.gradle.spotless;
|
17 | 17 |
|
18 | 18 | import static java.util.Collections.singleton;
|
| 19 | +import static java.util.Collections.singletonList; |
19 | 20 |
|
20 | 21 | import java.io.File;
|
21 | 22 | import java.io.IOException;
|
22 |
| -import java.nio.charset.StandardCharsets; |
23 | 23 | import java.nio.file.Files;
|
24 | 24 | import java.nio.file.Path;
|
25 |
| -import java.nio.file.Paths; |
26 | 25 | import java.util.ArrayList;
|
27 |
| -import java.util.Collections; |
28 | 26 | import java.util.List;
|
29 | 27 | import java.util.Set;
|
30 | 28 | import java.util.stream.Stream;
|
31 | 29 |
|
32 |
| -import org.apache.maven.plugin.AbstractMojo; |
33 | 30 | import org.apache.maven.plugin.MojoExecutionException;
|
34 | 31 | import org.apache.maven.plugin.MojoFailureException;
|
35 |
| -import org.apache.maven.plugins.annotations.Component; |
36 | 32 | import org.apache.maven.plugins.annotations.Mojo;
|
37 |
| -import org.apache.maven.plugins.annotations.Parameter; |
38 |
| -import org.apache.maven.project.MavenProject; |
39 |
| -import org.eclipse.aether.RepositorySystem; |
40 |
| -import org.eclipse.aether.RepositorySystemSession; |
41 |
| -import org.eclipse.aether.repository.RemoteRepository; |
42 | 33 |
|
| 34 | +import com.diffplug.spotless.FormatExceptionPolicyStrict; |
43 | 35 | import com.diffplug.spotless.Formatter;
|
44 | 36 | import com.diffplug.spotless.FormatterStep;
|
45 |
| -import com.diffplug.spotless.LineEnding; |
46 | 37 | import com.diffplug.spotless.Provisioner;
|
47 |
| -import com.diffplug.spotless.ThrowingEx; |
48 | 38 | import com.diffplug.spotless.extra.java.EclipseFormatterStep;
|
49 | 39 |
|
50 |
| -@Mojo(name = "spotless") |
51 |
| -public class SpotlessMojo extends AbstractMojo { |
52 |
| - |
53 |
| - @Component |
54 |
| - private RepositorySystem repositorySystem; |
55 |
| - |
56 |
| - @Parameter(defaultValue = "${repositorySystemSession}", required = true, readonly = true) |
57 |
| - private RepositorySystemSession repositorySystemSession; |
58 |
| - |
59 |
| - @Parameter(defaultValue = "${project.remotePluginRepositories}", required = true, readonly = true) |
60 |
| - private List<RemoteRepository> repositories; |
61 |
| - |
62 |
| - @Parameter(defaultValue = "${project}", required = true, readonly = true) |
63 |
| - private MavenProject project; |
64 |
| - |
65 |
| - @Parameter(property = "eclipseFormatFile", required = true) |
66 |
| - private String eclipseFormatFile; |
| 40 | +@Mojo(name = "apply") |
| 41 | +public class SpotlessMojo extends AbstractSpotlessMojo { |
67 | 42 |
|
68 | 43 | @Override
|
69 | 44 | public void execute() throws MojoExecutionException, MojoFailureException {
|
70 |
| - ArtifactResolver resolver = new ArtifactResolver(repositorySystem, repositorySystemSession, repositories); |
71 |
| - Provisioner provisioner = MavenProvisioner.create(resolver); |
| 45 | + List<File> filesToFormat = collectFilesToFormat(); |
72 | 46 |
|
73 |
| - // create the eclipse step |
74 |
| - Set<File> settingFiles = singleton(new File(eclipseFormatFile)); |
75 |
| - FormatterStep step = EclipseFormatterStep.create(EclipseFormatterStep.defaultVersion(), |
76 |
| - settingFiles, provisioner); |
| 47 | + Formatter formatter = createFormatter(filesToFormat); |
77 | 48 |
|
78 |
| - // collect all the files that are going to be formatted |
79 |
| - File rootDir = project.getFile(); |
| 49 | + formatAll(filesToFormat, formatter); |
| 50 | + } |
| 51 | + |
| 52 | + private List<File> collectFilesToFormat() throws MojoExecutionException { |
80 | 53 | List<File> toFormat = new ArrayList<>();
|
81 |
| - for (String compileSourceRoot : project.getCompileSourceRoots()) { |
82 |
| - Path root = Paths.get(compileSourceRoot); |
| 54 | + for (Path root : getAllSourceRoots()) { |
83 | 55 | try (Stream<Path> entries = Files.walk(root)) {
|
84 | 56 | entries.filter(Files::isRegularFile)
|
85 | 57 | .filter(file -> file.getFileName().toString().endsWith(".java"))
|
86 | 58 | .map(Path::toFile)
|
87 | 59 | .forEach(toFormat::add);
|
88 |
| - } catch (Exception e) { |
| 60 | + } catch (IOException e) { |
89 | 61 | throw new MojoExecutionException("Unable to walk the file tree", e);
|
90 | 62 | }
|
91 | 63 | }
|
| 64 | + return toFormat; |
| 65 | + } |
92 | 66 |
|
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)) |
| 67 | + private Formatter createFormatter(List<File> filesToFormat) { |
| 68 | + return Formatter.builder() |
| 69 | + .encoding(getEncoding()) |
| 70 | + .lineEndingsPolicy(getLineEndingsPolicy(filesToFormat)) |
| 71 | + .exceptionPolicy(new FormatExceptionPolicyStrict()) |
| 72 | + .steps(singletonList(createEclipseFormatterStep())) |
| 73 | + .rootDir(getRootDir().toPath()) |
99 | 74 | .build();
|
| 75 | + } |
| 76 | + |
| 77 | + private FormatterStep createEclipseFormatterStep() { |
| 78 | + ArtifactResolver artifactResolver = createArtifactResolver(); |
| 79 | + Provisioner provisioner = MavenProvisioner.create(artifactResolver); |
| 80 | + Set<File> settingFiles = singleton(getJavaConfig().getEclipseConfigFile()); |
| 81 | + return EclipseFormatterStep.create(EclipseFormatterStep.defaultVersion(), settingFiles, provisioner); |
| 82 | + } |
100 | 83 |
|
101 |
| - // use the formatter to format all the files |
102 |
| - try { |
103 |
| - for (File file : toFormat) { |
| 84 | + private static void formatAll(List<File> files, Formatter formatter) throws MojoExecutionException { |
| 85 | + for (File file : files) { |
| 86 | + try { |
104 | 87 | formatter.applyTo(file);
|
| 88 | + } catch (IOException e) { |
| 89 | + throw new MojoExecutionException("Unable to format file " + file, e); |
105 | 90 | }
|
106 |
| - } catch (IOException e) { |
107 |
| - throw ThrowingEx.asRuntime(e); |
108 | 91 | }
|
109 | 92 | }
|
110 | 93 | }
|
0 commit comments