Skip to content

Commit f926812

Browse files
committed
ktlint 0.50.0 support (fixes #686)
the ktlint changes were source-compatible but not bytecode compatible because of the use of optional parameters in the KtLintRuleEngine constructor
1 parent 8815ddb commit f926812

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
88
## [Unreleased]
99

1010
- update latest version text file manually [#685](https://github.com/JLLeitschuh/ktlint-gradle/pull/685)
11+
- ktlint 0.50.0 compatibility [#687](https://github.com/JLLeitschuh/ktlint-gradle/pull/687)
1112

1213
## [11.4.2] - 2023-06-22
1314

plugin/build.gradle.kts

+23-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,20 @@ sourceSets {
8585
val adapter49 by creating {
8686
compileClasspath += adapter.output
8787
}
88-
val adapters = listOf(adapter, adapter34, adapter41, adapter45, adapter46, adapter47, adapter48, adapter49)
88+
val adapter50 by creating {
89+
compileClasspath += adapter.output
90+
}
91+
val adapters = listOf(
92+
adapter,
93+
adapter34,
94+
adapter41,
95+
adapter45,
96+
adapter46,
97+
adapter47,
98+
adapter48,
99+
adapter49,
100+
adapter50
101+
)
89102
val main by getting {
90103
kotlin {
91104
compileClasspath = adapters.map { it.output }.fold(compileClasspath) { a, b -> a + b }
@@ -107,7 +120,8 @@ val adapterSources = listOf(
107120
sourceSets.named("adapter46"),
108121
sourceSets.named("adapter47"),
109122
sourceSets.named("adapter48"),
110-
sourceSets.named("adapter49")
123+
sourceSets.named("adapter49"),
124+
sourceSets.named("adapter50")
111125
)
112126
tasks.named<Jar>("shadowJar") {
113127
this.from(adapterSources.map { sourceSet -> sourceSet.map { it.output.classesDirs } })
@@ -133,11 +147,18 @@ dependencies {
133147
add("adapter46CompileOnly", "com.pinterest.ktlint:ktlint-core:0.46.1")
134148
add("adapter47CompileOnly", "com.pinterest.ktlint:ktlint-core:0.47.1")
135149
add("adapter48CompileOnly", "com.pinterest.ktlint:ktlint-core:0.48.2")
150+
136151
add("adapter49CompileOnly", "com.pinterest.ktlint:ktlint-core:0.49.1")
137152
add("adapter49CompileOnly", "com.pinterest.ktlint:ktlint-cli-reporter:0.49.1")
138153
add("adapter49CompileOnly", "com.pinterest.ktlint:ktlint-rule-engine:0.49.1")
139154
add("adapter49CompileOnly", "com.pinterest.ktlint:ktlint-ruleset-standard:0.49.1")
140155
add("adapter49CompileOnly", "com.pinterest.ktlint:ktlint-reporter-baseline:0.49.1")
156+
157+
add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-cli-reporter:0.50.0")
158+
add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-rule-engine:0.50.0")
159+
add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-ruleset-standard:0.50.0")
160+
add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-reporter-baseline:0.50.0")
161+
141162
compileOnly(libs.kotlin.gradle.plugin)
142163
compileOnly(libs.android.gradle.plugin)
143164
compileOnly(kotlin("stdlib-jdk8"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jlleitschuh.gradle.ktlint.worker
2+
3+
import com.pinterest.ktlint.rule.engine.api.Code
4+
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine
5+
import com.pinterest.ktlint.rule.engine.api.LintError
6+
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider
7+
import java.io.File
8+
9+
class KtLintInvocation50(
10+
private val engine: KtLintRuleEngine
11+
) : KtLintInvocation {
12+
companion object Factory : KtLintInvocationFactory {
13+
fun initialize(): KtLintInvocation {
14+
val engine = KtLintRuleEngine(
15+
ruleProviders = StandardRuleSetProvider().getRuleProviders()
16+
)
17+
return KtLintInvocation50(engine)
18+
}
19+
}
20+
21+
override fun invokeLint(file: File): LintErrorResult {
22+
val errors = mutableListOf<Pair<SerializableLintError, Boolean>>()
23+
engine.lint(Code.fromFile(file)) { le: LintError ->
24+
errors.add(le.toSerializable() to false)
25+
}
26+
return LintErrorResult(file, errors)
27+
}
28+
29+
override fun invokeFormat(file: File): Pair<String, LintErrorResult> {
30+
val errors = mutableListOf<Pair<SerializableLintError, Boolean>>()
31+
val newCode =
32+
engine.format(Code.fromFile(file)) { le, boolean ->
33+
errors.add(le.toSerializable() to boolean)
34+
}
35+
return newCode to LintErrorResult(file, errors)
36+
}
37+
38+
override fun trimMemory() {
39+
engine.trimMemory()
40+
}
41+
}
42+
43+
internal fun LintError.toSerializable(): SerializableLintError {
44+
return SerializableLintError(line, col, ruleId.value, detail, canBeAutoCorrected)
45+
}

plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtLintCompatibility.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation46
1717
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation47
1818
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation48
1919
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation49
20+
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation50
2021
import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocationFactory
2122
import java.io.Serializable
2223

@@ -31,11 +32,13 @@ internal fun selectInvocation(version: String): KtLintInvocationFactory {
3132
KtLintInvocation47
3233
} else if (semVer.minor == 48) {
3334
KtLintInvocation48
34-
} else {
35+
} else if (semVer.minor == 49) {
3536
KtLintInvocation49
37+
} else {
38+
KtLintInvocation50
3639
}
3740
} else {
38-
KtLintInvocation49
41+
KtLintInvocation50
3942
}
4043
}
4144

plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintWorkAction.kt

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ abstract class KtLintWorkAction : WorkAction<KtLintWorkAction.KtLintWorkParamete
8585
ktlintInvokerFactory.initialize()
8686
}
8787

88+
is KtLintInvocation50.Factory -> {
89+
ktlintInvokerFactory.initialize()
90+
}
8891
else -> {
8992
throw GradleException("Incompatible ktlint version ${parameters.ktLintVersion}")
9093
}

plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtLintSupportedVersionsTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() {
153153
"0.48.1",
154154
"0.48.2",
155155
// "0.49.0" did not expose needed baseline classes
156-
"0.49.1"
156+
"0.49.1",
157+
"0.50.0"
157158
).also {
158159
// "0.37.0" is failing on Windows machines that is fixed in the next version
159160
if (!OS.WINDOWS.isCurrentOs) it.add("0.37.0")

0 commit comments

Comments
 (0)