Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ktlint linting for Gradle Kotlin DSL files #129

Merged
merged 6 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ local.properties
## Plugin-specific files:

# IntelliJ
/out/
out/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had files showing up that should be ignored because of intellij. I can move this to a different PR if you really want but I figured it wasn't worth the overhead. (It's in its own commit).


# mpeltonen/sbt-idea plugin
.idea_modules/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
10 changes: 10 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```

<a name="custom"></a>

## Custom rules
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public void kotlin(Action<KotlinExtension> closure) {
configure(KotlinExtension.NAME, KotlinExtension.class, closure);
}

/** Configures the special Gradle Kotlin DSL specific extension. */
public void kotlinGradle(Action<KotlinGradleExtension> closure) {
requireNonNull(closure);
configure(KotlinGradleExtension.NAME, KotlinGradleExtension.class, closure);
}

/** Configures the special freshmark-specific extension. */
public void freshmark(Action<FreshMarkExtension> closure) {
requireNonNull(closure);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}