Skip to content

Commit

Permalink
Upgrade to ktlint 0.34.0 and introduce 2.0 with additional configurat…
Browse files Browse the repository at this point in the history
…ion (#108)

* Upgrade to ktlint 0.34.0 and introduce 2.0 with additional configuration

Breaks compatibility, but introduces powerful new rules available in 0.34.0 and granular ways to configure them.

- ktlint 0.34.0
- support editorconfig
- configuration for excluding rulesets
- configuration for excluding rules

* refactor to allow changing configuration options easy

* Add support for editorconfig

* Support for disabling rules

* Update readme
  • Loading branch information
jeremymailen authored Jul 22, 2019
1 parent d3062ca commit d2656c8
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 204 deletions.
132 changes: 71 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm

```kotlin
plugins {
id("org.jmailen.kotlinter") version "1.26.0"
id("org.jmailen.kotlinter") version "2.0.0"
}
```

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

```groovy
plugins {
id "org.jmailen.kotlinter" version "1.26.0"
id "org.jmailen.kotlinter" version "2.0.0"
}
```

Expand All @@ -46,7 +46,7 @@ buildscript {
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:1.26.0")
classpath("org.jmailen.gradle:kotlinter-gradle:2.0.0")
}
}
```
Expand All @@ -71,7 +71,7 @@ buildscript {
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:1.26.0"
classpath "org.jmailen.gradle:kotlinter-gradle:2.0.0"
}
}
```
Expand Down Expand Up @@ -120,58 +120,6 @@ Also `check` becomes dependent on `lintKotlin`.

Granular tasks exist for each source set in the project: `formatKotlin`*`SourceSet`* and `lintKotlin`*`SourceSet`*.

### Custom Tasks

If you haven't applied these plugins you can create custom tasks:

<details open>
<summary>Kotlin</summary>

```kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask

val ktLint by tasks.creating(LintTask::class) {
group = "verification"
source(files("src"))
reports = mapOf(
"plain" to file("build/lint-report.txt"),
"json" to file("build/lint-report.json")
)
}

val ktFormat by tasks.creating(FormatTask::class) {
group = "formatting"
source(files("src"))
report = file("build/format-report.txt")
}
```

</details>

<details>
<summary>Groovy</summary>

```groovy
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
task ktLint(type: LintTask, group: 'verification') {
source files('src')
reports = [
'plain': file('build/lint-report.txt'),
'json': file('build/lint-report.json')
]
}
task ktFormat(type: FormatTask, group: 'formatting') {
source files('src')
report = file('build/format-report.txt')
}
```

</details>

### Configuration
Options are configured in the `kotlinter` extension. Defaults shown (you may omit the configuration block entirely if you want these values).

Expand All @@ -185,7 +133,7 @@ kotlinter {
continuationIndentSize = 4
reporters = arrayOf("checkstyle", "plain")
experimentalRules = false
allowWildcardImports = true
disabledRules = emptyArray<String>()
fileBatchSize = 30
}
```
Expand All @@ -202,7 +150,7 @@ kotlinter {
continuationIndentSize = 4
reporters = ['checkstyle', 'plain']
experimentalRules = false
allowWildcardImports = true
disabledRules = []
fileBatchSize = 30
}
```
Expand All @@ -215,14 +163,24 @@ The html reporter is provided by [ktlint-html-reporter](https://github.com/mcass

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

*Note: `reporter` with a single value is deprecated but supported for backwards compatibility.

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

The `allowWildcardImports` property can be set to `false` if you wish to disallow use of wildcard imports.
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`.

The `fileBatchSize` property configures the number of files that are processed in one Gradle Worker API call.

### Editorconfig

Kotlinter will configure itself using an `.editorconfig` file if one is present in your root project directory.

For configuration values supported in both the `kotlinter` extension and `.editorconfig`, the `.editorconfig` values will take precedence.

See [Ktlint editorconfig](https://github.com/pinterest/ktlint#editorconfig) for supported values.

### Customizing Tasks

The `formatKotlin`*`SourceSet`* and `lintKotlin`*`SourceSet`* tasks inherit from [SourceTask](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceTask.html)
Expand Down Expand Up @@ -254,6 +212,58 @@ lintKotlinMain {

</details>

### Custom Tasks

If you haven't applied these plugins you can create custom tasks:

<details open>
<summary>Kotlin</summary>

```kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask

val ktLint by tasks.creating(LintTask::class) {
group = "verification"
source(files("src"))
reports = mapOf(
"plain" to file("build/lint-report.txt"),
"json" to file("build/lint-report.json")
)
}

val ktFormat by tasks.creating(FormatTask::class) {
group = "formatting"
source(files("src"))
report = file("build/format-report.txt")
}
```

</details>

<details>
<summary>Groovy</summary>

```groovy
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
task ktLint(type: LintTask, group: 'verification') {
source files('src')
reports = [
'plain': file('build/lint-report.txt'),
'json': file('build/lint-report.json')
]
}
task ktFormat(type: FormatTask, group: 'formatting') {
source files('src')
report = file('build/format-report.txt')
}
```

</details>

### Custom ktlint version

If you need to use a different version of `ktlint` you can override the dependency.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
}

dependencies {
implementation 'com.pinterest:ktlint:0.33.0'
implementation 'com.pinterest:ktlint:0.34.0'
implementation 'me.cassiano:ktlint-html-reporter:0.2.1'

compileOnly 'org.jetbrains.kotlin:kotlin-gradle-plugin'
Expand All @@ -30,7 +30,7 @@ tasks.withType(PluginUnderTestMetadata).configureEach {
pluginClasspath.from(configurations.compileOnly)
}

version = '1.26.0'
version = '2.0.0'
group = 'org.jmailen.gradle'
def pluginId = 'org.jmailen.kotlinter'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open class KotlinterExtension {
const val DEFAULT_CONTINUATION_INDENT_SIZE = 4
val DEFAULT_REPORTER = ReporterType.checkstyle.name
const val DEFAULT_EXPERIMENTAL_RULES = false
const val DEFAULT_ALLOW_WILDCARD_IMPORTS = true
val DEFAULT_DISABLED_RULES = emptyArray<String>()
const val DEFAULT_FILE_BATCH_SIZE = 30
}

Expand All @@ -20,17 +20,12 @@ open class KotlinterExtension {

var continuationIndentSize = DEFAULT_CONTINUATION_INDENT_SIZE

var reporter: String? = null

var reporters = arrayOf(DEFAULT_REPORTER)

var experimentalRules = DEFAULT_EXPERIMENTAL_RULES

var allowWildcardImports = DEFAULT_ALLOW_WILDCARD_IMPORTS
var disabledRules = DEFAULT_DISABLED_RULES

/** The file list is split into batches and processed together on a Worker API call */
var fileBatchSize = DEFAULT_FILE_BATCH_SIZE

// for backwards compatibility
fun reporters() = reporter?.let { arrayOf(it) } ?: reporters
}
25 changes: 16 additions & 9 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jmailen.gradle.kotlinter.support.KtLintParams
import org.jmailen.gradle.kotlinter.support.reporterFileExtension
import org.jmailen.gradle.kotlinter.tasks.FormatTask
import org.jmailen.gradle.kotlinter.tasks.LintTask
Expand Down Expand Up @@ -39,22 +40,24 @@ class KotlinterPlugin : Plugin<Project> {
}

project.afterEvaluate {
val ktLintParams = KtLintParams(
kotlinterExtension.indentSize,
kotlinterExtension.continuationIndentSize,
kotlinterExtension.experimentalRules,
kotlinterExtension.disabledRules,
project.editorConfigPath()
)

taskCreator.lintTasks.forEach { lintTask ->
lintTask.ignoreFailures = kotlinterExtension.ignoreFailures
lintTask.indentSize = kotlinterExtension.indentSize
lintTask.continuationIndentSize = kotlinterExtension.continuationIndentSize
lintTask.reports = kotlinterExtension.reporters().associate { reporter ->
lintTask.reports = kotlinterExtension.reporters.associate { reporter ->
reporter to project.reportFile("${lintTask.sourceSetId}-lint.${reporterFileExtension(reporter)}")
}
lintTask.experimentalRules = kotlinterExtension.experimentalRules
lintTask.allowWildcardImports = kotlinterExtension.allowWildcardImports
lintTask.ktLintParams = ktLintParams
lintTask.fileBatchSize = kotlinterExtension.fileBatchSize
}
taskCreator.formatTasks.forEach { formatTask ->
formatTask.indentSize = kotlinterExtension.indentSize
formatTask.continuationIndentSize = kotlinterExtension.continuationIndentSize
formatTask.experimentalRules = kotlinterExtension.experimentalRules
formatTask.allowWildcardImports = kotlinterExtension.allowWildcardImports
formatTask.ktLintParams = ktLintParams
formatTask.fileBatchSize = kotlinterExtension.fileBatchSize
}
}
Expand Down Expand Up @@ -156,3 +159,7 @@ val String.id
get() = split(" ").first()

fun Project.reportFile(name: String) = file("${project.buildDir}/reports/ktlint/$name")

fun Project.editorConfigPath() = with(rootProject.file(".editorconfig")) {
if (this.exists()) this.path else null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jmailen.gradle.kotlinter.support

import org.jmailen.gradle.kotlinter.KotlinterExtension
import java.io.Serializable

data class KtLintParams(
var indentSize: Int = KotlinterExtension.DEFAULT_INDENT_SIZE,
var continuationIndentSize: Int = KotlinterExtension.DEFAULT_CONTINUATION_INDENT_SIZE,
var experimentalRules: Boolean = KotlinterExtension.DEFAULT_EXPERIMENTAL_RULES,
var disabledRules: Array<String> = KotlinterExtension.DEFAULT_DISABLED_RULES,
var editorConfigPath: String? = null
) : Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jmailen.gradle.kotlinter.support

fun userData(ktLintParams: KtLintParams) = mapOf(
"indent_size" to ktLintParams.indentSize.toString(),
"continuation_indent_size" to ktLintParams.continuationIndentSize.toString(),
"disabled_rules" to ktLintParams.disabledRules.joinToString(",")
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package org.jmailen.gradle.kotlinter.support
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider
import org.jmailen.rulesets.NoWildcardImportsRuleSetProvider
import java.util.ServiceLoader
import kotlin.comparisons.compareBy

fun resolveRuleSets(
providers: Iterable<RuleSetProvider>,
includeExperimentalRules: Boolean = false,
allowWildcardImports: Boolean = true
includeExperimentalRules: Boolean = false
): List<RuleSet> {
return providers
.filter { includeExperimentalRules || it !is ExperimentalRuleSetProvider }
.filter { !allowWildcardImports || it !is NoWildcardImportsRuleSetProvider }
.map { it.get() }
.sortedWith(compareBy {
when (it.id) {
Expand All @@ -26,5 +23,4 @@ fun resolveRuleSets(

// statically resolve providers from plugin classpath
val defaultRuleSetProviders =
ServiceLoader.load(RuleSetProvider::class.java).map { it } +
NoWildcardImportsRuleSetProvider()
ServiceLoader.load(RuleSetProvider::class.java).map { it }

This file was deleted.

19 changes: 4 additions & 15 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkerExecutor
import org.jmailen.gradle.kotlinter.KotlinterExtension
import org.jmailen.gradle.kotlinter.support.ExecutionContextRepository
import org.jmailen.gradle.kotlinter.support.KtLintParams
import org.jmailen.gradle.kotlinter.support.defaultRuleSetProviders
import org.jmailen.gradle.kotlinter.tasks.format.FormatExecutionContext
import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerConfigurationAction
Expand All @@ -23,19 +24,10 @@ open class FormatTask @Inject constructor(
lateinit var report: File

@Input
var indentSize = KotlinterExtension.DEFAULT_INDENT_SIZE

@Input
var continuationIndentSize = KotlinterExtension.DEFAULT_CONTINUATION_INDENT_SIZE

@Input
var experimentalRules = KotlinterExtension.DEFAULT_EXPERIMENTAL_RULES

@Input
var allowWildcardImports = KotlinterExtension.DEFAULT_ALLOW_WILDCARD_IMPORTS
var fileBatchSize = KotlinterExtension.DEFAULT_FILE_BATCH_SIZE

@Input
var fileBatchSize = KotlinterExtension.DEFAULT_FILE_BATCH_SIZE
var ktLintParams = KtLintParams()

init {
outputs.upToDateWhen { false }
Expand All @@ -55,10 +47,7 @@ open class FormatTask @Inject constructor(
files = files,
projectDirectory = project.projectDir,
executionContextRepositoryId = executionContextRepositoryId,
experimentalRules = experimentalRules,
allowWildcardImports = allowWildcardImports,
indentSize = indentSize,
continuationIndentSize = continuationIndentSize
ktLintParams = ktLintParams
)
}
.forEach { parameters ->
Expand Down
Loading

0 comments on commit d2656c8

Please sign in to comment.