Skip to content

Commit

Permalink
Release 3.14.0 and various bugfixes (#317)
Browse files Browse the repository at this point in the history
- Fix issue #316 by upgrading kotlinter gradle's released version to ktlint 0.48.2.
- Remove configuration for `disabledRules` and `experimentalRules` to fully delegate this to .editorconfig
- Fix issue with resolution of rule enable / disable statements in .editorconfig
- Address issue #242 by providing readme example of excluding source files at the project tree level
- Address issue #314 by including sources in published artifacts
- Fix issue #313 by exporting `ReporterType`
- Address issue #315 by logging format log statements at level `WARNING` so they can be hidden
- Credit @mateuszkwiecinski as one of the developers of the plugin
  • Loading branch information
jeremymailen authored Mar 14, 2023
1 parent 6ab22c9 commit 5c890e8
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 245 deletions.
66 changes: 26 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm

```kotlin
plugins {
id("org.jmailen.kotlinter") version "3.13.0"
id("org.jmailen.kotlinter") version "3.14.0"
}
```

Expand All @@ -31,7 +31,7 @@ plugins {

```groovy
plugins {
id "org.jmailen.kotlinter" version "3.13.0"
id "org.jmailen.kotlinter" version "3.14.0"
}
```

Expand All @@ -45,7 +45,7 @@ Root `build.gradle.kts`

```kotlin
plugins {
id("org.jmailen.kotlinter") version "3.13.0" apply false
id("org.jmailen.kotlinter") version "3.14.0" apply false
}
```

Expand All @@ -65,7 +65,7 @@ Root `build.gradle`

```groovy
plugins {
id 'org.jmailen.kotlinter' version "3.13.0" apply false
id 'org.jmailen.kotlinter' version "3.14.0" apply false
}
```

Expand Down Expand Up @@ -155,8 +155,6 @@ Options are configured in the `kotlinter` extension. Defaults shown (you may omi
kotlinter {
ignoreFailures = false
reporters = arrayOf("checkstyle", "plain")
experimentalRules = false
disabledRules = emptyArray()
}
```

Expand All @@ -169,8 +167,6 @@ kotlinter {
kotlinter {
ignoreFailures = false
reporters = ['checkstyle', 'plain']
experimentalRules = false
disabledRules = []
}
```

Expand All @@ -180,21 +176,13 @@ Options for `reporters`: `checkstyle`, `html`, `json`, `plain`, `sarif`

Reporters behave as described at: https://github.com/pinterest/ktlint

The `experimentalRules` property enables rules which are part of ktlint's experimental rule set.

The `disabledRules` property can includes an array of rule ids you wish to disable. For example to allow wildcard imports:
```groovy
disabledRules = ["no-wildcard-imports"]
```
You must prefix rule ids not part of the standard rule set with `<rule-set-id>:<rule-id>`. For example `experimental:annotation`.

### Editorconfig

Kotlinter will configure itself using an `.editorconfig` file if one is present.

If a non-empty `disabledRules` value is specified in the `kotlinter` extension, it will take precedence over any `disabled_rules` in `.editorconfig`.
This configuration includes code style and linting rules.

See [Ktlint editorconfig](https://github.com/pinterest/ktlint#editorconfig) for supported values.
See [KtLint configuration](https://pinterest.github.io/ktlint/rules/configuration-ktlint/) for details.

### Customizing Tasks

Expand All @@ -205,8 +193,8 @@ so you can customize includes, excludes, and source.
<summary>Kotlin</summary>

```kotlin
tasks.lintKotlinMain {
exclude("com/example/**/generated/*.kt")
tasks.named("lintKotlinMain") {
source = source - fileTree("$buildDir/generated")
}
```

Expand All @@ -216,8 +204,8 @@ tasks.lintKotlinMain {
<summary>Groovy</summary>

```groovy
tasks.named('lintKotlinMain') {
exclude 'com/example/**/generated/*.kt'
tasks.named("lintKotlinMain") {
source = source - fileTree("$buildDir/generated")
}
```

Expand Down Expand Up @@ -270,15 +258,13 @@ tasks.register('ktLint', LintTask) {
'plain': file('build/lint-report.txt'),
'json' : file('build/lint-report.json')
]
disabledRules = ['import-ordering']
}
tasks.register('ktFormat', FormatTask) {
group 'formatting'
source files('src/test')
report = file('build/format-report.txt')
disabledRules = ['import-ordering']
}
```

Expand All @@ -296,14 +282,14 @@ buildscript {
configurations.classpath {
resolutionStrategy {
force(
"com.pinterest.ktlint:ktlint-core:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-json:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-html:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-plain:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-sarif:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.39.0"
"com.pinterest.ktlint:ktlint-core:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-json:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-html:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-plain:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-sarif:0.48.0",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0"
)
}
}
Expand All @@ -320,14 +306,14 @@ buildscript {
configurations.classpath {
resolutionStrategy {
force(
"com.pinterest.ktlint:ktlint-core:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-json:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-html:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-plain:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-sarif:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.39.0"
"com.pinterest.ktlint:ktlint-core:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-json:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-html:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-plain:0.48.0",
"com.pinterest.ktlint:ktlint-reporter-sarif:0.48.0",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0"
)
}
}
Expand Down
16 changes: 12 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.8.0"
kotlin("jvm") version "1.8.10"
id("com.gradle.plugin-publish") version "1.1.0"
`java-gradle-plugin`
`maven-publish`
Expand All @@ -20,7 +20,7 @@ val githubUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val webUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val projectDescription = "Lint and formatting for Kotlin using ktlint with configuration-free setup on JVM and Android projects"

version = "3.13.0"
version = "3.14.0"
group = "org.jmailen.gradle"
description = projectDescription

Expand Down Expand Up @@ -98,8 +98,8 @@ tasks {
}
withType<KotlinCompile>().configureEach {
kotlinOptions {
apiVersion = "1.4"
languageVersion = "1.4"
apiVersion = "1.8"
languageVersion = "1.8"
jvmTarget = targetJavaVersion.toString()
}
}
Expand Down Expand Up @@ -131,6 +131,10 @@ gradlePlugin {
}
}

java {
withSourcesJar()
}

publishing {
publications.withType<MavenPublication> {

Expand All @@ -156,6 +160,10 @@ publishing {
id.set("jeremymailen")
name.set("Jeremy Mailen")
}
developer {
id.set("mateuszkwiecinski")
name.set("Mateusz Kwieciński")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ import org.jmailen.gradle.kotlinter.support.ReporterType
open class KotlinterExtension {
companion object {
const val DEFAULT_IGNORE_FAILURES = false
const val DEFAULT_EXPERIMENTAL_RULES = false
val DEFAULT_REPORTER = ReporterType.checkstyle.name
val DEFAULT_DISABLED_RULES = emptyArray<String>()
}

var ignoreFailures = DEFAULT_IGNORE_FAILURES

var reporters = arrayOf(DEFAULT_REPORTER)

var experimentalRules = DEFAULT_EXPERIMENTAL_RULES

var disabledRules = DEFAULT_DISABLED_RULES
}
14 changes: 8 additions & 6 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class KotlinterPlugin : Plugin<Project> {
val formatKotlin = registerParentFormatTask()

sourceResolver.applyToAll(this) { id, resolvedSources ->
val lintTaskPerSourceSet = tasks.register("lintKotlin${id.capitalize()}", LintTask::class.java) { lintTask ->
val lintTaskPerSourceSet = tasks.register(
"lintKotlin${id.replaceFirstChar(Char::titlecase)}",
LintTask::class.java,
) { lintTask ->
lintTask.source(resolvedSources)
lintTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures })
lintTask.reports.set(
Expand All @@ -46,18 +49,17 @@ class KotlinterPlugin : Plugin<Project> {
}
},
)
lintTask.experimentalRules.set(provider { kotlinterExtension.experimentalRules })
lintTask.disabledRules.set(provider { kotlinterExtension.disabledRules.toList() })
}
lintKotlin.configure { lintTask ->
lintTask.dependsOn(lintTaskPerSourceSet)
}

val formatKotlinPerSourceSet = tasks.register("formatKotlin${id.capitalize()}", FormatTask::class.java) { formatTask ->
val formatKotlinPerSourceSet = tasks.register(
"formatKotlin${id.replaceFirstChar(Char::titlecase)}",
FormatTask::class.java,
) { formatTask ->
formatTask.source(resolvedSources)
formatTask.report.set(reportFile("$id-format.txt"))
formatTask.experimentalRules.set(provider { kotlinterExtension.experimentalRules })
formatTask.disabledRules.set(provider { kotlinterExtension.disabledRules.toList() })
}
formatKotlin.configure { formatTask ->
formatTask.dependsOn(formatKotlinPerSourceSet)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,8 @@
package org.jmailen.gradle.kotlinter.support

import com.pinterest.ktlint.core.api.EditorConfigDefaults
import com.pinterest.ktlint.core.api.EditorConfigOverride
import com.pinterest.ktlint.core.api.editorconfig.EditorConfigProperty
import org.ec4j.core.model.EditorConfig
import org.ec4j.core.model.Glob
import org.ec4j.core.model.Property
import org.ec4j.core.model.PropertyType
import org.ec4j.core.model.PropertyType.PropertyValueParser.IDENTITY_VALUE_PARSER
import org.ec4j.core.model.Section
import org.gradle.api.file.ProjectLayout
import java.io.File

internal fun editorConfigOverride(ktLintParams: KtLintParams) =
getPropertiesForDisabledRules(ktLintParams)
.let(::buildEditorConfigOverride)

internal fun editorConfigDefaults(ktLintParams: KtLintParams): EditorConfigDefaults =
getPropertiesForExperimentalRules(ktLintParams)
.let(::buildEditorConfigDefaults)

private fun getPropertiesForDisabledRules(
ktLintParams: KtLintParams,
): List<Pair<EditorConfigProperty<String>, String>> {
val rules = ktLintParams.disabledRules
return if (rules.isEmpty()) {
emptyList()
} else {
rules
.asSequence()
.map(::getKtlintRulePropertyName)
.map { propertyName ->
EditorConfigProperty(
type = PropertyType(propertyName, "Rule to be disabled", IDENTITY_VALUE_PARSER),
defaultValue = "disabled",
)
}
.map { it to "disabled" }
.toList()
}
}

private fun getPropertiesForExperimentalRules(ktLintParams: KtLintParams) =
Property
.builder()
.name("ktlint_experimental")
.value(if (ktLintParams.experimentalRules) "enabled" else "disabled")
.let(::listOf)

private fun buildEditorConfigOverride(editorConfigProperties: List<Pair<EditorConfigProperty<String>, String>>) =
if (editorConfigProperties.isEmpty()) {
EditorConfigOverride.EMPTY_EDITOR_CONFIG_OVERRIDE
} else {
EditorConfigOverride.from(*editorConfigProperties.toTypedArray())
}

private fun buildEditorConfigDefaults(kotlinSectionProperties: List<Property.Builder>) =
if (kotlinSectionProperties.isEmpty()) {
EditorConfigDefaults.EMPTY_EDITOR_CONFIG_DEFAULTS
} else {
EditorConfigDefaults(
EditorConfig
.builder()
.section(
Section
.builder()
.glob(Glob("*.{kt,kts}"))
.properties(kotlinSectionProperties),
)
.build(),
)
}

private fun getKtlintRulePropertyName(ruleName: String) =
if (ruleName.contains(':')) { // Rule from a non-standard rule set
"ktlint_${ruleName.replace(':', '_')}"
} else {
"ktlint_standard_$ruleName"
}

internal fun ProjectLayout.findApplicableEditorConfigFiles(): Sequence<File> {
val projectEditorConfig = projectDirectory.file(".editorconfig").asFile

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.logging.Logger
import java.io.File

internal fun createKtlintEngine(ktLintParams: KtLintParams) = KtLintRuleEngine(
// You must create a new KtLintRuleEngine per file with fresh rule providers.
// Otherwise, KtLint errors on resolving rule enable/disable statements in .editorconfig
internal fun createKtlintEngine() = KtLintRuleEngine(
ruleProviders = resolveRuleProviders(defaultRuleSetProviders),
editorConfigOverride = editorConfigOverride(ktLintParams),
editorConfigDefaults = editorConfigDefaults(ktLintParams),
)

internal fun KtLintRuleEngine.resetEditorconfigCacheIfNeeded(
internal fun resetEditorconfigCacheIfNeeded(
changedEditorconfigFiles: ConfigurableFileCollection,
logger: Logger,
) {
val engine = createKtlintEngine()
val changedFiles = changedEditorconfigFiles.files
if (changedFiles.any()) {
logger.info("Editorconfig changed, resetting KtLint caches")
changedFiles.map(File::toPath).forEach(::reloadEditorConfigFile)
changedFiles.map(File::toPath).forEach {
engine.reloadEditorConfigFile(it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.io.File
import java.io.PrintStream

/* ktlint-disable enum-entry-name-case */
internal enum class ReporterType(val fileExtension: String) {
enum class ReporterType(val fileExtension: String) {
checkstyle("xml"),
html("html"),
json("json"),
Expand Down
Loading

0 comments on commit 5c890e8

Please sign in to comment.