Skip to content

Commit

Permalink
Support for multiple lint file reporters addressing #22 (#24)
Browse files Browse the repository at this point in the history
* Support for multiple lint file reporters addressing #22

* Fix readme

* Who will lint the linter?
  • Loading branch information
jeremymailen authored Oct 29, 2017
1 parent da7fc8c commit 31091a7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm

```groovy
plugins {
id 'org.jmailen.kotlinter' version '1.4.1'
id 'org.jmailen.kotlinter' version '1.5.0'
}
```

Expand Down Expand Up @@ -66,10 +66,12 @@ Options are configured in the `kotlinter` extension. Defaults shown.
kotlinter {
ignoreFailures = false
indentSize = 4
reporter = 'checkstyle'
reporters = ['checkstyle', 'plain']
}
```
Options for `reporter`: checkstyle, json, plain
Options for `reporters`: checkstyle, json, plain

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

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

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
testRuntime 'com.android.tools.build:gradle:2.3.3'
}

version = '1.4.1'
version = '1.5.0'
group = 'org.jmailen.gradle'
def pluginId = 'org.jmailen.kotlinter'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ open class KotlinterExtension {

var indentSize = DEFAULT_INDENT_SIZE

var reporter = DEFAULT_REPORTER
var reporter: String? = null

var reporters = arrayOf(DEFAULT_REPORTER)

// for backwards compatibility
fun reporters() = reporter?.let { arrayOf(it) } ?: reporters
}

enum class ReporterType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class KotlinterPlugin : Plugin<Project> {
project.tasks.withType(LintTask::class.java) { lintTask ->
lintTask.ignoreFailures = kotlinterExtention.ignoreFailures
lintTask.indentSize = kotlinterExtention.indentSize
lintTask.reporter = kotlinterExtention.reporter
lintTask.report = project.reportFile(
"${lintTask.sourceSetId}-lint.${reporterFileExtension(kotlinterExtention.reporter)}")
lintTask.reports = kotlinterExtention.reporters().associate { reporter ->
reporter to project.reportFile("${lintTask.sourceSetId}-lint.${reporterFileExtension(reporter)}")
}
}
project.tasks.withType(FormatTask::class.java) { formatTask ->
formatTask.indentSize = kotlinterExtention.indentSize
Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.logging.LogLevel
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.ParallelizableTask
import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.TaskAction
Expand All @@ -20,11 +21,8 @@ import java.io.File
@ParallelizableTask
open class LintTask : SourceTask() {

@OutputFile
lateinit var report: File

@Input
var reporter = KotlinterExtension.DEFAULT_REPORTER
@OutputFiles
lateinit var reports: Map<String, File>

@Input
var ignoreFailures = KotlinterExtension.DEFAULT_IGNORE_FAILURES
Expand All @@ -38,13 +36,15 @@ open class LintTask : SourceTask() {
@TaskAction
fun run() {
var hasErrors = false
var fileReporter = reporterFor(reporter, report)
val fileReporters = reports.map { (reporter, report) ->
reporterFor(reporter, report)
}

fileReporter.beforeAll()
fileReporters.onEach { it.beforeAll() }

getSource().forEach { file ->
val relativePath = file.toRelativeString(project.projectDir)
fileReporter.before(relativePath)
fileReporters.onEach { it.before(relativePath) }
logger.log(LogLevel.DEBUG, "$name linting: $relativePath")

val lintFunc = when (file.extension) {
Expand All @@ -57,18 +57,18 @@ open class LintTask : SourceTask() {
}

lintFunc?.invoke(file, resolveRuleSets()) { error ->
fileReporter.onLintError(relativePath, error, false)
fileReporters.onEach { it.onLintError(relativePath, error, false) }

val errorStr = "$relativePath:${error.line}:${error.col}: ${error.detail}"
logger.log(LogLevel.QUIET, "Lint error > $errorStr")

hasErrors = true
}

fileReporter.after(relativePath)
fileReporters.onEach { it.after(relativePath) }
}

fileReporter.afterAll()
fileReporters.onEach { it.afterAll() }
if (hasErrors && !ignoreFailures) {
throw GradleException("Kotlin source failed lint check.")
}
Expand Down

0 comments on commit 31091a7

Please sign in to comment.