Skip to content

Commit

Permalink
Extract Detekt config in a dedicated plugin and simplify config (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 authored Sep 16, 2024
1 parent f65758a commit c7a7167
Show file tree
Hide file tree
Showing 25 changed files with 213 additions and 973 deletions.
6 changes: 6 additions & 0 deletions build-logic/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tasks.withType<KotlinCompile>().configureEach {

dependencies {
compileOnly(libs.android.gradle.api)
compileOnly(libs.detekt.gradle.plugin)
compileOnly(libs.kotlinx.kover.gradle)
compileOnly(libs.kotlin.gradle.plugin)

Expand Down Expand Up @@ -69,5 +70,10 @@ gradlePlugin {
id = "ch.srgssr.pillarbox.gradle.android_library_tested_module"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryTestedModulePlugin"
}

register("PillarboxDetekt") {
id = "ch.srgssr.pillarbox.gradle.detekt"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxDetektPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.AppConfig
import ch.srgssr.pillarbox.gradle.internal.libs
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import io.gitlab.arturbosch.detekt.report.ReportMergeTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType

/**
* Custom Gradle plugin to configure Detekt for Pillarbox.
*
* Check [Detekt's documentation](https://detekt.dev/docs/gettingstarted/gradle) for more information.
*/
class PillarboxDetektPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("android-reporting")

val detektReportMerge = tasks.register<ReportMergeTask>("detektReportMerge") {
output.set(rootProject.layout.buildDirectory.file("reports/detekt/pillarbox-android.sarif"))
}

allprojects {
pluginManager.apply("io.gitlab.arturbosch.detekt")

val detektTasks = tasks.withType<Detekt>()

detektTasks.configureEach {
jvmTarget = AppConfig.javaVersionName

reports {
html.required.set(true)
md.required.set(false)
sarif.required.set(true)
txt.required.set(false)
xml.required.set(false)
}

finalizedBy(detektReportMerge)
}

extensions.configure<DetektExtension> {
autoCorrect = true
basePath = rootDir.absolutePath
buildUponDefaultConfig = true
config.setFrom(rootProject.layout.projectDirectory.file("config/detekt/detekt.yml"))
ignoredBuildTypes = listOf("release")
parallel = true
}

detektReportMerge.configure {
input.from(detektTasks.map { it.sarifReportFile })
}

dependencies.add("detekt", libs.findLibrary("detekt-cli").get())
dependencies.add("detektPlugins", libs.findLibrary("detekt-formatting").get())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import org.gradle.api.JavaVersion
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

internal object AppConfig {
// When changing this value, don't forget to also update the Detekt config in the root `build.gradle.kts` file
private val javaVersionName = "17"

internal const val minSdk = 21
internal const val targetSdk = 34
internal const val compileSdk = 34

internal const val javaVersionName = "17"
internal val javaVersion = JavaVersion.valueOf("VERSION_$javaVersionName")
internal val jvmTarget = JvmTarget.fromTarget(javaVersionName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ package ch.srgssr.pillarbox.gradle.internal

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal val Project.libs: VersionCatalog
get() = extensions.getByType<VersionCatalogsExtension>().named("libs")

internal fun Project.configureAndroidModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
namespace = "ch.srgssr.pillarbox." + name.removePrefix("pillarbox-").replace('-', '.')
compileSdk = AppConfig.compileSdk
Expand Down
53 changes: 5 additions & 48 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.report.ReportMergeTask

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
Expand All @@ -16,65 +14,24 @@ plugins {
alias(libs.plugins.dependency.analysis.gradle.plugin)
alias(libs.plugins.dokka) apply false
alias(libs.plugins.kotlinx.kover)
alias(libs.plugins.pillarbox.detekt)
}

apply(plugin = "android-reporting")

val detektReportMerge by tasks.registering(ReportMergeTask::class) {
output.set(rootProject.layout.buildDirectory.file("reports/detekt/pillarbox-android.sarif"))
}

allprojects {
apply(plugin = "io.gitlab.arturbosch.detekt")
// Official site : https://detekt.dev/docs/gettingstarted/gradle
// Tutorial : https://medium.com/@nagendran.p/integrating-detekt-in-the-android-studio-442128e971f8
detekt {
config.setFrom(files("../config/detekt/detekt.yml"))
// preconfigure defaults
buildUponDefaultConfig = false
ignoredBuildTypes = listOf("release")
autoCorrect = true
parallel = true
}

dependencies {
detekt(libs.detekt.cli)
detektPlugins(libs.detekt.formatting)
}

tasks.withType<Detekt>().configureEach {
jvmTarget = JavaVersion.VERSION_17.majorVersion
basePath = rootDir.absolutePath
reports {
xml.required = false
html.required = true
txt.required = false
sarif.required = true
md.required = false
}
finalizedBy(detektReportMerge)
}

detektReportMerge {
input.from(tasks.withType<Detekt>().map { it.sarifReportFile })
}
}

// Configure the `wrapper` task, so it can be easily be updated by simply running `./gradlew wrapper`.
// Configure the `wrapper` task, so it can easily be updated by simply running `./gradlew wrapper`.
tasks.wrapper {
distributionType = Wrapper.DistributionType.ALL
gradleVersion = "latest"
}

tasks.register<Delete>("clean") {
val clean by tasks.registering(Delete::class) {
delete(rootProject.layout.buildDirectory)
}

/*
* https://detekt.dev/docs/gettingstarted/git-pre-commit-hook
* https://medium.com/@alistair.cerio/android-ktlint-and-pre-commit-git-hook-5dd606e230a9
*/
tasks.register<Copy>("installGitHook") {
val installGitHook by tasks.registering(Copy::class) {
description = "Install the Git pre-commit hook locally"
from(file("${rootProject.rootDir}/git_hooks/pre-commit"))
into { file("${rootProject.rootDir}/.git/hooks") }
Expand All @@ -83,7 +40,7 @@ tasks.register<Copy>("installGitHook") {
}
}

tasks.getByPath(":pillarbox-demo:preBuild").dependsOn(":installGitHook")
tasks.getByPath(":pillarbox-demo:preBuild").dependsOn(installGitHook)

dependencyAnalysis {
issues {
Expand Down
Loading

0 comments on commit c7a7167

Please sign in to comment.