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

Upgrade Ktlint maven coordinate (com.pinterest), default version (0.32.0) #394

Merged
merged 7 commits into from
Apr 24, 2019
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
30 changes: 23 additions & 7 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,9 +38,13 @@ public class KtLintStep {
// prevent direct instantiation
private KtLintStep() {}

private static final String DEFAULT_VERSION = "0.21.0";
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.github.shyiko: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);
Expand Down Expand Up @@ -77,13 +83,23 @@ 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<String, String> userData;

State(String version, Provisioner provisioner, boolean isScript, Map<String, String> userData) throws IOException {
this.userData = new TreeMap<>(userData);
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
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;
}
sharedprophet marked this conversation as resolved.
Show resolved Hide resolved
this.jarState = JarState.from(coordinate + version, provisioner);
this.isScript = isScript;
}

Expand All @@ -93,19 +109,19 @@ FormatterFunc createFormat() throws Exception {
// String KtLint::format(String input, Iterable<RuleSet> 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);
Expand All @@ -117,7 +133,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";
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public void integration_default() throws IOException {
assertFile("configuration.gradle.kts").sameAsResource("kotlin/ktlint/basic.clean");
}

@Test
public void integration_shyiko() 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(
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.diffplug.spotless.maven.MavenIntegrationTest;

public class KtlintTest extends MavenIntegrationTest {

@Test
public void testKtlint() throws Exception {
writePomWithKotlinSteps("<ktlint/>");
Expand All @@ -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 testKtlintShyiko() throws Exception {
writePomWithKotlinSteps("<ktlint><version>0.21.0</version></ktlint>");

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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down