diff --git a/diktat-cli/build.gradle.kts b/diktat-cli/build.gradle.kts index bd595ab1f2..090bda126e 100644 --- a/diktat-cli/build.gradle.kts +++ b/diktat-cli/build.gradle.kts @@ -15,15 +15,16 @@ plugins { project.description = "This module builds diktat-cli to run diktat as CLI using ktlint" dependencies { - implementation(projects.diktatApi) - implementation(projects.diktatKtlintEngine) - implementation(projects.diktatRules) + implementation(projects.diktatRunner) implementation(libs.kotlinx.cli) + implementation(libs.kotlinx.serialization.core) implementation(libs.kotlin.logging) implementation(libs.slf4j.api) implementation(libs.log4j2.core) implementation(libs.log4j2.slf4j2) + testImplementation(projects.diktatKtlintEngine) + testImplementation(projects.diktatRules) testImplementation(projects.diktatCommonTest) testImplementation(libs.junit.jupiter) testImplementation(libs.junit.platform.suite) diff --git a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/DiktatMain.kt b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/DiktatMain.kt index 11e93b1769..5bbae68f30 100644 --- a/diktat-cli/src/main/kotlin/com/saveourtool/diktat/DiktatMain.kt +++ b/diktat-cli/src/main/kotlin/com/saveourtool/diktat/DiktatMain.kt @@ -7,11 +7,6 @@ package com.saveourtool.diktat import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.cli.DiktatMode import com.saveourtool.diktat.cli.DiktatProperties -import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl -import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl -import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl -import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl -import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl import io.github.oshai.kotlinlogging.KotlinLogging @@ -31,13 +26,6 @@ private val loggingListener = object : DiktatProcessorListener { } fun main(args: Array) { - val diktatRunnerFactory = DiktatRunnerFactory( - DiktatRuleConfigReaderImpl(), - DiktatRuleSetFactoryImpl(), - DiktatProcessorFactoryImpl(), - DiktatBaselineFactoryImpl(), - DiktatReporterFactoryImpl(), - ) val properties = DiktatProperties.parse(diktatRunnerFactory.diktatReporterFactory, args) properties.configureLogger() 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 39d13e25af..1344cb368b 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 @@ -1,6 +1,7 @@ package com.saveourtool.diktat.cli import com.saveourtool.diktat.DiktatRunnerArguments +import com.saveourtool.diktat.ENGINE_INFO import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.api.DiktatReporterCreationArguments import com.saveourtool.diktat.api.DiktatReporterFactory @@ -10,14 +11,16 @@ import com.saveourtool.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF import com.saveourtool.diktat.util.isKotlinCodeOrScript import com.saveourtool.diktat.util.tryToPathIfExists import com.saveourtool.diktat.util.walkByGlob + import generated.DIKTAT_VERSION -import generated.KTLINT_VERSION import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.core.LoggerContext import org.slf4j.event.Level + import java.io.OutputStream import java.nio.file.Path import java.nio.file.Paths + import kotlin.io.path.createDirectories import kotlin.io.path.inputStream import kotlin.io.path.outputStream @@ -169,7 +172,7 @@ data class DiktatProperties( ) { """ Diktat: $DIKTAT_VERSION - Ktlint: $KTLINT_VERSION + $ENGINE_INFO """.trimIndent() } parser.addOptionAndShowTextWithExit( diff --git a/diktat-common/src/main/kotlin/com/saveourtool/diktat/common/cli/CliArgument.kt b/diktat-common/src/main/kotlin/com/saveourtool/diktat/common/cli/CliArgument.kt deleted file mode 100644 index 319455e313..0000000000 --- a/diktat-common/src/main/kotlin/com/saveourtool/diktat/common/cli/CliArgument.kt +++ /dev/null @@ -1,33 +0,0 @@ -package com.saveourtool.diktat.common.cli - -import org.apache.commons.cli.Option - -import kotlinx.serialization.Serializable - -/** - * This class is used to serialize/deserialize json representation - * that is used to store command line arguments - * - * @param shortName short argument representation like -h - * @param helpDescr - * @param longName long argument representation like --help - * @param hasArgs indicates if option should have explicit argument - * @param isRequired - */ -@Serializable -data class CliArgument( - private val shortName: String, - private val helpDescr: String, - private val longName: String, - private val hasArgs: Boolean, - private val isRequired: Boolean -) { - /** - * Converts parameters received from json to [Option] - * - * @return an [Option] - */ - fun convertToOption() = Option(shortName, longName, hasArgs, helpDescr).apply { - isRequired = this@CliArgument.isRequired - } -} diff --git a/diktat-common/src/main/kotlin/com/saveourtool/diktat/common/config/reader/ApplicationProperties.kt b/diktat-common/src/main/kotlin/com/saveourtool/diktat/common/config/reader/ApplicationProperties.kt deleted file mode 100644 index 52a5b79e59..0000000000 --- a/diktat-common/src/main/kotlin/com/saveourtool/diktat/common/config/reader/ApplicationProperties.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.saveourtool.diktat.common.config.reader - -import io.github.oshai.kotlinlogging.KotlinLogging - -import java.io.IOException -import java.util.Properties - -import kotlin.system.exitProcess - -/** - * Base class for working with properties files. - */ -@Suppress("SwallowedException") -open class ApplicationProperties(propertiesFileName: String) { - /** - * The [Properties] loaded from a file. - */ - val properties: Properties = Properties() - - init { - val propStream = javaClass.classLoader.getResourceAsStream(propertiesFileName) - propStream?.let { - try { - properties.load(it) - } catch (e: IOException) { - errorReadingConfig(propertiesFileName) - } - } - ?: errorReadingConfig(propertiesFileName) - } - - private fun errorReadingConfig(propertiesFileName: String) { - log.error { - "Cannot read file $propertiesFileName with configuration properties" - } - exitProcess(EXIT_STATUS_MISSING_PROPERTIES) - } - - companion object { - private val log = KotlinLogging.logger {} - private const val EXIT_STATUS_MISSING_PROPERTIES = 4 - } -} diff --git a/diktat-gradle-plugin/build.gradle.kts b/diktat-gradle-plugin/build.gradle.kts index 01e5b6de3f..65c2962af6 100644 --- a/diktat-gradle-plugin/build.gradle.kts +++ b/diktat-gradle-plugin/build.gradle.kts @@ -17,14 +17,13 @@ plugins { dependencies { implementation(kotlin("gradle-plugin-api")) - - implementation(projects.diktatRules) - implementation(projects.diktatKtlintEngine) + implementation(projects.diktatRunner) // merge sarif reports implementation(libs.sarif4k.jvm) implementation(libs.kotlinx.serialization.json) testImplementation(libs.junit.jupiter.api) testRuntimeOnly(libs.junit.jupiter.engine) + testImplementation(projects.diktatKtlintEngine) testImplementation(libs.ktlint.cli.reporter.core) testImplementation(libs.ktlint.cli.reporter.json) testImplementation(libs.ktlint.cli.reporter.plain) 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 21e8553e2b..19339a169b 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 @@ -3,21 +3,17 @@ package com.saveourtool.diktat.plugin.gradle.tasks import com.saveourtool.diktat.DiktatRunner import com.saveourtool.diktat.DiktatRunnerArguments import com.saveourtool.diktat.DiktatRunnerFactory +import com.saveourtool.diktat.ENGINE_INFO import com.saveourtool.diktat.api.DiktatProcessorListener import com.saveourtool.diktat.api.DiktatReporterCreationArguments import com.saveourtool.diktat.api.DiktatReporterType -import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl -import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl -import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl +import com.saveourtool.diktat.diktatRunnerFactory import com.saveourtool.diktat.plugin.gradle.DiktatExtension import com.saveourtool.diktat.plugin.gradle.extension.DefaultReporter import com.saveourtool.diktat.plugin.gradle.extension.PlainReporter import com.saveourtool.diktat.plugin.gradle.extension.Reporters -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.DefaultTask import org.gradle.api.GradleException import org.gradle.api.file.ConfigurableFileCollection @@ -110,18 +106,6 @@ 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 = diktatReporterFactory, - ) - } private val diktatRunnerArguments by lazy { val sourceRootDir by lazy { project.rootProject.projectDir.toPath() @@ -177,7 +161,7 @@ abstract class DiktatTaskBase( @TaskAction fun run() { if (extension.debug) { - project.logger.lifecycle("Running diktat $DIKTAT_VERSION with ktlint $KTLINT_VERSION") + project.logger.lifecycle("Running diktat $DIKTAT_VERSION with $ENGINE_INFO") } if (!shouldRun) { /* diff --git a/diktat-maven-plugin/build.gradle.kts b/diktat-maven-plugin/build.gradle.kts index 3497312994..3022df404d 100644 --- a/diktat-maven-plugin/build.gradle.kts +++ b/diktat-maven-plugin/build.gradle.kts @@ -16,8 +16,7 @@ dependencies { compileOnly(libs.maven.core) implementation(libs.kotlin.stdlib.jdk8) - implementation(projects.diktatRules) - implementation(projects.diktatKtlintEngine) + implementation(projects.diktatRunner) testImplementation(libs.junit.jupiter.api) testImplementation(libs.junit.vintage.engine) 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 f0475f5f7d..32307c7a77 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 @@ -2,14 +2,9 @@ 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.DiktatReporterCreationArguments import com.saveourtool.diktat.api.DiktatReporterType -import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl -import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl -import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl -import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl -import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl +import com.saveourtool.diktat.diktatRunnerFactory import org.apache.maven.plugin.AbstractMojo import org.apache.maven.plugin.Mojo @@ -57,18 +52,6 @@ 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. diff --git a/diktat-runner/build.gradle.kts b/diktat-runner/build.gradle.kts new file mode 100644 index 0000000000..c147193886 --- /dev/null +++ b/diktat-runner/build.gradle.kts @@ -0,0 +1,15 @@ +@Suppress("DSL_SCOPE_VIOLATION", "RUN_IN_SCRIPT") // https://github.com/gradle/gradle/issues/22797 +plugins { + id("com.saveourtool.diktat.buildutils.kotlin-jvm-configuration") + id("com.saveourtool.diktat.buildutils.code-quality-convention") + id("com.saveourtool.diktat.buildutils.publishing-default-configuration") +} + +project.description = "This module contains runner for diktat" + +dependencies { + api(projects.diktatApi) + api(projects.diktatCommon) + implementation(projects.diktatKtlintEngine) + implementation(projects.diktatRules) +} diff --git a/diktat-runner/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactoryProvider.kt b/diktat-runner/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactoryProvider.kt new file mode 100644 index 0000000000..ef5de0640c --- /dev/null +++ b/diktat-runner/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactoryProvider.kt @@ -0,0 +1,28 @@ +/** + * Contains only initialized [com.saveourtool.diktat.DiktatRunnerFactory] + */ + +package com.saveourtool.diktat + +import com.saveourtool.diktat.ktlint.DiktatBaselineFactoryImpl +import com.saveourtool.diktat.ktlint.DiktatProcessorFactoryImpl +import com.saveourtool.diktat.ktlint.DiktatReporterFactoryImpl +import com.saveourtool.diktat.ruleset.rules.DiktatRuleConfigReaderImpl +import com.saveourtool.diktat.ruleset.rules.DiktatRuleSetFactoryImpl +import generated.KTLINT_VERSION + +/** + * Info about engine + */ +const val ENGINE_INFO: String = "Ktlint: $KTLINT_VERSION" + +/** + * @return initialized [DiktatRunnerFactory] + */ +val diktatRunnerFactory: DiktatRunnerFactory = DiktatRunnerFactory( + DiktatRuleConfigReaderImpl(), + DiktatRuleSetFactoryImpl(), + DiktatProcessorFactoryImpl(), + DiktatBaselineFactoryImpl(), + DiktatReporterFactoryImpl(), +) diff --git a/settings.gradle.kts b/settings.gradle.kts index fb5ebee13c..1c8de8a54d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -56,6 +56,7 @@ include("diktat-rules") include("diktat-ruleset") include("diktat-dev-ksp") include("diktat-cli") +include("diktat-runner") enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")