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

Diktat rule configs #1686

Merged
merged 21 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
12 changes: 12 additions & 0 deletions diktat-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ plugins {
id("org.cqfn.diktat.buildutils.kotlin-jvm-configuration")
id("org.cqfn.diktat.buildutils.code-quality-convention")
id("org.cqfn.diktat.buildutils.publishing-signing-default-configuration")
alias(libs.plugins.kotlin.plugin.serialization)
}

project.description = "This module builds diktat-api"

dependencies {
implementation(libs.kotlin.compiler.embeddable)
implementation(libs.kotlinx.serialization.core)
}

val generateDiktatVersionFile by tasks.registering {
Expand Down Expand Up @@ -39,3 +41,13 @@ kotlin.sourceSets.getByName("main") {
}
)
}

sequenceOf("diktatFix", "diktatCheck").forEach { diktatTaskName ->
tasks.named(diktatTaskName) {
dependsOn(
generateDiktatVersionFile,
tasks.named("compileKotlin"),
tasks.named("processResources"),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.cqfn.diktat

import org.cqfn.diktat.api.DiktatProcessorListener
import java.io.InputStream
import java.io.OutputStream
import java.nio.file.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.inputStream

/**
* Arguments for [DiktatRunner]
*
* @property configFileName a config file to load Diktat's rules
* @property configInputStream an input stream with config to load Diktat's rules
* @property sourceRootDir a common root dir for all provided [files]
* @property files a collection of files which needs to be fixed
* @property baselineFile an optional path to file with baseline
Expand All @@ -19,7 +20,7 @@ import kotlin.io.path.absolutePathString
* @property loggingListener listener to log diktat runner phases, [DiktatProcessorListener.empty] by default
*/
data class DiktatRunnerArguments(
val configFileName: String,
val configInputStream: InputStream,
val sourceRootDir: Path,
val files: Collection<Path>,
val baselineFile: Path?,
Expand All @@ -40,7 +41,7 @@ data class DiktatRunnerArguments(
colorNameInPlain: String? = null,
loggingListener: DiktatProcessorListener = DiktatProcessorListener.empty,
) : this(
configFile.absolutePathString(),
configFile.inputStream(),
sourceRootDir,
files,
baselineFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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.DiktatRuleConfigReader
import org.cqfn.diktat.api.DiktatRuleSetFactory
import java.io.OutputStream
import java.nio.file.Path
Expand All @@ -16,6 +17,7 @@ import java.nio.file.Path
* @property diktatReporterFactory a factory for [DiktatReporter]
*/
class DiktatRunnerFactory(
private val diktatRuleConfigReader: DiktatRuleConfigReader,
private val diktatRuleSetFactory: DiktatRuleSetFactory,
private val diktatProcessorFactory: DiktatProcessorFactory,
private val diktatBaselineFactory: DiktatBaselineFactory,
Expand All @@ -26,7 +28,8 @@ class DiktatRunnerFactory(
* @return an instance of [DiktatRunner] created using [args]
*/
override fun invoke(args: DiktatRunnerArguments): DiktatRunner {
val diktatRuleSet = diktatRuleSetFactory.create(args.configFileName)
val diktatRuleConfigs = diktatRuleConfigReader(args.configInputStream)
val diktatRuleSet = diktatRuleSetFactory(diktatRuleConfigs)
val processor = diktatProcessorFactory(diktatRuleSet)
val (baseline, baselineGenerator) = resolveBaseline(args.baselineFile, args.sourceRootDir)
val (reporter, closer) = resolveReporter(
Expand Down
19 changes: 19 additions & 0 deletions diktat-api/src/main/kotlin/org/cqfn/diktat/api/DiktatRuleConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cqfn.diktat.api

import kotlinx.serialization.Serializable

/**
* Configuration of individual [DiktatRule]
*
* @property name name of the rule
* @property enabled
* @property configuration a map of strings with configuration options
* @property ignoreAnnotated if a code block is marked with these annotations - it will not be checked by this rule
*/
@Serializable
data class DiktatRuleConfig(
val name: String,
orchestr7 marked this conversation as resolved.
Show resolved Hide resolved
val enabled: Boolean = true,
val configuration: Map<String, String> = emptyMap(),
nulls marked this conversation as resolved.
Show resolved Hide resolved
val ignoreAnnotated: Set<String> = emptySet(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.cqfn.diktat.api

import java.io.InputStream

/**
* A reader for [DiktatRuleConfig]
*/
interface DiktatRuleConfigReader : Function1<InputStream, List<DiktatRuleConfig>> {
nulls marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param inputStream
* @return parsed [DiktatRuleConfig]s
*/
override operator fun invoke(inputStream: InputStream): List<DiktatRuleConfig>
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
package org.cqfn.diktat.api

import java.nio.file.Path
import kotlin.io.path.absolutePathString

/**
* A factory which creates a [DiktatRuleSet].
*/
interface DiktatRuleSetFactory : Function0<DiktatRuleSet> {
interface DiktatRuleSetFactory : Function1<List<DiktatRuleConfig>, DiktatRuleSet> {
nulls marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param rulesConfig all configurations for rules
* @return the default instance of [DiktatRuleSet]
*/
override operator fun invoke(): DiktatRuleSet

/**
* @param configFile a path to file with configuration for diktat (`diktat-analysis.yml`)
* @return created [DiktatRuleSet] using [configFile]
*/
fun create(configFile: String): DiktatRuleSet

/**
* @param configFile a file with configuration for diktat (`diktat-analysis.yml`)
* @return created [DiktatRuleSet] using [configFile]
*/
fun create(configFile: Path): DiktatRuleSet = create(configFile.absolutePathString())
override operator fun invoke(rulesConfig: List<DiktatRuleConfig>): DiktatRuleSet
}
2 changes: 2 additions & 0 deletions diktat-cli/src/main/kotlin/org/cqfn/diktat/DiktatMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.cqfn.diktat.cli.DiktatProperties
import org.cqfn.diktat.ktlint.DiktatBaselineFactoryImpl
import org.cqfn.diktat.ktlint.DiktatProcessorFactoryImpl
import org.cqfn.diktat.ktlint.DiktatReporterFactoryImpl
import org.cqfn.diktat.ruleset.rules.DiktatRuleConfigReaderImpl
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetFactoryImpl

import mu.KotlinLogging
Expand All @@ -31,6 +32,7 @@ private val loggingListener = object : DiktatProcessorListener {

fun main(args: Array<String>) {
val diktatRunnerFactory = DiktatRunnerFactory(
DiktatRuleConfigReaderImpl(),
DiktatRuleSetFactoryImpl(),
DiktatProcessorFactoryImpl(),
DiktatBaselineFactoryImpl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import java.io.OutputStream
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.createDirectories
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream
import kotlin.system.exitProcess
import kotlinx.cli.ArgParser
Expand Down Expand Up @@ -69,7 +70,7 @@ data class DiktatProperties(
sourceRootDir: Path,
loggingListener: DiktatProcessorListener,
): DiktatRunnerArguments = DiktatRunnerArguments(
configFileName = config,
configInputStream = Paths.get(config).inputStream(),
sourceRootDir = sourceRootDir,
files = getFiles(sourceRootDir),
baselineFile = null,
Expand Down
1 change: 1 addition & 0 deletions diktat-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
api(libs.kaml)
implementation(libs.apache.commons.cli)
implementation(libs.kotlin.logging)
implementation(projects.diktatApi)
testImplementation(libs.junit.jupiter)
testImplementation(libs.assertj.core)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.cqfn.diktat.common.config.reader

import mu.KotlinLogging
import java.io.IOException
import java.io.InputStream
import kotlin.jvm.Throws

/**
* This class is used to read input stream in any format that you will specify.
* Usage:
* 1) implement this class with implementing the method:
* a. parse - implement parser for your file format (for example parse it to a proper json)
* 2) Use your new class MyReader().read(someInputStream)
*
* @param T - class name parameter that will be used in calculation of classpath
*/
abstract class AbstractConfigReader<T : Any> {
/**
* @param inputStream - input stream
* @return object of type [T] if resource has been parsed successfully
*/
fun read(inputStream: InputStream): T? = try {
parse(inputStream)
} catch (e: IOException) {
log.error("Cannot read config from input stream due to: ", e)
null
}

/**
* you can specify your own parser, in example for parsing stream as a json
*
* @param inputStream a [InputStream] representing loaded content
* @return resource parsed as type [T]
* @throws IOException
*/
@Throws(IOException::class)
protected abstract fun parse(inputStream: InputStream): T

companion object {
private val log = KotlinLogging.logger {}
}
}

This file was deleted.

Loading