Skip to content

Commit

Permalink
load all RuleSetProviderV3 from the classpath in ktlint 49+ (#698)
Browse files Browse the repository at this point in the history
fixes #697 and #695

test custom rule loading in ktlin 0.50.0 by updating sample project

To Test: Edit Main.kt in the kotlin-rulesets-using projects according to inline comment, then
run ./gradlew samples:kotlin-rulesets-using:build

update root kotlin version to 1.8 since ktlint requires this
update android plugin to 4.1.3
  • Loading branch information
wakingrufus authored Aug 7, 2023
1 parent 840e72e commit f5114e4
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Fix custom rulesets not loading from classpath [#698](https://github.com/JLLeitschuh/ktlint-gradle/pull/698)
- update latest version text file manually [#688](https://github.com/JLLeitschuh/ktlint-gradle/pull/688)

## [11.5.0] - 2023-07-03
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
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.KtLintRuleEngine
import com.pinterest.ktlint.rule.engine.api.LintError
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider
import java.io.File
import java.util.ServiceLoader

class KtLintInvocation49(
private val engine: KtLintRuleEngine
) : KtLintInvocation {
companion object Factory : KtLintInvocationFactory {
fun initialize(): KtLintInvocation {
val engine = KtLintRuleEngine(
ruleProviders = StandardRuleSetProvider().getRuleProviders()
ruleProviders = loadRuleSetsFromClasspathWithRuleSetProviderV3()
)
return KtLintInvocation49(engine)
}

private fun loadRuleSetsFromClasspathWithRuleSetProviderV3(): Set<RuleProvider> {
return ServiceLoader
.load(RuleSetProviderV3::class.java)
.flatMap { it.getRuleProviders() }
.toSet()
}
}

override fun invokeLint(file: File): LintErrorResult {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
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.KtLintRuleEngine
import com.pinterest.ktlint.rule.engine.api.LintError
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider
import java.io.File
import java.util.ServiceLoader

class KtLintInvocation50(
private val engine: KtLintRuleEngine
) : KtLintInvocation {
companion object Factory : KtLintInvocationFactory {
fun initialize(): KtLintInvocation {
val engine = KtLintRuleEngine(
ruleProviders = StandardRuleSetProvider().getRuleProviders()
ruleProviders = loadRuleSetsFromClasspathWithRuleSetProviderV3()
)
return KtLintInvocation50(engine)
}

private fun loadRuleSetsFromClasspathWithRuleSetProviderV3(): Set<RuleProvider> {
return ServiceLoader
.load(RuleSetProviderV3::class.java)
.flatMap { it.getRuleProviders() }
.toSet()
}
}

override fun invokeLint(file: File): LintErrorResult {
Expand Down
1 change: 1 addition & 0 deletions samples/kotlin-js/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.js.compiler=ir
1 change: 1 addition & 0 deletions samples/kotlin-mpp/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.js.compiler=ir
2 changes: 1 addition & 1 deletion samples/kotlin-rulesets-creating/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ plugins {
dependencies {
compileOnly(pluginLibs.kotlin.reflect)
compileOnly(pluginLibs.kotlin.script.runtime)
compileOnly(pluginLibs.ktlint.core)
compileOnly("com.pinterest.ktlint:ktlint-cli-ruleset-core:0.50.0")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.jlleitschuh.gradle.ktlint.sample.kotlin

import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider
import com.pinterest.ktlint.rule.engine.core.api.RuleSetId

class CustomRuleSetProvider : RuleSetProvider {
class CustomRuleSetProvider : RuleSetProviderV3(RuleSetId("custom")) {

override fun get(): RuleSet = RuleSet("custom", NoVarRule())
override fun getRuleProviders(): Set<RuleProvider> {
return setOf(RuleProvider { NoVarRule() })
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.jlleitschuh.gradle.ktlint.sample.kotlin

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.rule.engine.core.api.Rule
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.KtStringTemplateEntry

class NoVarRule : Rule("no-var") {
override fun visit(
class NoVarRule : Rule(
RuleId("test:novar"),
About()
) {
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
Expand Down
1 change: 1 addition & 0 deletions samples/kotlin-rulesets-using/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
}

ktlint {
version.set("0.50.0")
verbose.set(true)
outputToConsole.set(true)
reporters {
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pluginManagement {
includeBuild("./plugin")

plugins {
id("org.jetbrains.kotlin.jvm") version "1.5.31"
id("org.jetbrains.kotlin.js") version "1.5.31"
id("org.jetbrains.kotlin.jvm") version "1.8.22"
id("org.jetbrains.kotlin.js") version "1.8.22"
}

repositories {
Expand All @@ -18,7 +18,7 @@ pluginManagement {
eachPlugin {
when (requested.id.id) {
"com.android.application" ->
useModule("com.android.tools.build:gradle:4.1.0")
useModule("com.android.tools.build:gradle:4.1.3")
}
}
}
Expand Down

0 comments on commit f5114e4

Please sign in to comment.