From 34cb08cf1569298941138f026f519481f540c00c Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 13:44:07 -0500 Subject: [PATCH 1/7] fix ktlint maven coordinate --- lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 5f3dff5d86..9422ca208e 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -38,7 +38,7 @@ private KtLintStep() {} private static final String DEFAULT_VERSION = "0.21.0"; static final String NAME = "ktlint"; - static final String MAVEN_COORDINATE = "com.github.shyiko:ktlint:"; + static final String MAVEN_COORDINATE = "com.pinterest:ktlint:"; public static FormatterStep create(Provisioner provisioner) { return create(defaultVersion(), provisioner); From e6bacb7f5f05372bd0ca24cb0dbaf6c4f45242ac Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 13:44:25 -0500 Subject: [PATCH 2/7] upgrade default version of ktlint --- lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 9422ca208e..2a61b7b7b3 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -36,7 +36,7 @@ public class KtLintStep { // prevent direct instantiation private KtLintStep() {} - private static final String DEFAULT_VERSION = "0.21.0"; + private static final String DEFAULT_VERSION = "0.32.0"; static final String NAME = "ktlint"; static final String MAVEN_COORDINATE = "com.pinterest:ktlint:"; From 9de9f08227e2d5abe1ad3ffb9a9a7c685b2e7e38 Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 13:50:50 -0500 Subject: [PATCH 3/7] CHANGES --- CHANGES.md | 1 + plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 1 + 3 files changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index e662db5f48..aea6f7e4d7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ You might be looking for: - [plugin-maven/CHANGES.md](plugin-maven/CHANGES.md) ### Version 1.23.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/)) +* Updated default ktlint from 0.21.0 to 0.32.0, and Maven coords to com.pinterest ([#394](https://github.com/diffplug/spotless/pull/394)) ### Version 1.22.0 - April 15th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.22.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.22.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra))) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index a62aacc507..40dbd2a01c 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -1,6 +1,7 @@ # spotless-plugin-gradle releases ### Version 3.23.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/)) +* Updated default ktlint from 0.21.0 to 0.32.0, and Maven coords to com.pinterest ([#394](https://github.com/diffplug/spotless/pull/394)) ### Version 3.22.0 - April 15th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.22.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.22.0)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 4c34ff2c00..035de7e6fd 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -1,6 +1,7 @@ # spotless-plugin-maven releases ### Version 1.23.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/)) +* Updated default ktlint from 0.21.0 to 0.32.0, and Maven coords to com.pinterest ([#394](https://github.com/diffplug/spotless/pull/394)) ### Version 1.22.0 - April 15th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.22.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.22.0)) From c1cf3cf6e0319202fb8b56ac725a4926e760430e Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 16:08:59 -0500 Subject: [PATCH 4/7] check version, fix tests --- .../diffplug/spotless/kotlin/KtLintStep.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 2a61b7b7b3..aac2c80b08 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -24,6 +24,8 @@ import java.util.Map; import java.util.Objects; import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; @@ -36,9 +38,13 @@ public class KtLintStep { // prevent direct instantiation private KtLintStep() {} + private static final Pattern VERSION_PRE_0_32 = Pattern.compile("0\\.(\\d+)\\.\\d+"); private static final String DEFAULT_VERSION = "0.32.0"; static final String NAME = "ktlint"; - static final String MAVEN_COORDINATE = "com.pinterest:ktlint:"; + static final String PACKAGE_PRE_0_32 = "com.github.shyiko"; + static final String PACKAGE = "com.pinterest"; + static final String MAVEN_COORDINATE_PRE_0_32 = PACKAGE_PRE_0_32 + ":ktlint:"; + static final String MAVEN_COORDINATE = PACKAGE + ":ktlint:"; public static FormatterStep create(Provisioner provisioner) { return create(defaultVersion(), provisioner); @@ -77,13 +83,22 @@ static final class State implements Serializable { /** Are the files being linted Kotlin script files. */ private final boolean isScript; + private final String pkg; /** The jar that contains the eclipse formatter. */ final JarState jarState; private final TreeMap userData; State(String version, Provisioner provisioner, boolean isScript, Map userData) throws IOException { this.userData = new TreeMap<>(userData); - this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner); + String coordinate = MAVEN_COORDINATE; + Matcher matcher = VERSION_PRE_0_32.matcher(version); + if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 32) { + coordinate = MAVEN_COORDINATE_PRE_0_32; + this.pkg = PACKAGE_PRE_0_32; + } else { + this.pkg = PACKAGE; + } + this.jarState = JarState.from(coordinate + version, provisioner); this.isScript = isScript; } @@ -93,19 +108,19 @@ FormatterFunc createFormat() throws Exception { // String KtLint::format(String input, Iterable rules, Function2 errorCallback) // first, we get the standard rules - Class standardRuleSetProviderClass = classLoader.loadClass("com.github.shyiko.ktlint.ruleset.standard.StandardRuleSetProvider"); + Class standardRuleSetProviderClass = classLoader.loadClass(pkg + ".ktlint.ruleset.standard.StandardRuleSetProvider"); Object standardRuleSet = standardRuleSetProviderClass.getMethod("get").invoke(standardRuleSetProviderClass.newInstance()); Iterable ruleSets = Collections.singletonList(standardRuleSet); // next, we create an error callback which throws an assertion error when the format is bad Class function2Interface = classLoader.loadClass("kotlin.jvm.functions.Function2"); - Class lintErrorClass = classLoader.loadClass("com.github.shyiko.ktlint.core.LintError"); + Class lintErrorClass = classLoader.loadClass(pkg + ".ktlint.core.LintError"); Method detailGetter = lintErrorClass.getMethod("getDetail"); Method lineGetter = lintErrorClass.getMethod("getLine"); Method colGetter = lintErrorClass.getMethod("getCol"); Object formatterCallback = Proxy.newProxyInstance(classLoader, new Class[]{function2Interface}, (proxy, method, args) -> { - Object lintError = args[0]; // com.github.shyiko.ktlint.core.LintError + Object lintError = args[0]; //ktlint.core.LintError boolean corrected = (Boolean) args[1]; if (!corrected) { String detail = (String) detailGetter.invoke(lintError); @@ -117,7 +132,7 @@ FormatterFunc createFormat() throws Exception { }); // grab the KtLint singleton - Class ktlintClass = classLoader.loadClass("com.github.shyiko.ktlint.core.KtLint"); + Class ktlintClass = classLoader.loadClass(pkg + ".ktlint.core.KtLint"); Object ktlint = ktlintClass.getDeclaredField("INSTANCE").get(null); // and its format method String formatterMethodName = isScript ? "formatScript" : "format"; From 6530af5c88dd26df90c707f5ce36da215cf0b87f Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 16:23:32 -0500 Subject: [PATCH 5/7] add tests for older ktlint versions --- .../spotless/KotlinGradleExtensionTest.java | 18 ++++++++++++++++++ .../spotless/maven/kotlin/KtlintTest.java | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) 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 index 62f574f060..3681215cf2 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java @@ -73,6 +73,24 @@ public void integration_default() throws IOException { assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/basic.clean"); } + @Test + public void integration_older() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'nebula.kotlin' version '1.0.6'", + " id 'com.diffplug.gradle.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " kotlinGradle {", + " ktlint('0.21.0')", + " }", + "}"); + setFile("configuration.gradle.kts").toResource("kotlin/ktlint/basic.dirty"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/basic.clean"); + } + @Test public void indentStep() throws IOException { setFile("build.gradle").toLines( diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java index eac462f71e..a743f40176 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java @@ -20,7 +20,6 @@ import com.diffplug.spotless.maven.MavenIntegrationTest; public class KtlintTest extends MavenIntegrationTest { - @Test public void testKtlint() throws Exception { writePomWithKotlinSteps(""); @@ -36,4 +35,20 @@ public void testKtlint() throws Exception { assertFile(path1).sameAsResource("kotlin/ktlint/basic.clean"); assertFile(path2).sameAsResource("kotlin/ktlint/basic.clean"); } + + @Test + public void testOlderKtlint() throws Exception { + writePomWithKotlinSteps("0.21.0"); + + String path1 = "src/main/kotlin/main1.kt"; + String path2 = "src/main/kotlin/main2.kt"; + + setFile(path1).toResource("kotlin/ktlint/basic.dirty"); + setFile(path2).toResource("kotlin/ktlint/basic.dirty"); + + mavenRunner().withArguments("spotless:apply").runNoError(); + + assertFile(path1).sameAsResource("kotlin/ktlint/basic.clean"); + assertFile(path2).sameAsResource("kotlin/ktlint/basic.clean"); + } } From 6be527b1e4d413731b9d447e570da05f07fbb325 Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 16:28:41 -0500 Subject: [PATCH 6/7] feedback --- lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java | 3 ++- .../diffplug/gradle/spotless/KotlinGradleExtensionTest.java | 2 +- .../java/com/diffplug/spotless/maven/kotlin/KtlintTest.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index aac2c80b08..9e0f6db433 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -90,12 +90,13 @@ static final class State implements Serializable { State(String version, Provisioner provisioner, boolean isScript, Map userData) throws IOException { this.userData = new TreeMap<>(userData); - String coordinate = MAVEN_COORDINATE; + String coordinate; Matcher matcher = VERSION_PRE_0_32.matcher(version); if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 32) { coordinate = MAVEN_COORDINATE_PRE_0_32; this.pkg = PACKAGE_PRE_0_32; } else { + coordinate = MAVEN_COORDINATE; this.pkg = PACKAGE; } this.jarState = JarState.from(coordinate + version, provisioner); 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 index 3681215cf2..e4cf7cc428 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java @@ -74,7 +74,7 @@ public void integration_default() throws IOException { } @Test - public void integration_older() throws IOException { + public void integration_shyiko() throws IOException { setFile("build.gradle").toLines( "plugins {", " id 'nebula.kotlin' version '1.0.6'", diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java index a743f40176..20be89f920 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java @@ -37,7 +37,7 @@ public void testKtlint() throws Exception { } @Test - public void testOlderKtlint() throws Exception { + public void testKtlintShyiko() throws Exception { writePomWithKotlinSteps("0.21.0"); String path1 = "src/main/kotlin/main1.kt"; From 45037827b3e6627f375c0ff9ae47542d6da5aacc Mon Sep 17 00:00:00 2001 From: Ethan Johnson Date: Tue, 23 Apr 2019 16:35:40 -0500 Subject: [PATCH 7/7] add core test --- .../spotless/kotlin/KtLintStepTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java index 258fad4677..921e11980e 100644 --- a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java @@ -38,6 +38,34 @@ public void behavior() throws Exception { }); } + @Test + public void worksShyiko() throws Exception { + // Must use jcenter because `com.andreapivetta.kolor:kolor:0.0.2` isn't available on mavenCentral. + // It is a dependency of ktlint. + FormatterStep step = KtLintStep.create("0.31.0", TestProvisioner.jcenter()); + StepHarness.forStep(step) + .testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean") + .testException("kotlin/ktlint/unsolvable.dirty", assertion -> { + assertion.isInstanceOf(AssertionError.class); + assertion.hasMessage("Error on line: 1, column: 1\n" + + "Wildcard import"); + }); + } + + @Test + public void worksPinterest() throws Exception { + // Must use jcenter because `com.andreapivetta.kolor:kolor:0.0.2` isn't available on mavenCentral. + // It is a dependency of ktlint. + FormatterStep step = KtLintStep.create("0.32.0", TestProvisioner.jcenter()); + StepHarness.forStep(step) + .testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean") + .testException("kotlin/ktlint/unsolvable.dirty", assertion -> { + assertion.isInstanceOf(AssertionError.class); + assertion.hasMessage("Error on line: 1, column: 1\n" + + "Wildcard import"); + }); + } + @Test public void equality() throws Exception { new SerializableEqualityTester() {