Skip to content

Commit

Permalink
Upgrade to ktlint 0.34.2 and allow full configuration on custom tasks (
Browse files Browse the repository at this point in the history
…#111)

Also print rule id failing on console
  • Loading branch information
jeremymailen authored Jul 23, 2019
1 parent d2656c8 commit ccfce9d
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 38 deletions.
12 changes: 8 additions & 4 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 "2.0.0"
id("org.jmailen.kotlinter") version "2.1.0"
}
```

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

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

Expand All @@ -46,7 +46,7 @@ buildscript {
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:2.0.0")
classpath("org.jmailen.gradle:kotlinter-gradle:2.1.0")
}
}
```
Expand All @@ -71,7 +71,7 @@ buildscript {
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:2.0.0"
classpath "org.jmailen.gradle:kotlinter-gradle:2.1.0"
}
}
```
Expand Down Expand Up @@ -254,11 +254,15 @@ task ktLint(type: LintTask, group: 'verification') {
'plain': file('build/lint-report.txt'),
'json': file('build/lint-report.json')
]
continuationIndentSize = 8
disabledRules = ["import-ordering"]
}
task ktFormat(type: FormatTask, group: 'formatting') {
source files('src')
report = file('build/format-report.txt')
continuationIndentSize = 8
disabledRules = ["import-ordering"]
}
```

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'com.gradle.plugin-publish' version '0.10.0'
id 'java-gradle-plugin'
id 'maven-publish'
id 'org.jmailen.kotlinter' version '1.26.0'
id 'org.jmailen.kotlinter' version '2.0.0'
id 'idea'
}

Expand All @@ -14,7 +14,7 @@ repositories {
}

dependencies {
implementation 'com.pinterest:ktlint:0.34.0'
implementation 'com.pinterest:ktlint:0.34.2'
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 = '2.0.0'
version = '2.1.0'
group = 'org.jmailen.gradle'
def pluginId = 'org.jmailen.kotlinter'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jmailen.gradle.kotlinter

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import java.io.File
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.FileTree
Expand All @@ -15,7 +16,6 @@ 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
import java.io.File

class KotlinterPlugin : Plugin<Project> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jmailen.gradle.kotlinter.support

import org.jmailen.gradle.kotlinter.tasks.format.FormatExecutionContext
import org.jmailen.gradle.kotlinter.tasks.lint.LintExecutionContext
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import org.jmailen.gradle.kotlinter.tasks.format.FormatExecutionContext
import org.jmailen.gradle.kotlinter.tasks.lint.LintExecutionContext

/**
* Global repository storing ExecutionContext that needs to be available in Worker Runnable.
Expand Down Expand Up @@ -36,4 +36,4 @@ class ExecutionContextRepository<ExecutionContextType : ExecutionContext> {
fun unregister(id: UUID) {
executionContextById.remove(id)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jmailen.gradle.kotlinter.support

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

data class KtLintParams(
var indentSize: Int = KotlinterExtension.DEFAULT_INDENT_SIZE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.reporter.checkstyle.CheckStyleReporter
import com.pinterest.ktlint.reporter.json.JsonReporter
import com.pinterest.ktlint.reporter.plain.PlainReporter
import me.cassiano.ktlint.reporter.html.HtmlReporter
import java.io.File
import java.io.PrintStream
import me.cassiano.ktlint.reporter.html.HtmlReporter

enum class ReporterType(val fileExtension: String) {
checkstyle("xml"),
Expand Down
24 changes: 22 additions & 2 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jmailen.gradle.kotlinter.tasks

import java.io.File
import javax.inject.Inject
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.SourceTask
Expand All @@ -13,8 +15,6 @@ import org.jmailen.gradle.kotlinter.tasks.format.FormatExecutionContext
import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerConfigurationAction
import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerParameters
import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerRunnable
import java.io.File
import javax.inject.Inject

open class FormatTask @Inject constructor(
private val workerExecutor: WorkerExecutor
Expand All @@ -29,6 +29,26 @@ open class FormatTask @Inject constructor(
@Input
var ktLintParams = KtLintParams()

fun setIndentSize(indentSize: Int) {
ktLintParams.indentSize = indentSize
}

fun setContinuationIndentSize(continuationIndentSize: Int) {
ktLintParams.continuationIndentSize = continuationIndentSize
}

fun setExperimentalRules(experimentalRules: Boolean) {
ktLintParams.experimentalRules = experimentalRules
}

fun setDisabledRules(disabledRules: Array<String>) {
ktLintParams.disabledRules = disabledRules
}

fun setEditorConfigPath(editorConfigPath: String) {
ktLintParams.editorConfigPath = editorConfigPath
}

init {
outputs.upToDateWhen { false }
}
Expand Down
30 changes: 27 additions & 3 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jmailen.gradle.kotlinter.tasks

import java.io.File
import javax.inject.Inject
import org.gradle.api.GradleException
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
Expand All @@ -12,13 +14,15 @@ import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkerExecutor
import org.jmailen.gradle.kotlinter.KotlinterExtension
import org.jmailen.gradle.kotlinter.support.*
import org.jmailen.gradle.kotlinter.support.ExecutionContextRepository
import org.jmailen.gradle.kotlinter.support.HasErrorReporter
import org.jmailen.gradle.kotlinter.support.KtLintParams
import org.jmailen.gradle.kotlinter.support.defaultRuleSetProviders
import org.jmailen.gradle.kotlinter.support.reporterFor
import org.jmailen.gradle.kotlinter.tasks.lint.LintExecutionContext
import org.jmailen.gradle.kotlinter.tasks.lint.LintWorkerConfigurationAction
import org.jmailen.gradle.kotlinter.tasks.lint.LintWorkerParameters
import org.jmailen.gradle.kotlinter.tasks.lint.LintWorkerRunnable
import java.io.File
import javax.inject.Inject

@CacheableTask
open class LintTask @Inject constructor(
Expand All @@ -42,6 +46,26 @@ open class LintTask @Inject constructor(
@Input
var ktLintParams = KtLintParams()

fun setIndentSize(indentSize: Int) {
ktLintParams.indentSize = indentSize
}

fun setContinuationIndentSize(continuationIndentSize: Int) {
ktLintParams.continuationIndentSize = continuationIndentSize
}

fun setExperimentalRules(experimentalRules: Boolean) {
ktLintParams.experimentalRules = experimentalRules
}

fun setDisabledRules(disabledRules: Array<String>) {
ktLintParams.disabledRules = disabledRules
}

fun setEditorConfigPath(editorConfigPath: String) {
ktLintParams.editorConfigPath = editorConfigPath
}

@Internal
var sourceSetId = ""

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jmailen.gradle.kotlinter.tasks.format

import com.pinterest.ktlint.core.RuleSetProvider
import org.gradle.api.logging.Logger
import org.jmailen.gradle.kotlinter.support.ExecutionContext
import java.util.Queue
import java.util.concurrent.ConcurrentLinkedQueue
import org.gradle.api.logging.Logger
import org.jmailen.gradle.kotlinter.support.ExecutionContext

/**
* Execution context for the formatting task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class FormatWorkerConfigurationAction(
workerConfiguration.isolationMode = IsolationMode.NONE
workerConfiguration.setParams(formatWorkerParameters)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.jmailen.gradle.kotlinter.tasks.format

import org.jmailen.gradle.kotlinter.support.KtLintParams
import java.io.File
import java.io.Serializable
import java.util.UUID
import org.jmailen.gradle.kotlinter.support.KtLintParams

/**
* Serializable stateless parameters that are needed by the FormatWorkerRunnable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.jmailen.gradle.kotlinter.tasks.format

import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.RuleSet
import java.io.File
import javax.inject.Inject
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.Logger
import org.jmailen.gradle.kotlinter.support.ExecutionContextRepository
import org.jmailen.gradle.kotlinter.support.KtLintParams
import org.jmailen.gradle.kotlinter.support.resolveRuleSets
import org.jmailen.gradle.kotlinter.support.userData
import java.io.File
import javax.inject.Inject

/**
* Runnable used in the Gradle Worker API to run format on a batch of files.
Expand Down Expand Up @@ -41,8 +42,8 @@ class FormatWorkerRunnable @Inject constructor(
}
}?.let { formatFunc ->
val ruleSets = resolveRuleSets(executionContext.ruleSetProviders, ktLintParams.experimentalRules)
val formattedText = formatFunc.invoke(file, ruleSets) { line, col, detail, corrected ->
val errorStr = "$relativePath:$line:$col: $detail"
val formattedText = formatFunc.invoke(file, ruleSets) { error, corrected ->
val errorStr = "$relativePath:${error.line}:${error.col}: [${error.ruleId}] ${error.detail}"
val msg = when (corrected) {
true -> "Format fixed > $errorStr"
false -> "Format could not fix > $errorStr"
Expand Down Expand Up @@ -74,11 +75,11 @@ class FormatWorkerRunnable @Inject constructor(
userData = userData(ktLintParams),
editorConfigPath = ktLintParams.editorConfigPath,
cb = { error, corrected ->
onError(error.line, error.col, error.detail, corrected)
onError(error, corrected)
}
)
)
}
}

typealias ErrorHandler = (line: Int, col: Int, detail: String, corrected: Boolean) -> Unit
typealias ErrorHandler = (error: LintError, corrected: Boolean) -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class LintWorkerConfigurationAction(
workerConfiguration.isolationMode = IsolationMode.NONE
workerConfiguration.setParams(lintWorkerParameters)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.jmailen.gradle.kotlinter.tasks.lint

import org.jmailen.gradle.kotlinter.support.KtLintParams
import java.io.File
import java.io.Serializable
import java.util.UUID
import org.jmailen.gradle.kotlinter.support.KtLintParams

/**
* Serializable stateless parameters that are needed by the LintWorkerRunnable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.core.RuleSet
import java.io.File
import javax.inject.Inject
import org.gradle.api.logging.Logger
import org.jmailen.gradle.kotlinter.support.ExecutionContextRepository
import org.jmailen.gradle.kotlinter.support.resolveRuleSets
import org.jmailen.gradle.kotlinter.support.userData
import java.io.File
import javax.inject.Inject

/**
* Runnable used in the Gradle Worker API to run lint on a batch of files.
Expand Down Expand Up @@ -46,7 +46,7 @@ class LintWorkerRunnable @Inject constructor(
lintFunc?.invoke(file, ruleSets) { error ->
reporters.onEach { it.onLintError(relativePath, error, false) }

val errorStr = "$relativePath:${error.line}:${error.col}: ${error.detail}"
val errorStr = "$relativePath:${error.line}:${error.col}: [${error.ruleId}] ${error.detail}"
logger.quiet("Lint error > $errorStr")
}

Expand Down
Loading

1 comment on commit ccfce9d

@henrik242
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.