Skip to content

Commit 2c2c4f7

Browse files
authored
Reuse configs for KotlinExtension and KotlinGradleExtension (#1890)
2 parents 05a3e2e + 210fb8b commit 2c2c4f7

File tree

5 files changed

+214
-385
lines changed

5 files changed

+214
-385
lines changed

lib/src/main/java/com/diffplug/spotless/kotlin/DiktatStep.java

-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public static FormatterStep create(String versionDiktat, Provisioner provisioner
5353
return create(versionDiktat, provisioner, false, config);
5454
}
5555

56-
public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) {
57-
return create(versionDiktat, provisioner, true, config);
58-
}
59-
6056
public static FormatterStep create(String versionDiktat, Provisioner provisioner, boolean isScript, @Nullable FileSignature config) {
6157
if (BadSemver.version(versionDiktat) < BadSemver.version(MIN_SUPPORTED_VERSION)) {
6258
throw new IllegalStateException("Minimum required Diktat version is " + MIN_SUPPORTED_VERSION + ", you tried " + versionDiktat + " which is too old");

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

-17
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,6 @@ public static FormatterStep create(String version, Provisioner provisioner,
5454
return create(version, provisioner, false, null, userData, editorConfigOverride);
5555
}
5656

57-
public static FormatterStep createForScript(String version, Provisioner provisioner) {
58-
return create(version, provisioner, true, null, Collections.emptyMap(), Collections.emptyMap());
59-
}
60-
61-
public static FormatterStep createForScript(String version,
62-
Provisioner provisioner,
63-
@Nullable FileSignature editorConfigPath,
64-
Map<String, String> userData,
65-
Map<String, Object> editorConfigOverride) {
66-
return create(version,
67-
provisioner,
68-
true,
69-
editorConfigPath,
70-
userData,
71-
editorConfigOverride);
72-
}
73-
7457
public static FormatterStep create(String version,
7558
Provisioner provisioner,
7659
boolean isScript,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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.gradle.spotless;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.util.Collections;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
import java.util.function.Consumer;
24+
25+
import javax.annotation.Nullable;
26+
27+
import com.diffplug.common.collect.ImmutableSortedMap;
28+
import com.diffplug.spotless.FileSignature;
29+
import com.diffplug.spotless.FormatterStep;
30+
import com.diffplug.spotless.kotlin.DiktatStep;
31+
import com.diffplug.spotless.kotlin.KtLintStep;
32+
import com.diffplug.spotless.kotlin.KtfmtStep;
33+
34+
abstract class BaseKotlinExtension extends FormatExtension {
35+
protected BaseKotlinExtension(SpotlessExtension spotless) {
36+
super(spotless);
37+
}
38+
39+
public DiktatConfig diktat() {
40+
return diktat(DiktatStep.defaultVersionDiktat());
41+
}
42+
43+
/** Adds the specified version of <a href="https://github.com/cqfn/diKTat">diktat</a>. */
44+
public DiktatConfig diktat(String version) {
45+
return new DiktatConfig(version);
46+
}
47+
48+
public KtlintConfig ktlint() throws IOException {
49+
return ktlint(KtLintStep.defaultVersion());
50+
}
51+
52+
/** Adds the specified version of <a href="https://github.com/pinterest/ktlint">ktlint</a>. */
53+
public KtlintConfig ktlint(String version) throws IOException {
54+
return new KtlintConfig(version, Collections.emptyMap(), Collections.emptyMap());
55+
}
56+
57+
/** Uses the <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> jar to format source code. */
58+
public KtfmtConfig ktfmt() {
59+
return ktfmt(KtfmtStep.defaultVersion());
60+
}
61+
62+
/**
63+
* Uses the given version of <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> and applies the dropbox style
64+
* option to format source code.
65+
*/
66+
public KtfmtConfig ktfmt(String version) {
67+
return new KtfmtConfig(version);
68+
}
69+
70+
protected abstract boolean isScript();
71+
72+
public class DiktatConfig {
73+
private final String version;
74+
private FileSignature config;
75+
76+
private DiktatConfig(String version) {
77+
Objects.requireNonNull(version, "version");
78+
this.version = version;
79+
addStep(createStep());
80+
}
81+
82+
public DiktatConfig configFile(Object file) throws IOException {
83+
// Specify the path to the configuration file
84+
if (file == null) {
85+
this.config = null;
86+
} else {
87+
this.config = FileSignature.signAsList(getProject().file(file));
88+
}
89+
replaceStep(createStep());
90+
return this;
91+
}
92+
93+
private FormatterStep createStep() {
94+
return DiktatStep.create(version, provisioner(), isScript(), config);
95+
}
96+
}
97+
98+
public class KtfmtConfig {
99+
private final String version;
100+
private final ConfigurableStyle configurableStyle = new ConfigurableStyle();
101+
private KtfmtStep.Style style;
102+
private KtfmtStep.KtfmtFormattingOptions options;
103+
104+
private KtfmtConfig(String version) {
105+
Objects.requireNonNull(version);
106+
this.version = Objects.requireNonNull(version);
107+
addStep(createStep());
108+
}
109+
110+
public ConfigurableStyle dropboxStyle() {
111+
return style(KtfmtStep.Style.DROPBOX);
112+
}
113+
114+
public ConfigurableStyle googleStyle() {
115+
return style(KtfmtStep.Style.GOOGLE);
116+
}
117+
118+
public ConfigurableStyle kotlinlangStyle() {
119+
return style(KtfmtStep.Style.KOTLINLANG);
120+
}
121+
122+
public void configure(Consumer<KtfmtStep.KtfmtFormattingOptions> optionsConfiguration) {
123+
this.configurableStyle.configure(optionsConfiguration);
124+
}
125+
126+
private ConfigurableStyle style(KtfmtStep.Style style) {
127+
this.style = style;
128+
replaceStep(createStep());
129+
return configurableStyle;
130+
}
131+
132+
private FormatterStep createStep() {
133+
return KtfmtStep.create(version, provisioner(), style, options);
134+
}
135+
136+
public class ConfigurableStyle {
137+
private void configure(Consumer<KtfmtStep.KtfmtFormattingOptions> optionsConfiguration) {
138+
KtfmtStep.KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtStep.KtfmtFormattingOptions();
139+
optionsConfiguration.accept(ktfmtFormattingOptions);
140+
options = ktfmtFormattingOptions;
141+
replaceStep(createStep());
142+
}
143+
}
144+
}
145+
146+
public class KtlintConfig {
147+
private final String version;
148+
private FileSignature editorConfigPath;
149+
private Map<String, String> userData;
150+
private Map<String, Object> editorConfigOverride;
151+
152+
private KtlintConfig(
153+
String version,
154+
Map<String, String> userData,
155+
Map<String, Object> editorConfigOverride) throws IOException {
156+
Objects.requireNonNull(version);
157+
File defaultEditorConfig = getProject().getRootProject().file(".editorconfig");
158+
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null;
159+
this.version = version;
160+
this.editorConfigPath = editorConfigPath;
161+
this.userData = userData;
162+
this.editorConfigOverride = editorConfigOverride;
163+
addStep(createStep());
164+
}
165+
166+
public KtlintConfig setEditorConfigPath(@Nullable Object editorConfigPath) throws IOException {
167+
if (editorConfigPath == null) {
168+
this.editorConfigPath = null;
169+
} else {
170+
File editorConfigFile = getProject().file(editorConfigPath);
171+
if (!editorConfigFile.exists()) {
172+
throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile);
173+
}
174+
this.editorConfigPath = FileSignature.signAsList(editorConfigFile);
175+
}
176+
replaceStep(createStep());
177+
return this;
178+
}
179+
180+
public KtlintConfig userData(Map<String, String> userData) {
181+
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
182+
// representation.
183+
this.userData = ImmutableSortedMap.copyOf(userData);
184+
replaceStep(createStep());
185+
return this;
186+
}
187+
188+
public KtlintConfig editorConfigOverride(Map<String, Object> editorConfigOverride) {
189+
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
190+
// representation.
191+
this.editorConfigOverride = ImmutableSortedMap.copyOf(editorConfigOverride);
192+
replaceStep(createStep());
193+
return this;
194+
}
195+
196+
private FormatterStep createStep() {
197+
return KtLintStep.create(
198+
version,
199+
provisioner(),
200+
isScript(),
201+
editorConfigPath,
202+
userData,
203+
editorConfigOverride);
204+
}
205+
}
206+
}

0 commit comments

Comments
 (0)