All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gitlab.arturbosch.detekt.cli.Main.kt Maven / Gradle / Ivy

There is a newer version: 1.23.7
Show newest version
@file:JvmName("Main")

package io.gitlab.arturbosch.detekt.cli

import io.github.detekt.tooling.api.InvalidConfig
import io.github.detekt.tooling.api.MaxIssuesReached
import io.github.detekt.tooling.api.UnexpectedError
import io.github.detekt.tooling.api.exitCode
import io.github.detekt.tooling.internal.NotApiButProbablyUsedByUsers
import io.gitlab.arturbosch.detekt.api.internal.whichKotlin
import io.gitlab.arturbosch.detekt.cli.runners.ConfigExporter
import io.gitlab.arturbosch.detekt.cli.runners.Executable
import io.gitlab.arturbosch.detekt.cli.runners.Runner
import io.gitlab.arturbosch.detekt.cli.runners.VersionPrinter
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import java.io.PrintStream
import kotlin.system.exitProcess

fun main(args: Array) {
    val result = CliRunner().run(args)
    @Suppress("ForbiddenMethodCall")
    when (val error = result.error) {
        is InvalidConfig, is MaxIssuesReached -> println(error.message)
        is UnexpectedError -> {
            when (val cause = error.cause) {
                is HelpRequest -> {
                    println(cause.usageText)
                    exitProcess(0)
                }
                is HandledArgumentViolation -> {
                    println(cause.message)
                    println(cause.usageText)
                }
                else -> cause.printStackTrace()
            }
        }
        else -> Unit // print nothing extra when there is no error
    }
    exitProcess(result.exitCode())
}

@NotApiButProbablyUsedByUsers
@Deprecated(
    "Don't build a runner yourself.",
    ReplaceWith(
        "DetektCli.load().run(args, outputPrinter, errorPrinter)",
        "io.github.detekt.tooling.api.DetektCli"
    )
)
fun buildRunner(
    args: Array,
    outputPrinter: PrintStream,
    errorPrinter: PrintStream
): Executable {
    check(KotlinCompilerVersion.VERSION == whichKotlin()) {
        """
            detekt was compiled with Kotlin ${whichKotlin()} but is currently running with ${KotlinCompilerVersion.VERSION}.
            This is not supported. See https://detekt.dev/docs/gettingstarted/gradle#dependencies for more information.
        """.trimIndent()
    }
    val arguments = parseArguments(args)
    return when {
        arguments.showVersion -> VersionPrinter(outputPrinter)
        arguments.generateConfig -> ConfigExporter(arguments, outputPrinter)
        else -> Runner(arguments, outputPrinter, errorPrinter)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy