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

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

There is a newer version: 1.23.7
Show newest version
package io.gitlab.arturbosch.detekt.cli

import com.beust.jcommander.IStringConverter
import com.beust.jcommander.ParameterException
import org.jetbrains.kotlin.config.LanguageVersion
import java.net.URL
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.notExists

class ExistingPathConverter : IStringConverter {
    override fun convert(value: String): Path {
        require(value.isNotBlank()) { "Provided path '$value' is empty." }
        val config = Path(value)
        if (config.notExists()) {
            throw ParameterException("Provided path '$value' does not exist!")
        }
        return config
    }
}

class PathConverter : IStringConverter {
    override fun convert(value: String) = Path(value)
}

interface DetektInputPathConverter : IStringConverter> {
    val converter: IStringConverter
    override fun convert(value: String): List =
        value.splitToSequence(SEPARATOR_COMMA, SEPARATOR_SEMICOLON)
            .map { it.trim() }
            .map { converter.convert(it) }
            .toList()
            .takeIf { it.isNotEmpty() }
            ?: error("Given input '$value' was impossible to parse!")
}

class MultipleClasspathResourceConverter : DetektInputPathConverter {
    override val converter = ClasspathResourceConverter()
}

class MultipleExistingPathConverter : DetektInputPathConverter {
    override val converter = ExistingPathConverter()
}

class LanguageVersionConverter : IStringConverter {
    override fun convert(value: String): LanguageVersion {
        val validValues by lazy { LanguageVersion.values().joinToString { it.versionString } }
        return requireNotNull(LanguageVersion.fromFullVersionString(value)) {
            "\"$value\" passed to --language-version, expected one of [$validValues]"
        }
    }
}

class ClasspathResourceConverter : IStringConverter {
    override fun convert(resource: String): URL {
        val relativeResource = if (resource.startsWith("/")) resource else "/$resource"
        return javaClass.getResource(relativeResource)
            ?: throw ParameterException("Classpath resource '$resource' does not exist!")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy