-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathLintTask.kt
70 lines (62 loc) · 2.63 KB
/
LintTask.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package org.jmailen.gradle.kotlinter.tasks
import org.gradle.api.GradleException
import org.gradle.api.file.FileTree
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.work.InputChanges
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_IGNORE_FAILURES
import org.jmailen.gradle.kotlinter.support.KotlinterError
import org.jmailen.gradle.kotlinter.support.LintFailure
import org.jmailen.gradle.kotlinter.tasks.lint.LintWorkerAction
import java.io.File
import javax.inject.Inject
@CacheableTask
open class LintTask @Inject constructor(
private val workerExecutor: WorkerExecutor,
objectFactory: ObjectFactory,
private val projectLayout: ProjectLayout,
) : ConfigurableKtLintTask(
projectLayout = projectLayout,
objectFactory = objectFactory,
) {
@OutputFiles
val reports: MapProperty<String, File> = objectFactory.mapProperty(default = emptyMap())
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
override fun getSource(): FileTree = super.getSource()
@Input
val ignoreFailures: Property<Boolean> = objectFactory.property(default = DEFAULT_IGNORE_FAILURES)
@TaskAction
fun run(inputChanges: InputChanges) {
val result = with(workerExecutor.noIsolation()) {
submit(LintWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(projectLayout.projectDirectory.asFile)
p.reporters.putAll(reports)
p.ktLintParams.set(getKtLintParams())
p.changedEditorconfigFiles.from(getChangedEditorconfigFiles(inputChanges))
}
runCatching { await() }
}
result.exceptionOrNull()?.workErrorCauses<KotlinterError>()?.ifNotEmpty {
forEach { logger.error(it.message, it.cause) }
throw GradleException("error linting sources for $name")
}
val lintFailures = result.exceptionOrNull()?.workErrorCauses<LintFailure>() ?: emptyList()
if (lintFailures.isNotEmpty() && !ignoreFailures.get()) {
throw GradleException("$name sources failed lint check")
}
}
}