Skip to content

Commit 2de1b68

Browse files
authored
Merge pull request #223 from lutovich/kotlin-fmt-maven
Kotlin support in Maven plugin
2 parents da83500 + 178ffad commit 2de1b68

File tree

18 files changed

+274
-41
lines changed

18 files changed

+274
-41
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}}
4343
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{no}} |',
4444
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{no}} |',
4545
extra('java.EclipseFormatterStep') +'{{yes}} | {{yes}} | {{no}} |',
46-
lib('kotlin.KtLintStep') +'{{yes}} | {{no}} | {{no}} |',
46+
lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{no}} |',
4747
lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} |',
4848
lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |',
4949
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
@@ -66,7 +66,7 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}}
6666
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :white_large_square: |
6767
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :white_large_square: |
6868
| [`java.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java) | :+1: | :+1: | :white_large_square: |
69-
| [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :white_large_square: | :white_large_square: |
69+
| [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :white_large_square: |
7070
| [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: |
7171
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: |
7272
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2016 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.kotlin;
17+
18+
public final class KotlinConstants {
19+
20+
// '^' is prepended to the regex in LICENSE_HEADER_DELIMITER later in FormatExtension.licenseHeader(String, String)
21+
public static final String LICENSE_HEADER_DELIMITER = "(package |@file)";
22+
23+
private KotlinConstants() {}
24+
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER;
19+
1820
import java.util.Objects;
1921

2022
import org.gradle.api.GradleException;
@@ -25,8 +27,6 @@
2527
import com.diffplug.spotless.kotlin.KtLintStep;
2628

2729
public class KotlinExtension extends FormatExtension {
28-
// '^' is prepended to the regex in LICENSE_HEADER_DELIMITER later in FormatExtension.licenseHeader(String, String)
29-
private static final String LICENSE_HEADER_DELIMITER = "(package |@file)";
3030
static final String NAME = "kotlin";
3131

3232
public KotlinExtension(SpotlessExtension rootExtension) {

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Version 1.10.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))
44

55
* Fixed a bug in `LicenseHeaderStep` which caused an exception with some malformed date-aware licenses. ([#222](https://github.com/diffplug/spotless/pull/222))
6+
* Added support for Kotlin and Ktlint in Maven plugin ([#223](https://github.com/diffplug/spotless/pull/223)).
67

78
### Version 1.0.0.BETA4 - February 27th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.0.0.BETA4/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.0.0.BETA4))
89

plugin-maven/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,55 @@ By default, all files matching `src/main/java/**/*.java` and `src/test/java/**/*
115115
</configuration>
116116
```
117117

118+
<a name="scala"></a>
119+
120+
## Applying to Scala source
121+
122+
By default, all files matching `src/main/scala/**/*.scala`, `src/test/scala/**/*.scala`, `src/main/scala/**/*.sc` and `src/test/scala/**/*.sc` Ant style pattern will be formatted. Each element under `<scala>` is a step, and they will be applied in the order specified. Every step is optional.
123+
124+
```xml
125+
<configuration>
126+
<scala>
127+
<licenseHeader>
128+
<!-- Specify either content or file, but not both -->
129+
<content>/* Licensed under Apache-2.0 */</content>
130+
<file>${basedir}/license-header</file>
131+
</licenseHeader>
132+
<endWithNewLine/>
133+
<trimTrailingWhitespace/>
134+
<scalafmt>
135+
<file>${basedir}/scalafmt.conf</file>
136+
<!-- Optional, available versions: https://github.com/scalameta/scalafmt/releases -->
137+
<version>1.1.0</version>
138+
</scalafmt>
139+
</scala>
140+
</configuration>
141+
```
142+
143+
<a name="kotlin"></a>
144+
145+
## Applying to Kotlin source
146+
147+
By default, all files matching `src/main/kotlin/**/*.kt` and `src/test/kotlin/**/*.kt` Ant style pattern will be formatted. Each element under `<kotlin>` is a step, and they will be applied in the order specified. Every step is optional.
148+
149+
```xml
150+
<configuration>
151+
<kotlin>
152+
<licenseHeader>
153+
<!-- Specify either content or file, but not both -->
154+
<content>/* Licensed under Apache-2.0 */</content>
155+
<file>${basedir}/license-header</file>
156+
</licenseHeader>
157+
<endWithNewLine/>
158+
<trimTrailingWhitespace/>
159+
<ktlint>
160+
<!-- Optional, available versions: https://github.com/shyiko/ktlint/releases -->
161+
<version>0.14.0</version>
162+
</ktlint>
163+
</kotlin>
164+
</configuration>
165+
```
166+
118167
<a name="format"></a>
119168

120169
## Applying to custom sources

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.diffplug.spotless.maven.generic.Format;
4343
import com.diffplug.spotless.maven.generic.LicenseHeader;
4444
import com.diffplug.spotless.maven.java.Java;
45+
import com.diffplug.spotless.maven.kotlin.Kotlin;
4546
import com.diffplug.spotless.maven.scala.Scala;
4647

4748
public abstract class AbstractSpotlessMojo extends AbstractMojo {
@@ -85,6 +86,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
8586
@Parameter
8687
private Scala scala;
8788

89+
@Parameter
90+
private Kotlin kotlin;
91+
8892
protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;
8993

9094
@Override
@@ -142,7 +146,7 @@ private FileLocator getFileLocator() {
142146
}
143147

144148
private List<FormatterFactory> getFormatterFactories() {
145-
return Stream.of(format, java, scala)
149+
return Stream.of(format, java, scala, kotlin)
146150
.filter(Objects::nonNull)
147151
.collect(toList());
148152
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.diffplug.spotless.Formatter;
3333
import com.diffplug.spotless.FormatterStep;
3434
import com.diffplug.spotless.LineEnding;
35-
import com.diffplug.spotless.maven.generic.LicenseHeader;
35+
import com.diffplug.spotless.maven.generic.*;
3636

3737
public abstract class FormatterFactory {
3838
@Parameter
@@ -87,7 +87,27 @@ public final void addLicenseHeader(LicenseHeader licenseHeader) {
8787
addStepFactory(licenseHeader);
8888
}
8989

90-
protected void addStepFactory(FormatterStepFactory stepFactory) {
90+
public final void addEndWithNewline(EndWithNewline endWithNewline) {
91+
addStepFactory(endWithNewline);
92+
}
93+
94+
public final void addIndent(Indent indent) {
95+
addStepFactory(indent);
96+
}
97+
98+
public final void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
99+
addStepFactory(trimTrailingWhitespace);
100+
}
101+
102+
public final void addReplace(Replace replace) {
103+
addStepFactory(replace);
104+
}
105+
106+
public final void addReplaceRegex(ReplaceRegex replaceRegex) {
107+
addStepFactory(replaceRegex);
108+
}
109+
110+
protected final void addStepFactory(FormatterStepFactory stepFactory) {
91111
Objects.requireNonNull(stepFactory);
92112
stepFactories.add(stepFactory);
93113
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java

+6-20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
import com.diffplug.spotless.maven.FormatterFactory;
2222

23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <format>...</format>} configuration element.
25+
* <p>
26+
* It defines a formatter for custom includes/excludes that executes list of generic, language agnostic steps,
27+
* like {@link LicenseHeader}.
28+
*/
2329
public class Format extends FormatterFactory {
2430

2531
@Override
@@ -32,24 +38,4 @@ public String licenseHeaderDelimiter() {
3238
// do not specify a default delimiter
3339
return null;
3440
}
35-
36-
public void addEndWithNewline(EndWithNewline endWithNewline) {
37-
addStepFactory(endWithNewline);
38-
}
39-
40-
public void addIndent(Indent indent) {
41-
addStepFactory(indent);
42-
}
43-
44-
public void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
45-
addStepFactory(trimTrailingWhitespace);
46-
}
47-
48-
public void addReplace(Replace replace) {
49-
addStepFactory(replace);
50-
}
51-
52-
public void addReplaceRegex(ReplaceRegex replaceRegex) {
53-
addStepFactory(replaceRegex);
54-
}
5541
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@
1515
*/
1616
package com.diffplug.spotless.maven.java;
1717

18-
import static com.diffplug.common.collect.Sets.newHashSet;
19-
import static java.util.Collections.unmodifiableSet;
20-
2118
import java.util.Set;
2219

23-
import com.diffplug.spotless.maven.generic.Format;
20+
import com.diffplug.common.collect.ImmutableSet;
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
import com.diffplug.spotless.maven.generic.LicenseHeader;
23+
24+
/**
25+
* A {@link FormatterFactory} implementation that corresponds to {@code <java>...</java>} configuration element.
26+
* <p>
27+
* It defines a formatter for java source files that can execute both language agnostic (e.g. {@link LicenseHeader})
28+
* and java-specific (e.g. {@link Eclipse}) steps.
29+
*/
30+
public class Java extends FormatterFactory {
2431

25-
public class Java extends Format {
26-
private static final Set<String> DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/java/**/*.java",
27-
"src/test/java/**/*.java"));
32+
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/java/**/*.java", "src/test/java/**/*.java");
2833
private static final String LICENSE_HEADER_DELIMITER = "package ";
2934

3035
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 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.maven.kotlin;
17+
18+
import static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER;
19+
20+
import java.util.Set;
21+
22+
import com.diffplug.common.collect.ImmutableSet;
23+
import com.diffplug.spotless.maven.FormatterFactory;
24+
25+
public class Kotlin extends FormatterFactory {
26+
27+
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/kotlin/**/*.kt", "src/test/kotlin/**/*.kt");
28+
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return DEFAULT_INCLUDES;
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return LICENSE_HEADER_DELIMITER;
37+
}
38+
39+
public void addKtlint(Ktlint ktlint) {
40+
addStepFactory(ktlint);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016 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.maven.kotlin;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.kotlin.KtLintStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Ktlint implements FormatterStepFactory {
26+
27+
@Parameter
28+
private String version;
29+
30+
@Override
31+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
32+
String ktlintVersion = version != null ? version : KtLintStep.defaultVersion();
33+
return KtLintStep.create(ktlintVersion, config.getProvisioner());
34+
}
35+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
*/
1616
package com.diffplug.spotless.maven.scala;
1717

18-
import static com.diffplug.common.collect.Sets.newHashSet;
19-
import static java.util.Collections.unmodifiableSet;
20-
2118
import java.util.Set;
2219

23-
import com.diffplug.spotless.maven.generic.Format;
20+
import com.diffplug.common.collect.ImmutableSet;
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
import com.diffplug.spotless.maven.generic.LicenseHeader;
23+
24+
/**
25+
* A {@link FormatterFactory} implementation that corresponds to {@code <scala>...</scala>} configuration element.
26+
* <p>
27+
* It defines a formatter for scala source files that can execute both language agnostic (e.g. {@link LicenseHeader})
28+
* and scala-specific (e.g. {@link Scalafmt}) steps.
29+
*/
30+
public class Scala extends FormatterFactory {
2431

25-
public class Scala extends Format {
26-
private static final Set<String> DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/scala/**/*.scala",
27-
"src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc"));
32+
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/scala/**/*.scala",
33+
"src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc");
2834
private static final String LICENSE_HEADER_DELIMITER = "package ";
2935

3036
@Override

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ protected void writePomWithScalaSteps(String... steps) throws IOException {
9898
writePom(groupWithSteps("scala", steps));
9999
}
100100

101+
protected void writePomWithKotlinSteps(String... steps) throws IOException {
102+
writePom(groupWithSteps("kotlin", steps));
103+
}
104+
101105
protected void writePom(String... configuration) throws IOException {
102106
writePom(null, configuration);
103107
}

0 commit comments

Comments
 (0)