forked from JLLeitschuh/ktlint-gradle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KtlintExtension.kt
164 lines (149 loc) · 5.53 KB
/
KtlintExtension.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.jlleitschuh.gradle.ktlint
import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.util.ConfigureUtil
import org.jlleitschuh.gradle.ktlint.reporter.CustomReporter
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
/**
* Extension class for configuring the [KtlintPlugin].
* @param filterTargetApplier When [KtlintExtension.filter] is called, this function is executed.
*/
@Suppress("UnstableApiUsage")
open class KtlintExtension
internal constructor(
objectFactory: ObjectFactory,
customReportersContainer: NamedDomainObjectContainer<CustomReporter>,
private val filterTargetApplier: FilterApplier,
kotlinScriptAdditionalPathApplier: KotlinScriptAdditionalPathApplier
) {
internal val reporterExtension = ReporterExtension(
customReportersContainer,
objectFactory
)
/**
* The version of ktlint to use.
*/
val version: Property<String> = objectFactory.property { set("0.40.0") }
/**
* Enable verbose mode.
*/
val verbose: Property<Boolean> = objectFactory.property { set(false) }
/**
* Enable debug mode.
*/
val debug: Property<Boolean> = objectFactory.property { set(false) }
/**
* Enable android mode.
*/
val android: Property<Boolean> = objectFactory.property { set(false) }
/**
* Enable console output mode.
*/
val outputToConsole: Property<Boolean> = objectFactory.property { set(true) }
/**
* Enabled colored output to console.
*/
val coloredOutput: Property<Boolean> = objectFactory.property { set(true) }
/**
* Specify the color of the terminal output.
*/
val outputColorName: Property<String> = objectFactory.property { set("") }
/**
* Whether or not to allow the build to continue if there are warnings;
* defaults to {@code false}, as for any other static code analysis tool.
* <p>
* Example: `ignoreFailures = true`
*/
val ignoreFailures: Property<Boolean> = objectFactory.property { set(false) }
/**
* Configure Ktlint output reporters.
*
* _By default_ `plain` reporter is enabled, if no other reporter is configured, otherwise you need to specify
* it explicitly.
*/
fun reporters(action: Action<ReporterExtension>) {
action.execute(reporterExtension)
}
/**
* Enable experimental ktlint rules.
*
* You can find [here](https://github.com/pinterest/ktlint/blob/master/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/ExperimentalRuleSetProvider.kt)
* list of experimental rules that will be enabled.
*
* @since ktlint `0.31.0`
*/
val enableExperimentalRules: Property<Boolean> = objectFactory.property {
set(false)
}
/**
* Provide additional `.editorconfig` file, that are not in the project or project parent folders.
*/
val additionalEditorconfigFile: RegularFileProperty = objectFactory.fileProperty()
/**
* Disable particular rules, by default enabled in ktlint, using rule id.
*
* @since ktlint `0.34.2`
*/
val disabledRules: SetProperty<String> = objectFactory.setProperty {
set(emptySet())
}
private val kscriptExtension = KScriptExtension(kotlinScriptAdditionalPathApplier)
/**
* Provide additional, relative to the project base dir, paths that contains kotlin script files.
*/
fun kotlinScriptAdditionalPaths(action: Action<KScriptExtension>) {
action.execute(kscriptExtension)
}
/**
* Filter sources by applying exclude or include specs/patterns.
*
* See [PatternFilterable](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html)
* for details how apply exclude or include specs/patterns.
*/
fun filter(filterAction: Action<PatternFilterable>) {
filterTargetApplier(filterAction)
}
class KScriptExtension(
private val ksApplier: KotlinScriptAdditionalPathApplier
) {
/**
* Adds given [fileTree] to [KOTLIN_SCRIPT_CHECK_TASK]/[KOTLIN_SCRIPT_FORMAT_TASK] tasks search.
*/
fun include(fileTree: ConfigurableFileTree) {
ksApplier(fileTree)
}
}
class ReporterExtension(
val customReporters: NamedDomainObjectContainer<CustomReporter>,
objectFactory: ObjectFactory
) {
internal val reporters: SetProperty<ReporterType> = objectFactory.setProperty {
set(emptySet())
}
/**
* Use one of default Ktlint output reporter
*
* _By default_ `plain` type is enabled if no reporter is explicitly specified.
*
* @param reporterType one of `plain`, `plain_group_by_file`, `checkstyle`, `json`.
*/
fun reporter(reporterType: ReporterType) {
reporters.add(reporterType)
}
/**
* Add 3rd party reporters.
*/
fun customReporters(configuration: Closure<NamedDomainObjectContainer<CustomReporter>>) {
// This method is needed for Groovy interop
// See https://discuss.gradle.org/t/multi-level-dsl-for-plugin-extension/19029/16
ConfigureUtil.configure(configuration, customReporters)
}
}
}