-
Notifications
You must be signed in to change notification settings - Fork 50
/
ConfigurableKtLintTask.kt
60 lines (52 loc) · 2.19 KB
/
ConfigurableKtLintTask.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
package org.jmailen.gradle.kotlinter.tasks
import org.gradle.api.file.FileCollection
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SourceTask
import org.gradle.internal.exceptions.MultiCauseException
import org.gradle.work.FileChange
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
import org.jmailen.gradle.kotlinter.support.findApplicableEditorConfigFiles
abstract class ConfigurableKtLintTask(
projectLayout: ProjectLayout,
objectFactory: ObjectFactory,
) : SourceTask() {
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:Incremental
internal val editorconfigFiles: FileCollection = objectFactory.fileCollection().apply {
from(projectLayout.findApplicableEditorConfigFiles().toList())
}
protected fun getChangedEditorconfigFiles(inputChanges: InputChanges) = if (inputChanges.isIncremental) {
inputChanges.getFileChanges(editorconfigFiles).map(FileChange::getFile)
} else {
emptyList()
}
}
internal inline fun <reified T> ObjectFactory.property(default: T? = null): Property<T> = property(T::class.java).apply {
set(default)
}
internal inline fun <reified T> ObjectFactory.listProperty(default: Iterable<T> = emptyList()): ListProperty<T> =
listProperty(T::class.java).apply {
set(default)
}
internal inline fun <reified K, reified V> ObjectFactory.mapProperty(default: Map<K, V> = emptyMap()): MapProperty<K, V> =
mapProperty(K::class.java, V::class.java).apply {
set(default)
}
inline fun <reified T : Throwable> Throwable.workErrorCauses(): List<Throwable> {
return when (this) {
is MultiCauseException -> this.causes.map { it.cause }
else -> listOf(this.cause)
}.filter {
// class instance comparison doesn't work due to different classloaders
it?.javaClass?.canonicalName == T::class.java.canonicalName
}.filterNotNull()
}