Skip to content

Commit

Permalink
WIP #4
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Nov 21, 2022
1 parent 391f663 commit b85651d
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.cqfn.diktat.plugin.maven

import org.cqfn.diktat.DiktatProcessCommand
import org.cqfn.diktat.api.DiktatLogLevel
import org.cqfn.diktat.ktlint.unwrap

import com.pinterest.ktlint.core.LintError
Expand All @@ -27,6 +26,7 @@ import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugin.MojoFailureException
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import org.slf4j.event.Level

import java.io.File
import java.io.FileOutputStream
Expand Down Expand Up @@ -245,7 +245,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
}
}
.logLevel(
if (debug) DiktatLogLevel.DEBUG else DiktatLogLevel.INFO
if (debug) Level.DEBUG else Level.INFO
)
.build()
runAction(command)
Expand Down
26 changes: 26 additions & 0 deletions diktat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
<artifactId>diktat-rules</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>
<!-- start: reporters -->
<dependency>
<groupId>com.pinterest.ktlint</groupId>
Expand Down Expand Up @@ -76,6 +85,23 @@
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
<includes>
<include>version</include>
</includes>
<targetPath>/META-INF/diktat</targetPath>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.basedir}/..</directory>
<includes>
<include>LICENSE</include>
</includes>
<targetPath>/META-INF/diktat</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<!-- Build fat jar, overriding the primary jar -->
Expand Down
105 changes: 45 additions & 60 deletions diktat/src/main/kotlin/org/cqfn/diktat/DiktatMain.kt
Original file line number Diff line number Diff line change
@@ -1,67 +1,52 @@
package org.cqfn.diktat

import org.cqfn.diktat.api.DiktatLogLevel
import org.cqfn.diktat.api.DiktatReporterType
import org.cqfn.diktat.common.config.rules.DIKTAT
import org.cqfn.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
import com.pinterest.ktlint.core.Reporter
import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
import kotlinx.cli.multiple
import kotlinx.cli.vararg
import org.cqfn.diktat.api.DiktatError
import org.cqfn.diktat.api.DiktatMode
import org.cqfn.diktat.cli.DiktatProperties
import org.cqfn.diktat.ktlint.unwrap
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.io.path.absolutePathString
import kotlin.io.path.extension

fun main(args: Array<String>) {
val parser = ArgParser(DIKTAT)
val config: String by parser.option(
type = ArgType.String,
fullName = "config",
shortName = "c",
description = """
Specify the location of the YAML configuration file.
By default, $DIKTAT_ANALYSIS_CONF in the current
directory is used.
""".trimIndent(),
).default(DIKTAT_ANALYSIS_CONF)
val doFormat: Boolean by parser.option(
type = ArgType.Boolean,
fullName = "format",
shortName = "F",
description = "Fix any deviations from the code style."
).default(false)
val reporters: List<DiktatReporterType> by parser.option(
type = ArgType.Choice<DiktatReporterType>(),
fullName = "reporter",
shortName = "r",
description = "The reporter to use",
)
.default(DiktatReporterType.PLAIN)
.multiple()
val output: String? by parser.option(
type = ArgType.String,
fullName = "output",
shortName = "o",
description = "Redirect the reporter output to a file.",
)
val logLevel: DiktatLogLevel by parser.option(
type = ArgType.Choice<DiktatLogLevel>(),
fullName = "log-level",
shortName = "l",
description = "Enable the output with specific level",
).default(DiktatLogLevel.INFO)
val patterns: List<String> by parser.argument(
type = ArgType.String,
description = ""
)
.vararg()
val properties = DiktatProperties.parse(args)
properties.configureLogger()

parser.parse(args)

val diktatRuleSetProvider = DiktatRuleSetProvider(config)
val reporter = properties.reporter()
reporter.beforeAll()

val currentFolder = Paths.get(".")
properties.patterns
.asSequence()
.flatMap { pattern ->
Files.newDirectoryStream(currentFolder, pattern).asSequence()
}
.map { file ->
val result = mutableListOf<Pair<DiktatError, Boolean>>()
DiktatProcessCommand.Builder()
.file(file)
.config(properties.config)
.callback { error, isCorrected ->
result.add(error to isCorrected)
}
.isScript(!file.extension.endsWith(".kt", ignoreCase = true))
.logLevel(properties.logLevel)
.build()
.let { command ->
when (properties.mode) {
DiktatMode.CHECK -> command.check()
DiktatMode.FIX -> command.check()
}
}
file to result
}
.forEach { (file, result) ->
reporter.before(file.absolutePathString())
result.forEach { (error, isCorrected) ->
reporter.onLintError(file.absolutePathString(), error.unwrap(), isCorrected)
}
reporter.after(file.absolutePathString())
}
reporter.afterAll()
}
//
//private fun loadReporter(): Reporter {
//
//}
23 changes: 17 additions & 6 deletions diktat/src/main/kotlin/org/cqfn/diktat/DiktatProcessCommand.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.cqfn.diktat

import org.cqfn.diktat.api.DiktatCallback
import org.cqfn.diktat.api.DiktatLogLevel
import org.cqfn.diktat.common.config.rules.DIKTAT_ANALYSIS_CONF
import org.cqfn.diktat.ktlint.unwrap
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.api.EditorConfigDefaults
import com.pinterest.ktlint.core.api.EditorConfigOverride
import org.slf4j.event.Level
import java.nio.file.Path
import kotlin.io.path.absolutePathString

Expand All @@ -19,9 +20,10 @@ import kotlin.io.path.absolutePathString
class DiktatProcessCommand private constructor(
val file: Path,
val fileContent: String,
private val config: String,
private val callback: DiktatCallback,
private val isScript: Boolean,
private val logLevel: DiktatLogLevel,
private val logLevel: Level,
) {
/**
* Run `diktat fix` using parameters from current command
Expand All @@ -41,13 +43,13 @@ class DiktatProcessCommand private constructor(
private fun ktLintParams(): KtLint.ExperimentalParams = KtLint.ExperimentalParams(
fileName = file.absolutePathString(),
text = fileContent,
ruleSets = setOf(DiktatRuleSetProvider().get()),
ruleSets = setOf(DiktatRuleSetProvider(config).get()),
ruleProviders = emptySet(),
userData = emptyMap(),
cb = callback.unwrap(),
script = isScript,
editorConfigPath = null,
debug = logLevel == DiktatLogLevel.DEBUG,
debug = logLevel == Level.DEBUG,
editorConfigDefaults = EditorConfigDefaults.emptyEditorConfigDefaults,
editorConfigOverride = EditorConfigOverride.emptyEditorConfigOverride,
isInvokedFromCli = false
Expand All @@ -58,16 +60,18 @@ class DiktatProcessCommand private constructor(
*
* @property file
* @property fileContent
* @property config
* @property callback
* @property isScript
* @property logLevel
*/
data class Builder(
var file: Path? = null,
var fileContent: String? = null,
var config: String = DIKTAT_ANALYSIS_CONF,
var callback: DiktatCallback? = null,
var isScript: Boolean? = null,
var logLevel: DiktatLogLevel = DiktatLogLevel.INFO,
var logLevel: Level = Level.INFO,
) {
/**
* @param file
Expand All @@ -81,6 +85,12 @@ class DiktatProcessCommand private constructor(
*/
fun fileContent(fileContent: String) = apply { this.fileContent = fileContent }

/**
* @param config
* @return updated builder
*/
fun config(config: String) = apply { this.config = config }

/**
* @param callback
* @return updated builder
Expand All @@ -97,7 +107,7 @@ class DiktatProcessCommand private constructor(
* @param logLevel
* @return updated builder
*/
fun logLevel(logLevel: DiktatLogLevel) = apply { this.logLevel = logLevel }
fun logLevel(logLevel: Level) = apply { this.logLevel = logLevel }

/**
* @return built [DiktatProcessCommand]
Expand All @@ -109,6 +119,7 @@ class DiktatProcessCommand private constructor(
requireNotNull(fileContent) {
"fileContent is required"
},
config,
requireNotNull(callback) {
"callback is required"
},
Expand Down
10 changes: 0 additions & 10 deletions diktat/src/main/kotlin/org/cqfn/diktat/api/DiktatLogLevel.kt

This file was deleted.

21 changes: 0 additions & 21 deletions diktat/src/main/kotlin/org/cqfn/diktat/api/DiktatReporterType.kt

This file was deleted.

Loading

0 comments on commit b85651d

Please sign in to comment.