Skip to content

Commit eefdc50

Browse files
committedNov 1, 2016
FormatExtension now has bumpThisNumberIfACustomRuleChanges(), so that all non-strictified rules will always run.
1 parent 51f0531 commit eefdc50

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed
 

‎src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+50-8
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import java.io.Serializable;
1819
import java.nio.charset.Charset;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.Objects;
25+
import java.util.function.Supplier;
2426
import java.util.regex.Pattern;
2527
import java.util.stream.Stream;
2628

@@ -29,6 +31,8 @@
2931
import org.gradle.api.file.FileCollection;
3032
import org.gradle.api.internal.file.UnionFileCollection;
3133

34+
import com.diffplug.common.base.Errors;
35+
import com.diffplug.common.base.Suppliers;
3236
import com.diffplug.common.base.Throwing;
3337
import com.diffplug.common.collect.ImmutableMap;
3438

@@ -149,6 +153,47 @@ protected FileCollection parseTarget(Object target) {
149153
/** The steps that need to be added. */
150154
protected List<FormatterStep> steps = new ArrayList<>();
151155

156+
/** Adds a new step. */
157+
protected void addStep(FormatterStep newStep) {
158+
for (FormatterStep step : steps) {
159+
if (step.getName().equals(name)) {
160+
throw new GradleException("Multiple steps with name '" + name + "' for spotless '" + name + "'");
161+
}
162+
}
163+
steps.add(newStep);
164+
}
165+
166+
/**
167+
* Spotless tracks what files have changed from run to run, so that it can run faster
168+
* by only checking files which have changed.
169+
*
170+
* If you have changed a custom function, then you must increment this number so
171+
* that spotless knows it needs to rerun the format check. This is not necessary
172+
* if you don't use any custom functions.
173+
*
174+
* If you use a custom function and don't call bumpThisNumberIfACustomRuleChanges, then spotless
175+
* cannot tell if you have changed the rules, and will be forced to always recheck all files.
176+
*/
177+
public void bumpThisNumberIfACustomRuleChanges(int number) {
178+
globalKey = number;
179+
}
180+
181+
private Serializable globalKey = new NeverUpToDateBetweenRuns();
182+
183+
static class NeverUpToDateBetweenRuns implements Serializable {
184+
private static final long serialVersionUID = 1L;
185+
186+
@Override
187+
public boolean equals(Object other) {
188+
return other == this;
189+
}
190+
191+
@Override
192+
public int hashCode() {
193+
return System.identityHashCode(this);
194+
}
195+
}
196+
152197
/**
153198
* Adds the given custom step, which is constructed lazily for performance reasons.
154199
*
@@ -158,12 +203,9 @@ protected FileCollection parseTarget(Object target) {
158203
* {@link #customLazyGroovy(String, com.diffplug.common.base.Throwing.Supplier)}.
159204
*/
160205
public void customLazy(String name, Throwing.Supplier<Throwing.Function<String, String>> formatterSupplier) {
161-
for (FormatterStep step : steps) {
162-
if (step.getName().equals(name)) {
163-
throw new GradleException("Multiple steps with name '" + name + "' for spotless '" + name + "'");
164-
}
165-
}
166-
steps.add(FormatterStep.createLazy(name, formatterSupplier));
206+
Supplier<Throwing.Function<String, String>> nonThrowing = Errors.rethrow().wrap(formatterSupplier);
207+
Supplier<Throwing.Function<String, String>> memoized = Suppliers.memoize(nonThrowing);
208+
addStep(FormatterStep.createLazy(name, () -> globalKey, (unusedKey, unix) -> Errors.rethrow().get(() -> memoized.get().apply(unix))));
167209
}
168210

169211
/** Same as {@link #customLazy(String, com.diffplug.common.base.Throwing.Supplier)}, but for Groovy closures. */
@@ -263,7 +305,7 @@ public void indentWithTabs() {
263305
* Spotless will look for a line that starts with this to know what the "top" is.
264306
*/
265307
public void licenseHeader(String licenseHeader, String delimiter) {
266-
steps.add(FormatterStep.create(LicenseHeaderStep.NAME,
308+
addStep(FormatterStep.create(LicenseHeaderStep.NAME,
267309
new LicenseHeaderStep(licenseHeader, delimiter),
268310
LicenseHeaderStep::format));
269311
}
@@ -275,7 +317,7 @@ public void licenseHeader(String licenseHeader, String delimiter) {
275317
* Spotless will look for a line that starts with this to know what the "top" is.
276318
*/
277319
public void licenseHeaderFile(Object licenseHeaderFile, String delimiter) {
278-
steps.add(FormatterStep.createLazy(LicenseHeaderStep.NAME,
320+
addStep(FormatterStep.createLazy(LicenseHeaderStep.NAME,
279321
() -> new LicenseHeaderStep(getProject().file(licenseHeaderFile), getEncoding(), delimiter),
280322
LicenseHeaderStep::format));
281323
}

0 commit comments

Comments
 (0)
Please sign in to comment.