-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpotlessCLI.java
More file actions
356 lines (314 loc) · 14.9 KB
/
SpotlessCLI.java
File metadata and controls
356 lines (314 loc) · 14.9 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright 2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.cli;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.LintState;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.cli.core.FileResolver;
import com.diffplug.spotless.cli.core.SpotlessActionContext;
import com.diffplug.spotless.cli.core.SpotlessCommandLineStream;
import com.diffplug.spotless.cli.core.SpotlessRunCleanup;
import com.diffplug.spotless.cli.core.TargetFileTypeInferer;
import com.diffplug.spotless.cli.core.TargetResolver;
import com.diffplug.spotless.cli.execution.FormatterStepsSupplier;
import com.diffplug.spotless.cli.execution.SpotlessExecutionStrategy;
import com.diffplug.spotless.cli.help.OptionConstants;
import com.diffplug.spotless.cli.logging.output.LoggingConfigurer;
import com.diffplug.spotless.cli.logging.output.Output;
import com.diffplug.spotless.cli.steps.ClangFormat;
import com.diffplug.spotless.cli.steps.CleanThat;
import com.diffplug.spotless.cli.steps.FormatAnnotations;
import com.diffplug.spotless.cli.steps.GoogleJavaFormat;
import com.diffplug.spotless.cli.steps.LicenseHeader;
import com.diffplug.spotless.cli.steps.PalantirJavaFormat;
import com.diffplug.spotless.cli.steps.Prettier;
import com.diffplug.spotless.cli.steps.RemoveUnusedImports;
import com.diffplug.spotless.cli.version.SpotlessCLIVersionProvider;
import picocli.CommandLine;
import picocli.CommandLine.Command;
@Command(
name = "spotless",
mixinStandardHelpOptions = true,
usageHelpAutoWidth = true,
versionProvider = SpotlessCLIVersionProvider.class,
description =
"%n@|magenta spotless|@ is a command line interface (CLI) for the spotless code formatter. "
+ "%nIt can either check if your files are formatted according to your configuration or apply the formatting to the files.%n",
header =
"""
__ __ \s
_________ ____ / /_/ /__ __________
/ ___/ __ \\/ __ \\/ __/ / _ \\/ ___/ ___/
(__ ) /_/ / /_/ / /_/ / __(__ |__ )\s
/____/ .___/\\____/\\__/_/\\___/____/____/ \s Spotless CLI
/_/ \s
""",
synopsisSubcommandLabel = "[FORMATTING_STEPS]",
commandListHeading = "%nAvailable formatting steps:%n",
exitCodeListHeading = "%nPossible exit codes:%n",
exitCodeOnExecutionException = -2,
exitCodeList = {
"""
0:Successful formatting.
In @|yellow APPLY|@ mode, this means all files were formatted successfully.
In @|yellow CHECK|@ mode, this means all files were already formatted properly.""",
"""
1:Some files need to be formatted.
In @|yellow APPLY|@ mode, this means some files failed to be formatted (see output for details).
In @|yellow CHECK|@ mode, this means some files are currently not formatted properly (and might be fixed in APPLY mode).""",
"""
-1:Some files did not converge. This can happen when one formatter does not converge on the file content.
You can find more about this special case here:
<https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md>""",
"-2:An exception occurred during execution."
},
subcommandsRepeatable = true,
subcommands = {
ClangFormat.class,
CleanThat.class,
FormatAnnotations.class,
GoogleJavaFormat.class,
LicenseHeader.class,
PalantirJavaFormat.class,
Prettier.class,
RemoveUnusedImports.class
})
public class SpotlessCLI implements SpotlessAction, SpotlessCommand, SpotlessActionContextProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(SpotlessCLI.class);
@CommandLine.Spec
CommandLine.Model.CommandSpec spec; // injected by picocli
private @NotNull Output output;
private final SpotlessRunCleanup spotlessRunCleanup = SpotlessRunCleanup.INSTANCE; // just keep a reference here
@CommandLine.Option(
names = {"--mode", "-m"},
defaultValue = "APPLY",
description =
"The mode to run spotless in." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX
+ OptionConstants.NEW_LINE
+ """
APPLY: Apply the correct formatting where needed (replace file contents with formatted content).
CHECK: Check if the files are formatted or show the diff of the formatting.""")
SpotlessMode spotlessMode;
@CommandLine.Option(
names = {"--basedir"},
hidden = true,
description = "The base directory to run spotless in. Intended for testing purposes only.")
Path baseDir;
@CommandLine.Option(
names = {"--target", "-t"},
description =
"""
The target files to format. Blobs are supported.
Examples:
-t 'src/**/*.java'
-t 'src/**/*.kt'
-t 'README.md'""")
public List<String> targets;
@CommandLine.Option(
names = {"--encoding", "-e"},
defaultValue = "UTF-8",
description = "The encoding of the files to format." + OptionConstants.DEFAULT_VALUE_SUFFIX)
public Charset encoding;
@CommandLine.Option(
names = {"--line-ending", "-l"},
defaultValue = "UNIX",
description = "The line ending of the files to format." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
public LineEnding lineEnding;
private int parallelity;
@CommandLine.Option(
names = {"--parallelity", "-p"},
paramLabel = "N",
description =
"The number of parallel formatter threads to run. " + OptionConstants.DEFAULT_VALUE_SUFFIX_BEGIN
+ "#cores * 0.5" + OptionConstants.DEFAULT_VALUE_SUFFIX_END)
public void setParallelity(int parallelity) {
if (parallelity < 1) {
throw new CommandLine.ParameterException(spec.commandLine(), "Error: --parallelity must be > 0");
}
this.parallelity = parallelity;
}
@CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
LoggingLevelOptions loggingLevelOptions;
public static class LoggingLevelOptions {
@CommandLine.Spec
CommandLine.Model.CommandSpec spec; // injected by picocli
private boolean[] verbosity;
@CommandLine.Option(
names = {"-v"},
description = "Enable verbose output. Multiple -v options increase the verbosity (max 5).",
arity = "0")
public void setVerbose(boolean[] verbosity) {
if (verbosity.length > 5) {
throw new CommandLine.ParameterException(
spec.commandLine(), "Error: --verbose can be used at most 5 times");
}
this.verbosity = verbosity;
}
@CommandLine.Option(
names = {"--quiet", "-q"},
description = "Disable as much output as possible.",
arity = "0")
public boolean quiet;
LoggingConfigurer.CLIOutputLevel toCliOutputLevel() {
if (quiet) {
return LoggingConfigurer.CLIOutputLevel.QUIET;
}
if (verbosity == null) {
return LoggingConfigurer.CLIOutputLevel.DEFAULT;
}
int verbosityCount = this.verbosity.length;
return LoggingConfigurer.CLIOutputLevel.verbosity(verbosityCount);
}
}
@CommandLine.Option(
names = {"--log-file"},
description = "The log file to write the output to.")
File logFile;
@CommandLine.Option(
names = {"--zzz"},
hidden = true,
defaultValue = "true",
description = "Just a hook to be able to initialize logging.")
void setFinal(boolean finalFlag) {
// this is a hack to make sure that the logging is initialized before any processing occurs outside of parsing
// the command line.
// Maybe there is a picocli hook I've missed for this?
this.output = setupLogging(spec, loggingLevelOptions, logFile);
}
private static @NotNull Output setupLogging(
CommandLine.Model.CommandSpec spec, LoggingLevelOptions loggingLevelOptions, File logFile) {
CommandLine commandLine = spec.commandLine();
LoggingConfigurer.CLIOutputLevel outputLevel = loggingLevelOptions != null
? loggingLevelOptions.toCliOutputLevel()
: LoggingConfigurer.CLIOutputLevel.DEFAULT;
Output output =
LoggingConfigurer.configureLogging(outputLevel, logFile, commandLine::getErr, commandLine::getOut);
// the following logs are to make sure that the logging is configured correctly
logMetaStatements();
return output;
}
private static void logMetaStatements() {
Logger spotlessCliLogger = LoggerFactory.getLogger("com.diffplug.spotless.cli.meta");
spotlessCliLogger.info("Meta: spotless cli loggers on level info enabled.");
spotlessCliLogger.debug("Meta: spotless cli loggers on level debug enabled.");
Logger spotlessLibLogger = LoggerFactory.getLogger("com.diffplug.spotless.meta");
spotlessLibLogger.info("Meta: spotless loggers on level info enabled.");
spotlessLibLogger.debug("Meta: spotless loggers on level debug enabled.");
Logger nonSpotlessLogger = LoggerFactory.getLogger("meta");
nonSpotlessLogger.info("Meta: non-spotless loggers on level info enabled.");
nonSpotlessLogger.debug("Meta: non-spotless loggers on level debug enabled.");
}
public @NotNull Output output() {
return this.output;
}
@Override
public @NotNull Integer executeSpotlessAction(@NotNull FormatterStepsSupplier formatterSteps) {
Objects.requireNonNull(output);
Objects.requireNonNull(formatterSteps);
LOGGER.info(
"Running spotless cli command line: {}",
spec.commandLine().getParseResult().originalArgs());
validateTargets();
TargetResolver targetResolver = targetResolver();
try (FormatterFactory formatterFactory =
new ThreadLocalFormatterFactory(lineEnding.createPolicy(), encoding, formatterSteps);
ExecutorService executor = createExecutorServiceForFormatting()) {
List<Future<Result>> stepResults = targetResolver
.resolveTargets()
.map(path -> {
return executor.submit(() -> {
Formatter formatter = formatterFactory.createFormatter();
// actual formatting
LOGGER.debug("Formatting file: {}", path);
LintState lintState = LintState.of(formatter, path.toFile());
LOGGER.debug("LintState for file {}: {}", path, lintState);
return new Result(path, lintState, formatter);
});
})
.toList();
ResultType resultType = stepResults.stream()
.map(future -> ThrowingEx.get(future::get))
.map(this::handleResult)
.reduce(ResultType.CLEAN, ResultType::combineWith);
return spotlessMode.translateResultTypeToExitCode(resultType);
}
}
private @NotNull ExecutorService createExecutorServiceForFormatting() {
return Executors.newFixedThreadPool(numberOfParallelThreads());
}
private int numberOfParallelThreads() {
return parallelity == 0 ? Math.max(Runtime.getRuntime().availableProcessors() / 2, 1) : parallelity;
}
private void validateTargets() {
if (targets == null || targets.isEmpty()) { // cannot use `required = true` because of the subcommands
throw new CommandLine.ParameterException(
spec.commandLine(),
"Error: Missing required argument (specify one of these): (--target=<targets> | -t)");
}
}
private ResultType handleResult(Result result) {
if (result.lintState().isClean()) {
LOGGER.debug("File is clean: {}", result.target().toFile());
return ResultType.CLEAN;
}
if (result.lintState().getDirtyState().didNotConverge()) {
LOGGER.warn("File did not converge: {}", result.target().toFile());
return ResultType.DID_NOT_CONVERGE;
}
return this.spotlessMode.handleResult(output, result);
}
private TargetResolver targetResolver() {
return new TargetResolver(baseDir(), targets);
}
private Path baseDir() {
return baseDir == null ? Path.of(System.getProperty("user.dir")) : baseDir;
}
@Override
public SpotlessActionContext spotlessActionContext(SpotlessCommandLineStream commandLineStream) {
validateTargets();
TargetResolver targetResolver = targetResolver();
TargetFileTypeInferer targetFileTypeInferer = new TargetFileTypeInferer(targetResolver);
return SpotlessActionContext.builder()
.targetFileType(targetFileTypeInferer.inferTargetFileType())
.fileResolver(new FileResolver(baseDir()))
.commandLineStream(commandLineStream)
.build();
}
public static void main(String... args) {
int exitCode = createCommandLine(createInstance()).execute(args);
System.exit(exitCode);
}
static SpotlessCLI createInstance() {
return new SpotlessCLI();
}
static CommandLine createCommandLine(SpotlessCLI spotlessCLI) {
return new CommandLine(spotlessCLI)
.setExecutionStrategy(new SpotlessExecutionStrategy())
.setCaseInsensitiveEnumValuesAllowed(true);
}
}