From a8cb51e092787a03a6eb27fbe2788a0b07a426cf Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Wed, 7 Jun 2023 17:19:02 +0300 Subject: [PATCH] Diktat runner ### What's done: - main runner It closes #1685 --- .../org/cqfn/diktat/DiktatMainRunner.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 diktat-cli/src/main/kotlin/org/cqfn/diktat/DiktatMainRunner.kt diff --git a/diktat-cli/src/main/kotlin/org/cqfn/diktat/DiktatMainRunner.kt b/diktat-cli/src/main/kotlin/org/cqfn/diktat/DiktatMainRunner.kt new file mode 100644 index 0000000000..5559ee6f76 --- /dev/null +++ b/diktat-cli/src/main/kotlin/org/cqfn/diktat/DiktatMainRunner.kt @@ -0,0 +1,66 @@ +package org.cqfn.diktat + +import org.cqfn.diktat.api.DiktatRuleSet +import org.cqfn.diktat.api.DiktatRuleSetFactory +import org.cqfn.diktat.ktlint.DiktatProcessorFactoryImpl +import org.cqfn.diktat.ruleset.rules.DiktatRuleSetFactoryImpl +import mu.KotlinLogging +import org.apache.logging.log4j.LogManager +import org.apache.logging.log4j.core.LoggerContext +import org.slf4j.event.Level + +private val log = KotlinLogging.logger { } + +fun main() { + // a temporary + val logLevel = Level.ERROR + // set log level + LogManager.getContext(false) + .let { it as LoggerContext } + .also { ctx -> + ctx.configuration.rootLogger.level = when (logLevel) { + Level.ERROR -> org.apache.logging.log4j.Level.ERROR + Level.WARN -> org.apache.logging.log4j.Level.WARN + Level.INFO -> org.apache.logging.log4j.Level.INFO + Level.DEBUG -> org.apache.logging.log4j.Level.DEBUG + Level.TRACE -> org.apache.logging.log4j.Level.TRACE + } + } + .updateLoggers() + + // default implementations + val diktatRuleSetFactory = DiktatRuleSetFactoryImpl() + val diktatProcessorFactory = DiktatProcessorFactoryImpl() + + // ruleSet + val diktatRuleSet: DiktatRuleSet = diktatRuleSetFactory() + val diktatProcessor: DiktatProcessor = diktatProcessorFactory(diktatRuleSet) + + val code = mutableListOf() + while (true) { + val line = readln() + if (line == "CHECK") { + diktatProcessor.check( + code = code.joinToString(), + isScript = true + ) { error, _ -> + println(error.toString()) + } + code.clear() + } else if (line == "FIX") { + val result = diktatProcessor.fix( + code = code.joinToString(), + isScript = true + ) { error, _ -> + println(error.toString()) + } + code.clear() + println("Formatted code:") + println(result) + } else if (line == "END") { + break + } else { + code.add(line) + } + } +}