-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathKtlintPlugin.kt
350 lines (309 loc) · 13.5 KB
/
KtlintPlugin.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package org.jlleitschuh.gradle.ktlint
import com.android.build.gradle.BaseExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.FileCollection
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.internal.HasConvention
import org.gradle.api.logging.configuration.ConsoleOutput
import org.gradle.api.plugins.Convention
import org.gradle.api.plugins.JavaPluginConvention
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.experimental.KotlinNativeComponent
import org.jetbrains.kotlin.gradle.plugin.konan.KonanArtifactContainer
import org.jetbrains.kotlin.gradle.plugin.konan.KonanExtension
import shadow.org.jetbrains.kotlin.gradle.plugin.tasks.KonanCompileTask
import java.util.concurrent.Callable
import kotlin.reflect.KClass
/**
* Plugin that provides a wrapper over the `ktlint` project.
*/
open class KtlintPlugin : Plugin<Project> {
override fun apply(target: Project) {
val extension = target.plugins.apply(KtlintBasePlugin::class.java).extension
// Apply the idea plugin
target.plugins.apply(KtlintIdeaPlugin::class.java)
addKtLintTasksToKotlinPlugin(target, extension)
}
private fun addKtLintTasksToKotlinPlugin(target: Project, extension: KtlintExtension) {
target.plugins.withId("kotlin", applyKtLint(target, extension))
target.plugins.withId("kotlin2js", applyKtLint(target, extension))
target.plugins.withId("kotlin-platform-common", applyKtLint(target, extension))
target.plugins.withId("kotlin-android", applyKtLintToAndroid(target, extension))
target.plugins.withId("konan", applyKtLintKonanNative(target, extension))
target.plugins.withId(
"org.jetbrains.kotlin.native",
applyKtLintNative(target, extension)
)
target.plugins.withId(
"org.jetbrains.kotlin.multiplatform",
applyKtlintMultiplatform(target, extension)
)
}
private fun applyKtlintMultiplatform(
target: Project,
extension: KtlintExtension
): (Plugin<in Any>) -> Unit = {
val ktLintConfig = createConfiguration(target, extension)
val multiplatformExtension = target.extensions.getByType(KotlinMultiplatformExtension::class.java)
multiplatformExtension.sourceSets.all { sourceSet ->
val checkTask = createCheckTask(
target,
extension,
sourceSet.name,
ktLintConfig,
sourceSet.kotlin.sourceDirectories
)
addKtlintCheckTaskToProjectMetaCheckTask(target, checkTask)
setCheckTaskDependsOnKtlintCheckTask(target, checkTask)
val ktlintSourceSetFormatTask = createFormatTask(
target,
extension,
sourceSet.name,
ktLintConfig,
sourceSet.kotlin.sourceDirectories
)
addKtlintFormatTaskToProjectMetaFormatTask(target, ktlintSourceSetFormatTask)
}
}
private fun applyKtLint(
target: Project,
extension: KtlintExtension
): (Plugin<in Any>) -> Unit {
return {
val ktLintConfig = createConfiguration(target, extension)
target.theHelper<JavaPluginConvention>().sourceSets.forEach { sourceSet ->
val kotlinSourceSet: SourceDirectorySet = (sourceSet as HasConvention)
.convention
.getPluginHelper<KotlinSourceSet>()
.kotlin
val checkTask = createCheckTask(
target,
extension,
sourceSet.name,
ktLintConfig,
kotlinSourceSet.sourceDirectories
)
addKtlintCheckTaskToProjectMetaCheckTask(target, checkTask)
setCheckTaskDependsOnKtlintCheckTask(target, checkTask)
val ktlintSourceSetFormatTask = createFormatTask(
target,
extension,
sourceSet.name,
ktLintConfig,
kotlinSourceSet.sourceDirectories
)
addKtlintFormatTaskToProjectMetaFormatTask(target, ktlintSourceSetFormatTask)
}
}
}
private fun applyKtLintToAndroid(
target: Project,
extension: KtlintExtension
): (Plugin<in Any>) -> Unit {
return {
val ktLintConfig = createConfiguration(target, extension)
fun createTasks(
fullVariantName: String,
sources: FileCollection
) {
val checkTask = createCheckTask(
target,
extension,
fullVariantName,
ktLintConfig,
sources
)
addKtlintCheckTaskToProjectMetaCheckTask(target, checkTask)
setCheckTaskDependsOnKtlintCheckTask(target, checkTask)
val ktlintSourceSetFormatTask = createFormatTask(
target,
extension,
fullVariantName,
ktLintConfig,
sources
)
addKtlintFormatTaskToProjectMetaFormatTask(target, ktlintSourceSetFormatTask)
}
/*
* Variant manager returns all sources for variant,
* so most probably main source set maybe checked several times.
* This approach creates one check tasks per one source set.
*/
fun getPluginConfigureAction(): (Plugin<Any>) -> Unit = {
target.extensions.configure(BaseExtension::class.java) { ext ->
ext.sourceSets { sourceSet ->
sourceSet.all { androidSourceSet ->
// Passing Callable, so returned FileCollection, will lazy evaluate it
// only when task will need it.
// Solves the problem of having additional source dirs in
// current AndroidSourceSet, that are not available on eager
// evaluation.
createTasks(
androidSourceSet.name,
target.files(Callable { androidSourceSet.java.srcDirs })
)
}
}
}
}
target.plugins.withId("com.android.application", getPluginConfigureAction())
target.plugins.withId("com.android.library", getPluginConfigureAction())
target.plugins.withId("com.android.instantapp", getPluginConfigureAction())
target.plugins.withId("com.android.feature", getPluginConfigureAction())
target.plugins.withId("com.android.test", getPluginConfigureAction())
}
}
private fun applyKtLintKonanNative(
project: Project,
extension: KtlintExtension
): (Plugin<in Any>) -> Unit {
return {
val ktLintConfig = createConfiguration(project, extension)
val compileTargets = project.theHelper<KonanExtension>().targets
project.theHelper<KonanArtifactContainer>().whenObjectAdded { buildConfig ->
addTasksForNativePlugin(project, extension, buildConfig.name, ktLintConfig) {
compileTargets.fold(initial = emptyList()) { acc, target ->
val compileTask = buildConfig.findByTarget(target)
if (compileTask != null) {
val sourceFiles = (compileTask as KonanCompileTask).srcFiles
acc + sourceFiles
} else {
acc
}
}
}
}
}
}
private fun applyKtLintNative(
project: Project,
extension: KtlintExtension
): (Plugin<in Any>) -> Unit {
return {
val ktLintConfig = createConfiguration(project, extension)
project.components.withType(KotlinNativeComponent::class.java) { component ->
addTasksForNativePlugin(project, extension, component.name, ktLintConfig) {
component.konanTargets.get()
.fold(initial = emptyList()) { acc, nativeTarget ->
acc + listOf(component.sources.getAllSources(nativeTarget))
}
}
}
}
}
private fun addTasksForNativePlugin(
project: Project,
extension: KtlintExtension,
sourceSetName: String,
ktlintConfiguration: Configuration,
gatherVariantSources: () -> List<FileCollection>
) {
val sourceDirectoriesList = gatherVariantSources()
if (sourceDirectoriesList.isNotEmpty()) {
val checkTask = createCheckTask(
project,
extension,
sourceSetName,
ktlintConfiguration,
sourceDirectoriesList
)
addKtlintCheckTaskToProjectMetaCheckTask(project, checkTask)
setCheckTaskDependsOnKtlintCheckTask(project, checkTask)
val ktlintSourceSetFormatTask = createFormatTask(
project,
extension,
sourceSetName,
ktlintConfiguration,
sourceDirectoriesList
)
addKtlintFormatTaskToProjectMetaFormatTask(project, ktlintSourceSetFormatTask)
}
}
private fun addKtlintCheckTaskToProjectMetaCheckTask(target: Project, checkTask: Task) {
target.getMetaKtlintCheckTask().dependsOn(checkTask)
if (target.rootProject != target) {
target.rootProject.getMetaKtlintCheckTask().dependsOn(checkTask)
}
}
private fun addKtlintFormatTaskToProjectMetaFormatTask(target: Project, formatTask: Task) {
target.getMetaKtlintFormatTask().dependsOn(formatTask)
if (target.rootProject != target) {
target.rootProject.getMetaKtlintFormatTask().dependsOn(formatTask)
}
}
private fun createFormatTask(
target: Project,
extension: KtlintExtension,
sourceSetName: String,
ktLintConfig: Configuration,
kotlinSourceDirectories: Iterable<*>
): Task {
return target.taskHelper<KtlintFormatTask>("ktlint${sourceSetName.capitalize()}Format") {
description = "Runs a check against all .kt files to ensure that they are formatted according to ktlint."
configurePluginTask(target, extension, ktLintConfig, kotlinSourceDirectories)
}
}
private fun createCheckTask(
target: Project,
extension: KtlintExtension,
sourceSetName: String,
ktLintConfig: Configuration,
kotlinSourceDirectories: Iterable<*>
): Task {
return target.taskHelper<KtlintCheckTask>("ktlint${sourceSetName.capitalize()}Check") {
description = "Runs a check against all .kt files to ensure that they are formatted according to ktlint."
configurePluginTask(target, extension, ktLintConfig, kotlinSourceDirectories)
}
}
private fun KtlintCheckTask.configurePluginTask(
target: Project,
extension: KtlintExtension,
ktLintConfig: Configuration,
kotlinSourceDirectories: Iterable<*>
) {
classpath.setFrom(ktLintConfig)
setSource(kotlinSourceDirectories)
ktlintVersion.set(extension.version)
verbose.set(extension.verbose)
debug.set(extension.debug)
android.set(extension.android)
ignoreFailures.set(extension.ignoreFailures)
outputToConsole.set(extension.outputToConsole)
coloredOutput.set(extension.coloredOutput.map {
if (target.isConsolePlain()) {
target.logger.info("Console type is plain: disabling colored output")
false
} else {
it
}
})
ruleSets.set(extension.ruleSets)
reporters.set(extension.reporters)
}
private fun Project.getMetaKtlintCheckTask(): Task = this.tasks.findByName(CHECK_PARENT_TASK_NAME)
?: this.task(CHECK_PARENT_TASK_NAME).apply {
group = VERIFICATION_GROUP
description = "Runs ktlint on all kotlin sources in this project."
}
private fun Project.getMetaKtlintFormatTask(): Task = this.tasks.findByName(FORMAT_PARENT_TASK_NAME)
?: this.task(FORMAT_PARENT_TASK_NAME).apply {
group = FORMATTING_GROUP
description = "Runs the ktlint formatter on all kotlin sources in this project."
}
private fun setCheckTaskDependsOnKtlintCheckTask(project: Project, ktlintCheck: Task) {
project.tasks.findByName("check")?.dependsOn(ktlintCheck)
}
/*
* Helper functions used until Gradle Script Kotlin solidifies it's plugin API.
*/
private inline fun <reified T : Any> Project.theHelper() =
theHelper(T::class)
private fun <T : Any> Project.theHelper(extensionType: KClass<T>) =
convention.findPlugin(extensionType.java) ?: convention.getByType(extensionType.java)
private inline fun <reified T> Convention.getPluginHelper() = getPlugin(T::class.java)
private fun Project.isConsolePlain() = gradle.startParameter.consoleOutput == ConsoleOutput.Plain
}