Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Ensure Kotlin compilation task reruns when detekt config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed Sep 12, 2020
1 parent 5217b9b commit a1f64cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class DetektCommandLineProcessor : CommandLineProcessor {
"Comma separated paths to detekt config files.",
false
),
CliOption(
Options.configDigest,
"<digest>",
"A digest calculated from the content of the config files. Used for Gradle incremental task invalidation.",
false
),
CliOption(
Options.baseline,
"<path>",
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/io/github/detekt/compiler/plugin/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ object Options {
const val isEnabled: String = "isEnabled"
const val debug: String = "debug"
const val config = "config"
const val configDigest: String = "configDigest"
const val baseline: String = "baseline"
const val useDefaultConfig: String = "useDefaultConfig"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package io.github.detekt.gradle
import io.github.detekt.compiler.plugin.Options
import io.github.detekt.gradle.extensions.DetektExtension
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import java.io.File
import java.security.MessageDigest
import java.util.Base64

class DetektKotlinCompilerPlugin : KotlinCompilerPluginSupportPlugin {

Expand All @@ -24,6 +28,7 @@ class DetektKotlinCompilerPlugin : KotlinCompilerPluginSupportPlugin {

val options = project.objects.listProperty(SubpluginOption::class.java).apply {
add(SubpluginOption(Options.debug, extension.debug.toString()))
add(SubpluginOption(Options.configDigest, extension.config.toDigest()))
add(SubpluginOption(Options.isEnabled, extension.isEnabled.toString()))
add(SubpluginOption(Options.useDefaultConfig, extension.buildUponDefaultConfig.toString()))
}
Expand All @@ -36,6 +41,17 @@ class DetektKotlinCompilerPlugin : KotlinCompilerPluginSupportPlugin {
return options
}

private fun ConfigurableFileCollection.toDigest(): String {
val concatenatedConfig = this
.filter { it.isFile }
.map(File::readBytes)
.fold(byteArrayOf()) { acc, file -> acc + file }

return Base64.getEncoder().encodeToString(
MessageDigest.getInstance("SHA-256").digest(concatenatedConfig)
)
}

override fun getCompilerPluginId(): String = DETEKT_COMPILER_PLUGIN

override fun getPluginArtifact(): SubpluginArtifact =
Expand Down

0 comments on commit a1f64cf

Please sign in to comment.