Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Removed DiktatRunnerFactory #1835

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private val loggingListener = object : DiktatProcessorListener {
}

fun main(args: Array<String>) {
val properties = DiktatProperties.parse(diktatRunnerFactory.diktatReporterFactory, args)
val properties = DiktatProperties.parse(diktatReporterFactory, args)
properties.configureLogger()

log.debug {
Expand All @@ -38,10 +38,9 @@ fun main(args: Array<String>) {
loggingListener = loggingListener,
)

val diktatRunner = diktatRunnerFactory(diktatRunnerArguments)
when (properties.mode) {
DiktatMode.CHECK -> diktatRunner.checkAll(diktatRunnerArguments)
DiktatMode.FIX -> diktatRunner.fixAll(diktatRunnerArguments) { updatedFile ->
DiktatMode.CHECK -> DiktatRunner.checkAll(diktatRunnerArguments)
DiktatMode.FIX -> DiktatRunner.fixAll(diktatRunnerArguments) { updatedFile ->
log.warn {
"Original and formatted content differ, writing to ${updatedFile.absolutePathString()}..."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ data class DiktatProperties(
files = getFiles(sourceRootDir),
baselineFile = null,
reporterArgsList = listOf(reporterCreationArguments),
loggingListener = loggingListener
loggingListener = loggingListener,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ abstract class DiktatCheckTask @Inject constructor(
objectFactory
) {
override fun doRun(
runner: DiktatRunner,
args: DiktatRunnerArguments
): Int = runner.checkAll(args)
): Int = DiktatRunner.checkAll(args)

companion object {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ abstract class DiktatFixTask @Inject constructor(
objectFactory
) {
override fun doRun(
runner: DiktatRunner,
args: DiktatRunnerArguments
): Int = runner.fixAll(args) { updatedFile ->
): Int = DiktatRunner.fixAll(args) { updatedFile ->
project.logger.info("Original and formatted content differ, writing to ${updatedFile.fileName}...")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ 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.diktatRunnerFactory
import com.saveourtool.diktat.plugin.gradle.DiktatExtension
import com.saveourtool.diktat.plugin.gradle.extension.DefaultReporter
import com.saveourtool.diktat.plugin.gradle.extension.PlainReporter
Expand All @@ -34,8 +32,8 @@ import org.gradle.api.tasks.TaskProvider
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.Files
import java.nio.file.Path

/**
Expand Down Expand Up @@ -141,14 +139,6 @@ abstract class DiktatTaskBase(
)
}

/**
* [DiktatRunner] created based on a default [DiktatRunnerFactory]
*/
@get:Internal
val diktatRunner by lazy {
diktatRunnerFactory(diktatRunnerArguments)
}

init {
group = LifecycleBasePlugin.VERIFICATION_GROUP
}
Expand Down Expand Up @@ -177,7 +167,6 @@ abstract class DiktatTaskBase(

private fun doRun() {
val errorCounter = doRun(
runner = diktatRunner,
args = diktatRunnerArguments
)
if (errorCounter > 0 && !ignoreFailures) {
Expand All @@ -193,7 +182,6 @@ abstract class DiktatTaskBase(
* @return count of errors
*/
abstract fun doRun(
runner: DiktatRunner,
args: DiktatRunnerArguments
): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.saveourtool.diktat.plugin.maven

import com.saveourtool.diktat.DiktatRunner
import com.saveourtool.diktat.DiktatRunnerArguments
import com.saveourtool.diktat.DiktatRunnerFactoryArguments
import com.saveourtool.diktat.diktatRunnerFactory
import com.saveourtool.diktat.plugin.maven.reporters.GitHubActionsReporter
import com.saveourtool.diktat.plugin.maven.reporters.PlainReporter
Expand Down Expand Up @@ -71,13 +72,11 @@ abstract class DiktatBaseMojo : AbstractMojo() {
lateinit var excludes: List<String>

/**
* @param runner instance of [DiktatRunner] used in analysis
* @param args arguments for [DiktatRunner]
* @return count of errors
*/
@Suppress("TOO_MANY_PARAMETERS")
abstract fun runAction(
runner: DiktatRunner,
args: DiktatRunnerArguments,
): Int

Expand Down Expand Up @@ -111,9 +110,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
baselineFile = baseline?.toPath(),
reporterArgsList = reporterArgsList,
)
val diktatRunner = diktatRunnerFactory(args)
val errorCounter = runAction(
runner = diktatRunner,
args = args,
)
if (errorCounter > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import org.apache.maven.plugins.annotations.Mojo
@Suppress("unused")
class DiktatCheckMojo : DiktatBaseMojo() {
override fun runAction(
runner: DiktatRunner,
args: DiktatRunnerArguments,
): Int = runner.checkAll(args)
): Int = DiktatRunner.checkAll(args)
}

/**
Expand All @@ -29,9 +28,8 @@ class DiktatCheckMojo : DiktatBaseMojo() {
@Suppress("unused")
class DiktatFixMojo : DiktatBaseMojo() {
override fun runAction(
runner: DiktatRunner,
args: DiktatRunnerArguments,
): Int = runner.fixAll(args) { updatedFile ->
): Int = DiktatRunner.fixAll(args) { updatedFile ->
log.info("Original and formatted content differ, writing to ${updatedFile.fileName}...")
}
}
30 changes: 29 additions & 1 deletion diktat-runner/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import com.saveourtool.diktat.buildutils.configurePublications
import com.saveourtool.diktat.buildutils.configurePublishing
import com.github.jengelman.gradle.plugins.shadow.ShadowExtension
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

@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")
id("com.saveourtool.diktat.buildutils.publishing-configuration")
alias(libs.plugins.shadow)
}

project.description = "This module contains runner for diktat"
Expand All @@ -13,3 +19,25 @@ dependencies {
implementation(projects.diktatKtlintEngine)
implementation(projects.diktatRules)
}

tasks.named<ShadowJar>("shadowJar") {
archiveClassifier.set("shadow")
duplicatesStrategy = DuplicatesStrategy.FAIL
}

publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
// it creates a publication for shadowJar
create<MavenPublication>("shadow") {
// https://github.com/johnrengelman/shadow/issues/417#issuecomment-830668442
project.extensions.configure<ShadowExtension> {
component(this@create)
}
}
}
}
configurePublications()
configurePublishing()
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Contains only initialized [com.saveourtool.diktat.DiktatRunnerFactory]
*/

package com.saveourtool.diktat

import com.saveourtool.diktat.api.DiktatBaselineFactory
import com.saveourtool.diktat.api.DiktatReporterFactory
import com.saveourtool.diktat.api.DiktatRuleConfigReader
import com.saveourtool.diktat.api.DiktatRuleSetFactory
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 [DiktatRuleConfigReader]
*/
val diktatRuleConfigReader: DiktatRuleConfigReader = DiktatRuleConfigReaderImpl()

/**
* @return initialized [DiktatRuleSetFactory]
*/
val diktatRuleSetFactory: DiktatRuleSetFactory = DiktatRuleSetFactoryImpl()

/**
* @return initialized [DiktatProcessorFactory]
*/
val diktatProcessorFactory: DiktatProcessorFactory = DiktatProcessorFactoryImpl()

/**
* @return initialized [DiktatBaselineFactory]
*/
val diktatBaselineFactory: DiktatBaselineFactory = DiktatBaselineFactoryImpl()

/**
* @return initialized [DiktatReporterFactory]
*/
val diktatReporterFactory: DiktatReporterFactory = DiktatReporterFactoryImpl()
Original file line number Diff line number Diff line change
@@ -1,5 +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.DiktatProcessorListener
import com.saveourtool.diktat.api.DiktatProcessorListener.Companion.countErrorsAsProcessorListener
import com.saveourtool.diktat.api.DiktatReporter
Expand All @@ -12,30 +14,8 @@ private typealias RunAction = (DiktatProcessor, DiktatProcessorListener) -> Unit

/**
* A runner for diktat on bunch of files using baseline and reporter
*
* @param diktatProcessor
* @property diktatReporter
*/
data class DiktatRunner(
private val diktatProcessor: DiktatProcessor,
val diktatReporter: DiktatReporter,
) {
private fun doRun(
args: DiktatRunnerArguments,
runAction: RunAction,
): Int {
val errorCounter = AtomicInteger()
runAction(
diktatProcessor,
DiktatProcessorListener(
args.loggingListener,
diktatReporter,
errorCounter.countErrorsAsProcessorListener()
),
)
return errorCounter.get()
}

object DiktatRunner {
/**
* Run `diktat fix` for all [DiktatRunnerArguments.files].
*
Expand Down Expand Up @@ -82,4 +62,41 @@ data class DiktatRunner(
}
listener.afterAll()
}
private fun doRun(
args: DiktatRunnerArguments,
runAction: RunAction,
): Int {
val diktatRuleConfigs = diktatRuleConfigReader(args.configInputStream)
val diktatRuleSet = diktatRuleSetFactory(diktatRuleConfigs)
val processor = diktatProcessorFactory(diktatRuleSet)
val (baseline, baselineGenerator) = resolveBaseline(args.baselineFile, args.sourceRootDir)

val reporter = args.reporterArgsList
.map { diktatReporterFactory(it) }
.let { DiktatReporter.union(it) }

val errorCounter = AtomicInteger()
runAction(
processor,
DiktatProcessorListener(
args.loggingListener,
DiktatReporter(reporter.skipKnownErrors(baseline), baselineGenerator),
errorCounter.countErrorsAsProcessorListener()
),
)
return errorCounter.get()
}

private fun resolveBaseline(
baselineFile: Path?,
sourceRootDir: Path?,
): Pair<DiktatBaseline, DiktatProcessorListener> = baselineFile
?.let { diktatBaselineFactory.tryToLoad(it, sourceRootDir) }
?.let { it to DiktatProcessorListener.empty }
?: run {
val baselineGenerator = baselineFile?.let {
diktatBaselineFactory.generator(it, sourceRootDir)
} ?: DiktatProcessorListener.empty
DiktatBaseline.empty to baselineGenerator
}
}
Loading
Loading