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 10 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
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 @@ -250,7 +250,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
29 changes: 22 additions & 7 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,36 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
nulls marked this conversation as resolved.
Show resolved Hide resolved
<id>generate-constants</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.cqfn.diktat.ruleset.generation.GenerationConstantsKt</mainClass>
<arguments>
<argument>${project.build.outputDirectory}</argument>
<argument>${project.version}</argument>
<argument>${project.basedir}/../LICENSE</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>generate-warnings</id>
<phase>generate-test-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.cqfn.diktat.ruleset.generation.GenerationKt</mainClass>
<arguments>
<argument>${project.build.sourceDirectory}</argument>
<argument>${project.basedir}/src/test/resources</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<mainClass>org.cqfn.diktat.ruleset.generation.GenerationKt</mainClass>
<arguments>
<argument>${project.build.sourceDirectory}</argument>
<argument>${project.basedir}/src/test/resources</argument>
</arguments>
</configuration>
</plugin>

<plugin>
Expand Down
4 changes: 2 additions & 2 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This document was auto generated, please don't modify it.
// This document contains all enum properties from Warnings.kt as Strings.
// | This document was auto generated, please don't modify it.
nulls marked this conversation as resolved.
Show resolved Hide resolved
// | This document contains all enum properties from Warnings.kt as Strings.
package generated

import kotlin.String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ import kotlin.io.path.writeLines
/**
* The comment that will be added to the generated sources file.
*/
private val autoGenerationComment =
"""
| This document was auto generated, please don't modify it.
| This document contains all enum properties from Warnings.kt as Strings.
""".trimMargin()
internal const val autoGenerationComment = "This document was auto generated, please don't modify it."
nulls marked this conversation as resolved.
Show resolved Hide resolved

fun main(args: Array<String>) {
require(args.size == 2) {
"Only two arguments are expected: <source root> <test resource root>"
"Only four arguments are expected: <source root> <test resource root>"
}
generateWarningNames(args[0])
validateYear(args[1])
Expand All @@ -59,7 +55,10 @@ private fun generateWarningNames(sourceDirectory: String) {
.builder("generated", "WarningNames")
.addType(fileBody)
.indent(" ")
.addFileComment(autoGenerationComment)
.addFileComment("""
| $autoGenerationComment
| This document contains all enum properties from Warnings.kt as Strings.
""".trimIndent())
.build()

kotlinFile.writeTo(Paths.get(sourceDirectory))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
nulls marked this conversation as resolved.
Show resolved Hide resolved
* This file contains code for codegen: generating a file with diktat constants
*/

package org.cqfn.diktat.ruleset.generation

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec

import java.nio.file.Paths

import kotlin.io.path.readText


fun main(args: Array<String>) {
require(args.size == 3) {
"Only four arguments are expected: <source root> <diktat version> <license file>"
}
generateDiktatConstants(args[0], args[1], args[2])
}

private fun generateDiktatConstants(
sourceDirectory: String,
version: String,
licenseFile: String,
) {
FileSpec
.builder("generated", "DiktatConstants")
.addProperty(
PropertySpec.builder("DIKTAT_VERSION", String::class)
.addModifiers(KModifier.CONST)
.addModifiers(KModifier.INTERNAL)
.initializer("%S", version)
.build()
)
.addProperty(
PropertySpec.builder("LICENSE", String::class)
.addModifiers(KModifier.CONST)
.addModifiers(KModifier.INTERNAL)
.initializer("%S", Paths.get(licenseFile).readText())
.build()
)
.indent(" ")
.addFileComment("""
| $autoGenerationComment.
| This document contains diktat constants.
""".trimMargin())
.build()
.writeTo(Paths.get(sourceDirectory))
}

This file was deleted.

129 changes: 52 additions & 77 deletions diktat-ruleset/pom.xml → diktat/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,48 @@
<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>
<version>2.19.0</version>
nulls marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<!-- start: reporters -->
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-plain</artifactId>
</dependency>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-json</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-common</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-sarif</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk7</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-checkstyle</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<scope>provided</scope>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-reporter-html</artifactId>
</dependency>
<!-- end: reporters -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<scope>provided</scope>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-cli-jvm</artifactId>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
<scope>provided</scope>
<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,44 +83,26 @@
</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>
</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>
<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 -->
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -142,6 +114,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
52 changes: 52 additions & 0 deletions diktat/src/main/kotlin/org/cqfn/diktat/DiktatMain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.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 properties = DiktatProperties.parse(args)
properties.configureLogger()

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()
}
Loading