Skip to content

Commit 72bd2a5

Browse files
committed
refactor: extract custom versioning into mixin
1 parent e7d0939 commit 72bd2a5

File tree

6 files changed

+109
-52
lines changed

6 files changed

+109
-52
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ CleanThat enables automatic refactoring of Java code.
288288
available.
289289
(default: 1.8)
290290
-v, --use-version=<useVersion>
291-
The version of CleanThat to use.
291+
The version of clean-that to use.
292292
(default: 2.24)
293293
-V, --version Print version information and exit.
294294
@@ -335,7 +335,7 @@ Runs Eclipse WTP formatter.
335335
run.
336336
One of: CSS, HTML, JS, JSON, XML, XHTML
337337
-v, --use-version=<useVersion>
338-
The version of Eclipse WTP formatter to use.
338+
The version of eclipse-wtp to use.
339339
(default: 4.21.0)
340340
-V, --version Print version information and exit.
341341
@@ -447,8 +447,7 @@ Runs google java format
447447
One of: AOSP, GOOGLE
448448
(default: GOOGLE)
449449
-v, --use-version=<useVersion>
450-
The version of google java format to use. Must be >=
451-
1.8.
450+
The version of google-java-format to use.
452451
(default: 1.28.0)
453452
-V, --version Print version information and exit.
454453
@@ -553,7 +552,7 @@ Runs palantir java format
553552
One of: PALANTIR, AOSP, GOOGLE
554553
(default: PALANTIR)
555554
-v, --use-version=<useVersion>
556-
The version of palantir java format to use.
555+
The version of palantir-java-format to use.
557556
(default: 2.80.0)
558557
-V, --version Print version information and exit.
559558

app/src/main/java/com/diffplug/spotless/cli/steps/CleanThat.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232
import picocli.CommandLine;
3333

34-
@CommandLine.Command(name = "clean-that", description = "CleanThat enables automatic refactoring of Java code.")
34+
@CommandLine.Command(
35+
name = "clean-that",
36+
description = "CleanThat enables automatic refactoring of Java code.",
37+
defaultValueProvider = CleanThat.DefaultValueProvider.class)
3538
@SupportedFileTypes("Java")
3639
@AdditionalInfoLinks({
3740
"https://github.com/solven-eu/cleanthat",
@@ -41,12 +44,15 @@ public class CleanThat extends SpotlessFormatterStep {
4144

4245
public static final String DEFAULT_MUTATORS = String.join(", ", CleanthatJavaStep.defaultMutators());
4346

44-
private static final String DEFAULT_VERSION_SYSPROP = "steps.clean-that.default-version";
45-
4647
static {
4748
// workaround for dynamic property resolution in help messages
4849
System.setProperty("usage.cleanthat.defaultMutators", DEFAULT_MUTATORS);
49-
System.setProperty(DEFAULT_VERSION_SYSPROP, CleanthatJavaStep.defaultVersion());
50+
}
51+
52+
static class DefaultValueProvider extends CustomVersion.CustomVersionDefaultValueProvider {
53+
DefaultValueProvider() {
54+
super(CleanthatJavaStep::defaultVersion);
55+
}
5056
}
5157

5258
@CommandLine.Option(
@@ -91,17 +97,14 @@ public class CleanThat extends SpotlessFormatterStep {
9197
+ OptionConstants.DEFAULT_VALUE_SUFFIX)
9298
String sourceCompatibility;
9399

94-
@CommandLine.Option(
95-
names = {"--use-version", "-v"},
96-
defaultValue = "${sys:" + DEFAULT_VERSION_SYSPROP + "}",
97-
description = "The version of CleanThat to use." + OptionConstants.DEFAULT_VALUE_SUFFIX)
98-
String useVersion;
100+
@CommandLine.Mixin
101+
CustomVersion customVersion;
99102

100103
@Override
101104
public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
102105
return Collections.singletonList(CleanthatJavaStep.create(
103106
CleanthatJavaStep.defaultGroupArtifact(),
104-
useVersion,
107+
customVersion.useVersion,
105108
this.sourceCompatibility,
106109
includedMutators(),
107110
excludedMutators(),
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2025 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.cli.steps;
17+
18+
import java.util.Objects;
19+
import java.util.function.Supplier;
20+
21+
import org.jetbrains.annotations.NotNull;
22+
23+
import com.diffplug.spotless.cli.help.OptionConstants;
24+
25+
import picocli.CommandLine;
26+
27+
public class CustomVersion {
28+
29+
private static final String LONG_OPTION = "--use-version";
30+
private static final String SHORT_OPTION = "-v";
31+
32+
@CommandLine.Option(
33+
names = {LONG_OPTION, SHORT_OPTION},
34+
description = "The version of ${COMMAND-NAME} to use." + OptionConstants.DEFAULT_VALUE_SUFFIX)
35+
String useVersion;
36+
37+
abstract static class CustomVersionDefaultValueProvider implements CommandLine.IDefaultValueProvider {
38+
private final Supplier<String> defaultVersionSupplier;
39+
40+
protected CustomVersionDefaultValueProvider(@NotNull Supplier<String> defaultVersionSupplier) {
41+
this.defaultVersionSupplier = Objects.requireNonNull(defaultVersionSupplier);
42+
}
43+
44+
@Override
45+
public String defaultValue(CommandLine.Model.ArgSpec argSpec) throws Exception {
46+
// if it is the use-version option, provide the default version, otherwise null
47+
if (!argSpec.isOption()) {
48+
return null;
49+
}
50+
if (!(argSpec instanceof CommandLine.Model.OptionSpec optionSpec)) {
51+
return null;
52+
}
53+
if (!optionSpec.longestName().equals(LONG_OPTION)) {
54+
return null;
55+
}
56+
return defaultVersionSupplier.get();
57+
}
58+
}
59+
}

app/src/main/java/com/diffplug/spotless/cli/steps/EclipseWtp.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@
3434

3535
import picocli.CommandLine;
3636

37-
@CommandLine.Command(name = "eclipse-wtp", description = "Runs Eclipse WTP formatter.")
37+
@CommandLine.Command(
38+
name = "eclipse-wtp",
39+
description = "Runs Eclipse WTP formatter.",
40+
defaultValueProvider = EclipseWtp.DefaultValueProvider.class)
3841
@SupportedFileTypes({"css", "html", "js", "json", "xml", "xhtml"})
3942
@AdditionalInfoLinks({
4043
"https://github.com/diffplug/spotless/tree/main/plugin-gradle#eclipse-web-tools-platform",
4144
"https://projects.eclipse.org/projects/webtools"
4245
})
4346
public class EclipseWtp extends SpotlessFormatterStep {
4447

45-
private static final String DEFAULT_VERSION_SYSPROP = "steps.eclipse-wtp.default-version";
46-
47-
static {
48-
// workaround for dynamic property values in annotations
49-
System.setProperty(DEFAULT_VERSION_SYSPROP, EclipseWtpFormatterStep.defaultVersion());
48+
static class DefaultValueProvider extends CustomVersion.CustomVersionDefaultValueProvider {
49+
DefaultValueProvider() {
50+
super(EclipseWtpFormatterStep::defaultVersion);
51+
}
5052
}
5153

5254
@CommandLine.Option(
@@ -63,11 +65,8 @@ public class EclipseWtp extends SpotlessFormatterStep {
6365
+ OptionConstants.VALID_VALUES_SUFFIX)
6466
Type type;
6567

66-
@CommandLine.Option(
67-
names = {"--use-version", "-v"},
68-
defaultValue = "${sys:" + DEFAULT_VERSION_SYSPROP + "}",
69-
description = "The version of Eclipse WTP formatter to use." + OptionConstants.DEFAULT_VALUE_SUFFIX)
70-
String useVersion;
68+
@CommandLine.Mixin
69+
CustomVersion customVersion;
7170

7271
public enum Type {
7372
CSS(EclipseWtpFormatterStep.CSS),
@@ -104,7 +103,7 @@ public enum Type {
104103
public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
105104
EclipseWtpFormatterStep wtpType = type(context::targetFileType).toEclipseWtpType();
106105
EclipseBasedStepBuilder builder = wtpType.createBuilder(context.provisioner());
107-
builder.setVersion(useVersion);
106+
builder.setVersion(customVersion.useVersion);
108107
if (configFiles != null && !configFiles.isEmpty()) {
109108
builder.setPreferences(configFiles.stream()
110109
.map(context::resolvePath)

app/src/main/java/com/diffplug/spotless/cli/steps/GoogleJavaFormat.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@
2626

2727
import picocli.CommandLine;
2828

29-
@CommandLine.Command(name = "google-java-format", description = "Runs google java format")
29+
@CommandLine.Command(
30+
name = "google-java-format",
31+
description = "Runs google java format",
32+
defaultValueProvider = GoogleJavaFormat.DefaultValueProvider.class)
3033
@SupportedFileTypes("Java")
3134
@AdditionalInfoLinks("https://github.com/google/google-java-format")
3235
public class GoogleJavaFormat extends SpotlessFormatterStep {
3336

34-
private static final String DEFAULT_VERSION_SYSPROP = "steps.google-java-format.default-version";
35-
36-
static {
37-
// workaround for dynamic property values in annotations
38-
System.setProperty(DEFAULT_VERSION_SYSPROP, GoogleJavaFormatStep.defaultVersion());
37+
static class DefaultValueProvider extends CustomVersion.CustomVersionDefaultValueProvider {
38+
DefaultValueProvider() {
39+
super(GoogleJavaFormatStep::defaultVersion);
40+
}
3941
}
4042

4143
@CommandLine.Option(
@@ -68,18 +70,14 @@ public enum Style {
6870
description = "Format javadoc." + OptionConstants.DEFAULT_VALUE_SUFFIX)
6971
boolean formatJavadoc;
7072

71-
@CommandLine.Option(
72-
names = {"--use-version", "-v"},
73-
defaultValue = "${sys:" + DEFAULT_VERSION_SYSPROP + "}",
74-
description =
75-
"The version of google java format to use. Must be >= 1.8." + OptionConstants.DEFAULT_VALUE_SUFFIX)
76-
String useVersion;
73+
@CommandLine.Mixin
74+
CustomVersion customVersion;
7775

7876
@Override
7977
public List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
8078
return List.of(GoogleJavaFormatStep.create(
8179
GoogleJavaFormatStep.defaultGroupArtifact(),
82-
useVersion,
80+
customVersion.useVersion,
8381
style.name(),
8482
context.provisioner(),
8583
reflowLongStrings,

app/src/main/java/com/diffplug/spotless/cli/steps/PalantirJavaFormat.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@
2828

2929
import picocli.CommandLine;
3030

31-
@CommandLine.Command(name = "palantir-java-format", description = "Runs palantir java format")
31+
@CommandLine.Command(
32+
name = "palantir-java-format",
33+
description = "Runs palantir java format",
34+
defaultValueProvider = PalantirJavaFormat.DefaultValueProvider.class)
3235
@SupportedFileTypes("Java")
3336
@AdditionalInfoLinks("https://github.com/palantir/palantir-java-format")
3437
public class PalantirJavaFormat extends SpotlessFormatterStep {
3538

36-
private static final String DEFAULT_VERSION_SYSPROP = "steps.palantir-java-format.default-version";
37-
38-
static {
39-
// workaround for dynamic property values in annotations
40-
System.setProperty(DEFAULT_VERSION_SYSPROP, PalantirJavaFormatStep.defaultVersion());
39+
static class DefaultValueProvider extends CustomVersion.CustomVersionDefaultValueProvider {
40+
DefaultValueProvider() {
41+
super(PalantirJavaFormatStep::defaultVersion);
42+
}
4143
}
4244

4345
@CommandLine.Option(
@@ -53,11 +55,8 @@ public class PalantirJavaFormat extends SpotlessFormatterStep {
5355
description = "Format javadoc." + OptionConstants.DEFAULT_VALUE_SUFFIX)
5456
boolean formatJavadoc;
5557

56-
@CommandLine.Option(
57-
names = {"--use-version", "-v"},
58-
defaultValue = "${sys:" + DEFAULT_VERSION_SYSPROP + "}",
59-
description = "The version of palantir java format to use." + OptionConstants.DEFAULT_VALUE_SUFFIX)
60-
String useVersion;
58+
@CommandLine.Mixin
59+
CustomVersion customVersion;
6160

6261
public enum Style {
6362
PALANTIR,
@@ -68,6 +67,6 @@ public enum Style {
6867
@Override
6968
public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
7069
return List.of(PalantirJavaFormatStep.create(
71-
PalantirJavaFormatStep.defaultVersion(), style.name(), formatJavadoc, context.provisioner()));
70+
customVersion.useVersion, style.name(), formatJavadoc, context.provisioner()));
7271
}
7372
}

0 commit comments

Comments
 (0)