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

Fixed maven plugin #1816

Merged
merged 10 commits into from
Nov 23, 2023
Merged
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.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.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 @@
* @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 @@
val args = DiktatRunnerArguments(
configInputStream = configFile.inputStream(),
sourceRootDir = sourceRootDir,
files = inputs.map(::Path),
files = files(),

Check warning on line 113 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L113

Added line #L113 was not covered by tests
baselineFile = baseline?.toPath(),
reporterArgsList = listOf(reporterArgs),
)
Expand Down Expand Up @@ -149,18 +146,40 @@
* 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() }

Check warning on line 159 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L159

Added line #L159 was not covered by tests
}

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

Check warning on line 168 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L163-L168

Added lines #L163 - L168 were not covered by tests
}
.map { it.toPath() }
.toList()

Check warning on line 171 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L170-L171

Added lines #L170 - L171 were not covered by tests
}

@Suppress("TYPE_ALIAS")
private fun File.files(
excludedDirs: List<File>,
excludedFiles: List<File>,
): Sequence<File> = walk()
.filter { file ->

Check warning on line 179 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L178-L179

Added lines #L178 - L179 were not covered by tests
file.isFile && file.toPath().isKotlinCodeOrScript()
}
.filterNot { file ->

Check warning on line 182 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L182

Added line #L182 was not covered by tests
file in excludedFiles || excludedDirs.any { file.startsWith(it) }
}

Check warning on line 184 in diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt

View check run for this annotation

Codecov / codecov/patch

diktat-maven-plugin/src/main/kotlin/com/saveourtool/diktat/plugin/maven/DiktatBaseMojo.kt#L184

Added line #L184 was not covered by tests
}
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
Loading