Skip to content

Commit 8c0893a

Browse files
authored
Merge pull request #129 from JLLeitschuh/task/JLL/115_lint_gradle_kotlin_dsl_files
Adds ktlint linting for Gradle Kotlin DSL files
2 parents 6a3f8ad + d66ac18 commit 8c0893a

File tree

7 files changed

+146
-1
lines changed

7 files changed

+146
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ local.properties
9797
## Plugin-specific files:
9898

9999
# IntelliJ
100-
/out/
100+
out/
101101

102102
# mpeltonen/sbt-idea plugin
103103
.idea_modules/

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ lib('scala.ScalaFmtStep') +'{{yes}} | {{no}}
8282
* Formatting by Eclipse
8383
+ Special thanks to [Mateusz Matela](https://waynebeaton.wordpress.com/2015/03/15/great-fixes-for-mars-winners-part-i/) for huge improvements to the eclipse code formatter!
8484
* Thanks to [Stanley Shyiko](https://github.com/shyiko) for his help integrating [ktlint](https://github.com/shyiko/ktlint).
85+
* Thanks to [Jonathan Leitschuh](https://github.com/JLLeitschuh) for adding [ktlint](https://github.com/shyiko/ktlint) support for [Gradle Kotlin DSL](https://github.com/gradle/kotlin-dsl) files.
8586
* Originally forked from [gradle-format-plugin](https://github.com/youribonnaffe/gradle-format-plugin) by Youri Bonnaffé.
8687
* Thanks to Gábor Bernát for improvements to logging and multi-project support.
8788
* Thanks to Andrew Oberstar for improvements to formatting java source in non-java source sets. [PR #60](https://github.com/diffplug/spotless/pull/60).

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Changed `importOrder` interface from array to varargs ([#125](https://github.com/diffplug/spotless/issues/125))
66
* The `kotlin` extension was mis-spelled as `kotin`.
7+
* Added `kotlinGradle` method to `SpotlessExtension` for linting [Gradle Kotlin DSL](https://github.com/gradle/kotlin-dsl) files with [ktlint](https://github.com/shyiko/ktlint). ([#115](https://github.com/diffplug/spotless/issues/115))
78

89
### Version 3.4.1 - July 11th 2017 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.4.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.4.1))
910

plugin-gradle/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,18 @@ spotless {
226226
licenseHeader '/* Licensed under Apache-2.0 */' // License header
227227
licenseHeaderFile 'path-to-license-file' // License header file
228228
}
229+
kotlinGradle {
230+
// same as kotlin, but for .gradle.kts files (defaults to '*.gradle.kts')
231+
target '*.gradle.kts', 'additionalScripts/*.gradle.kts'
232+
233+
ktlint()
234+
235+
// doesn't support licenseHeader, because scripts don't have a package statement
236+
// to clearly mark where the license should go
237+
}
229238
}
230239
```
240+
231241
<a name="custom"></a>
232242

233243
## Custom rules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.gradle.spotless;
17+
18+
import java.util.Objects;
19+
20+
import com.diffplug.spotless.kotlin.KtLintStep;
21+
22+
public class KotlinGradleExtension extends FormatExtension {
23+
private static final String GRADLE_KOTLIN_DSL_FILE_EXTENSION = "*.gradle.kts";
24+
25+
static final String NAME = "kotlinGradle";
26+
27+
public KotlinGradleExtension(SpotlessExtension rootExtension) {
28+
super(rootExtension);
29+
}
30+
31+
/** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */
32+
public void ktlint(String version) {
33+
Objects.requireNonNull(version, "version");
34+
addStep(KtLintStep.create(version, GradleProvisioner.fromProject(getProject())));
35+
}
36+
37+
public void ktlint() {
38+
ktlint(KtLintStep.defaultVersion());
39+
}
40+
41+
@Override
42+
protected void setupTask(SpotlessTask task) {
43+
if (target == null) {
44+
target = parseTarget(GRADLE_KOTLIN_DSL_FILE_EXTENSION);
45+
}
46+
super.setupTask(task);
47+
}
48+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public void kotlin(Action<KotlinExtension> closure) {
9191
configure(KotlinExtension.NAME, KotlinExtension.class, closure);
9292
}
9393

94+
/** Configures the special Gradle Kotlin DSL specific extension. */
95+
public void kotlinGradle(Action<KotlinGradleExtension> closure) {
96+
requireNonNull(closure);
97+
configure(KotlinGradleExtension.NAME, KotlinGradleExtension.class, closure);
98+
}
99+
94100
/** Configures the special freshmark-specific extension. */
95101
public void freshmark(Action<FreshMarkExtension> closure) {
96102
requireNonNull(closure);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.gradle.spotless;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
public class KotlinGradleExtensionTest extends GradleIntegrationTest {
24+
@Test
25+
public void integration() throws IOException {
26+
testInDirectory(null);
27+
}
28+
29+
@Test
30+
public void integration_script_in_subdir() throws IOException {
31+
testInDirectory("companionScripts");
32+
}
33+
34+
private void testInDirectory(final String directory) throws IOException {
35+
write("build.gradle",
36+
"plugins {",
37+
" id 'nebula.kotlin' version '1.0.6'",
38+
" id 'com.diffplug.gradle.spotless'",
39+
"}",
40+
"repositories { mavenCentral() }",
41+
"spotless {",
42+
" kotlinGradle {",
43+
" ktlint()",
44+
" target '**/*.gradle.kts'",
45+
" }",
46+
"}");
47+
String filePath = "configuration.gradle.kts";
48+
if (directory != null) {
49+
filePath = directory + "/" + filePath;
50+
}
51+
write(filePath,
52+
getTestResource("kotlin/ktlint/basic.dirty"));
53+
gradleRunner().withArguments("spotlessApply").build();
54+
String result = read(filePath);
55+
String formatted = getTestResource("kotlin/ktlint/basic.clean");
56+
Assert.assertEquals(formatted, result);
57+
}
58+
59+
@Test
60+
public void integration_default() throws IOException {
61+
write("build.gradle",
62+
"plugins {",
63+
" id 'nebula.kotlin' version '1.0.6'",
64+
" id 'com.diffplug.gradle.spotless'",
65+
"}",
66+
"repositories { mavenCentral() }",
67+
"spotless {",
68+
" kotlinGradle {",
69+
" ktlint()",
70+
" }",
71+
"}");
72+
write("configuration.gradle.kts",
73+
getTestResource("kotlin/ktlint/basic.dirty"));
74+
gradleRunner().withArguments("spotlessApply").build();
75+
String result = read("configuration.gradle.kts");
76+
String formatted = getTestResource("kotlin/ktlint/basic.clean");
77+
Assert.assertEquals(formatted, result);
78+
}
79+
}

0 commit comments

Comments
 (0)