-
Notifications
You must be signed in to change notification settings - Fork 436
/
KotlinGradleExtension.java
229 lines (191 loc) · 6.94 KB
/
KotlinGradleExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import javax.inject.Inject;
import com.diffplug.common.collect.ImmutableSortedMap;
import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.kotlin.DiktatStep;
import com.diffplug.spotless.kotlin.KtLintStep;
import com.diffplug.spotless.kotlin.KtfmtStep;
import com.diffplug.spotless.kotlin.KtfmtStep.KtfmtFormattingOptions;
import com.diffplug.spotless.kotlin.KtfmtStep.Style;
public class KotlinGradleExtension extends FormatExtension {
private static final String GRADLE_KOTLIN_DSL_FILE_EXTENSION = "*.gradle.kts";
static final String NAME = "kotlinGradle";
@Inject
public KotlinGradleExtension(SpotlessExtension spotless) {
super(spotless);
}
/** Adds the specified version of <a href="https://github.com/pinterest/ktlint">ktlint</a>. */
public KotlinFormatExtension ktlint(String version) throws IOException {
Objects.requireNonNull(version, "version");
File defaultEditorConfig = getProject().getRootProject().file(".editorconfig");
FileSignature editorConfigPath = defaultEditorConfig.exists() ? FileSignature.signAsList(defaultEditorConfig) : null;
return new KotlinFormatExtension(version, false, editorConfigPath, Collections.emptyMap(), Collections.emptyMap());
}
public KotlinFormatExtension ktlint() throws IOException {
return ktlint(KtLintStep.defaultVersion());
}
public class KotlinFormatExtension {
private final String version;
private boolean useExperimental;
@Nullable
private FileSignature editorConfigPath;
private Map<String, String> userData;
private Map<String, Object> editorConfigOverride;
KotlinFormatExtension(String version, boolean useExperimental, FileSignature editorConfigPath, Map<String, String> config,
Map<String, Object> editorConfigOverride) {
this.version = version;
this.useExperimental = useExperimental;
this.editorConfigPath = editorConfigPath;
this.userData = config;
this.editorConfigOverride = editorConfigOverride;
addStep(createStep());
}
public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException {
if (editorConfigPath == null) {
this.editorConfigPath = null;
} else {
this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigPath));
}
replaceStep(createStep());
return this;
}
public KotlinFormatExtension setUseExperimental(boolean useExperimental) {
this.useExperimental = useExperimental;
replaceStep(createStep());
return this;
}
public KotlinFormatExtension userData(Map<String, String> userData) {
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
// representation.
this.userData = ImmutableSortedMap.copyOf(userData);
replaceStep(createStep());
return this;
}
public KotlinFormatExtension editorConfigOverride(Map<String, Object> editorConfigOverride) {
// Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized
// representation.
this.editorConfigOverride = ImmutableSortedMap.copyOf(editorConfigOverride);
replaceStep(createStep());
return this;
}
private FormatterStep createStep() {
return KtLintStep.createForScript(
version,
provisioner(),
useExperimental,
editorConfigPath,
userData,
editorConfigOverride);
}
}
/** Uses the <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> jar to format source code. */
public KtfmtConfig ktfmt() {
return ktfmt(KtfmtStep.defaultVersion());
}
/**
* Uses the given version of <a href="https://github.com/facebookincubator/ktfmt">ktfmt</a> to format source
* code.
*/
public KtfmtConfig ktfmt(String version) {
Objects.requireNonNull(version);
return new KtfmtConfig(version);
}
public class KtfmtConfig {
final String version;
Style style;
KtfmtFormattingOptions options;
private final ConfigurableStyle configurableStyle = new ConfigurableStyle();
KtfmtConfig(String version) {
this.version = Objects.requireNonNull(version);
this.style = Style.DEFAULT;
addStep(createStep());
}
private ConfigurableStyle style(Style style) {
this.style = style;
replaceStep(createStep());
return configurableStyle;
}
public ConfigurableStyle dropboxStyle() {
return style(Style.DROPBOX);
}
public ConfigurableStyle googleStyle() {
return style(Style.GOOGLE);
}
public ConfigurableStyle kotlinlangStyle() {
return style(Style.KOTLINLANG);
}
public void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
this.configurableStyle.configure(optionsConfiguration);
}
private FormatterStep createStep() {
return KtfmtStep.create(version, provisioner(), style, options);
}
public class ConfigurableStyle {
public void configure(Consumer<KtfmtFormattingOptions> optionsConfiguration) {
KtfmtFormattingOptions ktfmtFormattingOptions = new KtfmtFormattingOptions();
optionsConfiguration.accept(ktfmtFormattingOptions);
options = ktfmtFormattingOptions;
replaceStep(createStep());
}
}
}
/** Adds the specified version of <a href="https://github.com/cqfn/diKTat">diktat</a>. */
public DiktatFormatExtension diktat(String version) {
Objects.requireNonNull(version, "version");
return new DiktatFormatExtension(version);
}
public DiktatFormatExtension diktat() {
return diktat(DiktatStep.defaultVersionDiktat());
}
public class DiktatFormatExtension {
private final String version;
private FileSignature config;
DiktatFormatExtension(String version) {
this.version = version;
addStep(createStep());
}
public DiktatFormatExtension configFile(Object file) throws IOException {
// Specify the path to the configuration file
if (file == null) {
this.config = null;
} else {
this.config = FileSignature.signAsList(getProject().file(file));
}
replaceStep(createStep());
return this;
}
private FormatterStep createStep() {
return DiktatStep.createForScript(version, provisioner(), config);
}
}
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
target = parseTarget(GRADLE_KOTLIN_DSL_FILE_EXTENSION);
}
super.setupTask(task);
}
}