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

Commit

Permalink
Add report support for KotlinCompile tasks (#34)
Browse files Browse the repository at this point in the history
* Add report support for KotlinCompile tasks

* Update README.md

Co-authored-by: M Schalk <[email protected]>
  • Loading branch information
3flex and schalkms authored Dec 29, 2020
1 parent 73b8341 commit 3358fa6
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 75 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ dependencies {
}
```

Reports can be customized using the `detekt` extension which is added to `KotlinCompile` tasks. Using Groovy this might
look like:

```groovy
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
detekt {
reports {
xml.enabled.set(true)
txt.enabled.set(false)
create("custom") {
enabled.set(false)
}
}
}
}
```

### Limitations

Everything our Gradle plugin (`DetektExtension`) supports, is also supported on the declaration side with this plugin.
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repositories {

dependencies {
compileOnly(gradleApi())
compileOnly(kotlin("gradle-plugin", kotlinVersion))
compileOnly(kotlin("gradle-plugin-api", kotlinVersion))
compileOnly(kotlin("stdlib", kotlinVersion))
compileOnly(kotlin("compiler-embeddable", kotlinVersion))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ class DetektCommandLineProcessor : CommandLineProcessor {
"<base64-encoded globs>",
"A base64-encoded list of the globs used to exclude paths from scanning.",
false
),
CliOption(
Options.report,
"<report-id:path>",
"Generates a report for given 'report-id' and stores it on given 'path'. " +
"Available 'report-id' values: 'txt', 'xml', 'html'.",
false,
allowMultipleOccurrences = true
)
)

Expand All @@ -74,6 +82,7 @@ class DetektCommandLineProcessor : CommandLineProcessor {
Options.useDefaultConfig -> configuration.put(Keys.USE_DEFAULT_CONFIG, value)
Options.rootPath -> configuration.put(Keys.ROOT_PATH, Paths.get(value))
Options.excludes -> configuration.put(Keys.EXCLUDES, value.decodeToGlobSet())
Options.report -> configuration.put(Keys.REPORTS, value.substringBefore(':'), Paths.get(value.substringAfter(':')))
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/github/detekt/compiler/plugin/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Options {
const val useDefaultConfig: String = "useDefaultConfig"
const val rootPath = "rootDir"
const val excludes = "excludes"
const val report = "report"
}

object Keys {
Expand All @@ -24,4 +25,5 @@ object Keys {
val USE_DEFAULT_CONFIG = CompilerConfigurationKey.create<String>(Options.useDefaultConfig)
val ROOT_PATH = CompilerConfigurationKey.create<Path>(Options.rootPath)
val EXCLUDES = CompilerConfigurationKey.create<Set<String>>(Options.excludes)
val REPORTS = CompilerConfigurationKey.create<Map<String, Path>>(Options.report)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ internal fun CompilerConfiguration.toSpec(log: MessageCollector) = ProcessingSpe
outputChannel = AppendableAdapter { log.info(it) }
errorChannel = AppendableAdapter { log.error(it) }
}
reports {
get(Keys.REPORTS)?.forEach {
report { Pair(it.key, it.value) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.detekt.gradle

import io.github.detekt.compiler.plugin.Options
import io.github.detekt.gradle.extensions.DetektExtension
import io.github.detekt.gradle.extensions.ProjectDetektExtension
import io.github.detekt.gradle.extensions.KotlinCompileTaskDetektExtension
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFile
import org.gradle.api.plugins.ReportingBasePlugin
import org.gradle.api.provider.Provider
import org.gradle.api.reporting.ReportingExtension
Expand All @@ -12,6 +14,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.ObjectOutputStream
Expand All @@ -22,7 +25,7 @@ class DetektKotlinCompilerPlugin : KotlinCompilerPluginSupportPlugin {

override fun apply(target: Project) {
target.pluginManager.apply(ReportingBasePlugin::class.java)
val extension = target.extensions.create(DETEKT_NAME, DetektExtension::class.java)
val extension = target.extensions.create(DETEKT_NAME, ProjectDetektExtension::class.java)
extension.reportsDir = target.extensions.getByType(ReportingExtension::class.java).file(DETEKT_NAME)
extension.excludes.add("**/${target.relativePath(target.buildDir)}/**")

Expand All @@ -37,12 +40,20 @@ class DetektKotlinCompilerPlugin : KotlinCompilerPluginSupportPlugin {
configuration.isTransitive = true
configuration.description = "The $CONFIGURATION_DETEKT_PLUGINS libraries to be used for this project."
}

target.tasks.withType(KotlinCompile::class.java).configureEach { task ->
task.extensions.create(DETEKT_NAME, KotlinCompileTaskDetektExtension::class.java, target)
}
}

override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider<List<SubpluginOption>> {
val project = kotlinCompilation.target.project
val providers = project.providers

val extension = project.extensions.getByType(DetektExtension::class.java)
val extension = project.extensions.getByType(ProjectDetektExtension::class.java)
val taskExtension = kotlinCompilation.compileKotlinTask.extensions.getByType(KotlinCompileTaskDetektExtension::class.java)

val reportsDir: Provider<RegularFile> = project.layout.file(providers.provider { extension.reportsDir })

project.configurations.getByName("kotlinCompilerPluginClasspath").apply {
extendsFrom(project.configurations.getAt(CONFIGURATION_DETEKT_PLUGINS))
Expand All @@ -55,6 +66,20 @@ class DetektKotlinCompilerPlugin : KotlinCompilerPluginSupportPlugin {
add(SubpluginOption(Options.useDefaultConfig, extension.buildUponDefaultConfig.toString()))
add(SubpluginOption(Options.rootPath, project.rootDir.toString()))
add(SubpluginOption(Options.excludes, extension.excludes.get().encodeToBase64()))

taskExtension.reports.all { report ->
report.enabled.convention(true)
report.destination.convention(
project.layout.projectDirectory.file(providers.provider {
val reportFileName = "${kotlinCompilation.name}.${report.name}"
File(reportsDir.get().asFile, reportFileName).absolutePath
})
)

if (report.enabled.get()) {
add(SubpluginOption(Options.report, "${report.name}:${report.destination.asFile.get().absolutePath}"))
}
}
}

extension.baseline?.let { options.add(SubpluginOption(Options.baseline, it.toString())) }
Expand Down

This file was deleted.

19 changes: 8 additions & 11 deletions src/main/kotlin/io/github/detekt/gradle/extensions/DetektReport.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.github.detekt.gradle.extensions

import java.io.File

class DetektReport(val type: DetektReportType) {

var enabled: Boolean? = null

var destination: File? = null

override fun toString(): String {
return "DetektReport(type='$type', enabled=$enabled, destination=$destination)"
}
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import javax.inject.Inject

open class DetektReport @Inject constructor(val name: String, objects: ObjectFactory) {
val enabled: Property<Boolean> = objects.property(Boolean::class.java)
val destination: RegularFileProperty = objects.fileProperty()
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.detekt.gradle.extensions

import org.gradle.api.Project

open class KotlinCompileTaskDetektExtension(project: Project) {
val reports = project.container(DetektReport::class.java)

init {
reports.create("xml")
reports.create("txt")
reports.create("html")
}

fun getXml() = reports.getByName("xml")
fun getHtml() = reports.getByName("html")
fun getTxt() = reports.getByName("txt")
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.github.detekt.gradle.extensions

import org.gradle.api.Action
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.quality.CodeQualityExtension
import org.gradle.api.provider.SetProperty
import java.io.File
import javax.inject.Inject

open class DetektExtension constructor(@Inject val objects: ObjectFactory) : CodeQualityExtension() {
open class ProjectDetektExtension constructor(@Inject val objects: ObjectFactory) : CodeQualityExtension() {

var isEnabled: Boolean = true
var baseline: File? = null
Expand All @@ -29,8 +28,4 @@ open class DetektExtension constructor(@Inject val objects: ObjectFactory) : Cod
set(value) {
isIgnoreFailures = value
}

val reports = DetektReports()

fun reports(configure: Action<DetektReports>) = configure.execute(reports)
}

0 comments on commit 3358fa6

Please sign in to comment.