-
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor compilation analysis task setup (#7016)
* Use new method to setup compilation tasks * Remove unused code * Refactor * Correct the logic to link test baseline tasks to detektBaselineTest * Throw exception when KotlinTargetsContainer extension isn't found This should never happen on a KMP project unless KGP API interfaces are changed unexpectedly.
- Loading branch information
Showing
9 changed files
with
221 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...dle-plugin/src/main/kotlin/dev/detekt/gradle/plugin/internal/DetektAndroidCompilations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package dev.detekt.gradle.plugin.internal | ||
|
||
import com.android.build.gradle.AppExtension | ||
import com.android.build.gradle.BaseExtension | ||
import com.android.build.gradle.LibraryExtension | ||
import com.android.build.gradle.TestExtension | ||
import com.android.build.gradle.api.BaseVariant | ||
import com.android.build.gradle.internal.api.TestedVariant | ||
import io.gitlab.arturbosch.detekt.DetektPlugin | ||
import io.gitlab.arturbosch.detekt.extensions.DetektExtension | ||
import org.gradle.api.DomainObjectSet | ||
import org.gradle.api.Project | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension | ||
|
||
internal object DetektAndroidCompilations { | ||
fun registerTasks(project: Project, extension: DetektExtension) { | ||
project.extensions.getByType(KotlinAndroidProjectExtension::class.java).target.compilations.all { compilation -> | ||
project.registerJvmCompilationDetektTask(extension, compilation) | ||
project.registerJvmCompilationCreateBaselineTask(extension, compilation) | ||
} | ||
} | ||
|
||
private fun DetektExtension.matchesIgnoredConfiguration(variant: BaseVariant): Boolean = | ||
ignoredVariants.get().contains(variant.name) || | ||
ignoredBuildTypes.get().contains(variant.buildType.name) || | ||
ignoredFlavors.get().contains(variant.flavorName) | ||
|
||
fun linkTasks(project: Project, extension: DetektExtension) { | ||
val mainTaskProvider = | ||
project.tasks.register("${DetektPlugin.DETEKT_TASK_NAME}Main") { | ||
it.group = "verification" | ||
it.description = "EXPERIMENTAL: Run detekt analysis for production classes across " + | ||
"all variants with type resolution" | ||
} | ||
|
||
val testTaskProvider = | ||
project.tasks.register("${DetektPlugin.DETEKT_TASK_NAME}Test") { | ||
it.group = "verification" | ||
it.description = "EXPERIMENTAL: Run detekt analysis for test classes across " + | ||
"all variants with type resolution" | ||
} | ||
|
||
val mainBaselineTaskProvider = | ||
project.tasks.register("${DetektPlugin.BASELINE_TASK_NAME}Main") { | ||
it.group = "verification" | ||
it.description = "EXPERIMENTAL: Creates detekt baseline files for production classes across " + | ||
"all variants with type resolution" | ||
} | ||
|
||
val testBaselineTaskProvider = | ||
project.tasks.register("${DetektPlugin.BASELINE_TASK_NAME}Test") { | ||
it.group = "verification" | ||
it.description = "EXPERIMENTAL: Creates detekt baseline files for test classes across " + | ||
"all variants with type resolution" | ||
} | ||
|
||
fun variants(extension: BaseExtension): DomainObjectSet<out BaseVariant>? = when (extension) { | ||
is AppExtension -> extension.applicationVariants | ||
is LibraryExtension -> extension.libraryVariants | ||
is TestExtension -> extension.applicationVariants | ||
else -> null | ||
} | ||
|
||
fun testVariants(baseVariant: BaseVariant): List<BaseVariant> = if (baseVariant is TestedVariant) { | ||
listOfNotNull(baseVariant.testVariant, baseVariant.unitTestVariant) | ||
} else { | ||
emptyList() | ||
} | ||
|
||
// There is not a single Android plugin, but each registers an extension based on BaseExtension, | ||
// so we catch them all by looking for this one | ||
project.extensions.findByType(BaseExtension::class.java)?.let { baseExtension -> | ||
variants(baseExtension) | ||
?.matching { !extension.matchesIgnoredConfiguration(it) } | ||
?.all { variant -> | ||
mainTaskProvider.configure { | ||
it.dependsOn(DetektPlugin.DETEKT_TASK_NAME + variant.name.capitalize()) | ||
} | ||
mainBaselineTaskProvider.configure { | ||
it.dependsOn(DetektPlugin.BASELINE_TASK_NAME + variant.name.capitalize()) | ||
} | ||
testVariants(variant) | ||
.filter { !extension.matchesIgnoredConfiguration(it) } | ||
.forEach { testVariant -> | ||
testTaskProvider.configure { | ||
it.dependsOn(DetektPlugin.DETEKT_TASK_NAME + testVariant.name.capitalize()) | ||
} | ||
testBaselineTaskProvider.configure { | ||
it.dependsOn(DetektPlugin.BASELINE_TASK_NAME + testVariant.name.capitalize()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...-gradle-plugin/src/main/kotlin/dev/detekt/gradle/plugin/internal/DetektJvmCompilations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.detekt.gradle.plugin.internal | ||
|
||
import io.gitlab.arturbosch.detekt.extensions.DetektExtension | ||
import org.gradle.api.Project | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension | ||
|
||
internal object DetektJvmCompilations { | ||
fun registerTasks(project: Project, extension: DetektExtension) { | ||
project.extensions.getByType(KotlinJvmProjectExtension::class.java).target.compilations.all { compilation -> | ||
project.registerJvmCompilationDetektTask(extension, compilation) | ||
project.registerJvmCompilationCreateBaselineTask(extension, compilation) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...adle-plugin/src/main/kotlin/dev/detekt/gradle/plugin/internal/DetektKmpJvmCompilations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.detekt.gradle.plugin.internal | ||
|
||
import io.gitlab.arturbosch.detekt.extensions.DetektExtension | ||
import org.gradle.api.Project | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.androidJvm | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.jvm | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetsContainer | ||
|
||
internal object DetektKmpJvmCompilations { | ||
fun registerTasks(project: Project, extension: DetektExtension) { | ||
val kotlinExtension = project.extensions.getByType(KotlinTargetsContainer::class.java) | ||
|
||
kotlinExtension.targets.matching { it.platformType in setOf(jvm, androidJvm) }.all { target -> | ||
target.compilations.all { compilation -> | ||
project.registerJvmCompilationDetektTask(extension, compilation, target) | ||
project.registerJvmCompilationCreateBaselineTask(extension, compilation, target) | ||
} | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
detekt-gradle-plugin/src/main/kotlin/dev/detekt/gradle/plugin/internal/SharedTasks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package dev.detekt.gradle.plugin.internal | ||
|
||
import io.gitlab.arturbosch.detekt.DetektPlugin | ||
import io.gitlab.arturbosch.detekt.extensions.DetektExtension | ||
import io.gitlab.arturbosch.detekt.internal.addVariantName | ||
import io.gitlab.arturbosch.detekt.internal.existingVariantOrBaseFile | ||
import io.gitlab.arturbosch.detekt.internal.registerCreateBaselineTask | ||
import io.gitlab.arturbosch.detekt.internal.registerDetektTask | ||
import org.gradle.api.Project | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile | ||
|
||
internal fun Project.registerJvmCompilationDetektTask( | ||
extension: DetektExtension, | ||
compilation: KotlinCompilation<KotlinCommonOptions>, | ||
target: KotlinTarget? = null, | ||
) { | ||
val taskSuffix = if (target != null) compilation.name + target.name.capitalize() else compilation.name | ||
registerDetektTask(DetektPlugin.DETEKT_TASK_NAME + taskSuffix.capitalize(), extension) { | ||
val siblingTask = compilation.compileTaskProvider.get() as KotlinJvmCompile | ||
|
||
setSource(siblingTask.sources) | ||
classpath.setFrom(compilation.output.classesDirs, siblingTask.libraries) | ||
|
||
// If a baseline file is configured as input file, it must exist to be configured, otherwise the task fails. | ||
// We try to find the configured baseline or alternatively a specific variant matching this task. | ||
extension.baseline.asFile.orNull?.existingVariantOrBaseFile(compilation.name)?.let { baselineFile -> | ||
baseline.convention(layout.file(provider { baselineFile })) | ||
} | ||
description = if (target != null) { | ||
"EXPERIMENTAL: Run detekt analysis for compilation ${compilation.name} on target " + | ||
"${compilation.target.name} with type resolution" | ||
} else { | ||
"EXPERIMENTAL: Run detekt analysis for ${compilation.name} classes with type resolution" | ||
} | ||
} | ||
} | ||
|
||
internal fun Project.registerJvmCompilationCreateBaselineTask( | ||
extension: DetektExtension, | ||
compilation: KotlinCompilation<KotlinCommonOptions>, | ||
target: KotlinTarget? = null, | ||
) { | ||
val taskSuffix = if (target != null) compilation.name + target.name.capitalize() else compilation.name | ||
registerCreateBaselineTask(DetektPlugin.BASELINE_TASK_NAME + taskSuffix.capitalize(), extension) { | ||
val siblingTask = compilation.compileTaskProvider.get() as KotlinJvmCompile | ||
|
||
setSource(siblingTask.sources) | ||
classpath.setFrom(compilation.output.classesDirs, siblingTask.libraries) | ||
|
||
val variantBaselineFile = extension.baseline.asFile.orNull?.addVariantName(compilation.name) | ||
baseline.convention(layout.file(provider { variantBaselineFile })) | ||
description = if (target != null) { | ||
"EXPERIMENTAL: Creates detekt baseline for compilation ${compilation.name} on target " + | ||
"${compilation.target.name} with type resolution" | ||
} else { | ||
"EXPERIMENTAL: Creates detekt baseline for ${compilation.name} classes with type resolution" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.