Skip to content

Commit b0d1619

Browse files
committed
Introduce Formatter.name, Improve synthesis log
1 parent fed3ffc commit b0d1619

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 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.
@@ -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 = "misc";
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
}
@@ -248,6 +264,7 @@ public String compute(String unix, File file) {
248264
public int hashCode() {
249265
final int prime = 31;
250266
int result = 1;
267+
result = prime * result + name.hashCode();
251268
result = prime * result + encoding.hashCode();
252269
result = prime * result + lineEndingsPolicy.hashCode();
253270
result = prime * result + rootDir.hashCode();
@@ -268,7 +285,8 @@ public boolean equals(Object obj) {
268285
return false;
269286
}
270287
Formatter other = (Formatter) obj;
271-
return encoding.equals(other.encoding) &&
288+
return name.equals(other.name) &&
289+
encoding.equals(other.encoding) &&
272290
lineEndingsPolicy.equals(other.lineEndingsPolicy) &&
273291
rootDir.equals(other.rootDir) &&
274292
steps.equals(other.steps) &&

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

Lines changed: 2 additions & 1 deletion
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/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

Lines changed: 3 additions & 1 deletion
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())

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
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+
*/
116
package com.diffplug.spotless.maven;
217

318
import java.util.concurrent.atomic.AtomicInteger;
419

20+
/**
21+
* Tracks the number of processed files, typically by a single Formatter for a whole repository
22+
*/
523
public class ImpactedFilesTracker {
24+
protected final AtomicInteger nbSkipped = new AtomicInteger();
625
protected final AtomicInteger nbChecked = new AtomicInteger();
726
protected final AtomicInteger nbCleaned = new AtomicInteger();
827

28+
/**
29+
* Some cache mechanism may indicate some content is clean, without having to execute the cleaning process
30+
*/
31+
public void skippedAsCleanCache() {
32+
nbSkipped.incrementAndGet();
33+
}
34+
35+
public int getSkipped() {
36+
return nbSkipped.get();
37+
}
38+
939
public void checked() {
1040
nbChecked.incrementAndGet();
1141
}
@@ -21,4 +51,5 @@ public void cleaned() {
2151
public int getCleaned() {
2252
return nbCleaned.get();
2353
}
54+
2455
}

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

Lines changed: 8 additions & 2 deletions
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.
@@ -37,6 +37,7 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
3737

3838
for (File file : files) {
3939
if (upToDateChecker.isUpToDate(file.toPath())) {
40+
impactedFilesTracker.skippedAsCleanCache();
4041
if (getLog().isDebugEnabled()) {
4142
getLog().debug("Spotless will not format an up-to-date file: " + file);
4243
}
@@ -60,6 +61,11 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
6061
}
6162

6263
// We print the number of considered files which is useful when ratchetFrom is setup
63-
getLog().info(String.format("A formatter with %s steps cleaned: %s files (for %s considered)", formatter.getSteps().size(), impactedFilesTracker.getCleaned(), impactedFilesTracker.getChecked()));
64+
int nbSkipped = impactedFilesTracker.getSkipped();
65+
int nbChecked = impactedFilesTracker.getChecked();
66+
int nbCleaned = impactedFilesTracker.getCleaned();
67+
int totalProcessed = nbSkipped + nbChecked + nbCleaned;
68+
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",
69+
formatter.getName(), totalProcessed, nbCleaned, nbChecked, nbSkipped));
6470
}
6571
}

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

Lines changed: 1 addition & 0 deletions
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)