Skip to content

Commit

Permalink
Add main for diktat.jar (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls authored Dec 6, 2022
1 parent a2ba01a commit 9a9e768
Show file tree
Hide file tree
Showing 18 changed files with 619 additions and 266 deletions.
34 changes: 0 additions & 34 deletions diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,6 @@
<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>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/kotlin</source>
<source>src/test/resources</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>kotlinx-serialization</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-serialization</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
23 changes: 0 additions & 23 deletions 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 Expand Up @@ -116,28 +115,6 @@
<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>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<!-- for some weird reason this is essential to compile kotlin test sources despite testSourceDirectory is set -->
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package org.cqfn.diktat.plugin.maven

import org.cqfn.diktat.DiktatProcessCommand
import org.cqfn.diktat.api.DiktatLogLevel
import org.cqfn.diktat.ktlint.unwrap
import org.cqfn.diktat.ruleset.utils.isKotlinCodeOrScript

import com.pinterest.ktlint.core.LintError
import com.pinterest.ktlint.core.Reporter
Expand Down Expand Up @@ -36,12 +36,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 @@ -204,7 +198,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
directory
.walk()
.filter { file ->
file.isDirectory || file.extension.let { it == "kt" || it == "kts" }
file.isDirectory || file.toPath().isKotlinCodeOrScript()
}
.filter { it.isFile }
.filterNot { file -> file in excludedFiles || excludedDirs.any { file.startsWith(it) } }
Expand Down Expand Up @@ -240,18 +234,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
23 changes: 0 additions & 23 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,6 @@
</sourceDirs>
</configuration>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

package org.cqfn.diktat.ruleset.utils

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

internal const val SRC_DIRECTORY_NAME = "src"
private const val KOTLIN_EXTENSION = "kt"
private const val KOTLIN_SCRIPT_EXTENSION = KOTLIN_EXTENSION + "s"

/**
* Splits [this] string by file path separator.
Expand All @@ -17,11 +22,25 @@ fun String.splitPathToDirs(): List<String> =
.split("/")

/**
* Checks if [this] String is a name of a kotlin script file by checking whether file extension equals 'kts'
* Checks if [this] [String] is a name of a kotlin script file by checking whether file extension equals 'kts'
*
* @return true if this is a kotlin script file name, false otherwise
*/
fun String.isKotlinScript() = endsWith(".$KOTLIN_SCRIPT_EXTENSION")

/**
* Check if [this] [Path] is a kotlin script by checking whether an extension equals to 'kts'
*
* @return true if this is a kotlin script file name, false otherwise
*/
fun String.isKotlinScript() = endsWith(".kts")
fun Path.isKotlinScript() = this.extension.lowercase() == KOTLIN_SCRIPT_EXTENSION

/**
* Check if [this] [Path] is a kotlin code or script by checking whether an extension equals to `kt` or 'kts'
*
* @return true if this is a kotlin code or script file name, false otherwise
*/
fun Path.isKotlinCodeOrScript() = this.extension.lowercase() in setOf(KOTLIN_EXTENSION, KOTLIN_SCRIPT_EXTENSION)

/**
* Checks if [this] String is a name of a gradle kotlin script file by checking whether file extension equals 'gradle.kts'
Expand Down
Loading

0 comments on commit 9a9e768

Please sign in to comment.