Skip to content

Commit b290d8c

Browse files
authored
mvn spotless:apply now prints out statistics (#1507)
2 parents a38b875 + eeee68a commit b290d8c

File tree

7 files changed

+101
-6
lines changed

7 files changed

+101
-6
lines changed

lib/src/main/java/com/diffplug/spotless/Formatter.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
public final class Formatter implements Serializable, AutoCloseable {
4040
private static final long serialVersionUID = 1L;
4141

42+
// The name is used for logging purpose. It does not convey any applicative purpose
43+
private String name;
4244
private LineEnding.Policy lineEndingsPolicy;
4345
private Charset encoding;
4446
private Path rootDir;
4547
private List<FormatterStep> steps;
4648
private FormatExceptionPolicy exceptionPolicy;
4749

48-
private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path rootDirectory, List<FormatterStep> steps, FormatExceptionPolicy exceptionPolicy) {
50+
private Formatter(String name, LineEnding.Policy lineEndingsPolicy, Charset encoding, Path rootDirectory, List<FormatterStep> steps, FormatExceptionPolicy exceptionPolicy) {
51+
this.name = name;
4952
this.lineEndingsPolicy = Objects.requireNonNull(lineEndingsPolicy, "lineEndingsPolicy");
5053
this.encoding = Objects.requireNonNull(encoding, "encoding");
5154
this.rootDir = Objects.requireNonNull(rootDirectory, "rootDir");
@@ -55,6 +58,7 @@ private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path ro
5558

5659
// override serialize output
5760
private void writeObject(ObjectOutputStream out) throws IOException {
61+
out.writeObject(name);
5862
out.writeObject(lineEndingsPolicy);
5963
out.writeObject(encoding.name());
6064
out.writeObject(rootDir.toString());
@@ -65,6 +69,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {
6569
// override serialize input
6670
@SuppressWarnings("unchecked")
6771
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
72+
name = (String) in.readObject();
6873
lineEndingsPolicy = (LineEnding.Policy) in.readObject();
6974
encoding = Charset.forName((String) in.readObject());
7075
rootDir = Paths.get((String) in.readObject());
@@ -78,6 +83,10 @@ private void readObjectNoData() throws ObjectStreamException {
7883
throw new UnsupportedOperationException();
7984
}
8085

86+
public String getName() {
87+
return name;
88+
}
89+
8190
public LineEnding.Policy getLineEndingsPolicy() {
8291
return lineEndingsPolicy;
8392
}
@@ -103,6 +112,8 @@ public static Formatter.Builder builder() {
103112
}
104113

105114
public static class Builder {
115+
// optional parameters
116+
private String name = "unnamed";
106117
// required parameters
107118
private LineEnding.Policy lineEndingsPolicy;
108119
private Charset encoding;
@@ -112,6 +123,11 @@ public static class Builder {
112123

113124
private Builder() {}
114125

126+
public Builder name(String name) {
127+
this.name = name;
128+
return this;
129+
}
130+
115131
public Builder lineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
116132
this.lineEndingsPolicy = lineEndingsPolicy;
117133
return this;
@@ -138,7 +154,7 @@ public Builder exceptionPolicy(FormatExceptionPolicy exceptionPolicy) {
138154
}
139155

140156
public Formatter build() {
141-
return new Formatter(lineEndingsPolicy, encoding, rootDir, steps,
157+
return new Formatter(name, lineEndingsPolicy, encoding, rootDir, steps,
142158
exceptionPolicy == null ? FormatExceptionPolicy.failOnlyOnError() : exceptionPolicy);
143159
}
144160
}
@@ -253,6 +269,7 @@ public String compute(String unix, File file) {
253269
public int hashCode() {
254270
final int prime = 31;
255271
int result = 1;
272+
result = prime * result + name.hashCode();
256273
result = prime * result + encoding.hashCode();
257274
result = prime * result + lineEndingsPolicy.hashCode();
258275
result = prime * result + rootDir.hashCode();
@@ -273,7 +290,8 @@ public boolean equals(Object obj) {
273290
return false;
274291
}
275292
Formatter other = (Formatter) obj;
276-
return encoding.equals(other.encoding) &&
293+
return name.equals(other.name) &&
294+
encoding.equals(other.encoding) &&
277295
lineEndingsPolicy.equals(other.lineEndingsPolicy) &&
278296
rootDir.equals(other.rootDir) &&
279297
steps.equals(other.steps) &&

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -184,6 +184,7 @@ String formatName() {
184184

185185
Formatter buildFormatter() {
186186
return Formatter.builder()
187+
.name(formatName())
187188
.lineEndingsPolicy(lineEndingsPolicy.get())
188189
.encoding(Charset.forName(encoding))
189190
.rootDir(getProjectDir().get().getAsFile().toPath())

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* A synthesis log with the number of considered files is added after each formatter execution ([#1507](https://github.com/diffplug/spotless/pull/1507))
68

79
## [2.31.0] - 2023-01-26
810
### Added

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -101,7 +101,9 @@ public final Formatter newFormatter(Supplier<Iterable<File>> filesToFormat, Form
101101
formatterSteps.add(pair.out());
102102
}
103103

104+
String formatterName = this.getClass().getSimpleName();
104105
return Formatter.builder()
106+
.name(formatterName)
105107
.encoding(formatterEncoding)
106108
.lineEndingsPolicy(formatterLineEndingPolicy)
107109
.exceptionPolicy(new FormatExceptionPolicyStrict())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven;
17+
18+
/**
19+
* Tracks the number of processed files, typically by a single Formatter for a whole repository
20+
*/
21+
class ImpactedFilesTracker {
22+
protected int nbskippedAsCleanCache = 0;
23+
protected int nbCheckedButAlreadyClean = 0;
24+
protected int nbCleaned = 0;
25+
26+
/**
27+
* Some cache mechanism may indicate some content is clean, without having to execute the cleaning process
28+
*/
29+
public void skippedAsCleanCache() {
30+
nbskippedAsCleanCache++;
31+
}
32+
33+
public int getSkippedAsCleanCache() {
34+
return nbskippedAsCleanCache;
35+
}
36+
37+
public void checkedButAlreadyClean() {
38+
nbCheckedButAlreadyClean++;
39+
}
40+
41+
public int getCheckedButAlreadyClean() {
42+
return nbCheckedButAlreadyClean;
43+
}
44+
45+
public void cleaned() {
46+
nbCleaned++;
47+
}
48+
49+
public int getCleaned() {
50+
return nbCleaned;
51+
}
52+
53+
public int getTotal() {
54+
return nbskippedAsCleanCache + nbCheckedButAlreadyClean + nbCleaned;
55+
}
56+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,8 +33,11 @@ public class SpotlessApplyMojo extends AbstractSpotlessMojo {
3333

3434
@Override
3535
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
36+
ImpactedFilesTracker counter = new ImpactedFilesTracker();
37+
3638
for (File file : files) {
3739
if (upToDateChecker.isUpToDate(file.toPath())) {
40+
counter.skippedAsCleanCache();
3841
if (getLog().isDebugEnabled()) {
3942
getLog().debug("Spotless will not format an up-to-date file: " + file);
4043
}
@@ -44,14 +47,26 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
4447
try {
4548
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
4649
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
50+
getLog().info(String.format("Writing clean file: %s", file));
4751
dirtyState.writeCanonicalTo(file);
4852
buildContext.refresh(file);
53+
counter.cleaned();
54+
} else {
55+
counter.checkedButAlreadyClean();
4956
}
5057
} catch (IOException e) {
5158
throw new MojoExecutionException("Unable to format file " + file, e);
5259
}
5360

5461
upToDateChecker.setUpToDate(file.toPath());
5562
}
63+
64+
// We print the number of considered files which is useful when ratchetFrom is setup
65+
if (counter.getTotal() > 0) {
66+
getLog().info(String.format("Spotless.%s is keeping %s files clean - %s were changed to be clean, %s were already clean, %s were skipped because caching determined they were already clean",
67+
formatter.getName(), counter.getTotal(), counter.getCleaned(), counter.getCheckedButAlreadyClean(), counter.getSkippedAsCleanCache()));
68+
} else {
69+
getLog().warn(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", formatter.getName()));
70+
}
5671
}
5772
}

testlib/src/main/java/com/diffplug/spotless/StepHarnessWithFile.java

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ private StepHarnessWithFile(ResourceHarness harness, Formatter formatter) {
3838
/** Creates a harness for testing steps which do depend on the file. */
3939
public static StepHarnessWithFile forStep(ResourceHarness harness, FormatterStep step) {
4040
return new StepHarnessWithFile(harness, Formatter.builder()
41+
.name(step.getName())
4142
.encoding(StandardCharsets.UTF_8)
4243
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
4344
.steps(Collections.singletonList(step))

0 commit comments

Comments
 (0)