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 close outputStream as separate listener #1688

Merged
merged 9 commits into from
Jun 13, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,7 +36,6 @@ data class DiktatRunner(
DiktatProcessorListener(
args.loggingListener,
diktatReporter.skipKnownErrors(diktatBaseline),
diktatReporterCloser,
diktatBaselineGenerator,
errorCounter.countErrorsAsProcessorListener()
),
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.saveourtool.diktat
import com.saveourtool.diktat.api.DiktatBaseline
import com.saveourtool.diktat.api.DiktatBaselineFactory
import com.saveourtool.diktat.api.DiktatProcessorListener
import com.saveourtool.diktat.api.DiktatProcessorListener.Companion.closeAfterAllAsProcessorListener
import com.saveourtool.diktat.api.DiktatReporter
import com.saveourtool.diktat.api.DiktatReporterFactory
import com.saveourtool.diktat.api.DiktatRuleSetFactory
Expand All @@ -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
Expand All @@ -39,7 +38,6 @@ class DiktatRunnerFactory(
diktatBaseline = baseline,
diktatBaselineGenerator = baselineGenerator,
diktatReporter = reporter,
diktatReporterCloser = closer,
)
}

Expand All @@ -62,13 +60,9 @@ class DiktatRunnerFactory(
colorNameInPlain: String?,
groupByFileInPlain: Boolean?,
sourceRootDir: Path,
): Pair<DiktatReporter, DiktatProcessorListener> {
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) {
Expand All @@ -79,6 +73,5 @@ class DiktatRunnerFactory(
}
diktatReporterFactory(reporterType, outputStream, sourceRootDir)
}
return actualReporter to closeListener
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.saveourtool.diktat.api

import java.io.OutputStream
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicInteger

Expand Down Expand Up @@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package com.saveourtool.diktat.ktlint
import com.saveourtool.diktat.api.DiktatBaseline
import com.saveourtool.diktat.api.DiktatBaselineFactory
import com.saveourtool.diktat.api.DiktatProcessorListener
import com.saveourtool.diktat.api.DiktatProcessorListener.Companion.closeAfterAllAsProcessorListener
import com.saveourtool.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
Expand All @@ -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,
Expand All @@ -34,11 +34,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()
)
}
override fun generator(baselineFile: Path, sourceRootDir: Path): DiktatProcessorListener =
baselineReporterProvider.get(baselineFile.outputStream(), emptyMap()).wrap(sourceRootDir)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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.pinterest.ktlint.cli.reporter.core.api.ReporterV2
import java.nio.file.Path

Expand Down Expand Up @@ -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}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import com.saveourtool.diktat.api.DiktatRuleSet
import com.saveourtool.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

Expand Down Expand Up @@ -89,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 <R : ReporterV2> ReporterProviderV2<R>.get(
out: OutputStream,
opt: Map<String, String>,
): ReporterV2 = get(out.printStream(), opt).closeAfterAll(out)
nulls marked this conversation as resolved.
Show resolved Hide resolved

/**
* Enables ignoring autocorrected errors when in "fix" mode (i.e. when
* [com.pinterest.ktlint.core.KtLint.format] is invoked).
Expand All @@ -108,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.saveourtool.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
*/
open 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
}
}
}