-
Notifications
You must be signed in to change notification settings - Fork 435
/
KotlinExtension.java
231 lines (192 loc) · 7.17 KB
/
KotlinExtension.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
230
231
/*
* Copyright 2016-2022 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 static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import javax.inject.Inject;
import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
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 KotlinExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "kotlin";
@Inject
public KotlinExtension(SpotlessExtension spotless) {
super(spotless);
}
@Override
public LicenseHeaderConfig licenseHeader(String licenseHeader) {
return licenseHeader(licenseHeader, LICENSE_HEADER_DELIMITER);
}
@Override
public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
return licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER);
}
/** Adds the specified version of <a href="https://github.com/pinterest/ktlint">ktlint</a>. */
public KotlinFormatExtension ktlint(String version) {
Objects.requireNonNull(version);
return new KotlinFormatExtension(version, false, Collections.emptyMap(), Collections.emptyMap());
}
public KotlinFormatExtension ktlint() {
return ktlint(KtLintStep.defaultVersion());
}
public class KotlinFormatExtension {
private final String version;
private boolean useExperimental;
private Map<String, String> userData;
private Map<String, Object> editorConfigOverride;
KotlinFormatExtension(String version, boolean useExperimental, Map<String, String> config,
Map<String, Object> editorConfigOverride) {
this.version = version;
this.useExperimental = useExperimental;
this.userData = config;
this.editorConfigOverride = editorConfigOverride;
addStep(createStep());
}
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.create(version, provisioner(), useExperimental, 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> and applies the dropbox style
* option 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);
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);
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.create(version, provisioner(), config);
}
}
/** If the user hasn't specified the files yet, we'll assume he/she means all of the kotlin files. */
@Override
protected void setupTask(SpotlessTask task) {
if (target == null) {
JavaPluginConvention javaPlugin = getProject().getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin == null) {
throw new GradleException("You must either specify 'target' manually or apply a kotlin plugin.");
}
FileCollection union = getProject().files();
for (SourceSet sourceSet : javaPlugin.getSourceSets()) {
union = union.plus(sourceSet.getAllSource().filter(file -> {
String name = file.getName();
return name.endsWith(".kt") || name.endsWith(".kts");
}));
}
target = union;
}
super.setupTask(task);
}
}