From 888644f06f07cc3630d918e90d4f0875b9fd63df Mon Sep 17 00:00:00 2001 From: wakingrufus Date: Wed, 6 Sep 2023 17:50:37 -0500 Subject: [PATCH] start 1.0 support --- plugin/build.gradle.kts | 14 +++- .../gradle/ktlint/worker/KtLintInvocation1.kt | 67 +++++++++++++++++++ .../gradle/ktlint/KtLintCompatibility.kt | 5 +- .../ktlint/KtLintSupportedVersionsTest.kt | 43 +++++++++++- .../gradle/ktlint/testdsl/TestDsl.kt | 10 +++ 5 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 plugin/src/adapter1/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintInvocation1.kt diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 6b155721..1b9d2039 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -88,6 +88,9 @@ sourceSets { val adapter50 by creating { compileClasspath += adapter.output } + val adapter1 by creating { + compileClasspath += adapter.output + } val adapters = listOf( adapter, adapter34, @@ -97,7 +100,8 @@ sourceSets { adapter47, adapter48, adapter49, - adapter50 + adapter50, + adapter1 ) val main by getting { kotlin { @@ -121,7 +125,8 @@ val adapterSources = listOf( sourceSets.named("adapter47"), sourceSets.named("adapter48"), sourceSets.named("adapter49"), - sourceSets.named("adapter50") + sourceSets.named("adapter50"), + sourceSets.named("adapter1") ) tasks.named("shadowJar") { this.from(adapterSources.map { sourceSet -> sourceSet.map { it.output.classesDirs } }) @@ -159,6 +164,11 @@ dependencies { add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-ruleset-standard:0.50.0") add("adapter50CompileOnly", "com.pinterest.ktlint:ktlint-reporter-baseline:0.50.0") + add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-cli-reporter-core:1.0.0") + add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-rule-engine:1.0.0") + add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-ruleset-standard:1.0.0") + add("adapter1CompileOnly", "com.pinterest.ktlint:ktlint-cli-reporter-baseline:1.0.0") + compileOnly(libs.kotlin.gradle.plugin) compileOnly(libs.android.gradle.plugin) compileOnly(kotlin("stdlib-jdk8")) diff --git a/plugin/src/adapter1/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintInvocation1.kt b/plugin/src/adapter1/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintInvocation1.kt new file mode 100644 index 00000000..6d5166e6 --- /dev/null +++ b/plugin/src/adapter1/kotlin/org/jlleitschuh/gradle/ktlint/worker/KtLintInvocation1.kt @@ -0,0 +1,67 @@ +package org.jlleitschuh.gradle.ktlint.worker + +import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 +import com.pinterest.ktlint.rule.engine.api.Code +import com.pinterest.ktlint.rule.engine.api.EditorConfigOverride +import com.pinterest.ktlint.rule.engine.api.EditorConfigPropertyRegistry +import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine +import com.pinterest.ktlint.rule.engine.api.LintError +import com.pinterest.ktlint.rule.engine.core.api.RuleProvider +import java.io.File +import java.util.ServiceLoader + +class KtLintInvocation1( + private val engine: KtLintRuleEngine +) : KtLintInvocation { + companion object Factory : KtLintInvocationFactory { + fun initialize( editorConfigOverrides: Map): KtLintInvocation { + val ruleProviders = loadRuleSetsFromClasspathWithRuleSetProviderV3() + val editorConfigPropertyRegistry = EditorConfigPropertyRegistry(ruleProviders) + val engine = if(editorConfigOverrides.isEmpty()){ + KtLintRuleEngine(ruleProviders = ruleProviders) + } else { + KtLintRuleEngine( + ruleProviders = ruleProviders, + editorConfigOverride = EditorConfigOverride.from(*editorConfigOverrides + .mapKeys { editorConfigPropertyRegistry.find(it.key) } + .entries + .map { it.key to it.value } + .toTypedArray()) + ) + } + return KtLintInvocation1(engine) + } + + private fun loadRuleSetsFromClasspathWithRuleSetProviderV3(): Set { + return ServiceLoader + .load(RuleSetProviderV3::class.java) + .flatMap { it.getRuleProviders() } + .toSet() + } + } + + override fun invokeLint(file: File): LintErrorResult { + val errors = mutableListOf>() + engine.lint(Code.fromFile(file)) { le: LintError -> + errors.add(le.toSerializable() to false) + } + return LintErrorResult(file, errors) + } + + override fun invokeFormat(file: File): Pair { + val errors = mutableListOf>() + val newCode = + engine.format(Code.fromFile(file)) { le, boolean -> + errors.add(le.toSerializable() to boolean) + } + return newCode to LintErrorResult(file, errors) + } + + override fun trimMemory() { + engine.trimMemory() + } +} + +internal fun LintError.toSerializable(): SerializableLintError { + return SerializableLintError(line, col, ruleId.value, detail, canBeAutoCorrected) +} diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtLintCompatibility.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtLintCompatibility.kt index 609221f4..392d5077 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtLintCompatibility.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtLintCompatibility.kt @@ -12,6 +12,7 @@ import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader46 import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader47 import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader48 import org.jlleitschuh.gradle.ktlint.worker.BaselineLoader49 +import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation1 import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation45 import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation46 import org.jlleitschuh.gradle.ktlint.worker.KtLintInvocation47 @@ -34,8 +35,10 @@ internal fun selectInvocation(version: String): KtLintInvocationFactory { KtLintInvocation48 } else if (semVer.minor == 49) { KtLintInvocation49 - } else { + } else if(semVer.minor == 50){ KtLintInvocation50 + } else { + KtLintInvocation1 } } else { KtLintInvocation50 diff --git a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtLintSupportedVersionsTest.kt b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtLintSupportedVersionsTest.kt index 7eadc77c..c941d063 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtLintSupportedVersionsTest.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/KtLintSupportedVersionsTest.kt @@ -103,10 +103,10 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() { } } - @DisplayName("Lint should use editorconfig override") + @DisplayName("Lint should use editorconfig override (standard rule)") @ParameterizedTest(name = "{0} with KtLint {1}: {displayName}") @ArgumentsSource(SupportedKtlintVersionsProvider::class) - internal fun `Lint should use editorconfig override`( + internal fun `Lint should use editorconfig override standard rule`( gradleVersion: GradleVersion, ktLintVersion: String ) { @@ -122,6 +122,40 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() { """.trimIndent() ) withFailingSources() + if (SemVer.parse(ktLintVersion) < SemVer(1, 0)) { + buildAndFail(CHECK_PARENT_TASK_NAME) { + assertThat(task(":$mainSourceSetCheckTaskName")?.outcome) + .`as`("standard rules not supported by additionalEditorconfig until 1.0") + .isEqualTo(TaskOutcome.FAILED) + assertThat(output).contains("additionalEditorconfig not supported until ktlint 0.49") + } + } else { + build(CHECK_PARENT_TASK_NAME) { + assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + } + } + } + + @DisplayName("Lint should use editorconfig override") + @ParameterizedTest(name = "{0} with KtLint {1}: {displayName}") + @ArgumentsSource(SupportedKtlintVersionsProvider::class) + internal fun `Lint should use editorconfig override`( + gradleVersion: GradleVersion, + ktLintVersion: String + ) { + project(gradleVersion) { + //language=Groovy + buildGradle.appendText( + """ + + ktlint.version = "$ktLintVersion" + ktlint.additionalEditorconfig = [ + "ij_kotlin_allow_trailing_comma": "true" + ] + """.trimIndent() + ) + withFailingMaxLineSources() if (SemVer.parse(ktLintVersion) < SemVer(0, 49)) { buildAndFail(CHECK_PARENT_TASK_NAME) { assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.FAILED) @@ -129,7 +163,10 @@ class KtLintSupportedVersionsTest : AbstractPluginTest() { } } else { build(CHECK_PARENT_TASK_NAME) { - assertThat(task(":$mainSourceSetCheckTaskName")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(task(":$mainSourceSetCheckTaskName")?.outcome) + .`as`("additionalEditorconfig takes effect") + .isEqualTo(TaskOutcome.SUCCESS) + assertThat(output).doesNotContain("additionalEditorconfig not supported until ktlint 0.49") } } } diff --git a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/testdsl/TestDsl.kt b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/testdsl/TestDsl.kt index 97c2ae80..e9b106bf 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/testdsl/TestDsl.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/testdsl/TestDsl.kt @@ -59,6 +59,16 @@ class TestProject( ) } + fun withFailingMaxLineSources() { + createSourceFile( + FAIL_SOURCE_FILE, + buildString { + append("val nameOfVariable = listOf(1, 2, 3,)") + append("\n") + } + ) + } + fun withAdditionalEditorConfig() { createSourceFile( ADDITIONAL_EDITOR_CONFIG + "/.editorconfig",