Skip to content

Commit 4597085

Browse files
committed
Reuse configs for KotlinExtension and KotlinGradleExtension
1 parent 2ff556b commit 4597085

File tree

5 files changed

+217
-385
lines changed

5 files changed

+217
-385
lines changed

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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 17 deletions
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,
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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+
public abstract class BaseKotlinExtension extends FormatExtension {
35+
public 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+
74+
private final String version;
75+
private FileSignature config;
76+
77+
DiktatConfig(String version) {
78+
Objects.requireNonNull(version, "version");
79+
this.version = version;
80+
addStep(createStep());
81+
}
82+
83+
public DiktatConfig configFile(Object file) throws IOException {
84+
// Specify the path to the configuration file
85+
if (file == null) {
86+
this.config = null;
87+
} else {
88+
this.config = FileSignature.signAsList(getProject().file(file));
89+
}
90+
replaceStep(createStep());
91+
return this;
92+
}
93+
94+
private FormatterStep createStep() {
95+
return DiktatStep.create(version, provisioner(), isScript(), config);
96+
}
97+
}
98+
99+
public class KtfmtConfig {
100+
final String version;
101+
KtfmtStep.Style style;
102+
KtfmtStep.KtfmtFormattingOptions options;
103+
104+
private final ConfigurableStyle configurableStyle = new ConfigurableStyle();
105+
106+
KtfmtConfig(String version) {
107+
Objects.requireNonNull(version);
108+
this.version = Objects.requireNonNull(version);
109+
addStep(createStep());
110+
}
111+
112+
private ConfigurableStyle style(KtfmtStep.Style style) {
113+
this.style = style;
114+
replaceStep(createStep());
115+
return configurableStyle;
116+
}
117+
118+
public ConfigurableStyle dropboxStyle() {
119+
return style(KtfmtStep.Style.DROPBOX);
120+
}
121+
122+
public ConfigurableStyle googleStyle() {
123+
return style(KtfmtStep.Style.GOOGLE);
124+
}
125+
126+
public ConfigurableStyle kotlinlangStyle() {
127+
return style(KtfmtStep.Style.KOTLINLANG);
128+
}
129+
130+
public void configure(Consumer<KtfmtStep.KtfmtFormattingOptions> optionsConfiguration) {
131+
this.configurableStyle.configure(optionsConfiguration);
132+
}
133+
134+
private FormatterStep createStep() {
135+
return KtfmtStep.create(version, provisioner(), style, options);
136+
}
137+
138+
public class ConfigurableStyle {
139+
140+
public void configure(Consumer<KtfmtStep.KtfmtFormattingOptions> optionsConfiguration) {
141+
KtfmtStep.KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtStep.KtfmtFormattingOptions();
142+
optionsConfiguration.accept(ktfmtFormattingOptions);
143+
options = ktfmtFormattingOptions;
144+
replaceStep(createStep());
145+
}
146+
}
147+
}
148+
149+
public class KtlintConfig {
150+
151+
private final String version;
152+
@Nullable
153+
private FileSignature editorConfigPath;
154+
private Map<String, String> userData;
155+
private Map<String, Object> editorConfigOverride;
156+
157+
KtlintConfig(String version, Map<String, String> config,
158+
Map<String, Object> editorConfigOverride) throws IOException {
159+
Objects.requireNonNull(version);
160+
File defaultEditorConfig = getProject().getRootProject().file(".editorconfig");
161+
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null;
162+
this.version = version;
163+
this.editorConfigPath = editorConfigPath;
164+
this.userData = config;
165+
this.editorConfigOverride = editorConfigOverride;
166+
addStep(createStep());
167+
}
168+
169+
public KtlintConfig setEditorConfigPath(Object editorConfigPath) throws IOException {
170+
if (editorConfigPath == null) {
171+
this.editorConfigPath = null;
172+
} else {
173+
File editorConfigFile = getProject().file(editorConfigPath);
174+
if (!editorConfigFile.exists()) {
175+
throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile);
176+
}
177+
this.editorConfigPath = FileSignature.signAsList(editorConfigFile);
178+
}
179+
replaceStep(createStep());
180+
return this;
181+
}
182+
183+
public KtlintConfig userData(Map<String, String> userData) {
184+
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
185+
// representation.
186+
this.userData = ImmutableSortedMap.copyOf(userData);
187+
replaceStep(createStep());
188+
return this;
189+
}
190+
191+
public KtlintConfig editorConfigOverride(Map<String, Object> editorConfigOverride) {
192+
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
193+
// representation.
194+
this.editorConfigOverride = ImmutableSortedMap.copyOf(editorConfigOverride);
195+
replaceStep(createStep());
196+
return this;
197+
}
198+
199+
private FormatterStep createStep() {
200+
return KtLintStep.create(
201+
version,
202+
provisioner(),
203+
isScript(),
204+
editorConfigPath,
205+
userData,
206+
editorConfigOverride);
207+
}
208+
}
209+
}

0 commit comments

Comments
 (0)