Skip to content

Commit

Permalink
Fixed maven plugin (#1816)
Browse files Browse the repository at this point in the history
- fixed checking configFile
- restored logic for input and exclude in maven plugin (removed in #1655)
- fixed warning about provided dependency in maven plugin
  • Loading branch information
nulls authored Nov 23, 2023
1 parent b01cd64 commit b5c1a37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
1 change: 0 additions & 1 deletion diktat-maven-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ plugins {
}

dependencies {
implementation(libs.maven.plugin.api)
compileOnly(libs.maven.plugin.annotations)
compileOnly(libs.maven.core)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.saveourtool.diktat.DiktatRunnerArguments
import com.saveourtool.diktat.api.DiktatReporterCreationArguments
import com.saveourtool.diktat.api.DiktatReporterType
import com.saveourtool.diktat.diktatRunnerFactory
import com.saveourtool.diktat.util.isKotlinCodeOrScript

import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugin.Mojo
Expand All @@ -18,7 +19,6 @@ import java.io.FileOutputStream
import java.io.OutputStream
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.Path
import kotlin.io.path.inputStream
import kotlin.io.path.isRegularFile

Expand Down Expand Up @@ -95,10 +95,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
* @throws MojoExecutionException if an exception in __KtLint__ has been thrown
*/
override fun execute() {
val configFile = resolveConfig()
if (configFile.isRegularFile()) {
throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist")
}
val configFile = resolveConfig() ?: throw MojoExecutionException("Configuration file $diktatConfigFile doesn't exist")
log.info("Running diKTat plugin with configuration file $configFile and inputs $inputs" +
if (excludes.isNotEmpty()) " and excluding $excludes" else ""
)
Expand All @@ -113,7 +110,7 @@ abstract class DiktatBaseMojo : AbstractMojo() {
val args = DiktatRunnerArguments(
configInputStream = configFile.inputStream(),
sourceRootDir = sourceRootDir,
files = inputs.map(::Path),
files = files(),
baselineFile = baseline?.toPath(),
reporterArgsList = listOf(reporterArgs),
)
Expand Down Expand Up @@ -149,18 +146,40 @@ abstract class DiktatBaseMojo : AbstractMojo() {
* If [diktatConfigFile] is absolute, it's path is used. If [diktatConfigFile] is relative, this method looks for it in all maven parent projects.
* This way config file can be placed in parent module directory and used in all child modules too.
*
* @return a configuration file. File by this path might not exist.
* @return a configuration file. File by this path exists.
*/
private fun resolveConfig(): Path {
private fun resolveConfig(): Path? {
val file = Paths.get(diktatConfigFile)
if (file.isAbsolute) {
return file
}

return generateSequence(mavenProject) { it.parent }
.map { it.basedir.toPath().resolve(diktatConfigFile) }
.run {
firstOrNull { it.isRegularFile() } ?: first()
.firstOrNull { it.isRegularFile() }
}

private fun files(): List<Path> {
val (excludedDirs, excludedFiles) = excludes.map(::File).partition { it.isDirectory }
return inputs
.asSequence()
.map(::File)
.flatMap {
it.files(excludedDirs, excludedFiles)
}
.map { it.toPath() }
.toList()
}

@Suppress("TYPE_ALIAS")
private fun File.files(
excludedDirs: List<File>,
excludedFiles: List<File>,
): Sequence<File> = walk()
.filter { file ->
file.isFile && file.toPath().isKotlinCodeOrScript()
}
.filterNot { file ->
file in excludedFiles || excludedDirs.any { file.startsWith(it) }
}
}
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ junit-platform-suite = { module = "org.junit.platform:junit-platform-suite-engin

# maven
maven-core = { module = "org.apache.maven:maven-core", version.ref = "maven-api" }
maven-embedder = { module = "org.apache.maven:maven-embedder", version.ref = "maven-api" }
maven-compat = { module = "org.apache.maven:maven-compat", version.ref = "maven-api" }
maven-plugin-api = { module = "org.apache.maven:maven-plugin-api", version.ref = "maven-api" }
maven-plugin-annotations = { module = "org.apache.maven.plugin-tools:maven-plugin-annotations", version.ref = "maven-plugin-tools" }
maven-plugin-testing-harness = { module = "org.apache.maven.plugin-testing:maven-plugin-testing-harness", version.ref = "maven-plugin-testing-harness" }
plexus-cipher = { module = "org.codehaus.plexus:plexus-cipher", version.ref = "plexus" }
Expand Down

0 comments on commit b5c1a37

Please sign in to comment.