From dc47027a5b601f9c0b8f3ce9b890eaab39438806 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 14 Nov 2023 14:01:20 +0300 Subject: [PATCH 1/5] SourceTask in Gradle plugin ### What's done: - migrated to SourceTask - removed inputs It closes #1026 --- .../plugin/gradle/DiktatJavaExecTaskBase.kt | 9 ---- .../plugin/gradle/tasks/DiktatCheckTask.kt | 3 +- .../plugin/gradle/tasks/DiktatFixTask.kt | 3 +- .../plugin/gradle/tasks/DiktatTaskBase.kt | 51 +++---------------- .../gradle/tasks/SarifReportMergeTask.kt | 4 +- 5 files changed, 11 insertions(+), 59 deletions(-) delete mode 100644 diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt deleted file mode 100644 index 25d1093126..0000000000 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskBase.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.saveourtool.diktat.plugin.gradle - -import org.gradle.api.Task - -/** - * An interface with old name for base class for backward compatibility in plugin configuration - */ -@Deprecated("will be removed in 2.x") -interface DiktatJavaExecTaskBase : Task diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt index 6f656825a8..b3d04b4496 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt @@ -15,8 +15,7 @@ import javax.inject.Inject */ abstract class DiktatCheckTask @Inject constructor( extension: DiktatExtension, - inputs: PatternFilterable -) : DiktatTaskBase(extension, inputs) { +) : DiktatTaskBase(extension) { override fun doRun( runner: DiktatRunner, args: DiktatRunnerArguments diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt index 0e7fdc4e4e..3498494bab 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt @@ -15,8 +15,7 @@ import javax.inject.Inject */ abstract class DiktatFixTask @Inject constructor( extension: DiktatExtension, - inputs: PatternFilterable -) : DiktatTaskBase(extension, inputs) { +) : DiktatTaskBase(extension) { override fun doRun( runner: DiktatRunner, args: DiktatRunnerArguments diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt index dd2e7d9e74..87c5e88ccf 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt @@ -15,58 +15,22 @@ import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl import generated.DIKTAT_VERSION import generated.KTLINT_VERSION -import org.gradle.api.DefaultTask import org.gradle.api.GradleException -import org.gradle.api.file.FileCollection -import org.gradle.api.tasks.IgnoreEmptyDirectories -import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal -import org.gradle.api.tasks.PathSensitive -import org.gradle.api.tasks.PathSensitivity -import org.gradle.api.tasks.SkipWhenEmpty +import org.gradle.api.tasks.SourceTask import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.VerificationTask -import org.gradle.api.tasks.util.PatternFilterable import java.nio.file.Path /** * A base task to run `diktat` * - * @param inputs * @property extension */ -@Suppress("WRONG_NEWLINES", "Deprecation") abstract class DiktatTaskBase( @get:Internal internal val extension: DiktatExtension, - private val inputs: PatternFilterable -) : DefaultTask(), VerificationTask, com.saveourtool.diktat.plugin.gradle.DiktatJavaExecTaskBase { - /** - * Files that will be analyzed by diktat - */ - @get:IgnoreEmptyDirectories - @get:SkipWhenEmpty - @get:PathSensitive(PathSensitivity.RELATIVE) - @get:InputFiles - val actualInputs: FileCollection by lazy { - if (inputs.includes.isEmpty() && inputs.excludes.isEmpty()) { - inputs.include("src/**/*.kt") - } - project.objects.fileCollection().from( - project.fileTree("${project.projectDir}").apply { - exclude("${project.buildDir}") - } - .matching(inputs) - ) - } - - /** - * Whether diktat should be executed - */ - @get:Internal - internal val shouldRun: Boolean by lazy { - !actualInputs.isEmpty - } +) : SourceTask(), VerificationTask { private val diktatRunnerFactory by lazy { DiktatRunnerFactory( diktatRuleConfigReader = DiktatRuleConfigReaderImpl(), @@ -80,7 +44,7 @@ abstract class DiktatTaskBase( DiktatRunnerArguments( configFile = extension.diktatConfigFile.toPath(), sourceRootDir = project.projectDir.toPath(), - files = actualInputs.files.map { it.toPath() }, + files = source.files.map { it.toPath() }, baselineFile = extension.baseline?.let { project.file(it).toPath() }, reporterType = project.getReporterType(extension), reporterOutput = project.getOutputFile(extension)?.outputStream(), @@ -106,6 +70,9 @@ abstract class DiktatTaskBase( init { ignoreFailures = extension.ignoreFailures + if (patternSet.includes.isEmpty() && patternSet.excludes.isEmpty()) { + patternSet.include("src/**/*.kt") + } } /** @@ -118,11 +85,7 @@ abstract class DiktatTaskBase( if (extension.debug) { project.logger.lifecycle("Running diktat $DIKTAT_VERSION with ktlint $KTLINT_VERSION") } - if (!shouldRun) { - /* - If ktlint receives empty patterns, it implicitly uses **/*.kt, **/*.kts instead. - This can lead to diktat analyzing gradle buildscripts and so on. We want to prevent it. - */ + if (source.isEmpty) { project.logger.warn("Inputs for $name do not exist, will not run diktat") project.logger.info("Skipping diktat execution") } else { diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/SarifReportMergeTask.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/SarifReportMergeTask.kt index 7991df9f01..c108fb395d 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/SarifReportMergeTask.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/SarifReportMergeTask.kt @@ -96,7 +96,7 @@ internal fun Project.configureMergeReportsTask(diktatExtension: DiktatExtension) reportMergeTask.shouldRunAfter(tasks.withType(DiktatTaskBase::class.java)) } } - tasks.withType(DiktatTaskBase::class.java).configureEach { diktatJavaExecTaskBase -> - diktatJavaExecTaskBase.finalizedBy(rootProject.tasks.withType(SarifReportMergeTask::class.java)) + tasks.withType(DiktatTaskBase::class.java).configureEach { diktatTaskBase -> + diktatTaskBase.finalizedBy(rootProject.tasks.withType(SarifReportMergeTask::class.java)) } } From 18e10854fa89562b4764cee80f4f08e91ac3e3a5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 14 Nov 2023 14:15:59 +0300 Subject: [PATCH 2/5] removed patternSet as argument --- .../diktat/plugin/gradle/DiktatExtension.kt | 15 +-------------- .../diktat/plugin/gradle/DiktatGradlePlugin.kt | 6 ++---- .../diktat/plugin/gradle/tasks/DiktatCheckTask.kt | 4 +--- .../diktat/plugin/gradle/tasks/DiktatFixTask.kt | 6 +----- 4 files changed, 5 insertions(+), 26 deletions(-) diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt index a51ed2152f..f9a135418c 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt @@ -9,12 +9,8 @@ import java.io.File /** * An extension to configure diktat in build.gradle(.kts) file - * - * @param patternSet */ -open class DiktatExtension( - private val patternSet: PatternSet -) { +open class DiktatExtension { /** * Boolean flag to support `ignoreFailures` property of [VerificationTask]. */ @@ -53,13 +49,4 @@ open class DiktatExtension( @get:InputFile @get:PathSensitive(PathSensitivity.RELATIVE) lateinit var diktatConfigFile: File - - /** - * Configure input files for diktat task - * - * @param action configuration lambda for `PatternFilterable` - */ - fun inputs(action: PatternFilterable.() -> Unit) { - action(patternSet) - } } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt index 8ed30418fe..7d34b3249b 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -17,17 +17,15 @@ class DiktatGradlePlugin : Plugin { */ @Suppress("TOO_LONG_FUNCTION") override fun apply(project: Project) { - val patternSet = PatternSet() val diktatExtension = project.extensions.create( DIKTAT_EXTENSION, DiktatExtension::class.java, - patternSet ).apply { diktatConfigFile = project.rootProject.file("diktat-analysis.yml") } - project.registerDiktatCheckTask(diktatExtension, patternSet) - project.registerDiktatFixTask(diktatExtension, patternSet) + project.registerDiktatCheckTask(diktatExtension) + project.registerDiktatFixTask(diktatExtension) project.configureMergeReportsTask(diktatExtension) } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt index b3d04b4496..2e83f345bc 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatCheckTask.kt @@ -6,7 +6,6 @@ import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin import org.gradle.api.Project import org.gradle.api.tasks.TaskProvider -import org.gradle.api.tasks.util.PatternFilterable import org.gradle.api.tasks.util.PatternSet import javax.inject.Inject @@ -29,11 +28,10 @@ abstract class DiktatCheckTask @Inject constructor( */ fun Project.registerDiktatCheckTask( diktatExtension: DiktatExtension, - patternSet: PatternSet ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_CHECK_TASK, DiktatCheckTask::class.java, - diktatExtension, patternSet + diktatExtension, ) } } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt index 3498494bab..3671f2b5a6 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatFixTask.kt @@ -6,8 +6,6 @@ import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin import org.gradle.api.Project import org.gradle.api.tasks.TaskProvider -import org.gradle.api.tasks.util.PatternFilterable -import org.gradle.api.tasks.util.PatternSet import javax.inject.Inject /** @@ -26,16 +24,14 @@ abstract class DiktatFixTask @Inject constructor( companion object { /** * @param diktatExtension [DiktatExtension] with some values for task configuration - * @param patternSet [PatternSet] to discover files for diktat fix * @return a [TaskProvider] */ fun Project.registerDiktatFixTask( diktatExtension: DiktatExtension, - patternSet: PatternSet ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_FIX_TASK, DiktatFixTask::class.java, - diktatExtension, patternSet + diktatExtension, ) } } From 42567abd0968da334d52b3c3d80051c882682fbd Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 14 Nov 2023 15:54:59 +0300 Subject: [PATCH 3/5] removed debug --- .../diktat/plugin/gradle/DiktatExtension.kt | 36 ++++++++++--------- .../plugin/gradle/DiktatGradlePlugin.kt | 3 +- .../saveourtool/diktat/plugin/gradle/Utils.kt | 30 +++++++++------- .../plugin/gradle/tasks/DiktatTaskBase.kt | 20 ++++++----- 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt index f9a135418c..b764205ebb 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt @@ -1,46 +1,43 @@ package com.saveourtool.diktat.plugin.gradle +import org.gradle.api.Action +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.Nested import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.util.PatternFilterable -import org.gradle.api.tasks.util.PatternSet -import java.io.File /** * An extension to configure diktat in build.gradle(.kts) file */ -open class DiktatExtension { +abstract class DiktatExtension { /** - * Boolean flag to support `ignoreFailures` property of [VerificationTask]. + * @return boolean flag to support `ignoreFailures` property of [VerificationTask]. */ - var ignoreFailures: Boolean = false + abstract fun getIgnoreFailures(): Property /** - * Flag that indicates whether to turn debug logging on + * @return Property that will be used if you need to publish the report to GitHub */ - var debug = false - - /** - * Property that will be used if you need to publish the report to GitHub - */ - var githubActions = false + abstract fun getGithubActions(): Property // = false /** * Type of the reporter to use */ - var reporter: String = "" + abstract fun getReporter(): Property // = "" /** * Destination for reporter. If empty, will write to stdout. */ - var output: String = "" + abstract fun getOutput(): Property // = "" /** - * Baseline file, containing a list of errors that will be ignored. + * @return Baseline file, containing a list of errors that will be ignored. * If this file doesn't exist, it will be created on the first invocation. */ - var baseline: String? = null + abstract fun getBaseline(): Property // null /** * Path to diktat yml config file. Can be either absolute or relative to project's root directory. @@ -48,5 +45,10 @@ open class DiktatExtension { */ @get:InputFile @get:PathSensitive(PathSensitivity.RELATIVE) - lateinit var diktatConfigFile: File + abstract val diktatConfigFile: RegularFileProperty + + @Nested + abstract fun getInputs(): PatternFilterable + + fun inputs(action: Action) = action.execute(getInputs()) } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt index 7d34b3249b..bebe2a6c70 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatGradlePlugin.kt @@ -5,7 +5,6 @@ import com.saveourtool.diktat.plugin.gradle.tasks.DiktatFixTask.Companion.regist import com.saveourtool.diktat.plugin.gradle.tasks.configureMergeReportsTask import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.tasks.util.PatternSet /** * Plugin that configures diktat and registers tasks to run diktat @@ -21,7 +20,7 @@ class DiktatGradlePlugin : Plugin { DIKTAT_EXTENSION, DiktatExtension::class.java, ).apply { - diktatConfigFile = project.rootProject.file("diktat-analysis.yml") + diktatConfigFile.set(project.rootProject.file("diktat-analysis.yml")) } project.registerDiktatCheckTask(diktatExtension) diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt index 57ef395c6c..da79f8591c 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt @@ -47,11 +47,12 @@ fun Any.closureOf(action: T.() -> Unit): Closure = * @return CLI flag as string */ fun Project.getReporterType(diktatExtension: DiktatExtension): String { - val name = diktatExtension.reporter.trim() + val reporter = diktatExtension.getReporter().getOrElse("") + val name = reporter val validReporters = listOf("sarif", "plain", "json", "html") val reporterType = when { - diktatExtension.githubActions -> { - if (diktatExtension.reporter.isNotEmpty()) { + diktatExtension.getGithubActions().getOrElse(false) -> { + if (reporter.isNotEmpty()) { logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used") } "sarif" @@ -77,16 +78,19 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { * @param diktatExtension extension of type [DiktatExtension] * @return destination [File] or null if stdout is used */ -internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? = when { - diktatExtension.githubActions -> project.layout.buildDirectory - .file("reports/diktat/diktat.sarif") - .get() - .asFile - .also { - Files.createDirectories(it.parentFile.toPath()) - } - diktatExtension.output.isNotEmpty() -> file(diktatExtension.output) - else -> null +internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? { + val output by lazy { diktatExtension.getOutput().getOrElse("") } + return when { + diktatExtension.getGithubActions().getOrElse(false) -> project.layout.buildDirectory + .file("reports/diktat/diktat.sarif") + .get() + .asFile + .also { + Files.createDirectories(it.parentFile.toPath()) + } + output.isNotEmpty() -> file(output) + else -> null + } } /** diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt index 87c5e88ccf..cd50f3a594 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt @@ -14,7 +14,6 @@ import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl import generated.DIKTAT_VERSION -import generated.KTLINT_VERSION import org.gradle.api.GradleException import org.gradle.api.tasks.Internal import org.gradle.api.tasks.SourceTask @@ -42,10 +41,10 @@ abstract class DiktatTaskBase( } private val diktatRunnerArguments by lazy { DiktatRunnerArguments( - configFile = extension.diktatConfigFile.toPath(), + configFile = extension.diktatConfigFile.get().asFile.toPath(), sourceRootDir = project.projectDir.toPath(), files = source.files.map { it.toPath() }, - baselineFile = extension.baseline?.let { project.file(it).toPath() }, + baselineFile = extension.getBaseline().map { project.file(it).toPath() }.orNull, reporterType = project.getReporterType(extension), reporterOutput = project.getOutputFile(extension)?.outputStream(), loggingListener = object : DiktatProcessorListener { @@ -69,9 +68,14 @@ abstract class DiktatTaskBase( } init { - ignoreFailures = extension.ignoreFailures - if (patternSet.includes.isEmpty() && patternSet.excludes.isEmpty()) { - patternSet.include("src/**/*.kt") + ignoreFailures = extension.getIgnoreFailures().getOrElse(false) + extension.getInputs().run { + if (includes.isEmpty() && excludes.isEmpty()) { + patternSet.include("src/**/*.kt") + } else { + patternSet.setIncludes(includes) + patternSet.setExcludes(excludes) + } } } @@ -82,9 +86,7 @@ abstract class DiktatTaskBase( */ @TaskAction fun run() { - if (extension.debug) { - project.logger.lifecycle("Running diktat $DIKTAT_VERSION with ktlint $KTLINT_VERSION") - } + project.logger.lifecycle("Running diktat $DIKTAT_VERSION") if (source.isEmpty) { project.logger.warn("Inputs for $name do not exist, will not run diktat") project.logger.info("Skipping diktat execution") From 2053375f81e7bfdf1ac8cbfb4e73c8db9bfe820b Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 14 Nov 2023 16:01:23 +0300 Subject: [PATCH 4/5] val instead of fun --- .../com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt | 2 +- .../main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt index b764205ebb..458c396488 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt @@ -21,7 +21,7 @@ abstract class DiktatExtension { /** * @return Property that will be used if you need to publish the report to GitHub */ - abstract fun getGithubActions(): Property // = false + abstract val githubActions: Property // = false /** * Type of the reporter to use diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt index da79f8591c..b4c9e10bdd 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt @@ -51,7 +51,7 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { val name = reporter val validReporters = listOf("sarif", "plain", "json", "html") val reporterType = when { - diktatExtension.getGithubActions().getOrElse(false) -> { + diktatExtension.githubActions.getOrElse(false) -> { if (reporter.isNotEmpty()) { logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used") } @@ -81,7 +81,7 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? { val output by lazy { diktatExtension.getOutput().getOrElse("") } return when { - diktatExtension.getGithubActions().getOrElse(false) -> project.layout.buildDirectory + diktatExtension.githubActions.getOrElse(false) -> project.layout.buildDirectory .file("reports/diktat/diktat.sarif") .get() .asFile From 2f373664396f26d46b78bf9ad621e3f8686a6c05 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 14 Nov 2023 16:17:14 +0300 Subject: [PATCH 5/5] all vals in extension --- .../diktat/plugin/gradle/DiktatExtension.kt | 23 ++++++++---- .../saveourtool/diktat/plugin/gradle/Utils.kt | 35 ++++++++----------- .../plugin/gradle/tasks/DiktatTaskBase.kt | 4 +-- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt index 458c396488..5bb0cbd070 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatExtension.kt @@ -7,6 +7,7 @@ import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Nested import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity +import org.gradle.api.tasks.VerificationTask import org.gradle.api.tasks.util.PatternFilterable /** @@ -14,30 +15,30 @@ import org.gradle.api.tasks.util.PatternFilterable */ abstract class DiktatExtension { /** - * @return boolean flag to support `ignoreFailures` property of [VerificationTask]. + * Boolean flag to support `ignoreFailures` property of [VerificationTask]. */ - abstract fun getIgnoreFailures(): Property + abstract val ignoreFailures: Property /** - * @return Property that will be used if you need to publish the report to GitHub + * Property that will be used if you need to publish the report to GitHub */ abstract val githubActions: Property // = false /** * Type of the reporter to use */ - abstract fun getReporter(): Property // = "" + abstract val reporter: Property /** * Destination for reporter. If empty, will write to stdout. */ - abstract fun getOutput(): Property // = "" + abstract val output: RegularFileProperty /** - * @return Baseline file, containing a list of errors that will be ignored. + * Baseline file, containing a list of errors that will be ignored. * If this file doesn't exist, it will be created on the first invocation. */ - abstract fun getBaseline(): Property // null + abstract val baseline: RegularFileProperty /** * Path to diktat yml config file. Can be either absolute or relative to project's root directory. @@ -47,8 +48,16 @@ abstract class DiktatExtension { @get:PathSensitive(PathSensitivity.RELATIVE) abstract val diktatConfigFile: RegularFileProperty + /** + * @return [PatternFilterable] to configure input files for diktat task + */ @Nested abstract fun getInputs(): PatternFilterable + /** + * Configure input files for diktat task + * + * @param action configuration lambda for [PatternFilterable] + */ fun inputs(action: Action) = action.execute(getInputs()) } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt index b4c9e10bdd..c20a776b31 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/Utils.kt @@ -47,25 +47,24 @@ fun Any.closureOf(action: T.() -> Unit): Closure = * @return CLI flag as string */ fun Project.getReporterType(diktatExtension: DiktatExtension): String { - val reporter = diktatExtension.getReporter().getOrElse("") - val name = reporter + val reporter = diktatExtension.reporter.getOrElse("") val validReporters = listOf("sarif", "plain", "json", "html") val reporterType = when { diktatExtension.githubActions.getOrElse(false) -> { if (reporter.isNotEmpty()) { - logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used") + logger.warn("`diktat.githubActions` is set to true, so custom reporter [$reporter] will be ignored and SARIF reporter will be used") } "sarif" } - name.isEmpty() -> { + reporter.isEmpty() -> { logger.info("Reporter name was not set. Using 'plain' reporter") "plain" } - name !in validReporters -> { - logger.warn("Reporter name is invalid (provided value: [$name]). Falling back to 'plain' reporter") + reporter !in validReporters -> { + logger.warn("Reporter name is invalid (provided value: [$reporter]). Falling back to 'plain' reporter") "plain" } - else -> name + else -> reporter } return reporterType @@ -78,19 +77,15 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { * @param diktatExtension extension of type [DiktatExtension] * @return destination [File] or null if stdout is used */ -internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? { - val output by lazy { diktatExtension.getOutput().getOrElse("") } - return when { - diktatExtension.githubActions.getOrElse(false) -> project.layout.buildDirectory - .file("reports/diktat/diktat.sarif") - .get() - .asFile - .also { - Files.createDirectories(it.parentFile.toPath()) - } - output.isNotEmpty() -> file(output) - else -> null - } +internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? = when { + diktatExtension.githubActions.getOrElse(false) -> project.layout.buildDirectory + .file("reports/diktat/diktat.sarif") + .get() + .asFile + .also { + Files.createDirectories(it.parentFile.toPath()) + } + else -> diktatExtension.output.map { it.asFile }.orNull } /** diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt index cd50f3a594..b96d5576c5 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt @@ -44,7 +44,7 @@ abstract class DiktatTaskBase( configFile = extension.diktatConfigFile.get().asFile.toPath(), sourceRootDir = project.projectDir.toPath(), files = source.files.map { it.toPath() }, - baselineFile = extension.getBaseline().map { project.file(it).toPath() }.orNull, + baselineFile = extension.baseline.map { it.asFile.toPath() }.orNull, reporterType = project.getReporterType(extension), reporterOutput = project.getOutputFile(extension)?.outputStream(), loggingListener = object : DiktatProcessorListener { @@ -68,7 +68,7 @@ abstract class DiktatTaskBase( } init { - ignoreFailures = extension.getIgnoreFailures().getOrElse(false) + ignoreFailures = extension.ignoreFailures.getOrElse(false) extension.getInputs().run { if (includes.isEmpty() && excludes.isEmpty()) { patternSet.include("src/**/*.kt")