From 1eaecb4cd3084982905ec55dfd7c36329efa8899 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 8 Jun 2023 18:30:00 +0300 Subject: [PATCH 1/6] Removed close outputStream as separate listener ### What's done: - added wrapper for ReporterV2 which closes outputstream in afterAll --- .../kotlin/org/cqfn/diktat/DiktatRunner.kt | 7 ++-- .../org/cqfn/diktat/DiktatRunnerFactory.kt | 15 +++----- .../diktat/api/DiktatProcessorListener.kt | 11 ------ .../ktlint/DiktatBaselineFactoryImpl.kt | 12 +++---- .../ktlint/DiktatReporterFactoryImpl.kt | 9 ++--- .../org/cqfn/diktat/ktlint/KtLintUtils.kt | 34 +++++++++++++++++++ 6 files changed, 46 insertions(+), 42 deletions(-) diff --git a/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunner.kt b/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunner.kt index 5bc85c14fe..86360efd9d 100644 --- a/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunner.kt +++ b/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunner.kt @@ -19,14 +19,12 @@ private typealias RunAction = (DiktatProcessor, DiktatProcessorListener) -> Unit * @property diktatBaseline * @property diktatBaselineGenerator * @property diktatReporter - * @property diktatReporterCloser */ data class DiktatRunner( val diktatProcessor: DiktatProcessor, val diktatBaseline: DiktatBaseline, private val diktatBaselineGenerator: DiktatProcessorListener, val diktatReporter: DiktatReporter, - private val diktatReporterCloser: DiktatProcessorListener, ) { private fun doRun( args: DiktatRunnerArguments, @@ -38,7 +36,6 @@ data class DiktatRunner( DiktatProcessorListener( args.loggingListener, diktatReporter.skipKnownErrors(diktatBaseline), - diktatReporterCloser, diktatBaselineGenerator, errorCounter.countErrorsAsProcessorListener() ), @@ -47,7 +44,7 @@ data class DiktatRunner( } /** - * Run `diktat fix` for all [files]. + * Run `diktat fix` for all [DiktatRunnerArguments.files]. * * @param args * @param fileUpdateNotifier notifier about updated files @@ -74,7 +71,7 @@ data class DiktatRunner( } /** - * Run `diktat check` for all [files]. + * Run `diktat check` for all [DiktatRunnerArguments.files]. * * @param args * @return count of detected errors diff --git a/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunnerFactory.kt b/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunnerFactory.kt index 7e886c787c..1cba9fd239 100644 --- a/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunnerFactory.kt +++ b/diktat-api/src/main/kotlin/org/cqfn/diktat/DiktatRunnerFactory.kt @@ -3,7 +3,6 @@ package org.cqfn.diktat import org.cqfn.diktat.api.DiktatBaseline import org.cqfn.diktat.api.DiktatBaselineFactory import org.cqfn.diktat.api.DiktatProcessorListener -import org.cqfn.diktat.api.DiktatProcessorListener.Companion.closeAfterAllAsProcessorListener import org.cqfn.diktat.api.DiktatReporter import org.cqfn.diktat.api.DiktatReporterFactory import org.cqfn.diktat.api.DiktatRuleSetFactory @@ -29,7 +28,7 @@ class DiktatRunnerFactory( val diktatRuleSet = diktatRuleSetFactory.create(args.configFileName) val processor = diktatProcessorFactory(diktatRuleSet) val (baseline, baselineGenerator) = resolveBaseline(args.baselineFile, args.sourceRootDir) - val (reporter, closer) = resolveReporter( + val reporter = resolveReporter( args.reporterType, args.reporterOutput, args.colorNameInPlain, args.groupByFileInPlain, args.sourceRootDir @@ -39,7 +38,6 @@ class DiktatRunnerFactory( diktatBaseline = baseline, diktatBaselineGenerator = baselineGenerator, diktatReporter = reporter, - diktatReporterCloser = closer, ) } @@ -62,13 +60,9 @@ class DiktatRunnerFactory( colorNameInPlain: String?, groupByFileInPlain: Boolean?, sourceRootDir: Path, - ): Pair { - val (outputStream, closeListener) = reporterOutput - ?.let { it to it.closeAfterAllAsProcessorListener() } - ?: run { - System.`out` to DiktatProcessorListener.empty - } - val actualReporter = if (reporterType == diktatReporterFactory.plainId) { + ): DiktatReporter { + val outputStream = reporterOutput ?: System.`out` + return if (reporterType == diktatReporterFactory.plainId) { diktatReporterFactory.createPlain(outputStream, sourceRootDir, colorNameInPlain, groupByFileInPlain) } else { require(colorNameInPlain == null) { @@ -79,6 +73,5 @@ class DiktatRunnerFactory( } diktatReporterFactory(reporterType, outputStream, sourceRootDir) } - return actualReporter to closeListener } } diff --git a/diktat-api/src/main/kotlin/org/cqfn/diktat/api/DiktatProcessorListener.kt b/diktat-api/src/main/kotlin/org/cqfn/diktat/api/DiktatProcessorListener.kt index 36927a71ff..84d0eb89db 100644 --- a/diktat-api/src/main/kotlin/org/cqfn/diktat/api/DiktatProcessorListener.kt +++ b/diktat-api/src/main/kotlin/org/cqfn/diktat/api/DiktatProcessorListener.kt @@ -1,6 +1,5 @@ package org.cqfn.diktat.api -import java.io.OutputStream import java.nio.file.Path import java.util.concurrent.atomic.AtomicInteger @@ -82,15 +81,5 @@ interface DiktatProcessorListener { incrementAndGet() } } - - /** - * @return An implementation of [DiktatProcessorListener] which closes [OutputStream] at the end - */ - fun OutputStream.closeAfterAllAsProcessorListener(): DiktatProcessorListener = object : DiktatProcessorListener { - override fun afterAll() { - this@closeAfterAllAsProcessorListener.flush() - this@closeAfterAllAsProcessorListener.close() - } - } } } diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt index e7d33321fd..8cc247ca25 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt @@ -3,14 +3,12 @@ package org.cqfn.diktat.ktlint import org.cqfn.diktat.api.DiktatBaseline import org.cqfn.diktat.api.DiktatBaselineFactory import org.cqfn.diktat.api.DiktatProcessorListener -import org.cqfn.diktat.api.DiktatProcessorListener.Companion.closeAfterAllAsProcessorListener import org.cqfn.diktat.ktlint.DiktatReporterImpl.Companion.wrap import com.pinterest.ktlint.cli.reporter.baseline.Baseline -import com.pinterest.ktlint.cli.reporter.baseline.BaselineReporter +import com.pinterest.ktlint.cli.reporter.baseline.BaselineReporterProvider import com.pinterest.ktlint.cli.reporter.baseline.loadBaseline -import java.io.PrintStream import java.nio.file.Path import kotlin.io.path.absolutePathString @@ -20,6 +18,8 @@ import kotlin.io.path.outputStream * A factory to create or generate [DiktatBaseline] using `KtLint` */ class DiktatBaselineFactoryImpl : DiktatBaselineFactory { + private val baselineReporterProvider = BaselineReporterProvider() + override fun tryToLoad( baselineFile: Path, sourceRootDir: Path, @@ -35,10 +35,6 @@ class DiktatBaselineFactoryImpl : DiktatBaselineFactory { } override fun generator(baselineFile: Path, sourceRootDir: Path): DiktatProcessorListener { - val outputStream = baselineFile.outputStream() - return DiktatProcessorListener( - BaselineReporter(PrintStream(outputStream)).wrap(sourceRootDir), - outputStream.closeAfterAllAsProcessorListener() - ) + return baselineReporterProvider.get(baselineFile.outputStream(), emptyMap()).wrap(sourceRootDir) } } diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterFactoryImpl.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterFactoryImpl.kt index 5ed4e141d4..d72224626a 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterFactoryImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterFactoryImpl.kt @@ -10,7 +10,6 @@ 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.io.PrintStream import java.nio.file.Path import kotlin.io.path.pathString @@ -55,7 +54,7 @@ class DiktatReporterFactoryImpl : DiktatReporterFactory { } else { emptyMap() } - return reporterProvider.get(outputStream.asPrintStream(), opt).wrap(sourceRootDir) + return reporterProvider.get(outputStream, opt).wrap(sourceRootDir) } override fun createPlain( @@ -74,10 +73,6 @@ class DiktatReporterFactoryImpl : DiktatReporterFactory { } groupByFile?.let { put("group_by_file", it) } }.mapValues { it.value.toString() } - return plainReporterProvider.get(outputStream.asPrintStream(), opt).wrap(sourceRootDir) - } - - companion object { - private fun OutputStream.asPrintStream(): PrintStream = (this as? PrintStream) ?: PrintStream(this) + return plainReporterProvider.get(outputStream, opt).wrap(sourceRootDir) } } diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt index 926c28e2c5..5354be57f4 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt @@ -10,9 +10,13 @@ import org.cqfn.diktat.api.DiktatRuleSet import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError +import com.pinterest.ktlint.cli.reporter.core.api.ReporterProviderV2 +import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 import com.pinterest.ktlint.rule.engine.api.LintError import com.pinterest.ktlint.rule.engine.core.api.RuleId import org.intellij.lang.annotations.Language +import java.io.OutputStream +import java.io.PrintStream import java.nio.file.Path @@ -159,3 +163,33 @@ fun lint( isScript = false, callback = cb.ignoreCorrectedErrors(), ) + +/** + * @param out [OutputStream] for [ReporterV2] + * @param opt configuration for [ReporterV2] + * @return created [ReporterV2] which closes [out] in [ReporterV2.afterAll] + */ +fun ReporterProviderV2.get( + out: OutputStream, + opt: Map, +): ReporterV2 = get(out.printStream(), opt).closeAfterAll(out) + +private fun OutputStream.printStream(): PrintStream = (this as? PrintStream) ?: PrintStream(this) + +private fun ReporterV2.closeAfterAll(outputStream: OutputStream): ReporterV2 { + return object : ReporterV2 { + override fun beforeAll() = this@closeAfterAll.beforeAll() + + override fun before(file: String) = this@closeAfterAll.before(file) + + override fun onLintError(file: String, ktlintCliError: KtlintCliError) = this@closeAfterAll.onLintError(file, ktlintCliError) + + override fun after(file: String) = this@closeAfterAll.after(file) + + override fun afterAll() { + this@closeAfterAll.afterAll() + outputStream.flush() + outputStream.close() + } + } +} From 8333970aed25a9fd6f23fbe26fe82c14c6d73bb5 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 9 Jun 2023 12:22:45 +0300 Subject: [PATCH 2/6] extracted ReportV2Wrapper to separate class --- .../ktlint/DiktatBaselineFactoryImpl.kt | 5 +- .../cqfn/diktat/ktlint/DiktatReporterImpl.kt | 3 +- .../org/cqfn/diktat/ktlint/KtLintUtils.kt | 50 ++++++++----------- .../cqfn/diktat/ktlint/ReporterV2Wrapper.kt | 32 ++++++++++++ 4 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt index 8cc247ca25..0c7bae1240 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatBaselineFactoryImpl.kt @@ -34,7 +34,6 @@ class DiktatBaselineFactoryImpl : DiktatBaselineFactory { } } - override fun generator(baselineFile: Path, sourceRootDir: Path): DiktatProcessorListener { - return baselineReporterProvider.get(baselineFile.outputStream(), emptyMap()).wrap(sourceRootDir) - } + override fun generator(baselineFile: Path, sourceRootDir: Path): DiktatProcessorListener = + baselineReporterProvider.get(baselineFile.outputStream(), emptyMap()).wrap(sourceRootDir) } diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterImpl.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterImpl.kt index eba6905830..2b41ca1777 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/DiktatReporterImpl.kt @@ -2,6 +2,7 @@ package org.cqfn.diktat.ktlint import org.cqfn.diktat.api.DiktatError import org.cqfn.diktat.api.DiktatReporter +import org.cqfn.diktat.ktlint.ReporterV2Wrapper.Companion.unwrapIfNeeded import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 import java.nio.file.Path @@ -32,7 +33,7 @@ class DiktatReporterImpl( /** * @return __KtLint__'s [ReporterV2] */ - fun DiktatReporter.unwrap(): ReporterV2 = (this as? DiktatReporterImpl)?.ktLintReporter + fun DiktatReporter.unwrap(): ReporterV2 = (this as? DiktatReporterImpl)?.ktLintReporter?.unwrapIfNeeded() ?: error("Unsupported wrapper of ${DiktatReporter::class.java.simpleName}: ${this::class.java.canonicalName}") } } diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt index 5354be57f4..07e1743e29 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/KtLintUtils.kt @@ -93,6 +93,16 @@ fun String.correctErrorDetail(canBeAutoCorrected: Boolean): String = if (canBeAu */ fun Path.relativePathStringTo(sourceRootDir: Path): String = relativeTo(sourceRootDir).invariantSeparatorsPathString +/** + * @param out [OutputStream] for [ReporterV2] + * @param opt configuration for [ReporterV2] + * @return created [ReporterV2] which closes [out] in [ReporterV2.afterAll] + */ +fun ReporterProviderV2.get( + out: OutputStream, + opt: Map, +): ReporterV2 = get(out.printStream(), opt).closeAfterAll(out) + /** * Enables ignoring autocorrected errors when in "fix" mode (i.e. when * [com.pinterest.ktlint.core.KtLint.format] is invoked). @@ -112,6 +122,16 @@ private fun DiktatCallback.ignoreCorrectedErrors(): DiktatCallback = DiktatCallb } } +private fun OutputStream.printStream(): PrintStream = (this as? PrintStream) ?: PrintStream(this) + +private fun ReporterV2.closeAfterAll(outputStream: OutputStream): ReporterV2 = object : ReporterV2Wrapper(this@closeAfterAll) { + override fun afterAll() { + super.afterAll() + outputStream.flush() + outputStream.close() + } +} + /** * @param ruleSetSupplier * @param file @@ -163,33 +183,3 @@ fun lint( isScript = false, callback = cb.ignoreCorrectedErrors(), ) - -/** - * @param out [OutputStream] for [ReporterV2] - * @param opt configuration for [ReporterV2] - * @return created [ReporterV2] which closes [out] in [ReporterV2.afterAll] - */ -fun ReporterProviderV2.get( - out: OutputStream, - opt: Map, -): ReporterV2 = get(out.printStream(), opt).closeAfterAll(out) - -private fun OutputStream.printStream(): PrintStream = (this as? PrintStream) ?: PrintStream(this) - -private fun ReporterV2.closeAfterAll(outputStream: OutputStream): ReporterV2 { - return object : ReporterV2 { - override fun beforeAll() = this@closeAfterAll.beforeAll() - - override fun before(file: String) = this@closeAfterAll.before(file) - - override fun onLintError(file: String, ktlintCliError: KtlintCliError) = this@closeAfterAll.onLintError(file, ktlintCliError) - - override fun after(file: String) = this@closeAfterAll.after(file) - - override fun afterAll() { - this@closeAfterAll.afterAll() - outputStream.flush() - outputStream.close() - } - } -} diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt new file mode 100644 index 0000000000..d5c90c76e9 --- /dev/null +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt @@ -0,0 +1,32 @@ +package org.cqfn.diktat.ktlint + +import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError +import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 + +/** + * Wrapper for [ReporterV2] + * + * @param reporterV2 + */ +abstract class ReporterV2Wrapper(private val reporterV2: ReporterV2) : ReporterV2 { + override fun beforeAll() = reporterV2.beforeAll() + + override fun before(file: String) = reporterV2.before(file) + + override fun onLintError(file: String, ktlintCliError: KtlintCliError) = reporterV2.onLintError(file, ktlintCliError) + + override fun after(file: String) = reporterV2.after(file) + + override fun afterAll() = reporterV2.afterAll() + + companion object { + /** + * @return unwrapped [ReporterV2Wrapper] if it's required + */ + fun ReporterV2.unwrapIfNeeded(): ReporterV2 = if (this is ReporterV2Wrapper) { + this.reporterV2 + } else { + this + } + } +} From 3976a01eb723c716eddb4dc4aedbc446bb8f0923 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Fri, 9 Jun 2023 12:55:08 +0300 Subject: [PATCH 3/6] detectAll --- .../src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt index d5c90c76e9..e115ac860a 100644 --- a/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt +++ b/diktat-ktlint-engine/src/main/kotlin/org/cqfn/diktat/ktlint/ReporterV2Wrapper.kt @@ -8,7 +8,7 @@ import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 * * @param reporterV2 */ -abstract class ReporterV2Wrapper(private val reporterV2: ReporterV2) : ReporterV2 { +open class ReporterV2Wrapper(private val reporterV2: ReporterV2) : ReporterV2 { override fun beforeAll() = reporterV2.beforeAll() override fun before(file: String) = reporterV2.before(file) From 4a67b9a72a614506552f83aca0073aa4decfb61a Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 13 Jun 2023 11:51:59 +0300 Subject: [PATCH 4/6] fixed package after merging the master --- .../kotlin/com/saveourtool/diktat/ktlint/DiktatReporterImpl.kt | 2 +- .../kotlin/com/saveourtool/diktat/ktlint/ReporterV2Wrapper.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 fbcfcf88c4..dcca2964ef 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 @@ -2,7 +2,7 @@ package com.saveourtool.diktat.ktlint import com.saveourtool.diktat.api.DiktatError import com.saveourtool.diktat.api.DiktatReporter -import org.cqfn.diktat.ktlint.ReporterV2Wrapper.Companion.unwrapIfNeeded +import com.saveourtool.diktat.ktlint.ReporterV2Wrapper.Companion.unwrapIfNeeded import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 import java.nio.file.Path diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/ReporterV2Wrapper.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/ReporterV2Wrapper.kt index e115ac860a..6669c794ce 100644 --- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/ReporterV2Wrapper.kt +++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/ReporterV2Wrapper.kt @@ -1,4 +1,4 @@ -package org.cqfn.diktat.ktlint +package com.saveourtool.diktat.ktlint import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2 From 4a9923f3e671ffdb78c48cbed84bd86e9a149c7e Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 13 Jun 2023 13:52:49 +0300 Subject: [PATCH 5/6] added a property to close output stream in DiktatReporter --- .../kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt | 6 +++--- .../com/saveourtool/diktat/api/DiktatReporterFactory.kt | 6 +++++- .../diktat/ktlint/DiktatReporterFactoryImpl.kt | 6 ++++-- .../kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt | 9 +++++++-- 4 files changed, 19 insertions(+), 8 deletions(-) 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 ccb42f70dd..6a48b26f26 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatRunnerFactory.kt @@ -61,9 +61,9 @@ class DiktatRunnerFactory( groupByFileInPlain: Boolean?, sourceRootDir: Path, ): DiktatReporter { - val outputStream = reporterOutput ?: System.`out` + val (outputStream, closeOutputStream) = reporterOutput?.let { it to true } ?: (System.`out` to false) return if (reporterType == diktatReporterFactory.plainId) { - diktatReporterFactory.createPlain(outputStream, sourceRootDir, colorNameInPlain, groupByFileInPlain) + diktatReporterFactory.createPlain(outputStream, closeOutputStream, sourceRootDir, colorNameInPlain, groupByFileInPlain) } else { require(colorNameInPlain == null) { "colorization is applicable only for plain reporter" @@ -71,7 +71,7 @@ class DiktatRunnerFactory( require(groupByFileInPlain == null) { "groupByFile is applicable only for plain reporter" } - diktatReporterFactory(reporterType, outputStream, sourceRootDir) + diktatReporterFactory(reporterType, outputStream, closeOutputStream, sourceRootDir) } } } 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 51438c9c8c..04794ab897 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 @@ -8,7 +8,7 @@ typealias DiktatReporter = DiktatProcessorListener /** * A factory to create [DiktatReporter] */ -interface DiktatReporterFactory : Function3 { +interface DiktatReporterFactory : Function4 { /** * Set of supported IDs */ @@ -27,17 +27,20 @@ interface DiktatReporterFactory : Function3 ReporterProviderV2.get( out: OutputStream, + closeOutAfterAll: Boolean, opt: Map, -): ReporterV2 = get(out.printStream(), opt).closeAfterAll(out) +): ReporterV2 = get(out.printStream(), opt).applyIf(closeOutAfterAll) { + closeAfterAll(out) +} /** * Enables ignoring autocorrected errors when in "fix" mode (i.e. when From fdaa950dc11b6097177410682965e71f9546b152 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Tue, 13 Jun 2023 13:54:48 +0300 Subject: [PATCH 6/6] fixed baseline factory --- .../com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt index 1f6dcbfb48..b71778964f 100644 --- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatBaselineFactoryImpl.kt @@ -35,5 +35,5 @@ class DiktatBaselineFactoryImpl : DiktatBaselineFactory { } override fun generator(baselineFile: Path, sourceRootDir: Path): DiktatProcessorListener = - baselineReporterProvider.get(baselineFile.outputStream(), emptyMap()).wrap(sourceRootDir) + baselineReporterProvider.get(baselineFile.outputStream(), closeOutAfterAll = true, emptyMap()).wrap(sourceRootDir) }