From 785220142f769053732b593bf9327c21b656f7b7 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 15 Nov 2023 15:17:45 +0300 Subject: [PATCH 01/17] Supported GradleActions reporter as additional reporter - supported Gradle action reporter as additional reporter It closes #1733 --- .../com/saveourtool/diktat/DiktatRunner.kt | 1 + .../diktat/DiktatRunnerArguments.kt | 5 +++ .../saveourtool/diktat/plugin/gradle/Utils.kt | 44 ------------------- .../plugin/gradle/tasks/DiktatTaskBase.kt | 40 ++++++++++++----- .../diktat/plugin/maven/DiktatBaseMojo.kt | 17 ++++--- 5 files changed, 47 insertions(+), 60 deletions(-) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt index de2503758c..77cea9e0de 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt @@ -36,6 +36,7 @@ data class DiktatRunner( DiktatProcessorListener( args.loggingListener, diktatReporter.skipKnownErrors(diktatBaseline), + args.additionalReporter.skipKnownErrors(diktatBaseline), diktatBaselineGenerator, errorCounter.countErrorsAsProcessorListener() ), diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt index 727f74f7ef..0350f8c874 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt @@ -1,6 +1,7 @@ package com.saveourtool.diktat import com.saveourtool.diktat.api.DiktatProcessorListener +import com.saveourtool.diktat.api.DiktatReporter import java.io.InputStream import java.io.OutputStream import java.nio.file.Path @@ -17,6 +18,7 @@ import kotlin.io.path.inputStream * @property reporterOutput output for reporter * @property groupByFileInPlain a flag `groupByFile` which is applicable for plain reporter only, **null** by default * @property colorNameInPlain a color name which is applicable for plain reporter only, **null** by default + * @property additionalReporter an additional report, [DiktatReporter.empty] by default * @property loggingListener listener to log diktat runner phases, [DiktatProcessorListener.empty] by default */ data class DiktatRunnerArguments( @@ -28,6 +30,7 @@ data class DiktatRunnerArguments( val reporterOutput: OutputStream?, val groupByFileInPlain: Boolean? = null, val colorNameInPlain: String? = null, + val additionalReporter: DiktatReporter = DiktatReporter.empty, val loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty, ) { constructor( @@ -39,6 +42,7 @@ data class DiktatRunnerArguments( reporterOutput: OutputStream?, groupByFileInPlain: Boolean? = null, colorNameInPlain: String? = null, + additionalReporter: DiktatReporter = DiktatReporter.empty, loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty, ) : this( configFile.inputStream(), @@ -49,6 +53,7 @@ data class DiktatRunnerArguments( reporterOutput, groupByFileInPlain, colorNameInPlain, + additionalReporter, loggingListener, ) } 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 609b106787..d187679b62 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 @@ -6,39 +6,8 @@ package com.saveourtool.diktat.plugin.gradle -import groovy.lang.Closure import org.gradle.api.Project import java.io.File -import java.nio.file.Files -import java.nio.file.Path - -@Suppress( - "MISSING_KDOC_TOP_LEVEL", - "MISSING_KDOC_CLASS_ELEMENTS", - "KDOC_NO_CONSTRUCTOR_PROPERTY", - "MISSING_KDOC_ON_FUNCTION", - "KDOC_WITHOUT_PARAM_TAG", - "KDOC_WITHOUT_RETURN_TAG" -) -class KotlinClosure1( - val function: T.() -> V?, - owner: Any? = null, - thisObject: Any? = null -) : Closure(owner, thisObject) { - @Suppress("unused") // to be called dynamically by Groovy - fun doCall(it: T): V? = it.function() -} - -// These two are copy-pasted from `kotlin-dsl` plugin's groovy interop. -// Because `kotlin-dsl` depends on kotlin 1.3.x. -@Suppress( - "MISSING_KDOC_TOP_LEVEL", - "MISSING_KDOC_ON_FUNCTION", - "KDOC_WITHOUT_PARAM_TAG", - "KDOC_WITHOUT_RETURN_TAG" -) -fun Any.closureOf(action: T.() -> Unit): Closure = - KotlinClosure1(action, this, this) /** * @param diktatExtension @@ -61,12 +30,6 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { val name = diktatExtension.reporter.trim() val validReporters = listOf("sarif", "plain", "json", "html") val reporterType = when { - diktatExtension.githubActions -> { - if (diktatExtension.reporter.isNotEmpty()) { - logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used") - } - "sarif" - } name.isEmpty() -> { logger.info("Reporter name was not set. Using 'plain' reporter") "plain" @@ -89,13 +52,6 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { * @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 } 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 8d7148d7d0..df9713b3cd 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 @@ -4,6 +4,7 @@ import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory import com.saveourtool.diktat.api.DiktatProcessorListener +import com.saveourtool.diktat.api.DiktatReporter import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl @@ -28,6 +29,7 @@ import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.VerificationTask import org.gradle.api.tasks.util.PatternFilterable +import java.nio.file.Files import java.nio.file.Path @@ -68,16 +70,41 @@ abstract class DiktatTaskBase( internal val shouldRun: Boolean by lazy { !actualInputs.isEmpty } + private val diktatReporterFactory by lazy { + DiktatReporterFactoryImpl() + } private val diktatRunnerFactory by lazy { DiktatRunnerFactory( diktatRuleConfigReader = DiktatRuleConfigReaderImpl(), diktatRuleSetFactory = DiktatRuleSetFactoryImpl(), diktatProcessorFactory = DiktatProcessorFactoryImpl(), diktatBaselineFactory = DiktatBaselineFactoryImpl(), - diktatReporterFactory = DiktatReporterFactoryImpl() + diktatReporterFactory = diktatReporterFactory, ) } private val diktatRunnerArguments by lazy { + val githubActionsReporter = if (extension.githubActions) { + val outputStream = project.layout.buildDirectory + .file("reports/diktat/diktat.sarif") + .get() + .asFile + .also { + Files.createDirectories(it.parentFile.toPath()) + } + .outputStream() + diktatReporterFactory(id = "sarif", outputStream = outputStream, closeOutputStreamAfterAll = true, sourceRootDir = project.projectDir.toPath()) + } else { + DiktatReporter.empty + } + val loggingListener = object : DiktatProcessorListener { + override fun beforeAll(files: Collection) { + project.logger.info("Analyzing {} files with diktat in project {}", files.size, project.name) + project.logger.debug("Analyzing {}", files) + } + override fun before(file: Path) { + project.logger.debug("Checking file {}", file) + } + } DiktatRunnerArguments( configFile = extension.diktatConfigFile.toPath(), sourceRootDir = project.getSourceRootDir(extension), @@ -85,15 +112,8 @@ abstract class DiktatTaskBase( baselineFile = extension.baseline?.let { project.file(it).toPath() }, reporterType = project.getReporterType(extension), reporterOutput = project.getOutputFile(extension)?.outputStream(), - loggingListener = object : DiktatProcessorListener { - override fun beforeAll(files: Collection) { - project.logger.info("Analyzing {} files with diktat in project {}", files.size, project.name) - project.logger.debug("Analyzing {}", files) - } - override fun before(file: Path) { - project.logger.debug("Checking file {}", file) - } - } + additionalReporter = githubActionsReporter, + loggingListener = loggingListener, ) } diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index d02dfdf45e..cc387e2424 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -3,6 +3,7 @@ package com.saveourtool.diktat.plugin.maven import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory +import com.saveourtool.diktat.api.DiktatReporter import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl @@ -107,13 +108,20 @@ abstract class DiktatBaseMojo : AbstractMojo() { ) val sourceRootDir = mavenProject.basedir.parentFile.toPath() + val diktatReporterFactory = DiktatReporterFactoryImpl() val diktatRunnerFactory = DiktatRunnerFactory( diktatRuleConfigReader = DiktatRuleConfigReaderImpl(), diktatRuleSetFactory = DiktatRuleSetFactoryImpl(), diktatProcessorFactory = DiktatProcessorFactoryImpl(), diktatBaselineFactory = DiktatBaselineFactoryImpl(), - diktatReporterFactory = DiktatReporterFactoryImpl() + diktatReporterFactory = diktatReporterFactory, ) + val githubActionsReporter = if (githubActions) { + val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) + diktatReporterFactory(id = "sarif", outputStream, closeOutputStreamAfterAll = true, sourceRootDir) + } else { + DiktatReporter.empty + } val args = DiktatRunnerArguments( configInputStream = configFile.inputStream(), sourceRootDir = sourceRootDir, @@ -121,6 +129,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { baselineFile = baseline?.toPath(), reporterType = getReporterType(), reporterOutput = getReporterOutput(), + additionalReporter = githubActionsReporter, ) val diktatRunner = diktatRunnerFactory(args) val errorCounter = runAction( @@ -132,9 +141,7 @@ abstract class DiktatBaseMojo : AbstractMojo() { } } - private fun getReporterType(): String = if (githubActions) { - "sarif" - } else if (reporter in setOf("sarif", "plain", "json", "html")) { + private fun getReporterType(): String = if (reporter in setOf("sarif", "plain", "json", "html")) { reporter } else { log.warn("Reporter name ${this.reporter} was not specified or is invalid. Falling to 'plain' reporter") @@ -143,8 +150,6 @@ abstract class DiktatBaseMojo : AbstractMojo() { private fun getReporterOutput(): OutputStream? = if (output.isNotBlank()) { FileOutputStream(this.output, false) - } else if (githubActions) { - FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) } else { null } From a82fcbf6d20965745d3d6b6ebd41c419d4dcedd8 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 15 Nov 2023 17:24:47 +0300 Subject: [PATCH 02/17] supported multiple reporters in DiktatRunner --- .../com/saveourtool/diktat/DiktatRunner.kt | 14 +-- .../diktat/DiktatRunnerArguments.kt | 27 +----- .../saveourtool/diktat/DiktatRunnerFactory.kt | 39 ++------ .../diktat/api/DiktatProcessorListener.kt | 8 +- .../diktat/api/DiktatReporterArguments.kt | 94 +++++++++++++++++++ .../diktat/api/DiktatReporterFactory.kt | 48 +++------- .../diktat/cli/DiktatProperties.kt | 31 +++--- .../saveourtool/diktat/plugin/gradle/Utils.kt | 2 +- .../plugin/gradle/tasks/DiktatTaskBase.kt | 20 ++-- .../ktlint/DiktatReporterFactoryImpl.kt | 58 +++++------- .../diktat/plugin/maven/DiktatBaseMojo.kt | 20 ++-- 11 files changed, 201 insertions(+), 160 deletions(-) create mode 100644 diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt index 77cea9e0de..c82eefb88b 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt @@ -1,7 +1,5 @@ package com.saveourtool.diktat -import com.saveourtool.diktat.api.DiktatBaseline -import com.saveourtool.diktat.api.DiktatBaseline.Companion.skipKnownErrors import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.api.DiktatProcessorListener.Companion.countErrorsAsProcessorListener import com.saveourtool.diktat.api.DiktatReporter @@ -15,16 +13,12 @@ private typealias RunAction = (DiktatProcessor, DiktatProcessorListener) -> Unit /** * A runner for diktat on bunch of files using baseline and reporter * - * @param diktatBaselineGenerator * @property diktatProcessor - * @property diktatBaseline * @property diktatReporter */ data class DiktatRunner( - val diktatProcessor: DiktatProcessor, - val diktatBaseline: DiktatBaseline, - private val diktatBaselineGenerator: DiktatProcessorListener, - val diktatReporter: DiktatReporter, + private val diktatProcessor: DiktatProcessor, + private val diktatReporter: DiktatReporter, ) { private fun doRun( args: DiktatRunnerArguments, @@ -35,9 +29,7 @@ data class DiktatRunner( diktatProcessor, DiktatProcessorListener( args.loggingListener, - diktatReporter.skipKnownErrors(diktatBaseline), - args.additionalReporter.skipKnownErrors(diktatBaseline), - diktatBaselineGenerator, + diktatReporter, errorCounter.countErrorsAsProcessorListener() ), ) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt index 0350f8c874..fc4ca0b87f 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt @@ -1,9 +1,8 @@ package com.saveourtool.diktat import com.saveourtool.diktat.api.DiktatProcessorListener -import com.saveourtool.diktat.api.DiktatReporter +import com.saveourtool.diktat.api.DiktatReporterArguments import java.io.InputStream -import java.io.OutputStream import java.nio.file.Path import kotlin.io.path.inputStream @@ -14,11 +13,7 @@ import kotlin.io.path.inputStream * @property sourceRootDir a common root dir for all provided [files] * @property files a collection of files which needs to be fixed * @property baselineFile an optional path to file with baseline - * @property reporterType type of reporter to report the detected errors - * @property reporterOutput output for reporter - * @property groupByFileInPlain a flag `groupByFile` which is applicable for plain reporter only, **null** by default - * @property colorNameInPlain a color name which is applicable for plain reporter only, **null** by default - * @property additionalReporter an additional report, [DiktatReporter.empty] by default + * @property reporterArgsList list of arguments to create reporters to report result * @property loggingListener listener to log diktat runner phases, [DiktatProcessorListener.empty] by default */ data class DiktatRunnerArguments( @@ -26,11 +21,7 @@ data class DiktatRunnerArguments( val sourceRootDir: Path?, val files: Collection, val baselineFile: Path?, - val reporterType: String, - val reporterOutput: OutputStream?, - val groupByFileInPlain: Boolean? = null, - val colorNameInPlain: String? = null, - val additionalReporter: DiktatReporter = DiktatReporter.empty, + val reporterArgsList: List = emptyList(), val loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty, ) { constructor( @@ -38,22 +29,14 @@ data class DiktatRunnerArguments( sourceRootDir: Path?, files: Collection, baselineFile: Path?, - reporterType: String, - reporterOutput: OutputStream?, - groupByFileInPlain: Boolean? = null, - colorNameInPlain: String? = null, - additionalReporter: DiktatReporter = DiktatReporter.empty, + reporterArgsList: List = emptyList(), loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty, ) : this( configFile.inputStream(), sourceRootDir, files, baselineFile, - reporterType, - reporterOutput, - groupByFileInPlain, - colorNameInPlain, - additionalReporter, + reporterArgsList, loggingListener, ) } diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt index bd9477c029..e87f608d6a 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt @@ -1,6 +1,7 @@ package com.saveourtool.diktat import com.saveourtool.diktat.api.DiktatBaseline +import com.saveourtool.diktat.api.DiktatBaseline.Companion.skipKnownErrors import com.saveourtool.diktat.api.DiktatBaselineFactory import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.api.DiktatReporter @@ -9,7 +10,6 @@ import com.saveourtool.diktat.api.DiktatRuleConfig import com.saveourtool.diktat.api.DiktatRuleConfigReader import com.saveourtool.diktat.api.DiktatRuleSet import com.saveourtool.diktat.api.DiktatRuleSetFactory -import java.io.OutputStream import java.nio.file.Path /** @@ -37,16 +37,16 @@ class DiktatRunnerFactory( val diktatRuleSet = diktatRuleSetFactory(diktatRuleConfigs) val processor = diktatProcessorFactory(diktatRuleSet) val (baseline, baselineGenerator) = resolveBaseline(args.baselineFile, args.sourceRootDir) - val reporter = resolveReporter( - args.reporterType, args.reporterOutput, - args.colorNameInPlain, args.groupByFileInPlain, - args.sourceRootDir - ) + + val reporter = args.reporterArgsList.map { + diktatReporterFactory(it) + }.let { + DiktatReporter.union(it) + } + return DiktatRunner( diktatProcessor = processor, - diktatBaseline = baseline, - diktatBaselineGenerator = baselineGenerator, - diktatReporter = reporter, + diktatReporter = DiktatReporter(reporter.skipKnownErrors(baseline), baselineGenerator), ) } @@ -62,25 +62,4 @@ class DiktatRunnerFactory( } ?: DiktatProcessorListener.empty DiktatBaseline.empty to baselineGenerator } - - private fun resolveReporter( - reporterType: String, - reporterOutput: OutputStream?, - colorNameInPlain: String?, - groupByFileInPlain: Boolean?, - sourceRootDir: Path?, - ): DiktatReporter { - val (outputStream, closeOutputStream) = reporterOutput?.let { it to true } ?: (System.`out` to false) - return if (reporterType == diktatReporterFactory.plainId) { - diktatReporterFactory.createPlain(outputStream, closeOutputStream, sourceRootDir, colorNameInPlain, groupByFileInPlain) - } else { - require(colorNameInPlain == null) { - "colorization is applicable only for plain reporter" - } - require(groupByFileInPlain == null) { - "groupByFile is applicable only for plain reporter" - } - diktatReporterFactory(reporterType, outputStream, closeOutputStream, sourceRootDir) - } - } } diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt index 5da4aa2404..39edbad2ad 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt @@ -57,7 +57,7 @@ interface DiktatProcessorListener { * @param listeners * @return a single [DiktatProcessorListener] which uses all provided [listeners] */ - operator fun invoke(vararg listeners: DiktatProcessorListener): DiktatProcessorListener = object : DiktatProcessorListener { + fun union(listeners: Iterable): DiktatProcessorListener = object : DiktatProcessorListener { override fun beforeAll(files: Collection) = listeners.forEach { it.beforeAll(files) } override fun before(file: Path) = listeners.forEach { it.before(file) } override fun onError( @@ -69,6 +69,12 @@ interface DiktatProcessorListener { override fun afterAll() = listeners.forEach(DiktatProcessorListener::afterAll) } + /** + * @param listeners + * @return a single [DiktatProcessorListener] which uses all provided [listeners] + */ + operator fun invoke(vararg listeners: DiktatProcessorListener): DiktatProcessorListener = union(listeners.asIterable()) + /** * @return An implementation of [DiktatProcessorListener] which counts [DiktatError]s */ diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt new file mode 100644 index 0000000000..bbf62195e4 --- /dev/null +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt @@ -0,0 +1,94 @@ +/** + * Contains a base interface and implementations for a container with arguments to create a reporter + */ + +package com.saveourtool.diktat.api + +import java.io.OutputStream +import java.nio.file.Path + +/** + * Arguments to create [DiktatReporter] using [DiktatReporterFactory] + */ +sealed interface DiktatReporterArguments { + /** + * Identifier of [DiktatReporter] which needs to be created + */ + val id: String + + /** + * Output for [DiktatReporter] + */ + val outputStream: OutputStream + + /** + * Should [outputStream] be closed af the end of [DiktatReporter] + */ + val closeOutputStreamAfterAll: Boolean + + /** + * Directory to base source root to report relative paths in [DiktatReporter] + */ + val sourceRootDir: Path? + + companion object { + /** + * @param id ID of [DiktatReporter] + * @param outputStream stdout will be used when it's empty + * @param sourceRootDir a dir to detect relative path for processing files + * @param colorNameInPlain a color name for colorful output which is applicable for plain ([DiktatReporterFactory.PLAIN_ID]) reporter only, `null` means to disable colorization. + * @param groupByFileInPlain a flag `groupByFile` which is applicable for plain ([DiktatReporterFactory.PLAIN_ID]) reporter only. + * @return created [DiktatReporter] + */ + operator fun invoke( + id: String, + outputStream: OutputStream?, + sourceRootDir: Path?, + colorNameInPlain: String? = null, + groupByFileInPlain: Boolean? = null, + ): DiktatReporterArguments { + val (outputStreamOrStdout, closeOutputStreamAfterAll) = outputStream?.let { it to true } ?: (System.`out` to false) + return if (id == DiktatReporterFactory.PLAIN_ID) { + PlainDiktatReporterArguments( + outputStreamOrStdout, closeOutputStreamAfterAll, sourceRootDir, colorNameInPlain, groupByFileInPlain + ) + } else { + require(colorNameInPlain == null) { + "colorization is applicable only for plain reporter" + } + require(groupByFileInPlain == null) { + "groupByFile is applicable only for plain reporter" + } + DiktatReporterArgumentsImpl( + id, outputStreamOrStdout, closeOutputStreamAfterAll, sourceRootDir + ) + } + } + } +} + +private data class DiktatReporterArgumentsImpl( + override val id: String, + override val outputStream: OutputStream, + override val closeOutputStreamAfterAll: Boolean, + override val sourceRootDir: Path?, +) : DiktatReporterArguments + +/** + * Implementation of [DiktatReporterArguments] for [DiktatReporterFactory.PLAIN_ID] + * + * @property outputStream + * @property closeOutputStreamAfterAll + * @property sourceRootDir + * @property colorName name of color for colorful output, `null` means to disable colorization. + * @property groupByFile + */ +data class PlainDiktatReporterArguments( + override val outputStream: OutputStream, + override val closeOutputStreamAfterAll: Boolean, + override val sourceRootDir: Path?, + val colorName: String? = null, + val groupByFile: Boolean? = null, +) : DiktatReporterArguments { + override val id: String = DiktatReporterFactory.PLAIN_ID +} diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt index 57b493d8ef..3e094d5c10 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt @@ -1,56 +1,38 @@ package com.saveourtool.diktat.api -import java.io.OutputStream -import java.nio.file.Path - typealias DiktatReporter = DiktatProcessorListener /** * A factory to create [DiktatReporter] */ -interface DiktatReporterFactory : Function4 { +interface DiktatReporterFactory : Function1 { /** - * Set of supported IDs + * Set of supported IDs, must contain [DiktatReporterFactory.NONE_ID] */ val ids: Set - /** - * ID of [DiktatReporter] for plain output - */ - val plainId: String - /** * Names of color for plain output */ val colorNamesInPlain: Set /** - * @param id ID of [DiktatReporter] - * @param outputStream - * @param closeOutputStreamAfterAll close [outputStream] in [DiktatProcessorListener.afterAll] - * @param sourceRootDir a dir to detect relative path for processing files + * @param args * @return created [DiktatReporter] */ override operator fun invoke( - id: String, - outputStream: OutputStream, - closeOutputStreamAfterAll: Boolean, - sourceRootDir: Path?, + args: DiktatReporterArguments, ): DiktatReporter - /** - * @param outputStream - * @param closeOutputStreamAfterAll close [outputStream] in [DiktatProcessorListener.afterAll] - * @param sourceRootDir a dir to detect relative path for processing files - * @param colorName name of color for colorful output, `null` means to disable colorization. - * @param groupByFile - * @return [DiktatReporter] for plain output - */ - fun createPlain( - outputStream: OutputStream, - closeOutputStreamAfterAll: Boolean, - sourceRootDir: Path?, - colorName: String? = null, - groupByFile: Boolean? = null, - ): DiktatReporter + companion object { + /** + * ID of [DiktatReporter] for disabled reporter + */ + const val NONE_ID: String = "none" + + /** + * ID of [DiktatReporter] for plain output + */ + const val PLAIN_ID: String = "plain" + } } diff --git a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt index ad388681bb..56983a24f0 100644 --- a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt +++ b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt @@ -2,6 +2,7 @@ package com.saveourtool.diktat.cli import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.api.DiktatProcessorListener +import com.saveourtool.diktat.api.DiktatReporterArguments import com.saveourtool.diktat.api.DiktatReporterFactory import com.saveourtool.diktat.common.config.rules.DIKTAT import com.saveourtool.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF @@ -72,17 +73,23 @@ data class DiktatProperties( fun toRunnerArguments( sourceRootDir: Path, loggingListener: DiktatProcessorListener, - ): DiktatRunnerArguments = DiktatRunnerArguments( - configInputStream = Paths.get(config).inputStream(), - sourceRootDir = sourceRootDir, - files = getFiles(sourceRootDir), - baselineFile = null, - reporterType = reporterProviderId, - reporterOutput = getReporterOutput(), - groupByFileInPlain = groupByFileInPlain, - colorNameInPlain = colorNameInPlain, - loggingListener = loggingListener, - ) + ): DiktatRunnerArguments { + val reporterArguments = DiktatReporterArguments( + id = reporterProviderId, + outputStream = getReporterOutput(), + groupByFileInPlain = groupByFileInPlain, + colorNameInPlain = colorNameInPlain, + sourceRootDir = sourceRootDir, + ) + return DiktatRunnerArguments( + configInputStream = Paths.get(config).inputStream(), + sourceRootDir = sourceRootDir, + files = getFiles(sourceRootDir), + baselineFile = null, + reporterArgsList = listOf(reporterArguments), + loggingListener = loggingListener + ) + } private fun getFiles(sourceRootDir: Path): Collection = patterns .asSequence() @@ -205,7 +212,7 @@ data class DiktatProperties( shortName = "r", description = "The reporter to use" ) - .default(diktatReporterFactory.plainId) + .default(DiktatReporterFactory.NONE_ID) /** * @param diktatReporterFactory 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 d187679b62..e0bd4866e0 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 @@ -8,13 +8,13 @@ package com.saveourtool.diktat.plugin.gradle import org.gradle.api.Project import java.io.File +import java.nio.file.Path /** * @param diktatExtension * @return returns sourceRootDir as projectDir for sarif report */ fun Project.getSourceRootDir(diktatExtension: DiktatExtension): Path? = when { - diktatExtension.githubActions -> projectDir.toPath() diktatExtension.reporter == "sarif" -> projectDir.toPath() 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 df9713b3cd..20aa78e606 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 @@ -5,6 +5,7 @@ import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.api.DiktatReporter +import com.saveourtool.diktat.api.DiktatReporterArguments import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl @@ -83,7 +84,7 @@ abstract class DiktatTaskBase( ) } private val diktatRunnerArguments by lazy { - val githubActionsReporter = if (extension.githubActions) { + val githubActionsReporterArgs = if (extension.githubActions) { val outputStream = project.layout.buildDirectory .file("reports/diktat/diktat.sarif") .get() @@ -92,10 +93,19 @@ abstract class DiktatTaskBase( Files.createDirectories(it.parentFile.toPath()) } .outputStream() - diktatReporterFactory(id = "sarif", outputStream = outputStream, closeOutputStreamAfterAll = true, sourceRootDir = project.projectDir.toPath()) + DiktatReporterArguments( + id = "sarif", + outputStream = outputStream, + sourceRootDir = project.rootProject.projectDir.toPath() + ) } else { - DiktatReporter.empty + null } + val reporterArguments = DiktatReporterArguments( + id = project.getReporterType(extension), + outputStream = project.getOutputFile(extension)?.outputStream(), + sourceRootDir = null, + ) val loggingListener = object : DiktatProcessorListener { override fun beforeAll(files: Collection) { project.logger.info("Analyzing {} files with diktat in project {}", files.size, project.name) @@ -110,9 +120,7 @@ abstract class DiktatTaskBase( sourceRootDir = project.getSourceRootDir(extension), files = actualInputs.files.map { it.toPath() }, baselineFile = extension.baseline?.let { project.file(it).toPath() }, - reporterType = project.getReporterType(extension), - reporterOutput = project.getOutputFile(extension)?.outputStream(), - additionalReporter = githubActionsReporter, + reporterArgsList = listOf(githubActionsReporterArgs, reporterArguments).mapNotNull { it }, loggingListener = loggingListener, ) } diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterFactoryImpl.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterFactoryImpl.kt index 19f899ee68..8792d3984b 100644 --- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterFactoryImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterFactoryImpl.kt @@ -1,7 +1,9 @@ package com.saveourtool.diktat.ktlint import com.saveourtool.diktat.api.DiktatReporter +import com.saveourtool.diktat.api.DiktatReporterArguments import com.saveourtool.diktat.api.DiktatReporterFactory +import com.saveourtool.diktat.api.PlainDiktatReporterArguments import com.saveourtool.diktat.ktlint.DiktatReporterImpl.Companion.wrap import com.pinterest.ktlint.cli.reporter.checkstyle.CheckStyleReporterProvider import com.pinterest.ktlint.cli.reporter.html.HtmlReporterProvider @@ -9,8 +11,6 @@ import com.pinterest.ktlint.cli.reporter.json.JsonReporterProvider import com.pinterest.ktlint.cli.reporter.plain.Color import com.pinterest.ktlint.cli.reporter.plain.PlainReporterProvider import com.pinterest.ktlint.cli.reporter.sarif.SarifReporterProvider -import java.io.OutputStream -import java.nio.file.Path import kotlin.io.path.pathString /** @@ -23,58 +23,46 @@ class DiktatReporterFactoryImpl : DiktatReporterFactory { * All reporters which __KtLint__ provides */ private val reporterProviders = setOf( - plainReporterProvider, JsonReporterProvider(), SarifReporterProvider(), CheckStyleReporterProvider(), HtmlReporterProvider(), ) - .associateBy { it.id } + .associateBy { it.id } + (DiktatReporterFactory.PLAIN_ID to plainReporterProvider) override val ids: Set get() = reporterProviders.keys - override val plainId: String - get() = plainReporterProvider.id - override val colorNamesInPlain: Set get() = Color.entries.map { it.name }.toSet() override fun invoke( - id: String, - outputStream: OutputStream, - closeOutputStreamAfterAll: Boolean, - sourceRootDir: Path?, + args: DiktatReporterArguments, ): DiktatReporter { - val reporterProvider = reporterProviders[id] ?: throw IllegalArgumentException("Not supported reporter id by ${DiktatBaselineFactoryImpl::class.simpleName}") - if (reporterProvider is SarifReporterProvider) { - sourceRootDir?.let { System.setProperty("user.home", it.pathString) } + if (args.id == DiktatReporterFactory.NONE_ID) { + return DiktatReporter.empty } - val opt = if (reporterProvider is PlainReporterProvider) { + val opts = if (args is PlainDiktatReporterArguments) { + buildMap { + args.colorName?.let { + put("color", true) + put("color_name", it) + } ?: run { + put("color", false) + put("color_name", Color.DARK_GRAY) + } + args.groupByFile?.let { put("group_by_file", it) } + }.mapValues { it.value.toString() } + } else if (args.id == DiktatReporterFactory.PLAIN_ID) { mapOf("color_name" to Color.DARK_GRAY.name) } else { emptyMap() } - return reporterProvider.get(outputStream, closeOutputStreamAfterAll, opt).wrap(sourceRootDir) - } - override fun createPlain( - outputStream: OutputStream, - closeOutputStreamAfterAll: Boolean, - sourceRootDir: Path?, - colorName: String?, - groupByFile: Boolean?, - ): DiktatReporter { - val opt = buildMap { - colorName?.let { - put("color", true) - put("color_name", it) - } ?: run { - put("color", false) - put("color_name", Color.DARK_GRAY) - } - groupByFile?.let { put("group_by_file", it) } - }.mapValues { it.value.toString() } - return plainReporterProvider.get(outputStream, closeOutputStreamAfterAll, opt).wrap(sourceRootDir) + val reporterProvider = reporterProviders[args.id] ?: throw IllegalArgumentException("Not supported reporter id by ${DiktatBaselineFactoryImpl::class.simpleName}") + if (reporterProvider is SarifReporterProvider) { + args.sourceRootDir?.let { System.setProperty("user.home", it.pathString) } + } + return reporterProvider.get(args.outputStream, args.closeOutputStreamAfterAll, opts).wrap(args.sourceRootDir) } } diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index cc387e2424..89126b53a6 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -3,7 +3,7 @@ package com.saveourtool.diktat.plugin.maven import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory -import com.saveourtool.diktat.api.DiktatReporter +import com.saveourtool.diktat.api.DiktatReporterArguments import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl @@ -116,20 +116,22 @@ abstract class DiktatBaseMojo : AbstractMojo() { diktatBaselineFactory = DiktatBaselineFactoryImpl(), diktatReporterFactory = diktatReporterFactory, ) - val githubActionsReporter = if (githubActions) { - val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) - diktatReporterFactory(id = "sarif", outputStream, closeOutputStreamAfterAll = true, sourceRootDir) - } else { - DiktatReporter.empty + val reporterArgsList = buildList { + if (githubActions) { + val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) + val args = DiktatReporterArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir) + add(args) + } + add( + DiktatReporterArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = null) + ) } val args = DiktatRunnerArguments( configInputStream = configFile.inputStream(), sourceRootDir = sourceRootDir, files = inputs.map(::Path), baselineFile = baseline?.toPath(), - reporterType = getReporterType(), - reporterOutput = getReporterOutput(), - additionalReporter = githubActionsReporter, + reporterArgsList = reporterArgsList, ) val diktatRunner = diktatRunnerFactory(args) val errorCounter = runAction( From 20f66f65801bc89936f8e7b1d277969170a2d137 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 15 Nov 2023 17:35:44 +0300 Subject: [PATCH 03/17] diktatFix --- .../com/saveourtool/diktat/DiktatRunner.kt | 4 ++-- .../saveourtool/diktat/DiktatRunnerFactory.kt | 8 +++---- .../diktat/api/DiktatReporterArguments.kt | 23 ++++++++++++------- .../plugin/gradle/tasks/DiktatTaskBase.kt | 1 - .../diktat/plugin/maven/DiktatBaseMojo.kt | 23 +++++++++++-------- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt index c82eefb88b..fb8c07155f 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt @@ -13,8 +13,8 @@ private typealias RunAction = (DiktatProcessor, DiktatProcessorListener) -> Unit /** * A runner for diktat on bunch of files using baseline and reporter * - * @property diktatProcessor - * @property diktatReporter + * @param diktatProcessor + * @param diktatReporter */ data class DiktatRunner( private val diktatProcessor: DiktatProcessor, diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt index e87f608d6a..85c917fdf2 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt @@ -38,11 +38,9 @@ class DiktatRunnerFactory( val processor = diktatProcessorFactory(diktatRuleSet) val (baseline, baselineGenerator) = resolveBaseline(args.baselineFile, args.sourceRootDir) - val reporter = args.reporterArgsList.map { - diktatReporterFactory(it) - }.let { - DiktatReporter.union(it) - } + val reporter = args.reporterArgsList + .map { diktatReporterFactory(it) } + .let { DiktatReporter.union(it) } return DiktatRunner( diktatProcessor = processor, diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt index bbf62195e4..b4bdedc1c8 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt @@ -36,7 +36,8 @@ sealed interface DiktatReporterArguments { * @param id ID of [DiktatReporter] * @param outputStream stdout will be used when it's empty * @param sourceRootDir a dir to detect relative path for processing files - * @param colorNameInPlain a color name for colorful output which is applicable for plain ([DiktatReporterFactory.PLAIN_ID]) reporter only, `null` means to disable colorization. + * @param colorNameInPlain a color name for colorful output which is applicable for plain ([DiktatReporterFactory.PLAIN_ID]) reporter only, + * `null` means to disable colorization. * @param groupByFileInPlain a flag `groupByFile` which is applicable for plain ([DiktatReporterFactory.PLAIN_ID]) reporter only. * @return created [DiktatReporter] */ @@ -67,13 +68,6 @@ sealed interface DiktatReporterArguments { } } -private data class DiktatReporterArgumentsImpl( - override val id: String, - override val outputStream: OutputStream, - override val closeOutputStreamAfterAll: Boolean, - override val sourceRootDir: Path?, -) : DiktatReporterArguments - /** * Implementation of [DiktatReporterArguments] for [DiktatReporterFactory.PLAIN_ID] * @@ -92,3 +86,16 @@ data class PlainDiktatReporterArguments( ) : DiktatReporterArguments { override val id: String = DiktatReporterFactory.PLAIN_ID } + +/** + * @property id + * @property outputStream + * @property closeOutputStreamAfterAll + * @property sourceRootDir + */ +private data class DiktatReporterArgumentsImpl( + override val id: String, + override val outputStream: OutputStream, + override val closeOutputStreamAfterAll: Boolean, + override val sourceRootDir: Path?, +) : DiktatReporterArguments 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 20aa78e606..d9f6e9f96f 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 @@ -4,7 +4,6 @@ import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory import com.saveourtool.diktat.api.DiktatProcessorListener -import com.saveourtool.diktat.api.DiktatReporter import com.saveourtool.diktat.api.DiktatReporterArguments import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index 89126b53a6..6fb368fdab 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -56,6 +56,18 @@ abstract class DiktatBaseMojo : AbstractMojo() { */ @Parameter(property = "diktat.baseline") var baseline: File? = null + private val diktatReporterFactory by lazy { + DiktatReporterFactoryImpl() + } + private val diktatRunnerFactory by lazy { + DiktatRunnerFactory( + diktatRuleConfigReader = DiktatRuleConfigReaderImpl(), + diktatRuleSetFactory = DiktatRuleSetFactoryImpl(), + diktatProcessorFactory = DiktatProcessorFactoryImpl(), + diktatBaselineFactory = DiktatBaselineFactoryImpl(), + diktatReporterFactory = diktatReporterFactory, + ) + } /** * Path to diktat yml config file. Can be either absolute or relative to project's root directory. @@ -108,19 +120,10 @@ abstract class DiktatBaseMojo : AbstractMojo() { ) val sourceRootDir = mavenProject.basedir.parentFile.toPath() - val diktatReporterFactory = DiktatReporterFactoryImpl() - val diktatRunnerFactory = DiktatRunnerFactory( - diktatRuleConfigReader = DiktatRuleConfigReaderImpl(), - diktatRuleSetFactory = DiktatRuleSetFactoryImpl(), - diktatProcessorFactory = DiktatProcessorFactoryImpl(), - diktatBaselineFactory = DiktatBaselineFactoryImpl(), - diktatReporterFactory = diktatReporterFactory, - ) val reporterArgsList = buildList { if (githubActions) { val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) - val args = DiktatReporterArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir) - add(args) + add(DiktatReporterArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir)) } add( DiktatReporterArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = null) From 551df11cd4aca3e443dacf234938a3577cf3cab4 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 15 Nov 2023 17:42:54 +0300 Subject: [PATCH 04/17] renamed DiktatReporterCreationArguments --- .../diktat/DiktatRunnerArguments.kt | 6 +++--- ...s.kt => DiktatReporterCreationArguments.kt} | 18 +++++++++--------- .../diktat/api/DiktatReporterFactory.kt | 4 ++-- .../saveourtool/diktat/cli/DiktatProperties.kt | 4 ++-- .../plugin/gradle/tasks/DiktatTaskBase.kt | 6 +++--- .../diktat/ktlint/DiktatReporterFactoryImpl.kt | 8 ++++---- .../diktat/plugin/maven/DiktatBaseMojo.kt | 6 +++--- 7 files changed, 26 insertions(+), 26 deletions(-) rename diktat-api/src/main/kotlin/com/saveourtool/diktat/api/{DiktatReporterArguments.kt => DiktatReporterCreationArguments.kt} (86%) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt index fc4ca0b87f..fb8db04854 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerArguments.kt @@ -1,7 +1,7 @@ package com.saveourtool.diktat import com.saveourtool.diktat.api.DiktatProcessorListener -import com.saveourtool.diktat.api.DiktatReporterArguments +import com.saveourtool.diktat.api.DiktatReporterCreationArguments import java.io.InputStream import java.nio.file.Path import kotlin.io.path.inputStream @@ -21,7 +21,7 @@ data class DiktatRunnerArguments( val sourceRootDir: Path?, val files: Collection, val baselineFile: Path?, - val reporterArgsList: List = emptyList(), + val reporterArgsList: List = emptyList(), val loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty, ) { constructor( @@ -29,7 +29,7 @@ data class DiktatRunnerArguments( sourceRootDir: Path?, files: Collection, baselineFile: Path?, - reporterArgsList: List = emptyList(), + reporterArgsList: List = emptyList(), loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty, ) : this( configFile.inputStream(), diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterCreationArguments.kt similarity index 86% rename from diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt rename to diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterCreationArguments.kt index b4bdedc1c8..1ac5424ddc 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterArguments.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterCreationArguments.kt @@ -10,7 +10,7 @@ import java.nio.file.Path /** * Arguments to create [DiktatReporter] using [DiktatReporterFactory] */ -sealed interface DiktatReporterArguments { +sealed interface DiktatReporterCreationArguments { /** * Identifier of [DiktatReporter] which needs to be created */ @@ -47,10 +47,10 @@ sealed interface DiktatReporterArguments { sourceRootDir: Path?, colorNameInPlain: String? = null, groupByFileInPlain: Boolean? = null, - ): DiktatReporterArguments { + ): DiktatReporterCreationArguments { val (outputStreamOrStdout, closeOutputStreamAfterAll) = outputStream?.let { it to true } ?: (System.`out` to false) return if (id == DiktatReporterFactory.PLAIN_ID) { - PlainDiktatReporterArguments( + PlainDiktatReporterCreationArguments( outputStreamOrStdout, closeOutputStreamAfterAll, sourceRootDir, colorNameInPlain, groupByFileInPlain ) } else { @@ -60,7 +60,7 @@ sealed interface DiktatReporterArguments { require(groupByFileInPlain == null) { "groupByFile is applicable only for plain reporter" } - DiktatReporterArgumentsImpl( + DiktatReporterCreationArgumentsImpl( id, outputStreamOrStdout, closeOutputStreamAfterAll, sourceRootDir ) } @@ -69,7 +69,7 @@ sealed interface DiktatReporterArguments { } /** - * Implementation of [DiktatReporterArguments] for [DiktatReporterFactory.PLAIN_ID] + * Implementation of [DiktatReporterCreationArguments] for [DiktatReporterFactory.PLAIN_ID] * * @property outputStream * @property closeOutputStreamAfterAll @@ -77,13 +77,13 @@ sealed interface DiktatReporterArguments { * @property colorName name of color for colorful output, `null` means to disable colorization. * @property groupByFile */ -data class PlainDiktatReporterArguments( +data class PlainDiktatReporterCreationArguments( override val outputStream: OutputStream, override val closeOutputStreamAfterAll: Boolean, override val sourceRootDir: Path?, val colorName: String? = null, val groupByFile: Boolean? = null, -) : DiktatReporterArguments { +) : DiktatReporterCreationArguments { override val id: String = DiktatReporterFactory.PLAIN_ID } @@ -93,9 +93,9 @@ data class PlainDiktatReporterArguments( * @property closeOutputStreamAfterAll * @property sourceRootDir */ -private data class DiktatReporterArgumentsImpl( +private data class DiktatReporterCreationArgumentsImpl( override val id: String, override val outputStream: OutputStream, override val closeOutputStreamAfterAll: Boolean, override val sourceRootDir: Path?, -) : DiktatReporterArguments +) : DiktatReporterCreationArguments diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt index 3e094d5c10..a9c27aa959 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatReporterFactory.kt @@ -5,7 +5,7 @@ typealias DiktatReporter = DiktatProcessorListener /** * A factory to create [DiktatReporter] */ -interface DiktatReporterFactory : Function1 { +interface DiktatReporterFactory : Function1 { /** * Set of supported IDs, must contain [DiktatReporterFactory.NONE_ID] */ @@ -21,7 +21,7 @@ interface DiktatReporterFactory : Function1 { args.colorName?.let { put("color", true) diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index 6fb368fdab..ab1c4ca4cb 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -3,7 +3,7 @@ package com.saveourtool.diktat.plugin.maven import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory -import com.saveourtool.diktat.api.DiktatReporterArguments +import com.saveourtool.diktat.api.DiktatReporterCreationArguments import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl @@ -123,10 +123,10 @@ abstract class DiktatBaseMojo : AbstractMojo() { val reporterArgsList = buildList { if (githubActions) { val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) - add(DiktatReporterArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir)) + add(DiktatReporterCreationArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir)) } add( - DiktatReporterArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = null) + DiktatReporterCreationArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = null) ) } val args = DiktatRunnerArguments( From 587c2ffba5e5acf437b1707ae65259680f805a44 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 15 Nov 2023 18:26:35 +0300 Subject: [PATCH 05/17] we need it for tests --- .../src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt index fb8c07155f..8a1605b809 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunner.kt @@ -14,11 +14,11 @@ private typealias RunAction = (DiktatProcessor, DiktatProcessorListener) -> Unit * A runner for diktat on bunch of files using baseline and reporter * * @param diktatProcessor - * @param diktatReporter + * @property diktatReporter */ data class DiktatRunner( private val diktatProcessor: DiktatProcessor, - private val diktatReporter: DiktatReporter, + val diktatReporter: DiktatReporter, ) { private fun doRun( args: DiktatRunnerArguments, From ce4119f1b4d00833aecf78226d220f4d42985c4d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 13:20:13 +0300 Subject: [PATCH 06/17] added DiktatProcessorListenerWrapper to support a single method to unwrap --- .../saveourtool/diktat/api/DiktatBaseline.kt | 16 ++-- .../diktat/api/DiktatProcessorListener.kt | 18 ++-- .../util/DiktatProcessorListenerWrapper.kt | 90 +++++++++++++++++++ .../plugin/gradle/tasks/DiktatTaskBase.kt | 10 ++- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 44 +++++++-- .../diktat/ktlint/DiktatReporterImpl.kt | 21 ++--- 6 files changed, 163 insertions(+), 36 deletions(-) create mode 100644 diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt index f415801708..5b6d1326fb 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt @@ -1,5 +1,6 @@ package com.saveourtool.diktat.api +import com.saveourtool.diktat.util.DiktatProcessorListenerWrapper import java.nio.file.Path /** @@ -22,21 +23,22 @@ fun interface DiktatBaseline { * @param baseline * @return wrapped [DiktatProcessorListener] which skips known errors based on [baseline] */ - fun DiktatProcessorListener.skipKnownErrors(baseline: DiktatBaseline): DiktatProcessorListener = object : DiktatProcessorListener { - override fun onError( + fun DiktatProcessorListener.skipKnownErrors(baseline: DiktatBaseline): DiktatProcessorListener = object : DiktatProcessorListenerWrapper(this@skipKnownErrors) { + override fun doOnError( + wrappedValue: DiktatProcessorListener, file: Path, error: DiktatError, isCorrected: Boolean ) { if (!baseline.errorsByFile(file).contains(error)) { - this@skipKnownErrors.onError(file, error, isCorrected) + wrappedValue.onError(file, error, isCorrected) } } - override fun beforeAll(files: Collection) = this@skipKnownErrors.beforeAll(files) - override fun before(file: Path) = this@skipKnownErrors.before(file) - override fun after(file: Path) = this@skipKnownErrors.after(file) - override fun afterAll() = this@skipKnownErrors.afterAll() + override fun doBeforeAll(wrappedValue: DiktatProcessorListener, files: Collection) = wrappedValue.beforeAll(files) + override fun doBefore(wrappedValue: DiktatProcessorListener, file: Path) = wrappedValue.before(file) + override fun doAfter(wrappedValue: DiktatProcessorListener, file: Path) = wrappedValue.after(file) + override fun doAfterAll(wrappedValue: DiktatProcessorListener) = wrappedValue.afterAll() } } } diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt index 39edbad2ad..35c202804d 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatProcessorListener.kt @@ -1,8 +1,11 @@ package com.saveourtool.diktat.api +import com.saveourtool.diktat.util.DiktatProcessorListenerWrapper import java.nio.file.Path import java.util.concurrent.atomic.AtomicInteger +private typealias DiktatProcessorListenerIterable = Iterable + /** * A listener for [com.saveourtool.diktat.DiktatProcessor] */ @@ -57,16 +60,17 @@ interface DiktatProcessorListener { * @param listeners * @return a single [DiktatProcessorListener] which uses all provided [listeners] */ - fun union(listeners: Iterable): DiktatProcessorListener = object : DiktatProcessorListener { - override fun beforeAll(files: Collection) = listeners.forEach { it.beforeAll(files) } - override fun before(file: Path) = listeners.forEach { it.before(file) } - override fun onError( + fun union(listeners: DiktatProcessorListenerIterable): DiktatProcessorListener = object : DiktatProcessorListenerWrapper(listeners) { + override fun doBeforeAll(wrappedValue: DiktatProcessorListenerIterable, files: Collection) = wrappedValue.forEach { it.beforeAll(files) } + override fun doBefore(wrappedValue: DiktatProcessorListenerIterable, file: Path) = wrappedValue.forEach { it.before(file) } + override fun doOnError( + wrappedValue: DiktatProcessorListenerIterable, file: Path, error: DiktatError, isCorrected: Boolean - ) = listeners.forEach { it.onError(file, error, isCorrected) } - override fun after(file: Path) = listeners.forEach { it.after(file) } - override fun afterAll() = listeners.forEach(DiktatProcessorListener::afterAll) + ) = wrappedValue.forEach { it.onError(file, error, isCorrected) } + override fun doAfter(wrappedValue: DiktatProcessorListenerIterable, file: Path) = wrappedValue.forEach { it.after(file) } + override fun doAfterAll(wrappedValue: DiktatProcessorListenerIterable) = wrappedValue.forEach(DiktatProcessorListener::afterAll) } /** diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt new file mode 100644 index 0000000000..7ffa60f754 --- /dev/null +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt @@ -0,0 +1,90 @@ +package com.saveourtool.diktat.util + +import com.saveourtool.diktat.api.DiktatError +import com.saveourtool.diktat.api.DiktatProcessorListener +import java.nio.file.Path + +/** + * A common wrapper for [DiktatProcessorListener] + * + * @property wrappedValue + */ +abstract class DiktatProcessorListenerWrapper( + val wrappedValue: T, +): DiktatProcessorListener { + + override fun beforeAll(files: Collection): Unit = doBeforeAll(wrappedValue, files) + + /** + * Called once, before [com.saveourtool.diktat.DiktatProcessor] starts process a bunch of files. + * + * @param wrappedValue + * @param files + */ + protected open fun doBeforeAll(wrappedValue: T, files: Collection): Unit = Unit + + override fun before(file: Path): Unit = doBefore(wrappedValue, file) + + /** + * Called before each file when [com.saveourtool.diktat.DiktatProcessor] starts to process it. + * + * @param wrappedValue + * @param file + */ + protected open fun doBefore(wrappedValue: T, file: Path): Unit = Unit + + override fun onError( + file: Path, + error: DiktatError, + isCorrected: Boolean + ): Unit = doOnError(wrappedValue, file, error, isCorrected) + + /** + * Called on each error when [com.saveourtool.diktat.DiktatProcessor] detects such one. + * + * @param wrappedValue + * @param file + * @param error + * @param isCorrected + */ + protected open fun doOnError( + wrappedValue: T, + file: Path, + error: DiktatError, + isCorrected: Boolean + ): Unit = Unit + + override fun after(file: Path): Unit = doAfter(wrappedValue, file) + + /** + * Called after each file when [com.saveourtool.diktat.DiktatProcessor] finished to process it. + * + * @param wrappedValue + * @param file + */ + protected open fun doAfter(wrappedValue: T, file: Path): Unit = Unit + + override fun afterAll(): Unit = doAfterAll(wrappedValue) + + /** + * Called once, after the processing of [com.saveourtool.diktat.DiktatProcessor] finished. + * + * @param wrappedValue + */ + protected open fun doAfterAll(wrappedValue: T): Unit = Unit + + companion object { + /** + * @return wrapped value [T] if it's possible + */ + inline fun DiktatProcessorListener.tryUnwrap(): T? = (this as? DiktatProcessorListenerWrapper<*>) + ?.wrappedValue + ?.let { it as? T } + + /** + * @return wrapped value [T] or an error + */ + inline fun DiktatProcessorListener.unwrap(): T = tryUnwrap() + ?: error("Unsupported wrapper of ${DiktatProcessorListener::class.java.simpleName} to ${T::class.simpleName}: ${this.javaClass.name}") + } +} 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 68ba84f343..6434956527 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 @@ -83,6 +83,9 @@ abstract class DiktatTaskBase( ) } private val diktatRunnerArguments by lazy { + val sourceRootDir by lazy { + project.rootProject.projectDir.toPath() + } val githubActionsReporterArgs = if (extension.githubActions) { val outputStream = project.layout.buildDirectory .file("reports/diktat/diktat.sarif") @@ -95,15 +98,16 @@ abstract class DiktatTaskBase( DiktatReporterCreationArguments( id = "sarif", outputStream = outputStream, - sourceRootDir = project.rootProject.projectDir.toPath() + sourceRootDir = sourceRootDir, ) } else { null } + val reporterId = project.getReporterType(extension) val reporterArguments = DiktatReporterCreationArguments( - id = project.getReporterType(extension), + id = reporterId, outputStream = project.getOutputFile(extension)?.outputStream(), - sourceRootDir = null, + sourceRootDir = if (reporterId == "sarif") sourceRootDir else null, ) val loggingListener = object : DiktatProcessorListener { override fun beforeAll(files: Collection) { diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 2e89333cd9..5fad253d1b 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -1,7 +1,9 @@ package com.saveourtool.diktat.plugin.gradle -import com.saveourtool.diktat.ktlint.DiktatReporterImpl.Companion.unwrap +import com.saveourtool.diktat.api.DiktatReporter +import com.saveourtool.diktat.ktlint.DiktatReporterImpl.Companion.unwrapToKtlint import com.saveourtool.diktat.plugin.gradle.tasks.DiktatCheckTask +import com.saveourtool.diktat.util.DiktatProcessorListenerWrapper.Companion.unwrap import com.pinterest.ktlint.cli.reporter.json.JsonReporter import com.pinterest.ktlint.cli.reporter.plain.PlainReporter import com.pinterest.ktlint.cli.reporter.sarif.SarifReporter @@ -55,7 +57,7 @@ class DiktatJavaExecTaskTest { } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is PlainReporter) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is PlainReporter) } @Test @@ -82,7 +84,7 @@ class DiktatJavaExecTaskTest { } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is PlainReporter) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is PlainReporter) } @Test @@ -119,7 +121,9 @@ class DiktatJavaExecTaskTest { output = "some.txt" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is JsonReporter) + assert( + task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter + ) } @Test @@ -130,7 +134,9 @@ class DiktatJavaExecTaskTest { reporter = "json" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is JsonReporter) + assert( + task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter + ) } @Test @@ -141,7 +147,9 @@ class DiktatJavaExecTaskTest { githubActions = true } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is SarifReporter) + assert( + task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter + ) Assertions.assertEquals( project.rootDir.toString(), System.getProperty("user.home") @@ -158,7 +166,17 @@ class DiktatJavaExecTaskTest { output = "report.json" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is SarifReporter) + val (firstReporter, secondReporter) = task.diktatRunner.diktatReporter + .unwrap>().first() + .unwrap() + .unwrap>() + .toList() + assert( + firstReporter.unwrapToKtlint() is SarifReporter + ) + assert( + secondReporter.unwrapToKtlint() is JsonReporter + ) Assertions.assertEquals( project.rootDir.toString(), System.getProperty("user.home") @@ -173,9 +191,11 @@ class DiktatJavaExecTaskTest { reporter = "sarif" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert(task.diktatRunner.diktatReporter.unwrap() is SarifReporter) + assert( + task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter + ) Assertions.assertEquals( - project.rootDir.toString(), + project.rootProject.projectDir.toPath().toString(), System.getProperty("user.home") ) } @@ -262,5 +282,11 @@ class DiktatJavaExecTaskTest { companion object { private const val DIKTAT_CHECK_TASK = "diktatCheck" + + private fun DiktatReporter.unwrapFirst() = this + .unwrap>().first() + .unwrap() + .unwrap>().first() + .unwrapToKtlint() } } diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt index 8f15b7002d..153941a59c 100644 --- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt @@ -3,6 +3,7 @@ package com.saveourtool.diktat.ktlint import com.saveourtool.diktat.api.DiktatError import com.saveourtool.diktat.api.DiktatReporter import com.saveourtool.diktat.ktlint.ReporterV2Wrapper.Companion.unwrapIfNeeded +import com.saveourtool.diktat.util.DiktatProcessorListenerWrapper import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 import java.nio.file.Path @@ -13,18 +14,19 @@ import java.nio.file.Path * @param sourceRootDir */ class DiktatReporterImpl( - private val ktLintReporter: ReporterV2, + ktLintReporter: ReporterV2, private val sourceRootDir: Path?, -) : DiktatReporter { - override fun beforeAll(files: Collection): Unit = ktLintReporter.beforeAll() - override fun before(file: Path): Unit = ktLintReporter.before(file.relativePathStringTo(sourceRootDir)) - override fun onError( +) : DiktatProcessorListenerWrapper(ktLintReporter) { + override fun doBeforeAll(wrappedValue: ReporterV2, files: Collection): Unit = wrappedValue.beforeAll() + override fun doBefore(wrappedValue: ReporterV2, file: Path): Unit = wrappedValue.before(file.relativePathStringTo(sourceRootDir)) + override fun doOnError( + wrappedValue: ReporterV2, file: Path, error: DiktatError, isCorrected: Boolean, - ): Unit = ktLintReporter.onLintError(file.relativePathStringTo(sourceRootDir), error.toKtLintForCli()) - override fun after(file: Path): Unit = ktLintReporter.after(file.relativePathStringTo(sourceRootDir)) - override fun afterAll(): Unit = ktLintReporter.afterAll() + ): Unit = wrappedValue.onLintError(file.relativePathStringTo(sourceRootDir), error.toKtLintForCli()) + override fun doAfter(wrappedValue: ReporterV2, file: Path): Unit = wrappedValue.after(file.relativePathStringTo(sourceRootDir)) + override fun doAfterAll(wrappedValue: ReporterV2): Unit = wrappedValue.afterAll() companion object { /** @@ -36,7 +38,6 @@ class DiktatReporterImpl( /** * @return __KtLint__'s [ReporterV2] */ - fun DiktatReporter.unwrap(): ReporterV2 = (this as? DiktatReporterImpl)?.ktLintReporter?.unwrapIfNeeded() - ?: error("Unsupported wrapper of ${DiktatReporter::class.java.simpleName}: ${this::class.java.canonicalName}") + fun DiktatReporter.unwrapToKtlint(): ReporterV2 = unwrap().unwrapIfNeeded() } } From 01c4c56f6f5502ed842375133f0526d7ae6aa32e Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 13:30:25 +0300 Subject: [PATCH 07/17] diktatFix --- .../com/saveourtool/diktat/api/DiktatBaseline.kt | 4 +++- .../diktat/util/DiktatProcessorListenerWrapper.kt | 3 +-- .../diktat/plugin/gradle/tasks/DiktatTaskBase.kt | 2 +- .../diktat/plugin/gradle/DiktatJavaExecTaskTest.kt | 12 +++--------- .../diktat/plugin/maven/DiktatBaseMojo.kt | 3 ++- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt index 5b6d1326fb..2fbe97833e 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/api/DiktatBaseline.kt @@ -23,7 +23,9 @@ fun interface DiktatBaseline { * @param baseline * @return wrapped [DiktatProcessorListener] which skips known errors based on [baseline] */ - fun DiktatProcessorListener.skipKnownErrors(baseline: DiktatBaseline): DiktatProcessorListener = object : DiktatProcessorListenerWrapper(this@skipKnownErrors) { + fun DiktatProcessorListener.skipKnownErrors(baseline: DiktatBaseline): DiktatProcessorListener = object : DiktatProcessorListenerWrapper( + this@skipKnownErrors + ) { override fun doOnError( wrappedValue: DiktatProcessorListener, file: Path, diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt index 7ffa60f754..d81983d854 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt @@ -11,8 +11,7 @@ import java.nio.file.Path */ abstract class DiktatProcessorListenerWrapper( val wrappedValue: T, -): DiktatProcessorListener { - +) : DiktatProcessorListener { override fun beforeAll(files: Collection): Unit = doBeforeAll(wrappedValue, files) /** 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 6434956527..d0e678dc0f 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 @@ -107,7 +107,7 @@ abstract class DiktatTaskBase( val reporterArguments = DiktatReporterCreationArguments( id = reporterId, outputStream = project.getOutputFile(extension)?.outputStream(), - sourceRootDir = if (reporterId == "sarif") sourceRootDir else null, + sourceRootDir = sourceRootDir.takeIf { reporterId == "sarif" }, ) val loggingListener = object : DiktatProcessorListener { override fun beforeAll(files: Collection) { diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 5fad253d1b..68950655ad 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -121,9 +121,7 @@ class DiktatJavaExecTaskTest { output = "some.txt" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert( - task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter - ) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter) } @Test @@ -147,9 +145,7 @@ class DiktatJavaExecTaskTest { githubActions = true } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert( - task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter - ) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter) Assertions.assertEquals( project.rootDir.toString(), System.getProperty("user.home") @@ -191,9 +187,7 @@ class DiktatJavaExecTaskTest { reporter = "sarif" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert( - task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter - ) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter) Assertions.assertEquals( project.rootProject.projectDir.toPath().toString(), System.getProperty("user.home") diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index ab1c4ca4cb..13e80f8d07 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -125,8 +125,9 @@ abstract class DiktatBaseMojo : AbstractMojo() { val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) add(DiktatReporterCreationArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir)) } + val reporterId = getReporterType() add( - DiktatReporterCreationArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = null) + DiktatReporterCreationArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = sourceRootDir.takeIf { reporterId == "sarif" }) ) } val args = DiktatRunnerArguments( From 2b5b2f53fbdcd88e4a2002d6440d8ce9e0ce8c07 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 13:36:56 +0300 Subject: [PATCH 08/17] detektAll --- .../saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt index d81983d854..e78ceae879 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/util/DiktatProcessorListenerWrapper.kt @@ -9,7 +9,7 @@ import java.nio.file.Path * * @property wrappedValue */ -abstract class DiktatProcessorListenerWrapper( +open class DiktatProcessorListenerWrapper( val wrappedValue: T, ) : DiktatProcessorListener { override fun beforeAll(files: Collection): Unit = doBeforeAll(wrappedValue, files) From b1fba0c56eb189c648dc36ab023c208fd196e146 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 15:47:03 +0300 Subject: [PATCH 09/17] updated gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1d8b85b767..87dfd162e9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ out/ *.sarif # a generated file for github action -/gradle/libs.versions.toml_snapshot +/gradle/libs.versions.toml_backup From 1be0680e2185fafc86e85cab87e111d5607d9644 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 17:20:30 +0300 Subject: [PATCH 10/17] supported reporters on plugin level --- build.gradle.kts | 4 +++- .../com/saveourtool/diktat/cli/DiktatProperties.kt | 2 +- .../diktat/plugin/gradle/DiktatExtension.kt | 7 ++++++- .../diktat/plugin/gradle/DiktatGradlePlugin.kt | 5 ++++- .../diktat/plugin/gradle/extensions/Reporter.kt | 14 ++++++++++++++ .../diktat/plugin/gradle/extensions/Reporters.kt | 14 ++++++++++++++ .../diktat/plugin/gradle/tasks/DiktatCheckTask.kt | 6 ++++-- .../diktat/plugin/gradle/tasks/DiktatFixTask.kt | 6 ++++-- .../diktat/plugin/gradle/tasks/DiktatTaskBase.kt | 13 ++++++++++++- .../diktat/plugin/gradle/DiktatJavaExecTaskTest.kt | 7 +++++++ .../diktat-convention-configuration.gradle.kts | 14 ++++++++++++++ 11 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt create mode 100644 diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt diff --git a/build.gradle.kts b/build.gradle.kts index b3566af866..071b4c417b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,7 @@ import org.jetbrains.kotlin.incremental.createDirectory +import java.nio.file.CopyOption import java.nio.file.Files +import java.nio.file.StandardCopyOption @Suppress("DSL_SCOPE_VIOLATION", "RUN_IN_SCRIPT") // https://github.com/gradle/gradle/issues/22797 plugins { @@ -60,7 +62,7 @@ tasks.create("generateLibsForDiktatSnapshot") { .let { val libsFileForDiktatSnapshot = dir.resolve(libsFileName) Files.write(libsFileForDiktatSnapshot.toPath(), it) - Files.move(libsFile.toPath(), libsFileBackup.toPath()) + Files.move(libsFile.toPath(), libsFileBackup.toPath(), StandardCopyOption.REPLACE_EXISTING) Files.copy(libsFileForDiktatSnapshot.toPath(), libsFile.toPath()) } diff --git a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt index 4688500118..eaa71480e9 100644 --- a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt +++ b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt @@ -212,7 +212,7 @@ data class DiktatProperties( shortName = "r", description = "The reporter to use" ) - .default(DiktatReporterFactory.NONE_ID) + .default("plain") /** * @param diktatReporterFactory 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..d59ceb8cf9 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,5 +1,7 @@ package com.saveourtool.diktat.plugin.gradle +import com.saveourtool.diktat.plugin.gradle.extensions.Reporters +import org.gradle.api.Action import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity @@ -13,7 +15,8 @@ import java.io.File * @param patternSet */ open class DiktatExtension( - private val patternSet: PatternSet + private val patternSet: PatternSet, + val reporters: Reporters, ) { /** * Boolean flag to support `ignoreFailures` property of [VerificationTask]. @@ -62,4 +65,6 @@ open class DiktatExtension( fun inputs(action: PatternFilterable.() -> Unit) { action(patternSet) } + + fun reporters(action: Action): Unit = action.execute(reporters) } 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..a64d1f5325 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 @@ -1,5 +1,6 @@ package com.saveourtool.diktat.plugin.gradle +import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import com.saveourtool.diktat.plugin.gradle.tasks.DiktatCheckTask.Companion.registerDiktatCheckTask import com.saveourtool.diktat.plugin.gradle.tasks.DiktatFixTask.Companion.registerDiktatFixTask import com.saveourtool.diktat.plugin.gradle.tasks.configureMergeReportsTask @@ -17,11 +18,13 @@ 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 + patternSet, + project.objects.newInstance(Reporters::class.java), ).apply { diktatConfigFile = project.rootProject.file("diktat-analysis.yml") } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt new file mode 100644 index 0000000000..721ea7830b --- /dev/null +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt @@ -0,0 +1,14 @@ +package com.saveourtool.diktat.plugin.gradle.extensions + +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile + +abstract class Reporter { + @get:Input + abstract val id: Property + + @get:OutputFile + abstract val output: RegularFileProperty +} diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt new file mode 100644 index 0000000000..340b7f73a1 --- /dev/null +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt @@ -0,0 +1,14 @@ +package com.saveourtool.diktat.plugin.gradle.extensions + +import org.gradle.api.Action +import org.gradle.api.model.ObjectFactory +import javax.inject.Inject + +abstract class Reporters @Inject constructor( + private val objectFactory: ObjectFactory +) { + val values = mutableListOf() + + fun configure(action: Action): Unit = + action.execute(objectFactory.newInstance(Reporter::class.java).apply { values.add(this) }) +} 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..059da11dd4 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 @@ -5,6 +5,7 @@ import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.util.PatternFilterable import org.gradle.api.tasks.util.PatternSet @@ -15,8 +16,9 @@ import javax.inject.Inject */ abstract class DiktatCheckTask @Inject constructor( extension: DiktatExtension, - inputs: PatternFilterable -) : DiktatTaskBase(extension, inputs) { + inputs: PatternFilterable, + objectFactory: ObjectFactory, +) : DiktatTaskBase(extension, inputs, objectFactory) { 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..5742f842e6 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 @@ -5,6 +5,7 @@ import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.util.PatternFilterable import org.gradle.api.tasks.util.PatternSet @@ -15,8 +16,9 @@ import javax.inject.Inject */ abstract class DiktatFixTask @Inject constructor( extension: DiktatExtension, - inputs: PatternFilterable -) : DiktatTaskBase(extension, inputs) { + inputs: PatternFilterable, + objectFactory: ObjectFactory, +) : DiktatTaskBase(extension, inputs, objectFactory) { 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 d0e678dc0f..f51b436448 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 @@ -9,6 +9,7 @@ import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl import com.saveourtool.diktat.plugin.gradle.DiktatExtension +import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import com.saveourtool.diktat.plugin.gradle.getOutputFile import com.saveourtool.diktat.plugin.gradle.getReporterType import com.saveourtool.diktat.plugin.gradle.getSourceRootDir @@ -17,9 +18,11 @@ import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl import generated.DIKTAT_VERSION import generated.KTLINT_VERSION +import org.gradle.api.Action import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.file.FileCollection +import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.IgnoreEmptyDirectories import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal @@ -29,6 +32,7 @@ import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.VerificationTask import org.gradle.api.tasks.util.PatternFilterable +import org.gradle.language.base.plugins.LifecycleBasePlugin import java.nio.file.Files import java.nio.file.Path @@ -42,7 +46,8 @@ import java.nio.file.Path @Suppress("WRONG_NEWLINES", "Deprecation") abstract class DiktatTaskBase( @get:Internal internal val extension: DiktatExtension, - private val inputs: PatternFilterable + private val inputs: PatternFilterable, + private val objectFactory: ObjectFactory, ) : DefaultTask(), VerificationTask, com.saveourtool.diktat.plugin.gradle.DiktatJavaExecTaskBase { /** * Files that will be analyzed by diktat @@ -138,8 +143,14 @@ abstract class DiktatTaskBase( init { ignoreFailures = extension.ignoreFailures + group = LifecycleBasePlugin.VERIFICATION_GROUP } + @get:Internal + val reporters: Reporters = objectFactory.newInstance(Reporters::class.java) + + fun reporters(action: Action) = action.execute(reporters) + /** * Function to execute diKTat * diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 68950655ad..42fb084d1e 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -7,6 +7,7 @@ import com.saveourtool.diktat.util.DiktatProcessorListenerWrapper.Companion.unwr import com.pinterest.ktlint.cli.reporter.json.JsonReporter import com.pinterest.ktlint.cli.reporter.plain.PlainReporter import com.pinterest.ktlint.cli.reporter.sarif.SarifReporter +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder @@ -119,6 +120,12 @@ class DiktatJavaExecTaskTest { diktatConfigFile = project.file("../diktat-analysis.yml") reporter = "json" output = "some.txt" + reporters { rs -> + rs.configure { r -> + r.id.set("json") + r.output.set(project.file("some.txt")) + } + } } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert(task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter) diff --git a/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts b/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts index 532aba2bf6..db10eea2ef 100644 --- a/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts +++ b/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts @@ -1,5 +1,7 @@ package com.saveourtool.diktat.buildutils +import com.saveourtool.diktat.plugin.gradle.tasks.DiktatTaskBase + plugins { id("com.saveourtool.diktat") } @@ -23,3 +25,15 @@ diktat { } } } + +tasks.withType(DiktatTaskBase::class.java) { + reporters { + configure { + id = "plain" + output = project.layout.buildDirectory.file("test.txt") + } + configure { + + } + } +} From 73abfd97a48ffea6307e54e0ec287ddd16d8e76d Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:11:15 +0300 Subject: [PATCH 11/17] reused reporters from extension --- .../diktat/plugin/gradle/DiktatExtension.kt | 19 ++++++---------- .../plugin/gradle/DiktatGradlePlugin.kt | 8 ++++--- .../plugin/gradle/extensions/Reporters.kt | 5 ++--- .../plugin/gradle/tasks/DiktatCheckTask.kt | 8 +++++-- .../plugin/gradle/tasks/DiktatFixTask.kt | 9 ++++++-- .../plugin/gradle/tasks/DiktatTaskBase.kt | 6 +++-- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 22 ++++++++++++++----- .../plugin/gradle/ReporterSelectionTest.kt | 6 ++++- ...diktat-convention-configuration.gradle.kts | 6 +++++ 9 files changed, 58 insertions(+), 31 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 d59ceb8cf9..c77b24b4c4 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 @@ -16,7 +16,7 @@ import java.io.File */ open class DiktatExtension( private val patternSet: PatternSet, - val reporters: Reporters, + private val reporters: Reporters, ) { /** * Boolean flag to support `ignoreFailures` property of [VerificationTask]. @@ -33,16 +33,6 @@ open class DiktatExtension( */ var githubActions = false - /** - * Type of the reporter to use - */ - var reporter: String = "" - - /** - * Destination for reporter. If empty, will write to stdout. - */ - var output: String = "" - /** * 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. @@ -66,5 +56,10 @@ open class DiktatExtension( action(patternSet) } - fun reporters(action: Action): Unit = action.execute(reporters) + /** + * Configure reporters + * + * @param action configuration lambda for [Reporters] + */ + fun reporters(action: Action): Unit = action.execute(reporters) } 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 a64d1f5325..d0cd03c5fd 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 @@ -1,5 +1,6 @@ package com.saveourtool.diktat.plugin.gradle +import com.saveourtool.diktat.plugin.gradle.extensions.Reporter import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import com.saveourtool.diktat.plugin.gradle.tasks.DiktatCheckTask.Companion.registerDiktatCheckTask import com.saveourtool.diktat.plugin.gradle.tasks.DiktatFixTask.Companion.registerDiktatFixTask @@ -20,17 +21,18 @@ class DiktatGradlePlugin : Plugin { override fun apply(project: Project) { val patternSet = PatternSet() + val reporters = mutableListOf() val diktatExtension = project.extensions.create( DIKTAT_EXTENSION, DiktatExtension::class.java, patternSet, - project.objects.newInstance(Reporters::class.java), + project.objects.newInstance(Reporters::class.java, reporters), ).apply { diktatConfigFile = project.rootProject.file("diktat-analysis.yml") } - project.registerDiktatCheckTask(diktatExtension, patternSet) - project.registerDiktatFixTask(diktatExtension, patternSet) + project.registerDiktatCheckTask(diktatExtension, patternSet, reporters) + project.registerDiktatFixTask(diktatExtension, patternSet, reporters) project.configureMergeReportsTask(diktatExtension) } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt index 340b7f73a1..9aa06c6934 100644 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt +++ b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt @@ -5,10 +5,9 @@ import org.gradle.api.model.ObjectFactory import javax.inject.Inject abstract class Reporters @Inject constructor( - private val objectFactory: ObjectFactory + private val objectFactory: ObjectFactory, + private val values: MutableList, ) { - val values = mutableListOf() - fun configure(action: Action): Unit = action.execute(objectFactory.newInstance(Reporter::class.java).apply { values.add(this) }) } 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 059da11dd4..5208c6307c 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 @@ -4,6 +4,8 @@ import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin +import com.saveourtool.diktat.plugin.gradle.extensions.Reporter +import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.TaskProvider @@ -28,15 +30,17 @@ abstract class DiktatCheckTask @Inject constructor( /** * @param diktatExtension [DiktatExtension] with some values for task configuration * @param patternSet [PatternSet] to discover files for diktat check + * @param reporters [List] of [Reporter] to configure reporters for diktat check * @return a [TaskProvider] */ fun Project.registerDiktatCheckTask( diktatExtension: DiktatExtension, - patternSet: PatternSet + patternSet: PatternSet, + reporters: List, ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_CHECK_TASK, DiktatCheckTask::class.java, - diktatExtension, patternSet + diktatExtension, patternSet, reporters, ) } } 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 5742f842e6..20b1bde826 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 @@ -4,6 +4,8 @@ import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin +import com.saveourtool.diktat.plugin.gradle.extensions.Reporter +import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.TaskProvider @@ -17,6 +19,7 @@ import javax.inject.Inject abstract class DiktatFixTask @Inject constructor( extension: DiktatExtension, inputs: PatternFilterable, + reporters: List, objectFactory: ObjectFactory, ) : DiktatTaskBase(extension, inputs, objectFactory) { override fun doRun( @@ -30,15 +33,17 @@ abstract class DiktatFixTask @Inject constructor( /** * @param diktatExtension [DiktatExtension] with some values for task configuration * @param patternSet [PatternSet] to discover files for diktat fix + * @param reporters [List] of [Reporter] to configure reporters for diktat fix * @return a [TaskProvider] */ fun Project.registerDiktatFixTask( diktatExtension: DiktatExtension, - patternSet: PatternSet + patternSet: PatternSet, + reporters: List, ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_FIX_TASK, DiktatFixTask::class.java, - diktatExtension, patternSet + diktatExtension, patternSet, reporters, ) } } 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 f51b436448..9d233944e9 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 @@ -9,6 +9,7 @@ import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl import com.saveourtool.diktat.plugin.gradle.DiktatExtension +import com.saveourtool.diktat.plugin.gradle.extensions.Reporter import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import com.saveourtool.diktat.plugin.gradle.getOutputFile import com.saveourtool.diktat.plugin.gradle.getReporterType @@ -47,6 +48,7 @@ import java.nio.file.Path abstract class DiktatTaskBase( @get:Internal internal val extension: DiktatExtension, private val inputs: PatternFilterable, + private val reporters: List, private val objectFactory: ObjectFactory, ) : DefaultTask(), VerificationTask, com.saveourtool.diktat.plugin.gradle.DiktatJavaExecTaskBase { /** @@ -147,9 +149,9 @@ abstract class DiktatTaskBase( } @get:Internal - val reporters: Reporters = objectFactory.newInstance(Reporters::class.java) + val _reporters: Reporters = objectFactory.newInstance(Reporters::class.java) - fun reporters(action: Action) = action.execute(reporters) + fun reporters(action: Action) = action.execute(_reporters) /** * Function to execute diKTat diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 42fb084d1e..d5c617f2b2 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -118,8 +118,6 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") - reporter = "json" - output = "some.txt" reporters { rs -> rs.configure { r -> r.id.set("json") @@ -136,7 +134,11 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") - reporter = "json" + reporters { rs -> + rs.configure { r -> + r.id.set("json") + } + } } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert( @@ -165,8 +167,12 @@ class DiktatJavaExecTaskTest { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") githubActions = true - reporter = "json" - output = "report.json" + reporters { rs -> + rs.configure { r -> + r.id.set("json") + r.output.set(project.file("report.json")) + } + } } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask val (firstReporter, secondReporter) = task.diktatRunner.diktatReporter @@ -191,7 +197,11 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") - reporter = "sarif" + reporters { rs -> + rs.configure { r -> + r.id.set("sarif") + } + } } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert(task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter) diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt index 3d9d9e71d4..9e36c568ce 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt @@ -1,5 +1,6 @@ package com.saveourtool.diktat.plugin.gradle +import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Project import org.gradle.api.tasks.util.PatternSet import org.gradle.testfixtures.ProjectBuilder @@ -22,7 +23,10 @@ class ReporterSelectionTest { @Test fun `should fallback to plain reporter for unknown reporter types`() { - val diktatExtension = DiktatExtension(PatternSet()).apply { + val diktatExtension = DiktatExtension( + PatternSet(), + object : Reporters(project.objects) {}, + ).apply { reporter = "jsonx" } diff --git a/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts b/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts index db10eea2ef..129a935dd2 100644 --- a/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts +++ b/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts @@ -9,6 +9,12 @@ plugins { diktat { diktatConfigFile = rootProject.file("diktat-analysis.yml") githubActions = findProperty("diktat.githubActions")?.toString()?.toBoolean() ?: false + reporters { + configure { + id = "plain" + output = file("text.txt") + } + } inputs { // using `Project#path` here, because it must be unique in gradle's project hierarchy if (path == rootProject.path) { From 2ac8f2b910f943c5d41f79be1b10f535eaea8fe1 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:26:36 +0300 Subject: [PATCH 12/17] reverted changes about reporters --- build.gradle.kts | 1 - .../diktat/cli/DiktatProperties.kt | 7 ++++--- .../diktat/plugin/gradle/DiktatExtension.kt | 21 ++++++++++--------- .../plugin/gradle/DiktatGradlePlugin.kt | 11 +++------- .../plugin/gradle/extensions/Reporter.kt | 14 ------------- .../plugin/gradle/extensions/Reporters.kt | 13 ------------ .../plugin/gradle/tasks/DiktatCheckTask.kt | 12 +++-------- .../plugin/gradle/tasks/DiktatFixTask.kt | 11 ++-------- .../plugin/gradle/tasks/DiktatTaskBase.kt | 13 ++---------- .../plugin/gradle/ReporterSelectionTest.kt | 2 -- 10 files changed, 25 insertions(+), 80 deletions(-) delete mode 100644 diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt delete mode 100644 diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt diff --git a/build.gradle.kts b/build.gradle.kts index 071b4c417b..83c0b37d01 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,4 @@ import org.jetbrains.kotlin.incremental.createDirectory -import java.nio.file.CopyOption import java.nio.file.Files import java.nio.file.StandardCopyOption diff --git a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt index eaa71480e9..49a2b74393 100644 --- a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt +++ b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/cli/DiktatProperties.kt @@ -4,6 +4,7 @@ import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.api.DiktatReporterCreationArguments import com.saveourtool.diktat.api.DiktatReporterFactory +import com.saveourtool.diktat.api.DiktatReporterFactory.Companion.PLAIN_ID import com.saveourtool.diktat.common.config.rules.DIKTAT import com.saveourtool.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF import com.saveourtool.diktat.util.isKotlinCodeOrScript @@ -74,7 +75,7 @@ data class DiktatProperties( sourceRootDir: Path, loggingListener: DiktatProcessorListener, ): DiktatRunnerArguments { - val reporterArguments = DiktatReporterCreationArguments( + val reporterCreationArguments = DiktatReporterCreationArguments( id = reporterProviderId, outputStream = getReporterOutput(), groupByFileInPlain = groupByFileInPlain, @@ -86,7 +87,7 @@ data class DiktatProperties( sourceRootDir = sourceRootDir, files = getFiles(sourceRootDir), baselineFile = null, - reporterArgsList = listOf(reporterArguments), + reporterArgsList = listOf(reporterCreationArguments), loggingListener = loggingListener ) } @@ -212,7 +213,7 @@ data class DiktatProperties( shortName = "r", description = "The reporter to use" ) - .default("plain") + .default(PLAIN_ID) /** * @param diktatReporterFactory 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 c77b24b4c4..a640e680c5 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,6 +1,5 @@ package com.saveourtool.diktat.plugin.gradle -import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Action import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.PathSensitive @@ -15,8 +14,7 @@ import java.io.File * @param patternSet */ open class DiktatExtension( - private val patternSet: PatternSet, - private val reporters: Reporters, + private val patternSet: PatternSet ) { /** * Boolean flag to support `ignoreFailures` property of [VerificationTask]. @@ -33,6 +31,16 @@ open class DiktatExtension( */ var githubActions = false + /** + * Type of the reporter to use + */ + var reporter: String = "" + + /** + * Destination for reporter. If empty, will write to stdout. + */ + var output: String = "" + /** * 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. @@ -55,11 +63,4 @@ open class DiktatExtension( fun inputs(action: PatternFilterable.() -> Unit) { action(patternSet) } - - /** - * Configure reporters - * - * @param action configuration lambda for [Reporters] - */ - fun reporters(action: Action): Unit = action.execute(reporters) } 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 d0cd03c5fd..8ed30418fe 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 @@ -1,7 +1,5 @@ package com.saveourtool.diktat.plugin.gradle -import com.saveourtool.diktat.plugin.gradle.extensions.Reporter -import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import com.saveourtool.diktat.plugin.gradle.tasks.DiktatCheckTask.Companion.registerDiktatCheckTask import com.saveourtool.diktat.plugin.gradle.tasks.DiktatFixTask.Companion.registerDiktatFixTask import com.saveourtool.diktat.plugin.gradle.tasks.configureMergeReportsTask @@ -19,20 +17,17 @@ class DiktatGradlePlugin : Plugin { */ @Suppress("TOO_LONG_FUNCTION") override fun apply(project: Project) { - val patternSet = PatternSet() - val reporters = mutableListOf() val diktatExtension = project.extensions.create( DIKTAT_EXTENSION, DiktatExtension::class.java, - patternSet, - project.objects.newInstance(Reporters::class.java, reporters), + patternSet ).apply { diktatConfigFile = project.rootProject.file("diktat-analysis.yml") } - project.registerDiktatCheckTask(diktatExtension, patternSet, reporters) - project.registerDiktatFixTask(diktatExtension, patternSet, reporters) + project.registerDiktatCheckTask(diktatExtension, patternSet) + project.registerDiktatFixTask(diktatExtension, patternSet) project.configureMergeReportsTask(diktatExtension) } diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt deleted file mode 100644 index 721ea7830b..0000000000 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporter.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.saveourtool.diktat.plugin.gradle.extensions - -import org.gradle.api.file.RegularFileProperty -import org.gradle.api.provider.Property -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.OutputFile - -abstract class Reporter { - @get:Input - abstract val id: Property - - @get:OutputFile - abstract val output: RegularFileProperty -} diff --git a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt b/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt deleted file mode 100644 index 9aa06c6934..0000000000 --- a/diktat-gradle-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/gradle/extensions/Reporters.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.saveourtool.diktat.plugin.gradle.extensions - -import org.gradle.api.Action -import org.gradle.api.model.ObjectFactory -import javax.inject.Inject - -abstract class Reporters @Inject constructor( - private val objectFactory: ObjectFactory, - private val values: MutableList, -) { - fun configure(action: Action): Unit = - action.execute(objectFactory.newInstance(Reporter::class.java).apply { values.add(this) }) -} 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 5208c6307c..69cc647458 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 @@ -4,10 +4,7 @@ import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin -import com.saveourtool.diktat.plugin.gradle.extensions.Reporter -import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Project -import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.util.PatternFilterable import org.gradle.api.tasks.util.PatternSet @@ -18,9 +15,8 @@ import javax.inject.Inject */ abstract class DiktatCheckTask @Inject constructor( extension: DiktatExtension, - inputs: PatternFilterable, - objectFactory: ObjectFactory, -) : DiktatTaskBase(extension, inputs, objectFactory) { + inputs: PatternFilterable +) : DiktatTaskBase(extension, inputs) { override fun doRun( runner: DiktatRunner, args: DiktatRunnerArguments @@ -30,17 +26,15 @@ abstract class DiktatCheckTask @Inject constructor( /** * @param diktatExtension [DiktatExtension] with some values for task configuration * @param patternSet [PatternSet] to discover files for diktat check - * @param reporters [List] of [Reporter] to configure reporters for diktat check * @return a [TaskProvider] */ fun Project.registerDiktatCheckTask( diktatExtension: DiktatExtension, patternSet: PatternSet, - reporters: List, ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_CHECK_TASK, DiktatCheckTask::class.java, - diktatExtension, patternSet, reporters, + diktatExtension, patternSet, ) } } 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 20b1bde826..923e19baeb 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 @@ -4,10 +4,7 @@ import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin -import com.saveourtool.diktat.plugin.gradle.extensions.Reporter -import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Project -import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.util.PatternFilterable import org.gradle.api.tasks.util.PatternSet @@ -19,9 +16,7 @@ import javax.inject.Inject abstract class DiktatFixTask @Inject constructor( extension: DiktatExtension, inputs: PatternFilterable, - reporters: List, - objectFactory: ObjectFactory, -) : DiktatTaskBase(extension, inputs, objectFactory) { +) : DiktatTaskBase(extension, inputs) { override fun doRun( runner: DiktatRunner, args: DiktatRunnerArguments @@ -33,17 +28,15 @@ abstract class DiktatFixTask @Inject constructor( /** * @param diktatExtension [DiktatExtension] with some values for task configuration * @param patternSet [PatternSet] to discover files for diktat fix - * @param reporters [List] of [Reporter] to configure reporters for diktat fix * @return a [TaskProvider] */ fun Project.registerDiktatFixTask( diktatExtension: DiktatExtension, patternSet: PatternSet, - reporters: List, ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_FIX_TASK, DiktatFixTask::class.java, - diktatExtension, patternSet, reporters, + diktatExtension, patternSet, ) } } 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 9d233944e9..eec7e53805 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 @@ -9,8 +9,6 @@ import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl import com.saveourtool.diktat.plugin.gradle.DiktatExtension -import com.saveourtool.diktat.plugin.gradle.extensions.Reporter -import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import com.saveourtool.diktat.plugin.gradle.getOutputFile import com.saveourtool.diktat.plugin.gradle.getReporterType import com.saveourtool.diktat.plugin.gradle.getSourceRootDir @@ -48,8 +46,6 @@ import java.nio.file.Path abstract class DiktatTaskBase( @get:Internal internal val extension: DiktatExtension, private val inputs: PatternFilterable, - private val reporters: List, - private val objectFactory: ObjectFactory, ) : DefaultTask(), VerificationTask, com.saveourtool.diktat.plugin.gradle.DiktatJavaExecTaskBase { /** * Files that will be analyzed by diktat @@ -111,7 +107,7 @@ abstract class DiktatTaskBase( null } val reporterId = project.getReporterType(extension) - val reporterArguments = DiktatReporterCreationArguments( + val reporterCreationArguments = DiktatReporterCreationArguments( id = reporterId, outputStream = project.getOutputFile(extension)?.outputStream(), sourceRootDir = sourceRootDir.takeIf { reporterId == "sarif" }, @@ -130,7 +126,7 @@ abstract class DiktatTaskBase( sourceRootDir = project.getSourceRootDir(extension), files = actualInputs.files.map { it.toPath() }, baselineFile = extension.baseline?.let { project.file(it).toPath() }, - reporterArgsList = listOf(githubActionsReporterArgs, reporterArguments).mapNotNull { it }, + reporterArgsList = listOf(githubActionsReporterArgs, reporterCreationArguments).mapNotNull { it }, loggingListener = loggingListener, ) } @@ -148,11 +144,6 @@ abstract class DiktatTaskBase( group = LifecycleBasePlugin.VERIFICATION_GROUP } - @get:Internal - val _reporters: Reporters = objectFactory.newInstance(Reporters::class.java) - - fun reporters(action: Action) = action.execute(_reporters) - /** * Function to execute diKTat * diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt index 9e36c568ce..9911e47a67 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt @@ -1,6 +1,5 @@ package com.saveourtool.diktat.plugin.gradle -import com.saveourtool.diktat.plugin.gradle.extensions.Reporters import org.gradle.api.Project import org.gradle.api.tasks.util.PatternSet import org.gradle.testfixtures.ProjectBuilder @@ -25,7 +24,6 @@ class ReporterSelectionTest { fun `should fallback to plain reporter for unknown reporter types`() { val diktatExtension = DiktatExtension( PatternSet(), - object : Reporters(project.objects) {}, ).apply { reporter = "jsonx" } From dc79d9e6378b23a6ce990894088ebf268bcf3aa8 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:34:43 +0300 Subject: [PATCH 13/17] reverted additional changes --- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 27 ++++--------------- .../plugin/gradle/ReporterSelectionTest.kt | 4 +-- .../diktat/plugin/maven/DiktatBaseMojo.kt | 10 +++---- ...diktat-convention-configuration.gradle.kts | 20 -------------- 4 files changed, 11 insertions(+), 50 deletions(-) diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index d5c617f2b2..dc97154c83 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -118,12 +118,8 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") - reporters { rs -> - rs.configure { r -> - r.id.set("json") - r.output.set(project.file("some.txt")) - } - } + reporter = "json" + output = "some.txt" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert(task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter) @@ -134,11 +130,7 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") - reporters { rs -> - rs.configure { r -> - r.id.set("json") - } - } + reporter = "json" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert( @@ -167,12 +159,8 @@ class DiktatJavaExecTaskTest { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") githubActions = true - reporters { rs -> - rs.configure { r -> - r.id.set("json") - r.output.set(project.file("report.json")) - } - } + reporter = "json" + output = "report.json" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask val (firstReporter, secondReporter) = task.diktatRunner.diktatReporter @@ -197,11 +185,6 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") - reporters { rs -> - rs.configure { r -> - r.id.set("sarif") - } - } } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert(task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter) diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt index 9911e47a67..3d9d9e71d4 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/ReporterSelectionTest.kt @@ -22,9 +22,7 @@ class ReporterSelectionTest { @Test fun `should fallback to plain reporter for unknown reporter types`() { - val diktatExtension = DiktatExtension( - PatternSet(), - ).apply { + val diktatExtension = DiktatExtension(PatternSet()).apply { reporter = "jsonx" } diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index 13e80f8d07..ee5ac62696 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -121,10 +121,6 @@ abstract class DiktatBaseMojo : AbstractMojo() { val sourceRootDir = mavenProject.basedir.parentFile.toPath() val reporterArgsList = buildList { - if (githubActions) { - val outputStream = FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) - add(DiktatReporterCreationArguments(id = "sarif", outputStream = outputStream, sourceRootDir = sourceRootDir)) - } val reporterId = getReporterType() add( DiktatReporterCreationArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = sourceRootDir.takeIf { reporterId == "sarif" }) @@ -147,7 +143,9 @@ abstract class DiktatBaseMojo : AbstractMojo() { } } - private fun getReporterType(): String = if (reporter in setOf("sarif", "plain", "json", "html")) { + private fun getReporterType(): String = if (githubActions) { + "sarif" + } else if (reporter in setOf("sarif", "plain", "json", "html")) { reporter } else { log.warn("Reporter name ${this.reporter} was not specified or is invalid. Falling to 'plain' reporter") @@ -156,6 +154,8 @@ abstract class DiktatBaseMojo : AbstractMojo() { private fun getReporterOutput(): OutputStream? = if (output.isNotBlank()) { FileOutputStream(this.output, false) + } else if (githubActions) { + FileOutputStream("${mavenProject.basedir}/${mavenProject.name}.sarif", false) } else { null } diff --git a/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts b/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts index 129a935dd2..532aba2bf6 100644 --- a/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts +++ b/gradle/plugins/src/main/kotlin/com/saveourtool/diktat/buildutils/diktat-convention-configuration.gradle.kts @@ -1,7 +1,5 @@ package com.saveourtool.diktat.buildutils -import com.saveourtool.diktat.plugin.gradle.tasks.DiktatTaskBase - plugins { id("com.saveourtool.diktat") } @@ -9,12 +7,6 @@ plugins { diktat { diktatConfigFile = rootProject.file("diktat-analysis.yml") githubActions = findProperty("diktat.githubActions")?.toString()?.toBoolean() ?: false - reporters { - configure { - id = "plain" - output = file("text.txt") - } - } inputs { // using `Project#path` here, because it must be unique in gradle's project hierarchy if (path == rootProject.path) { @@ -31,15 +23,3 @@ diktat { } } } - -tasks.withType(DiktatTaskBase::class.java) { - reporters { - configure { - id = "plain" - output = project.layout.buildDirectory.file("test.txt") - } - configure { - - } - } -} From 9f49b3999d857ba6a4265c42c7184498417b1b09 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:40:13 +0300 Subject: [PATCH 14/17] reverted changed in gradle plugin --- .../diktat/plugin/gradle/DiktatExtension.kt | 1 - .../saveourtool/diktat/plugin/gradle/Utils.kt | 15 +++++++++++++ .../plugin/gradle/tasks/DiktatTaskBase.kt | 21 ++----------------- .../plugin/gradle/DiktatJavaExecTaskTest.kt | 4 +--- 4 files changed, 18 insertions(+), 23 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 a640e680c5..a51ed2152f 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,6 +1,5 @@ package com.saveourtool.diktat.plugin.gradle -import org.gradle.api.Action import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity 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 e0bd4866e0..273f70df71 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 @@ -8,6 +8,7 @@ package com.saveourtool.diktat.plugin.gradle import org.gradle.api.Project import java.io.File +import java.nio.file.Files import java.nio.file.Path /** @@ -15,6 +16,7 @@ import java.nio.file.Path * @return returns sourceRootDir as projectDir for sarif report */ fun Project.getSourceRootDir(diktatExtension: DiktatExtension): Path? = when { + diktatExtension.githubActions -> projectDir.toPath() diktatExtension.reporter == "sarif" -> projectDir.toPath() else -> null } @@ -30,6 +32,12 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { val name = diktatExtension.reporter.trim() val validReporters = listOf("sarif", "plain", "json", "html") val reporterType = when { + diktatExtension.githubActions -> { + if (diktatExtension.reporter.isNotEmpty()) { + logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used") + } + "sarif" + } name.isEmpty() -> { logger.info("Reporter name was not set. Using 'plain' reporter") "plain" @@ -52,6 +60,13 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String { * @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 } 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 eec7e53805..3f3c77ae41 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 @@ -89,23 +89,6 @@ abstract class DiktatTaskBase( val sourceRootDir by lazy { project.rootProject.projectDir.toPath() } - val githubActionsReporterArgs = if (extension.githubActions) { - val outputStream = project.layout.buildDirectory - .file("reports/diktat/diktat.sarif") - .get() - .asFile - .also { - Files.createDirectories(it.parentFile.toPath()) - } - .outputStream() - DiktatReporterCreationArguments( - id = "sarif", - outputStream = outputStream, - sourceRootDir = sourceRootDir, - ) - } else { - null - } val reporterId = project.getReporterType(extension) val reporterCreationArguments = DiktatReporterCreationArguments( id = reporterId, @@ -123,10 +106,10 @@ abstract class DiktatTaskBase( } DiktatRunnerArguments( configFile = extension.diktatConfigFile.toPath(), - sourceRootDir = project.getSourceRootDir(extension), + sourceRootDir = sourceRootDir, files = actualInputs.files.map { it.toPath() }, baselineFile = extension.baseline?.let { project.file(it).toPath() }, - reporterArgsList = listOf(githubActionsReporterArgs, reporterCreationArguments).mapNotNull { it }, + reporterArgsList = listOf(reporterCreationArguments), loggingListener = loggingListener, ) } diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index dc97154c83..01e78e3f0c 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -133,9 +133,7 @@ class DiktatJavaExecTaskTest { reporter = "json" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - assert( - task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter - ) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is JsonReporter) } @Test From 02d687b2d7d726c3d29834e3adb32356cc2a3af0 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:44:24 +0300 Subject: [PATCH 15/17] reverted changes --- .../diktat/plugin/gradle/tasks/DiktatCheckTask.kt | 4 ++-- .../diktat/plugin/gradle/tasks/DiktatFixTask.kt | 6 +++--- .../diktat/plugin/gradle/DiktatJavaExecTaskTest.kt | 1 - .../diktat/plugin/maven/DiktatBaseMojo.kt | 14 +++++++------- 4 files changed, 12 insertions(+), 13 deletions(-) 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 69cc647458..6f656825a8 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 @@ -30,11 +30,11 @@ abstract class DiktatCheckTask @Inject constructor( */ fun Project.registerDiktatCheckTask( diktatExtension: DiktatExtension, - patternSet: PatternSet, + patternSet: PatternSet ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_CHECK_TASK, DiktatCheckTask::class.java, - diktatExtension, patternSet, + diktatExtension, patternSet ) } } 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 923e19baeb..0e7fdc4e4e 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,7 +15,7 @@ import javax.inject.Inject */ abstract class DiktatFixTask @Inject constructor( extension: DiktatExtension, - inputs: PatternFilterable, + inputs: PatternFilterable ) : DiktatTaskBase(extension, inputs) { override fun doRun( runner: DiktatRunner, @@ -32,11 +32,11 @@ abstract class DiktatFixTask @Inject constructor( */ fun Project.registerDiktatFixTask( diktatExtension: DiktatExtension, - patternSet: PatternSet, + patternSet: PatternSet ): TaskProvider = tasks.register( DiktatGradlePlugin.DIKTAT_FIX_TASK, DiktatFixTask::class.java, - diktatExtension, patternSet, + diktatExtension, patternSet ) } } diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index 01e78e3f0c..d85e6deea4 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -7,7 +7,6 @@ import com.saveourtool.diktat.util.DiktatProcessorListenerWrapper.Companion.unwr import com.pinterest.ktlint.cli.reporter.json.JsonReporter import com.pinterest.ktlint.cli.reporter.plain.PlainReporter import com.pinterest.ktlint.cli.reporter.sarif.SarifReporter -import org.gradle.api.Action import org.gradle.api.Project import org.gradle.testfixtures.ProjectBuilder diff --git a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt index ee5ac62696..d29f7d954f 100644 --- a/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt +++ b/diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt @@ -120,18 +120,18 @@ abstract class DiktatBaseMojo : AbstractMojo() { ) val sourceRootDir = mavenProject.basedir.parentFile.toPath() - val reporterArgsList = buildList { - val reporterId = getReporterType() - add( - DiktatReporterCreationArguments(id = getReporterType(), outputStream = getReporterOutput(), sourceRootDir = sourceRootDir.takeIf { reporterId == "sarif" }) - ) - } + val reporterId = getReporterType() + val reporterArgs = DiktatReporterCreationArguments( + id = reporterId, + outputStream = getReporterOutput(), + sourceRootDir = sourceRootDir.takeIf { reporterId == "sarif" }, + ) val args = DiktatRunnerArguments( configInputStream = configFile.inputStream(), sourceRootDir = sourceRootDir, files = inputs.map(::Path), baselineFile = baseline?.toPath(), - reporterArgsList = reporterArgsList, + reporterArgsList = listOf(reporterArgs), ) val diktatRunner = diktatRunnerFactory(args) val errorCounter = runAction( From 5656f3c492f0116f9dc07f01db83c0a3c343231e Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:48:51 +0300 Subject: [PATCH 16/17] reverted changes in tests --- .../diktat/plugin/gradle/DiktatJavaExecTaskTest.kt | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt index d85e6deea4..c5b4d1430f 100644 --- a/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt +++ b/diktat-gradle-plugin/src/test/kotlin/com/saveourtool/diktat/plugin/gradle/DiktatJavaExecTaskTest.kt @@ -160,17 +160,7 @@ class DiktatJavaExecTaskTest { output = "report.json" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask - val (firstReporter, secondReporter) = task.diktatRunner.diktatReporter - .unwrap>().first() - .unwrap() - .unwrap>() - .toList() - assert( - firstReporter.unwrapToKtlint() is SarifReporter - ) - assert( - secondReporter.unwrapToKtlint() is JsonReporter - ) + assert(task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter) Assertions.assertEquals( project.rootDir.toString(), System.getProperty("user.home") @@ -182,6 +172,7 @@ class DiktatJavaExecTaskTest { assertFiles(emptyList()) { inputs { exclude("*") } diktatConfigFile = project.file("../diktat-analysis.yml") + reporter = "sarif" } val task = project.tasks.getByName(DIKTAT_CHECK_TASK) as DiktatCheckTask assert(task.diktatRunner.diktatReporter.unwrapFirst() is SarifReporter) From ef4fb6bb7ce954717fa58f40f314fa4f398eecfd Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 16 Nov 2023 18:59:46 +0300 Subject: [PATCH 17/17] diktatFix --- .../saveourtool/diktat/plugin/gradle/tasks/DiktatTaskBase.kt | 4 ---- 1 file changed, 4 deletions(-) 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 3f3c77ae41..d200f19278 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 @@ -11,17 +11,14 @@ import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.getOutputFile import com.saveourtool.diktat.plugin.gradle.getReporterType -import com.saveourtool.diktat.plugin.gradle.getSourceRootDir 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.Action import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.file.FileCollection -import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.IgnoreEmptyDirectories import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal @@ -32,7 +29,6 @@ import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.VerificationTask import org.gradle.api.tasks.util.PatternFilterable import org.gradle.language.base.plugins.LifecycleBasePlugin -import java.nio.file.Files import java.nio.file.Path