Skip to content

Commit

Permalink
all vals in extension
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Nov 14, 2023
1 parent 2053375 commit 2f37366
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,38 @@ import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.VerificationTask
import org.gradle.api.tasks.util.PatternFilterable

/**
* An extension to configure diktat in build.gradle(.kts) file
*/
abstract class DiktatExtension {
/**
* @return boolean flag to support `ignoreFailures` property of [VerificationTask].
* Boolean flag to support `ignoreFailures` property of [VerificationTask].
*/
abstract fun getIgnoreFailures(): Property<Boolean>
abstract val ignoreFailures: Property<Boolean>

/**
* @return Property that will be used if you need to publish the report to GitHub
* Property that will be used if you need to publish the report to GitHub
*/
abstract val githubActions: Property<Boolean> // = false

/**
* Type of the reporter to use
*/
abstract fun getReporter(): Property<String> // = ""
abstract val reporter: Property<String>

/**
* Destination for reporter. If empty, will write to stdout.
*/
abstract fun getOutput(): Property<String> // = ""
abstract val output: RegularFileProperty

/**
* @return Baseline file, containing a list of errors that will be ignored.
* Baseline file, containing a list of errors that will be ignored.
* If this file doesn't exist, it will be created on the first invocation.
*/
abstract fun getBaseline(): Property<String> // null
abstract val baseline: RegularFileProperty

/**
* Path to diktat yml config file. Can be either absolute or relative to project's root directory.
Expand All @@ -47,8 +48,16 @@ abstract class DiktatExtension {
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val diktatConfigFile: RegularFileProperty

/**
* @return [PatternFilterable] to configure input files for diktat task
*/
@Nested
abstract fun getInputs(): PatternFilterable

/**
* Configure input files for diktat task
*
* @param action configuration lambda for [PatternFilterable]
*/
fun inputs(action: Action<in PatternFilterable>) = action.execute(getInputs())
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,24 @@ fun <T> Any.closureOf(action: T.() -> Unit): Closure<Any?> =
* @return CLI flag as string
*/
fun Project.getReporterType(diktatExtension: DiktatExtension): String {
val reporter = diktatExtension.getReporter().getOrElse("")
val name = reporter
val reporter = diktatExtension.reporter.getOrElse("")
val validReporters = listOf("sarif", "plain", "json", "html")
val reporterType = when {
diktatExtension.githubActions.getOrElse(false) -> {
if (reporter.isNotEmpty()) {
logger.warn("`diktat.githubActions` is set to true, so custom reporter [$name] will be ignored and SARIF reporter will be used")
logger.warn("`diktat.githubActions` is set to true, so custom reporter [$reporter] will be ignored and SARIF reporter will be used")
}
"sarif"
}
name.isEmpty() -> {
reporter.isEmpty() -> {
logger.info("Reporter name was not set. Using 'plain' reporter")
"plain"
}
name !in validReporters -> {
logger.warn("Reporter name is invalid (provided value: [$name]). Falling back to 'plain' reporter")
reporter !in validReporters -> {
logger.warn("Reporter name is invalid (provided value: [$reporter]). Falling back to 'plain' reporter")
"plain"
}
else -> name
else -> reporter
}

return reporterType
Expand All @@ -78,19 +77,15 @@ fun Project.getReporterType(diktatExtension: DiktatExtension): String {
* @param diktatExtension extension of type [DiktatExtension]
* @return destination [File] or null if stdout is used
*/
internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? {
val output by lazy { diktatExtension.getOutput().getOrElse("") }
return when {
diktatExtension.githubActions.getOrElse(false) -> project.layout.buildDirectory
.file("reports/diktat/diktat.sarif")
.get()
.asFile
.also {
Files.createDirectories(it.parentFile.toPath())
}
output.isNotEmpty() -> file(output)
else -> null
}
internal fun Project.getOutputFile(diktatExtension: DiktatExtension): File? = when {
diktatExtension.githubActions.getOrElse(false) -> project.layout.buildDirectory
.file("reports/diktat/diktat.sarif")
.get()
.asFile
.also {
Files.createDirectories(it.parentFile.toPath())
}
else -> diktatExtension.output.map { it.asFile }.orNull
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class DiktatTaskBase(
configFile = extension.diktatConfigFile.get().asFile.toPath(),
sourceRootDir = project.projectDir.toPath(),
files = source.files.map { it.toPath() },
baselineFile = extension.getBaseline().map { project.file(it).toPath() }.orNull,
baselineFile = extension.baseline.map { it.asFile.toPath() }.orNull,
reporterType = project.getReporterType(extension),
reporterOutput = project.getOutputFile(extension)?.outputStream(),
loggingListener = object : DiktatProcessorListener {
Expand All @@ -68,7 +68,7 @@ abstract class DiktatTaskBase(
}

init {
ignoreFailures = extension.getIgnoreFailures().getOrElse(false)
ignoreFailures = extension.ignoreFailures.getOrElse(false)
extension.getInputs().run {
if (includes.isEmpty() && excludes.isEmpty()) {
patternSet.include("src/**/*.kt")
Expand Down

0 comments on commit 2f37366

Please sign in to comment.