Skip to content

Commit

Permalink
Major update with kotlin 1.4.0 and ktlint 0.38.1 (#154)
Browse files Browse the repository at this point in the history
* Run kotlinter lint and format in classloader isolation

Use the worker classpath to prevent clashes with the configured kotlin compiler version of a project.
This hopefully allows a smoother upgrade path to kotlin 1.4 and fewer compiler version clashes in the future.

One limition is we're doing one worker per sourceSet whereas before that work could be split between workers.
Returning data from workers to tasks is problematic, so we couldn't easily stitch together into a final report. Instead report writing is delegated to the worker.

Since it's a major change, anticipating making the major upgrade to kotlinter and moving all deprecated code.
Also anticipating an upgrade to ktlint 0.38.0 once finalized.

* use a single WorkQueue which seems to improve performance modestly

* Use noIsolation worker, update readme

* temporarily disable linting

* fix readme desc
  • Loading branch information
jeremymailen authored Aug 25, 2020
1 parent f9356fc commit c1789b1
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 452 deletions.
46 changes: 32 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm

```kotlin
plugins {
id("org.jmailen.kotlinter") version "2.4.1"
id("org.jmailen.kotlinter") version "3.0.0"
}
```

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

```groovy
plugins {
id "org.jmailen.kotlinter" version "2.4.1"
id "org.jmailen.kotlinter" version "3.0.0"
}
```

Expand All @@ -50,7 +50,7 @@ buildscript {
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:2.4.1")
classpath("org.jmailen.gradle:kotlinter-gradle:3.0.0")
}
}
```
Expand All @@ -75,7 +75,7 @@ buildscript {
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:2.4.1"
classpath "org.jmailen.gradle:kotlinter-gradle:3.0.0"
}
}
```
Expand All @@ -90,7 +90,8 @@ apply plugin: "org.jmailen.kotlinter"

### Compatibility

Kotlinter is compatible with Kotlin Gradle plugins 1.3.30+ and Java 13/12/11/10/9/8.
Kotlinter >= 3.0.0 is compatible with Kotlin Gradle plugins 1.4.0+ and Java 13/12/11/10/9/8.
Kotlinter <= 2.4.1 is compatible with Kotlin Gradle plugins 1.3.30+ and Java 13/12/11/10/9/8.

### Features

Expand Down Expand Up @@ -133,7 +134,6 @@ kotlinter {
reporters = arrayOf("checkstyle", "plain")
experimentalRules = false
disabledRules = emptyArray<String>()
fileBatchSize = 30
}
```

Expand All @@ -149,7 +149,6 @@ kotlinter {
reporters = ['checkstyle', 'plain']
experimentalRules = false
disabledRules = []
fileBatchSize = 30
}
```

Expand All @@ -167,8 +166,6 @@ 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.
Expand Down Expand Up @@ -219,7 +216,7 @@ If you aren't using autoconfiguration from a supported plugin or otherwise need
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask

val ktLint by tasks.creating(LintTask::class) {
tasks.create<LintTask>("ktLint") {
group = "verification"
source(files("src"))
reports = mapOf(
Expand All @@ -228,7 +225,7 @@ val ktLint by tasks.creating(LintTask::class) {
)
}

val ktFormat by tasks.creating(FormatTask::class) {
tasks.create<FormatTask>("ktFormat") {
group = "formatting"
source(files("src"))
report = file("build/format-report.txt")
Expand Down Expand Up @@ -271,8 +268,19 @@ If you need to use a different version of `ktlint` you can override the dependen

```kotlin
buildscript {
configurations.classpath
.resolutionStrategy.force("com.github.pinterest:ktlint:0.36.0")
configurations.classpath {
resolutionStrategy {
force(
"com.pinterest.ktlint:ktlint-core:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-json:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-html:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-plain:0.37.2",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.37.2",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.37.2"
)
}
}
}
```

Expand All @@ -284,7 +292,17 @@ buildscript {
```groovy
buildscript {
configurations.classpath {
resolutionStrategy { force 'com.github.pinterest:ktlint:0.36.0' }
resolutionStrategy {
force(
"com.pinterest.ktlint:ktlint-core:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-json:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-html:0.37.2",
"com.pinterest.ktlint:ktlint-reporter-plain:0.37.2",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.37.2",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.37.2"
)
}
}
}
```
Expand Down
49 changes: 32 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version Versions.kotlin
id("com.gradle.plugin-publish") version Versions.pluginPublish
kotlin("jvm") version "1.4.0"
id("com.gradle.plugin-publish") version "0.12.0"
`java-gradle-plugin`
`maven-publish`
id("org.jmailen.kotlinter") version Versions.kotlinter
`idea`
// id("org.jmailen.kotlinter") version "2.4.1"
idea
}

repositories {
Expand All @@ -19,22 +19,34 @@ 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 = "2.4.1"
version = "3.0.0"
group = "org.jmailen.gradle"
description = projectDescription

dependencies {
implementation("com.pinterest.ktlint:ktlint-core:${Versions.ktlint}")
implementation("com.pinterest.ktlint:ktlint-reporter-checkstyle:${Versions.ktlint}")
implementation("com.pinterest.ktlint:ktlint-reporter-json:${Versions.ktlint}")
implementation("com.pinterest.ktlint:ktlint-reporter-html:${Versions.ktlint}")
implementation("com.pinterest.ktlint:ktlint-reporter-plain:${Versions.ktlint}")
implementation("com.pinterest.ktlint:ktlint-ruleset-experimental:${Versions.ktlint}")
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:${Versions.ktlint}")
object Versions {
const val androidTools = "4.0.1"
const val jetbrainsAnnotations = "20.0.0"
const val junit = "4.13"
const val ktlint = "0.38.1"
const val mockitoKotlin = "2.2.0"
}

dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin")
compileOnly("com.android.tools.build:gradle:${Versions.androidTools}")

listOf(
"ktlint-core",
"ktlint-reporter-checkstyle",
"ktlint-reporter-json",
"ktlint-reporter-html",
"ktlint-reporter-plain",
"ktlint-ruleset-experimental",
"ktlint-ruleset-standard"
).forEach { module ->
implementation("com.pinterest.ktlint:$module:${Versions.ktlint}")
}

testImplementation("junit:junit:${Versions.junit}")
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:${Versions.mockitoKotlin}")
testImplementation("org.jetbrains:annotations:${Versions.jetbrainsAnnotations}")
Expand All @@ -49,15 +61,15 @@ val sourcesJar by tasks.registering(Jar::class) {
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
group = JavaBasePlugin.DOCUMENTATION_GROUP
description = "Assembles sources JAR"
classifier = "sources"
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}

val javadocJar by tasks.registering(Jar::class) {
dependsOn(JavaPlugin.JAVADOC_TASK_NAME)
group = JavaBasePlugin.DOCUMENTATION_GROUP
description = "Assembles javaDoc JAR"
classifier = "javadoc"
archiveClassifier.set("javadoc")
from(tasks["javadoc"])
}

Expand Down Expand Up @@ -116,13 +128,16 @@ publishing {
}
}
}

}
}

tasks {
withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
kotlinOptions {
apiVersion = "1.3"
languageVersion = "1.3"
jvmTarget = "1.8"
}
}

wrapper {
Expand Down
9 changes: 0 additions & 9 deletions buildSrc/build.gradle.kts

This file was deleted.

10 changes: 0 additions & 10 deletions buildSrc/src/main/kotlin/Versions.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@ import org.jmailen.gradle.kotlinter.support.ReporterType
open class KotlinterExtension {
companion object {
const val DEFAULT_IGNORE_FAILURES = false
val DEFAULT_REPORTER = ReporterType.checkstyle.name
const val DEFAULT_EXPERIMENTAL_RULES = false
val DEFAULT_REPORTER = ReporterType.checkstyle.name
val DEFAULT_DISABLED_RULES = emptyArray<String>()
const val DEFAULT_FILE_BATCH_SIZE = 30
}

/** Don't fail build on lint issues */
var ignoreFailures = DEFAULT_IGNORE_FAILURES

var indentSize: Int? = null

@Deprecated("Scheduled to be removed in 3.0.0")
var continuationIndentSize: Int? = null

var reporters = arrayOf(DEFAULT_REPORTER)

var experimentalRules = DEFAULT_EXPERIMENTAL_RULES

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
}
25 changes: 14 additions & 11 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.file.FileTree
import org.gradle.api.file.SourceDirectorySet
Expand All @@ -12,14 +13,22 @@ import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.workers.WorkQueue
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jmailen.gradle.kotlinter.support.reporterFileExtension
import org.jmailen.gradle.kotlinter.tasks.FormatTask
import org.jmailen.gradle.kotlinter.tasks.InstallPreCommitHookTask
import org.jmailen.gradle.kotlinter.tasks.InstallPrePushHookTask
import org.jmailen.gradle.kotlinter.tasks.LintTask
import javax.inject.Inject

class KotlinterPlugin : Plugin<Project> {
class KotlinterPlugin @Inject constructor(
private val workerExecutor: WorkerExecutor
) : Plugin<Project> {
companion object {
lateinit var workQueue: WorkQueue
}

val extendablePlugins = mapOf(
"org.jetbrains.kotlin.jvm" to KotlinJvmSourceSetResolver,
Expand All @@ -28,11 +37,7 @@ class KotlinterPlugin : Plugin<Project> {

override fun apply(project: Project) = with(project) {
val kotlinterExtension = extensions.create("kotlinter", KotlinterExtension::class.java)
afterEvaluate {
if (kotlinterExtension.continuationIndentSize != null) {
logger.warn("`continuationIndentSize` does not have any effect and will be removed in 3.0.0")
}
}
workQueue = workerExecutor.noIsolation()

if (project.rootProject == project) {
registerPrePushHookTask()
Expand All @@ -45,9 +50,9 @@ class KotlinterPlugin : Plugin<Project> {
val lintKotlin = registerParentLintTask()
val formatKotlin = registerParentFormatTask()

sourceResolver.applyToAll(project) { id, resolveSources ->
sourceResolver.applyToAll(project) { id, resolvedSources ->
val lintTaskPerSourceSet = tasks.register("lintKotlin${id.capitalize()}", LintTask::class.java) { lintTask ->
lintTask.source(resolveSources)
lintTask.source(resolvedSources)
lintTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures })
lintTask.reports.set(
provider {
Expand All @@ -59,19 +64,17 @@ class KotlinterPlugin : Plugin<Project> {
lintTask.indentSize.set(provider { kotlinterExtension.indentSize })
lintTask.experimentalRules.set(provider { kotlinterExtension.experimentalRules })
lintTask.disabledRules.set(provider { kotlinterExtension.disabledRules.toList() })
lintTask.fileBatchSize.set(provider { kotlinterExtension.fileBatchSize })
}
lintKotlin.configure { lintTask ->
lintTask.dependsOn(lintTaskPerSourceSet)
}

val formatKotlinPerSourceSet = tasks.register("formatKotlin${id.capitalize()}", FormatTask::class.java) { formatTask ->
formatTask.source(resolveSources)
formatTask.source(resolvedSources)
formatTask.report.set(reportFile("$id-format.txt"))
formatTask.indentSize.set(provider { kotlinterExtension.indentSize })
formatTask.experimentalRules.set(provider { kotlinterExtension.experimentalRules })
formatTask.disabledRules.set(provider { kotlinterExtension.disabledRules.toList() })
formatTask.fileBatchSize.set(provider { kotlinterExtension.fileBatchSize })
}
formatKotlin.configure { formatTask ->
formatTask.dependsOn(formatKotlinPerSourceSet)
Expand Down

This file was deleted.

Loading

0 comments on commit c1789b1

Please sign in to comment.