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

Add main for diktat.jar #1566

Merged
merged 32 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion diktat-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
Expand Down
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 Down Expand Up @@ -36,12 +35,6 @@ import java.io.PrintStream
* Base [Mojo] for checking and fixing code using diktat
*/
abstract class DiktatBaseMojo : AbstractMojo() {
/**
* Flag that indicates whether to turn debug logging on
*/
@Parameter(property = "diktat.debug")
var debug = false

/**
* Property that will be used if you need to publish the report to GitHub
*/
Expand Down Expand Up @@ -240,18 +233,13 @@ abstract class DiktatBaseMojo : AbstractMojo() {
) {
val command = DiktatProcessCommand.Builder()
.file(file.toPath())
.fileContent(file.readText(Charsets.UTF_8))
.isScript(file.extension.equals("kts", ignoreCase = true))
.callback { error, isCorrected ->
val ktLintError = error.unwrap()
if (!baselineErrors.containsLintError(ktLintError)) {
reporterImpl.onLintError(file.absolutePath, ktLintError, isCorrected)
lintErrors.add(ktLintError)
}
}
.logLevel(
if (debug) DiktatLogLevel.DEBUG else DiktatLogLevel.INFO
)
.build()
runAction(command)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProviderV2

import org.apache.maven.plugins.annotations.Mojo
import kotlin.io.path.absolutePathString
import kotlin.io.path.readText
import kotlin.io.path.writeText

/**
Expand Down Expand Up @@ -37,7 +38,7 @@ class DiktatFixMojo : DiktatBaseMojo() {
*/
override fun runAction(command: DiktatProcessCommand) {
val fileName = command.file.absolutePathString()
val fileContent = command.fileContent
val fileContent = command.file.readText(Charsets.UTF_8)
val formattedText = command.fix()
if (fileContent != formattedText) {
log.info("Original and formatted content differ, writing to $fileName...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class DiktatBaseMojoTest {
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-test</artifactId>
<version>1.0.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
Expand All @@ -70,7 +70,6 @@ class DiktatBaseMojoTest {
val mavenProject = projectBuilder.build(pom, buildingRequest).project

val diktatCheckMojo = mojoRule.lookupConfiguredMojo(mavenProject, "check") as DiktatCheckMojo
Assertions.assertFalse(diktatCheckMojo.debug)
Assertions.assertEquals("diktat-analysis.yml", diktatCheckMojo.diktatConfigFile)
Assertions.assertIterableEquals(listOf(pom.parentFile.toPath() / "src"), diktatCheckMojo.inputs.map { Path(it) })
Assertions.assertTrue(diktatCheckMojo.excludes.isEmpty())
Expand All @@ -85,18 +84,17 @@ class DiktatBaseMojoTest {
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-test</artifactId>
<version>1.0.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-maven-plugin</artifactId>
<configuration>
<debug>true</debug>
<diktatConfigFile>my-diktat-config.yml</diktatConfigFile>
<inputs>
<input>${'$'}{project.basedir}/src/main/kotlin</input>
Expand All @@ -122,7 +120,6 @@ class DiktatBaseMojoTest {
val mavenProject = projectBuilder.build(pom, buildingRequest).project

val diktatCheckMojo = mojoRule.lookupConfiguredMojo(mavenProject, "check") as DiktatCheckMojo
Assertions.assertTrue(diktatCheckMojo.debug)
Assertions.assertEquals("my-diktat-config.yml", diktatCheckMojo.diktatConfigFile)
Assertions.assertIterableEquals(
listOf(pom.parentFile.toPath() / "src/main/kotlin", pom.parentFile.toPath() / "src/test/kotlin"),
Expand Down
124 changes: 51 additions & 73 deletions diktat-ruleset/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>diktat</artifactId>
<description>This module builds jar that can be used to run diktat using ktlint -R via command line</description>
<description>This module builds jar that can be used to run diktat using java -jar via command line</description>

<parent>
<groupId>org.cqfn.diktat</groupId>
Expand All @@ -17,58 +17,47 @@
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-rules</artifactId>
<version>${project.version}</version>
<exclusions>
<!-- Kotlin runtime & libraries will be provided by ktlint executable -->
<!-- FIXME: should be removed after making diktat.jar as executable -->
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-common</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-test-framework</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- start: reporters -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-common</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-plain</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk7</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-json</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-sarif</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-checkstyle</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-html</artifactId>
</dependency>
<!-- end: reporters -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-cli-jvm</artifactId>
</dependency>

<dependency>
<groupId>org.cqfn.diktat</groupId>
<artifactId>diktat-test-framework</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -93,43 +82,29 @@
</dependencies>

<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>
nulls marked this conversation as resolved.
Show resolved Hide resolved
</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>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
nulls marked this conversation as resolved.
Show resolved Hide resolved
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
<source>src/main/resources</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<!-- For some weird reason if main sourceset is removed from here, syntax highlighting in idea an ability to launch tests is broken -->
<source>src/main/kotlin</source>
<source>src/test/kotlin</source>
<source>${project.basedir}/src/main/kotlin</source>
<source>${project.basedir}/src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build fat jar, overriding the primary jar -->
Expand All @@ -142,6 +117,9 @@
<finalName>diktat-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>org.cqfn.diktat.DiktatMainKt</mainClass>
</manifest>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
Expand Down
101 changes: 101 additions & 0 deletions diktat-ruleset/src/main/kotlin/org/cqfn/diktat/DiktatMain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
* The file contains main method
*/

package org.cqfn.diktat

import org.cqfn.diktat.api.DiktatError
import org.cqfn.diktat.api.DiktatMode
import org.cqfn.diktat.cli.DiktatProperties
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ktlint.unwrap
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetFactory
import mu.KotlinLogging
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.PathWalkOption
import kotlin.io.path.absolutePathString
import kotlin.io.path.exists
import kotlin.io.path.extension
import kotlin.io.path.walk
import kotlin.io.path.writeText

@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
Fixed Show fixed Hide fixed

typealias DiktatErrorWithCorrectionInfo = Pair<DiktatError, Boolean>

private fun String.tryToPath(): Path? = try {
Paths.get(this).takeIf { it.exists() }
} catch (e: InvalidPathException) {
null
}

@OptIn(ExperimentalPathApi::class)
@Suppress(
"LongMethod",
"TOO_LONG_FUNCTION"
)
fun main(args: Array<String>) {
val properties = DiktatProperties.parse(args)
properties.configureLogger()

log.debug {
"Loading diktatRuleSet using config ${properties.config}"
}
val diktatRuleSetFactory = DiktatRuleSetFactory(properties.config)
val reporter = properties.reporter()
reporter.beforeAll()

log.debug {
"Resolving files by patterns: ${properties.patterns}"
}
val currentFolder = Paths.get(".")
properties.patterns
.asSequence()
.flatMap { pattern ->
pattern.tryToPath()?.let { sequenceOf(it) }
?: run {
nulls marked this conversation as resolved.
Show resolved Hide resolved
// create a matcher and return a filter that uses it.
val matcher = currentFolder.fileSystem.getPathMatcher("glob:$pattern")
nulls marked this conversation as resolved.
Show resolved Hide resolved
currentFolder.walk(PathWalkOption.INCLUDE_DIRECTORIES)
.filter { matcher.matches(it) }
}
}
.filter { file -> file.extension in setOf("kt", "kts") }
.distinct()
.map { it.normalize() }
.map { file ->
log.debug {
"Start processing the file: $file"
}
val result: MutableList<DiktatErrorWithCorrectionInfo> = mutableListOf()
DiktatProcessCommand.Builder()
nulls marked this conversation as resolved.
Show resolved Hide resolved
.file(file)
.diktatRuleSetFactory(diktatRuleSetFactory)
.callback { error, isCorrected ->
result.add(error to isCorrected)
}
.build()
.let { command ->
when (properties.mode) {
DiktatMode.CHECK -> command.check()
DiktatMode.FIX -> {
val formattedFileContent = command.fix()
file.writeText(formattedFileContent, Charsets.UTF_8)
}
}
}
file to result
}
.forEach { (file, result) ->
nulls marked this conversation as resolved.
Show resolved Hide resolved
reporter.before(file.absolutePathString())
result.forEach { (error, isCorrected) ->
reporter.onLintError(file.absolutePathString(), error.unwrap(), isCorrected)
}
reporter.after(file.absolutePathString())
}
reporter.afterAll()
}
Loading