Skip to content

Commit

Permalink
Do not use the project property at execution time - #2811
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch committed Jun 20, 2020
1 parent 44e5a51 commit 21c4018
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ open class Detekt @Inject constructor(
group = LifecycleBasePlugin.VERIFICATION_GROUP
}

private val invoker: DetektInvoker = DetektInvoker.create(project)

@Suppress("DEPRECATION")
@TaskAction
fun check() {
Expand Down Expand Up @@ -216,7 +218,7 @@ open class Detekt @Inject constructor(
CustomReportArgument(reportId, objects.fileProperty().getOrElse { destination })
})

DetektInvoker.create(project).invokeCli(
invoker.invokeCli(
arguments = arguments.toList(),
ignoreFailures = ignoreFailures,
classpath = detektClasspath.plus(pluginClasspath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ open class DetektCreateBaselineTask : SourceTask() {
@get:Optional
val autoCorrect: Property<Boolean> = project.objects.property(Boolean::class.javaObjectType)

private val invoker: DetektInvoker = DetektInvoker.create(project)

@TaskAction
fun baseline() {
val arguments = mutableListOf(
Expand All @@ -92,7 +94,7 @@ open class DetektCreateBaselineTask : SourceTask() {
DisableDefaultRuleSetArgument(disableDefaultRuleSets.getOrElse(false))
)

DetektInvoker.create(project).invokeCli(
invoker.invokeCli(
arguments = arguments.toList(),
ignoreFailures = ignoreFailures.getOrElse(false),
classpath = detektClasspath.plus(pluginClasspath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ open class DetektGenerateConfigTask : DefaultTask() {
@get:Classpath
val detektClasspath = project.configurableFileCollection()

private val invoker: DetektInvoker = DetektInvoker.create(project)
private val configDir = project.mkdir("${project.rootDir}/$CONFIG_DIR_NAME")
private val config = project.files("${configDir.canonicalPath}/$CONFIG_FILE")

@TaskAction
fun generateConfig() {

val configDir = project.mkdir("${project.rootDir}/$CONFIG_DIR_NAME")
val config = project.files("${configDir.canonicalPath}/$CONFIG_FILE")

val arguments = mutableListOf(
GenerateConfigArgument,
ConfigArgument(config)
)

try {
if (config.singleFile.exists()) {
project.logger.warn("Skipping config file generation; file already exists at ${config.singleFile}")
logger.warn("Skipping config file generation; file already exists at ${config.singleFile}")
} else {
DetektInvoker.create(project).invokeCli(arguments.toList(), detektClasspath, name)
invoker.invokeCli(arguments.toList(), detektClasspath, name)
}
} catch (e: IllegalStateException) {
project.logger.error("Unexpected error. Please raise an issue on detekt's issue tracker.", e)
logger.error("Unexpected error. Please raise an issue on detekt's issue tracker.", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.gitlab.arturbosch.detekt.internal.ClassLoaderCache
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.Logger
import java.io.PrintStream
import java.lang.reflect.InvocationTargetException

Expand All @@ -19,7 +20,7 @@ internal interface DetektInvoker {
companion object {
fun create(project: Project): DetektInvoker =
if (project.isDryRunEnabled()) {
DryRunInvoker(project)
DryRunInvoker(project.logger)
} else {
DefaultCliInvoker()
}
Expand Down Expand Up @@ -64,7 +65,7 @@ private class DefaultCliInvoker : DetektInvoker {
msg != null && "Build failed with" in msg && "issues" in msg
}

private class DryRunInvoker(private val project: Project) : DetektInvoker {
private class DryRunInvoker(private val logger: Logger) : DetektInvoker {

override fun invokeCli(
arguments: List<CliArgument>,
Expand All @@ -73,10 +74,10 @@ private class DryRunInvoker(private val project: Project) : DetektInvoker {
ignoreFailures: Boolean
) {
val cliArguments = arguments.flatMap(CliArgument::toArgument)
project.logger.info("Invoking detekt with dry-run.")
project.logger.info("Task: $taskName")
project.logger.info("Arguments: ${cliArguments.joinToString(" ")}")
project.logger.info("Classpath: ${classpath.files}")
project.logger.info("Ignore failures: $ignoreFailures")
logger.info("Invoking detekt with dry-run.")
logger.info("Task: $taskName")
logger.info("Arguments: ${cliArguments.joinToString(" ")}")
logger.info("Classpath: ${classpath.files}")
logger.info("Ignore failures: $ignoreFailures")
}
}

0 comments on commit 21c4018

Please sign in to comment.