Skip to content

Instantly share code, notes, and snippets.

@pavelkorolevxyz
Created May 16, 2022 15:22
Show Gist options
  • Select an option

  • Save pavelkorolevxyz/29f27c19c297992b15e7a11667c682f2 to your computer and use it in GitHub Desktop.

Select an option

Save pavelkorolevxyz/29f27c19c297992b15e7a11667c682f2 to your computer and use it in GitHub Desktop.
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.DetektPlugin
import io.gitlab.arturbosch.detekt.report.ReportMergeTask
plugins {
base
id("io.gitlab.arturbosch.detekt") // We need to apply this plugin with version in build.gradle.kts
}
dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.20.0") // This is formatting rule set
}
val configFile = files("$rootDir/config/detekt/config.yml") // This is where our rules config will be stored
val baselineFile = file("config/detekt/baseline.xml") // This is the location of baseline files in each module, will be ignored if not found
val mergedReportFile = file("${rootProject.buildDir}/reports/detekt/report.xml") // Location of merged report. Basically a final result of detekt work
/**
* Location of single module report inside its build directory
* Must be named detekt.xml
* Workaround for https://github.com/detekt/detekt/issues/4192#issuecomment-946325201
*/
val outputReportFile = file("$buildDir/reports/detekt/detekt.xml")
detekt {
ignoreFailures = true // We do not want to crash if warnings found, cause they will be used later
parallel = true // Runs detekt checks in parallel
config.setFrom(configFile)
buildUponDefaultConfig = true // Tells the detekt to use default config if its settings are not overriden in explicit config
baseline = baselineFile // Baseline file location
}
val reportMerge: TaskProvider<ReportMergeTask> = rootProject.registerMaybe("reportMerge") {
description = "Runs merge of all detekt reports into single one"
output.set(mergedReportFile)
}
/**
* Enable only XML reports output and set its location
*/
tasks.withType<Detekt>().configureEach {
reports {
html.required.set(false)
sarif.required.set(false)
txt.required.set(false)
xml.required.set(true)
xml.outputLocation.set(outputReportFile)
}
}
/**
* Finalizes every detekt task with ReportMergeTask
*/
plugins.withType<DetektPlugin> {
tasks.withType<Detekt> {
finalizedBy(reportMerge)
reportMerge.configure {
input.from(xmlReportFile)
}
}
}
/**
* Workaround to get [TaskProvider] by task name if it already exists in given [Project]
* or register it otherwise
* [Github - Introduce TaskContainer.maybeNamed](https://github.com/gradle/gradle/issues/8057)
*/
inline fun <reified T : Task> Project.registerMaybe(
taskName: String,
configuration: Action<in T> = Action {},
): TaskProvider<T> {
if (taskName in tasks.names) {
return tasks.named(taskName, T::class, configuration)
}
return tasks.register(taskName, T::class, configuration)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment