diff --git a/.gitignore b/.gitignore index 63bfac5a56..a1a7c08822 100644 --- a/.gitignore +++ b/.gitignore @@ -97,7 +97,7 @@ local.properties ## Plugin-specific files: # IntelliJ -/out/ +out/ # mpeltonen/sbt-idea plugin .idea_modules/ diff --git a/README.md b/README.md index f694e42fd5..1b23d87570 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ lib('scala.ScalaFmtStep') +'{{yes}} | {{no}} * Formatting by Eclipse + 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! * Thanks to [Stanley Shyiko](https://github.com/shyiko) for his help integrating [ktlint](https://github.com/shyiko/ktlint). +* 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. * Originally forked from [gradle-format-plugin](https://github.com/youribonnaffe/gradle-format-plugin) by Youri Bonnaffé. * Thanks to Gábor Bernát for improvements to logging and multi-project support. * Thanks to Andrew Oberstar for improvements to formatting java source in non-java source sets. [PR #60](https://github.com/diffplug/spotless/pull/60). diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index d66523f1bb..af02eef505 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -4,6 +4,7 @@ * Changed `importOrder` interface from array to varargs ([#125](https://github.com/diffplug/spotless/issues/125)) * The `kotlin` extension was mis-spelled as `kotin`. +* 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)) ### 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)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 25cf65fc46..7a4eea7484 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -226,8 +226,18 @@ spotless { licenseHeader '/* Licensed under Apache-2.0 */' // License header licenseHeaderFile 'path-to-license-file' // License header file } + kotlinGradle { + // same as kotlin, but for .gradle.kts files (defaults to '*.gradle.kts') + target '*.gradle.kts', 'additionalScripts/*.gradle.kts' + + ktlint() + + // doesn't support licenseHeader, because scripts don't have a package statement + // to clearly mark where the license should go + } } ``` + ## Custom rules diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java new file mode 100644 index 0000000000..65a0380d81 --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016 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.util.Objects; + +import com.diffplug.spotless.kotlin.KtLintStep; + +public class KotlinGradleExtension extends FormatExtension { + private static final String GRADLE_KOTLIN_DSL_FILE_EXTENSION = "*.gradle.kts"; + + static final String NAME = "kotlinGradle"; + + public KotlinGradleExtension(SpotlessExtension rootExtension) { + super(rootExtension); + } + + /** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */ + public void ktlint(String version) { + Objects.requireNonNull(version, "version"); + addStep(KtLintStep.create(version, GradleProvisioner.fromProject(getProject()))); + } + + public void ktlint() { + ktlint(KtLintStep.defaultVersion()); + } + + @Override + protected void setupTask(SpotlessTask task) { + if (target == null) { + target = parseTarget(GRADLE_KOTLIN_DSL_FILE_EXTENSION); + } + super.setupTask(task); + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 9b80852ab3..d218cd8a34 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -91,6 +91,12 @@ public void kotlin(Action closure) { configure(KotlinExtension.NAME, KotlinExtension.class, closure); } + /** Configures the special Gradle Kotlin DSL specific extension. */ + public void kotlinGradle(Action closure) { + requireNonNull(closure); + configure(KotlinGradleExtension.NAME, KotlinGradleExtension.class, closure); + } + /** Configures the special freshmark-specific extension. */ public void freshmark(Action closure) { requireNonNull(closure); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java new file mode 100644 index 0000000000..f0f075320c --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016 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.IOException; + +import org.junit.Assert; +import org.junit.Test; + +public class KotlinGradleExtensionTest extends GradleIntegrationTest { + @Test + public void integration() throws IOException { + testInDirectory(null); + } + + @Test + public void integration_script_in_subdir() throws IOException { + testInDirectory("companionScripts"); + } + + private void testInDirectory(final String directory) throws IOException { + write("build.gradle", + "plugins {", + " id 'nebula.kotlin' version '1.0.6'", + " id 'com.diffplug.gradle.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlinGradle {", + " ktlint()", + " target '**/*.gradle.kts'", + " }", + "}"); + String filePath = "configuration.gradle.kts"; + if (directory != null) { + filePath = directory + "/" + filePath; + } + write(filePath, + getTestResource("kotlin/ktlint/basic.dirty")); + gradleRunner().withArguments("spotlessApply").build(); + String result = read(filePath); + String formatted = getTestResource("kotlin/ktlint/basic.clean"); + Assert.assertEquals(formatted, result); + } + + @Test + public void integration_default() throws IOException { + write("build.gradle", + "plugins {", + " id 'nebula.kotlin' version '1.0.6'", + " id 'com.diffplug.gradle.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlinGradle {", + " ktlint()", + " }", + "}"); + write("configuration.gradle.kts", + getTestResource("kotlin/ktlint/basic.dirty")); + gradleRunner().withArguments("spotlessApply").build(); + String result = read("configuration.gradle.kts"); + String formatted = getTestResource("kotlin/ktlint/basic.clean"); + Assert.assertEquals(formatted, result); + } +}