Skip to content

Commit c6a4ae7

Browse files
authored
Merge pull request #394 from sharedprophet/ktlint-maven-coordinate
Upgrade Ktlint maven coordinate (com.pinterest), default version (0.32.0)
2 parents fd75332 + 4503782 commit c6a4ae7

File tree

7 files changed

+88
-8
lines changed

7 files changed

+88
-8
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ You might be looking for:
66
- [plugin-maven/CHANGES.md](plugin-maven/CHANGES.md)
77

88
### 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/))
9+
* 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))
910

1011
### 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)))
1112

lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.Objects;
2626
import java.util.TreeMap;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
2729

2830
import com.diffplug.spotless.FormatterFunc;
2931
import com.diffplug.spotless.FormatterStep;
@@ -36,9 +38,13 @@ public class KtLintStep {
3638
// prevent direct instantiation
3739
private KtLintStep() {}
3840

39-
private static final String DEFAULT_VERSION = "0.21.0";
41+
private static final Pattern VERSION_PRE_0_32 = Pattern.compile("0\\.(\\d+)\\.\\d+");
42+
private static final String DEFAULT_VERSION = "0.32.0";
4043
static final String NAME = "ktlint";
41-
static final String MAVEN_COORDINATE = "com.github.shyiko:ktlint:";
44+
static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
45+
static final String PACKAGE = "com.pinterest";
46+
static final String MAVEN_COORDINATE_PRE_0_32 = PACKAGE_PRE_0_32 + ":ktlint:";
47+
static final String MAVEN_COORDINATE = PACKAGE + ":ktlint:";
4248

4349
public static FormatterStep create(Provisioner provisioner) {
4450
return create(defaultVersion(), provisioner);
@@ -77,13 +83,23 @@ static final class State implements Serializable {
7783

7884
/** Are the files being linted Kotlin script files. */
7985
private final boolean isScript;
86+
private final String pkg;
8087
/** The jar that contains the eclipse formatter. */
8188
final JarState jarState;
8289
private final TreeMap<String, String> userData;
8390

8491
State(String version, Provisioner provisioner, boolean isScript, Map<String, String> userData) throws IOException {
8592
this.userData = new TreeMap<>(userData);
86-
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
93+
String coordinate;
94+
Matcher matcher = VERSION_PRE_0_32.matcher(version);
95+
if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 32) {
96+
coordinate = MAVEN_COORDINATE_PRE_0_32;
97+
this.pkg = PACKAGE_PRE_0_32;
98+
} else {
99+
coordinate = MAVEN_COORDINATE;
100+
this.pkg = PACKAGE;
101+
}
102+
this.jarState = JarState.from(coordinate + version, provisioner);
87103
this.isScript = isScript;
88104
}
89105

@@ -93,19 +109,19 @@ FormatterFunc createFormat() throws Exception {
93109
// String KtLint::format(String input, Iterable<RuleSet> rules, Function2 errorCallback)
94110

95111
// first, we get the standard rules
96-
Class<?> standardRuleSetProviderClass = classLoader.loadClass("com.github.shyiko.ktlint.ruleset.standard.StandardRuleSetProvider");
112+
Class<?> standardRuleSetProviderClass = classLoader.loadClass(pkg + ".ktlint.ruleset.standard.StandardRuleSetProvider");
97113
Object standardRuleSet = standardRuleSetProviderClass.getMethod("get").invoke(standardRuleSetProviderClass.newInstance());
98114
Iterable<?> ruleSets = Collections.singletonList(standardRuleSet);
99115

100116
// next, we create an error callback which throws an assertion error when the format is bad
101117
Class<?> function2Interface = classLoader.loadClass("kotlin.jvm.functions.Function2");
102-
Class<?> lintErrorClass = classLoader.loadClass("com.github.shyiko.ktlint.core.LintError");
118+
Class<?> lintErrorClass = classLoader.loadClass(pkg + ".ktlint.core.LintError");
103119
Method detailGetter = lintErrorClass.getMethod("getDetail");
104120
Method lineGetter = lintErrorClass.getMethod("getLine");
105121
Method colGetter = lintErrorClass.getMethod("getCol");
106122
Object formatterCallback = Proxy.newProxyInstance(classLoader, new Class[]{function2Interface},
107123
(proxy, method, args) -> {
108-
Object lintError = args[0]; // com.github.shyiko.ktlint.core.LintError
124+
Object lintError = args[0]; //ktlint.core.LintError
109125
boolean corrected = (Boolean) args[1];
110126
if (!corrected) {
111127
String detail = (String) detailGetter.invoke(lintError);
@@ -117,7 +133,7 @@ FormatterFunc createFormat() throws Exception {
117133
});
118134

119135
// grab the KtLint singleton
120-
Class<?> ktlintClass = classLoader.loadClass("com.github.shyiko.ktlint.core.KtLint");
136+
Class<?> ktlintClass = classLoader.loadClass(pkg + ".ktlint.core.KtLint");
121137
Object ktlint = ktlintClass.getDeclaredField("INSTANCE").get(null);
122138
// and its format method
123139
String formatterMethodName = isScript ? "formatScript" : "format";

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# spotless-plugin-gradle releases
22

33
### 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/))
4+
* 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))
45

56
### 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))
67

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ public void integration_default() throws IOException {
7373
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/basic.clean");
7474
}
7575

76+
@Test
77+
public void integration_shyiko() throws IOException {
78+
setFile("build.gradle").toLines(
79+
"plugins {",
80+
" id 'nebula.kotlin' version '1.0.6'",
81+
" id 'com.diffplug.gradle.spotless'",
82+
"}",
83+
"repositories { mavenCentral() }",
84+
"spotless {",
85+
" kotlinGradle {",
86+
" ktlint('0.21.0')",
87+
" }",
88+
"}");
89+
setFile("configuration.gradle.kts").toResource("kotlin/ktlint/basic.dirty");
90+
gradleRunner().withArguments("spotlessApply").build();
91+
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/basic.clean");
92+
}
93+
7694
@Test
7795
public void indentStep() throws IOException {
7896
setFile("build.gradle").toLines(

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# spotless-plugin-maven releases
22

33
### 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/))
4+
* 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))
45

56
### 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))
67

plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtlintTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.diffplug.spotless.maven.MavenIntegrationTest;
2121

2222
public class KtlintTest extends MavenIntegrationTest {
23-
2423
@Test
2524
public void testKtlint() throws Exception {
2625
writePomWithKotlinSteps("<ktlint/>");
@@ -36,4 +35,20 @@ public void testKtlint() throws Exception {
3635
assertFile(path1).sameAsResource("kotlin/ktlint/basic.clean");
3736
assertFile(path2).sameAsResource("kotlin/ktlint/basic.clean");
3837
}
38+
39+
@Test
40+
public void testKtlintShyiko() throws Exception {
41+
writePomWithKotlinSteps("<ktlint><version>0.21.0</version></ktlint>");
42+
43+
String path1 = "src/main/kotlin/main1.kt";
44+
String path2 = "src/main/kotlin/main2.kt";
45+
46+
setFile(path1).toResource("kotlin/ktlint/basic.dirty");
47+
setFile(path2).toResource("kotlin/ktlint/basic.dirty");
48+
49+
mavenRunner().withArguments("spotless:apply").runNoError();
50+
51+
assertFile(path1).sameAsResource("kotlin/ktlint/basic.clean");
52+
assertFile(path2).sameAsResource("kotlin/ktlint/basic.clean");
53+
}
3954
}

testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ public void behavior() throws Exception {
3838
});
3939
}
4040

41+
@Test
42+
public void worksShyiko() throws Exception {
43+
// Must use jcenter because `com.andreapivetta.kolor:kolor:0.0.2` isn't available on mavenCentral.
44+
// It is a dependency of ktlint.
45+
FormatterStep step = KtLintStep.create("0.31.0", TestProvisioner.jcenter());
46+
StepHarness.forStep(step)
47+
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
48+
.testException("kotlin/ktlint/unsolvable.dirty", assertion -> {
49+
assertion.isInstanceOf(AssertionError.class);
50+
assertion.hasMessage("Error on line: 1, column: 1\n" +
51+
"Wildcard import");
52+
});
53+
}
54+
55+
@Test
56+
public void worksPinterest() throws Exception {
57+
// Must use jcenter because `com.andreapivetta.kolor:kolor:0.0.2` isn't available on mavenCentral.
58+
// It is a dependency of ktlint.
59+
FormatterStep step = KtLintStep.create("0.32.0", TestProvisioner.jcenter());
60+
StepHarness.forStep(step)
61+
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
62+
.testException("kotlin/ktlint/unsolvable.dirty", assertion -> {
63+
assertion.isInstanceOf(AssertionError.class);
64+
assertion.hasMessage("Error on line: 1, column: 1\n" +
65+
"Wildcard import");
66+
});
67+
}
68+
4169
@Test
4270
public void equality() throws Exception {
4371
new SerializableEqualityTester() {

0 commit comments

Comments
 (0)