-
Notifications
You must be signed in to change notification settings - Fork 23
/
AbstractPrettierMojo.java
179 lines (154 loc) · 5.28 KB
/
AbstractPrettierMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.hubspot.maven.plugins.prettier;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
public abstract class AbstractPrettierMojo extends PrettierArgs {
@Parameter(defaultValue = "false")
private boolean skip;
protected abstract String getPrettierCommand();
protected abstract void handlePrettierLogLine(String line);
protected abstract void handlePrettierNonZeroExit(int status)
throws MojoExecutionException, MojoFailureException;
@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().info("Skipping plugin execution");
return;
}
List<Path> inputDirectories = determineInputPaths();
if (inputDirectories.isEmpty()) {
getLog().info("No input directories found");
return;
}
try {
String glob = computeGlob(inputDirectories);
List<String> command = new ArrayList<>(basePrettierCommand());
command.add("--" + getPrettierCommand());
command.add(glob);
if (getLog().isDebugEnabled()) {
getLog().debug("Running prettier with args " + command);
}
Process process = new ProcessBuilder(command.toArray(new String[0]))
.directory(project.getBasedir())
.start();
try (
InputStreamReader stdoutReader = new InputStreamReader(
process.getInputStream(),
StandardCharsets.UTF_8
);
BufferedReader stdout = new BufferedReader(stdoutReader);
InputStreamReader stderrReader = new InputStreamReader(
process.getErrorStream(),
StandardCharsets.UTF_8
);
BufferedReader stderr = new BufferedReader(stderrReader)
) {
String line;
while ((line = stdout.readLine()) != null) {
handlePrettierLogLine(line);
}
boolean noMatchingFiles = false;
while ((line = stderr.readLine()) != null) {
if (line.contains("No matching files.") || line.contains("No files matching")) {
noMatchingFiles = true;
} else if (line.contains("error")) {
getLog().error(line);
} else {
getLog().warn(line);
}
}
int status = process.waitFor();
if (status != 0) {
if (status == 2 && noMatchingFiles) {
getLog().info("No files found matching glob " + glob);
} else {
handlePrettierNonZeroExit(status);
}
}
}
} catch (IOException | InterruptedException e) {
throw new MojoExecutionException("Error trying to run prettier-java", e);
}
}
protected List<String> basePrettierCommand() throws MojoExecutionException {
Path nodeExecutable = resolveNodeExecutable();
Path prettierJavaDirectory = extractPrettierJava();
Path prettierBin = prettierJavaDirectory
.resolve("prettier-java")
.resolve("node_modules")
.resolve("prettier")
.resolve("bin-prettier.js");
Path prettierJavaPlugin = prettierJavaDirectory
.resolve("prettier-java")
.resolve("node_modules")
.resolve("prettier-plugin-java");
List<String> command = new ArrayList<>();
command.add(toString(nodeExecutable));
command.add(toString(prettierBin));
command.add("--plugin=" + toString(prettierJavaPlugin));
command.add("--color");
if (printWidth != null) {
command.add("--print-width");
command.add(printWidth);
}
if (tabWidth != null) {
command.add("--tab-width");
command.add(tabWidth);
}
if (useTabs != null) {
command.add("--use-tabs");
command.add(useTabs.toString());
}
if (endOfLine != null) {
command.add("--end-of-line");
command.add(endOfLine);
}
if (ignoreConfigFile) {
command.add("--no-config");
}
if (ignoreEditorConfig) {
command.add("--no-editorconfig");
}
return command;
}
private List<Path> determineInputPaths() {
List<String> inputPaths = new ArrayList<>();
// don't use compile source roots because it seems to include generated sources
inputPaths.add(project.getBuild().getSourceDirectory());
inputPaths.add(project.getBuild().getTestSourceDirectory());
Path basePath = project.getBasedir().toPath();
return inputPaths
.stream()
.map(Paths::get)
.filter(Files::isDirectory)
.map(basePath::relativize)
.collect(Collectors.toList());
}
private String computeGlob(List<Path> inputPaths) {
final String joinedPaths;
if (inputPaths.size() > 1) {
joinedPaths =
inputPaths
.stream()
.map(Path::toString)
.collect(Collectors.joining(",", "{", "}"));
} else {
joinedPaths = inputPaths.get(0).toString();
}
return joinedPaths + "/**/*.java";
}
// Convert Windows Path to Unix style
private String toString(Path path) {
return path.toString().replace("\\", "/");
}
}