Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detekt: Add baseline feature and rewrite the internals. #138

Merged
merged 1 commit into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.{kt,kts}]
indent_size=2
55 changes: 51 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlinVersion = '1.2.41'
ext.kotlinVersion = '1.2.50'

repositories {
mavenCentral()
Expand All @@ -8,8 +8,10 @@ buildscript {
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath 'com.gradle.publish:plugin-publish-plugin:0.9.10'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.19.0'
classpath 'com.vanniktech:gradle-code-quality-tools-plugin:0.10.0'
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.11.0'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.2.0'
}
Expand All @@ -18,11 +20,43 @@ buildscript {
apply plugin: 'groovy'
apply plugin: 'java-library'
apply plugin: 'java-gradle-plugin'
apply plugin: 'kotlin'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'com.gradle.plugin-publish'
apply plugin: 'com.vanniktech.code.quality.tools'
apply plugin: 'com.vanniktech.android.junit.jacoco'
apply plugin: 'com.gradle.plugin-publish'
apply plugin: "com.vanniktech.maven.publish"

// Workaround for having both Groovy + Kotlin.
compileGroovy {
dependsOn tasks.getByPath('compileKotlin')
classpath += files(compileKotlin.destinationDir)
}

codeQualityTools {
ktlint {
toolVersion = '0.23.1'
}
detekt {
toolVersion = '1.0.0.RC7'
}
findbugs {
enabled = false
}
pmd {
enabled = false
}
checkstyle {
enabled = false
}
cpd {
enabled = false
}
errorProne {
enabled = false
}
}

gradlePlugin {
plugins {
codeQualityToolsPlugin {
Expand All @@ -38,6 +72,16 @@ repositories {
gradlePluginPortal()
}

configurations {
fixtureClasspath
}

// Append any extra dependencies to the test fixtures via a custom configuration classpath. This
// allows us to apply additional plugins in a fixture while still leveraging dependency resolution
// and de-duplication semantics.
def metadata = tasks.getByName('pluginUnderTestMetadata')
metadata.setPluginClasspath(metadata.getPluginClasspath() + configurations.fixtureClasspath)

dependencies {
api gradleApi()
api localGroovy()
Expand All @@ -48,7 +92,10 @@ dependencies {

testImplementation 'com.android.tools.build:gradle:3.1.2'
testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.10.0'
testImplementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

fixtureClasspath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}

sourceCompatibility = JavaVersion.VERSION_1_7
Expand All @@ -67,7 +114,7 @@ pluginBundle {
}
}

task wrapper(type: Wrapper) {
wrapper {
gradleVersion = '4.8'
distributionType = Wrapper.DistributionType.ALL
}
19 changes: 19 additions & 0 deletions code_quality_tools/detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
failFast: true

potential-bugs:
UnsafeCast:
active: false # We know what we're doing.
LateinitUsage:
active: false # We know what we're doing.

style:
TopLevelPropertyNaming:
active: false
MaxLineLength:
active: false

comments:
UndocumentedPublicClass:
active: false
UndocumentedPublicFunction:
active: false
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CodeQualityToolsPlugin implements Plugin<Project> {
rootProject.codeQualityTools.extensions.create('pmd', CodeQualityToolsPluginExtension.Pmd)
rootProject.codeQualityTools.extensions.create('lint', CodeQualityToolsPluginExtension.Lint)
rootProject.codeQualityTools.extensions.create('ktlint', CodeQualityToolsPluginExtension.Ktlint)
rootProject.codeQualityTools.extensions.create('detekt', CodeQualityToolsPluginExtension.Detekt)
rootProject.codeQualityTools.extensions.create('detekt', DetektExtension)
rootProject.codeQualityTools.extensions.create('cpd', CodeQualityToolsPluginExtension.Cpd)
rootProject.codeQualityTools.extensions.create('errorProne', CodeQualityToolsPluginExtension.ErrorProne)

Expand Down Expand Up @@ -326,33 +326,22 @@ class CodeQualityToolsPlugin implements Plugin<Project> {
def isDetektSupported = isKotlinProject(project)

if (isNotIgnored && isEnabled && isDetektSupported) {
project.configurations {
detektCheck
def task = project.tasks.create("detektCheck", DetektCheckTask)
task.version = extension.detekt.toolVersion
task.group = GROUP_VERIFICATION
task.description = "Runs detekt."
task.outputDirectory = new File(project.buildDir, "reports/detekt/")
task.configFile = rootProject.file(extension.detekt.config)
task.inputs.files(project.fileTree(dir: ".", include: "**/*.kt"))

task.inputs.property("baseline-file-exists", false)

if (extension.detekt.baselineFileName != null) {
def file = project.file(extension.detekt.baselineFileName)
task.baselineFilePath = file.toString()
task.inputs.property("baseline-file-exists", file.exists())
}

project.dependencies {
detektCheck "io.gitlab.arturbosch.detekt:detekt-cli:${extension.detekt.toolVersion}"
}

def output = new File(project.buildDir, "reports/detekt/")

project.task('detektCheck', type: JavaExec) {
def configFile = rootProject.file(extension.detekt.config)
inputs.files(project.fileTree(dir: "src", include: "**/*.kt"), configFile)
outputs.dir(output.toString())
group = GROUP_VERIFICATION
description = 'Runs detekt.'
main = 'io.gitlab.arturbosch.detekt.cli.Main'
classpath = project.configurations.detektCheck
args = [
"--config", configFile,
"--input", project.file("."),
"--output", output
]
}

project.check.dependsOn 'detektCheck'

return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,6 @@ class CodeQualityToolsPluginExtension {
String toolVersion = '0.14.0'
}

static class Detekt {
/**
* ability to enable or disable only detekt for every subproject that is not ignored
* @since 0.6.0
*/
boolean enabled = true

/** @since 0.6.0 */
String toolVersion = '1.0.0.RC6'

/** @since 0.6.0 */
String config = 'code_quality_tools/detekt.yml'
}

static class Cpd {
/**
* ability to enable or disable only cpd for every subproject that is not ignored
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.vanniktech.code.quality.tools

import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskExecutionException
import java.io.File

open class DetektCheckTask : DefaultTask() {
@Input lateinit var version: String

// Ideally this would be an optional input file - https://github.com/gradle/gradle/issues/2016
@Input @Optional var baselineFilePath: String? = null
@InputFile lateinit var configFile: File

@OutputDirectory lateinit var outputDirectory: File

@TaskAction fun run() {
val configuration = project.configurations.create("detekt")

project.dependencies.add("detekt", "io.gitlab.arturbosch.detekt:detekt-cli:$version")

baselineFilePath?.let { file ->
if (!File(file).exists()) {
executeDetekt(configuration, shouldCreateBaseLine = true)
throw TaskExecutionException(this, GradleException("Aborting build since new baseline file was created"))
}
}

executeDetekt(configuration)

project.tasks.getByName("check").dependsOn("detektCheck")
}

private fun executeDetekt(configuration: FileCollection, shouldCreateBaseLine: Boolean = false) {
project.javaexec { task ->
task.main = "io.gitlab.arturbosch.detekt.cli.Main"
task.classpath = configuration
task.args(
"--config", configFile,
"--input", project.file("."),
"--output", outputDirectory
)

if (shouldCreateBaseLine) {
task.args("--create-baseline")
}

baselineFilePath?.let {
task.args("--baseline", it)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vanniktech.code.quality.tools

open class DetektExtension {
/**
* Ability to enable or disable only detekt for every subproject that is not ignored.
* @since 0.6.0
*/
var enabled: Boolean = true

/** @since 0.6.0 */
var toolVersion: String = "1.0.0.RC6"

/** @since 0.6.0 */
var config: String = "code_quality_tools/detekt.yml"

/**
* Optional baseline file. If one is present it will be used in the detektCheck task.
* If this name is specified however the file is not present it will be generated.
* This mirrors the baseline mechanism from Android Lint.
*
* @since 0.11.0
*/
var baselineFileName: String? = null
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package com.vanniktech.code.quality.tools
CodeQualityToolsPluginExtension.Pmd pmd = new CodeQualityToolsPluginExtension.Pmd()
CodeQualityToolsPluginExtension.Lint lint = new CodeQualityToolsPluginExtension.Lint()
CodeQualityToolsPluginExtension.Ktlint ktlint = new CodeQualityToolsPluginExtension.Ktlint()
CodeQualityToolsPluginExtension.Detekt detekt = new CodeQualityToolsPluginExtension.Detekt()
DetektExtension detekt = new DetektExtension()
CodeQualityToolsPluginExtension.Cpd cpd = new CodeQualityToolsPluginExtension.Cpd()
CodeQualityToolsPluginExtension.ErrorProne errorProne = new CodeQualityToolsPluginExtension.ErrorProne()
}