Skip to content

Commit

Permalink
fix detekt issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed Sep 2, 2023
1 parent 751a094 commit be2e63d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,26 @@ public class KtormKspMavenPluginExtension : KotlinMavenPluginExtension {

override fun getPluginOptions(project: MavenProject, execution: MojoExecution): List<PluginOption> {
val userOptions = parseUserOptions(execution)
val options = buildPluginOptions(project, execution, userOptions)

for (key in listOf(KspCliOption.JAVA_OUTPUT_DIR_OPTION, KspCliOption.KOTLIN_OUTPUT_DIR_OPTION)) {
if (execution.mojoDescriptor.goal == "compile") {
if (execution.mojoDescriptor.goal == "compile") {
val options = buildPluginOptions(project, execution, userOptions)
for (key in listOf(KspCliOption.JAVA_OUTPUT_DIR_OPTION, KspCliOption.KOTLIN_OUTPUT_DIR_OPTION)) {
project.addCompileSourceRoot(options[key] ?: userOptions[key]!![0])
}
if (execution.mojoDescriptor.goal == "test-compile") {

return options.map { (option, value) -> PluginOption("ksp", compilerPluginId, option.optionName, value) }
}

if (execution.mojoDescriptor.goal == "test-compile") {
val options = buildTestPluginOptions(project, execution, userOptions)
for (key in listOf(KspCliOption.JAVA_OUTPUT_DIR_OPTION, KspCliOption.KOTLIN_OUTPUT_DIR_OPTION)) {
project.addTestCompileSourceRoot(options[key] ?: userOptions[key]!![0])
}

return options.map { (option, value) -> PluginOption("ksp", compilerPluginId, option.optionName, value) }
}

return options.map { (option, value) -> PluginOption("ksp", compilerPluginId, option.optionName, value) }
return emptyList()
}

private fun parseUserOptions(execution: MojoExecution): Map<KspCliOption, List<String>> {
Expand All @@ -82,74 +90,80 @@ public class KtormKspMavenPluginExtension : KotlinMavenPluginExtension {
val buildDir = project.build.directory
val options = LinkedHashMap<KspCliOption, String>()

if (execution.mojoDescriptor.goal == "compile") {
if (KspCliOption.CLASS_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.CLASS_OUTPUT_DIR_OPTION] = project.build.outputDirectory
}
if (KspCliOption.JAVA_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.JAVA_OUTPUT_DIR_OPTION] = path(buildDir, "generated-sources", "ksp-java")
}
if (KspCliOption.KOTLIN_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KOTLIN_OUTPUT_DIR_OPTION] = path(buildDir, "generated-sources", "ksp")
}
if (KspCliOption.RESOURCE_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.RESOURCE_OUTPUT_DIR_OPTION] = project.build.outputDirectory
}
if (KspCliOption.CACHES_DIR_OPTION !in userOptions) {
options[KspCliOption.CACHES_DIR_OPTION] = path(buildDir, "ksp-caches")
}
if (KspCliOption.PROJECT_BASE_DIR_OPTION !in userOptions) {
options[KspCliOption.PROJECT_BASE_DIR_OPTION] = baseDir
}
if (KspCliOption.KSP_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KSP_OUTPUT_DIR_OPTION] = path(buildDir, "ksp")
}
if (KspCliOption.PROCESSOR_CLASSPATH_OPTION !in userOptions) {
options[KspCliOption.PROCESSOR_CLASSPATH_OPTION] = processorClasspath(project, execution)
}
if (KspCliOption.WITH_COMPILATION_OPTION !in userOptions) {
options[KspCliOption.WITH_COMPILATION_OPTION] = "true"
}
if (KspCliOption.CLASS_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.CLASS_OUTPUT_DIR_OPTION] = project.build.outputDirectory
}
if (KspCliOption.JAVA_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.JAVA_OUTPUT_DIR_OPTION] = path(buildDir, "generated-sources", "ksp-java")
}
if (KspCliOption.KOTLIN_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KOTLIN_OUTPUT_DIR_OPTION] = path(buildDir, "generated-sources", "ksp")
}
if (KspCliOption.RESOURCE_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.RESOURCE_OUTPUT_DIR_OPTION] = project.build.outputDirectory
}
if (KspCliOption.CACHES_DIR_OPTION !in userOptions) {
options[KspCliOption.CACHES_DIR_OPTION] = path(buildDir, "ksp-caches")
}
if (KspCliOption.PROJECT_BASE_DIR_OPTION !in userOptions) {
options[KspCliOption.PROJECT_BASE_DIR_OPTION] = baseDir
}
if (KspCliOption.KSP_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KSP_OUTPUT_DIR_OPTION] = path(buildDir, "ksp")
}
if (KspCliOption.PROCESSOR_CLASSPATH_OPTION !in userOptions) {
options[KspCliOption.PROCESSOR_CLASSPATH_OPTION] = processorClasspath(project, execution)
}
if (KspCliOption.WITH_COMPILATION_OPTION !in userOptions) {
options[KspCliOption.WITH_COMPILATION_OPTION] = "true"
}

val apOptions = userOptions[KspCliOption.PROCESSING_OPTIONS_OPTION] ?: emptyList()
if (apOptions.none { it.startsWith("ktorm.ktlintExecutable=") }) {
options[KspCliOption.PROCESSING_OPTIONS_OPTION] = "ktorm.ktlintExecutable=${ktlintExecutable(project)}"
}
val apOptions = userOptions[KspCliOption.PROCESSING_OPTIONS_OPTION] ?: emptyList()
if (apOptions.none { it.startsWith("ktorm.ktlintExecutable=") }) {
options[KspCliOption.PROCESSING_OPTIONS_OPTION] = "ktorm.ktlintExecutable=${ktlintExecutable(project)}"
}

if (execution.mojoDescriptor.goal == "test-compile") {
if (KspCliOption.CLASS_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.CLASS_OUTPUT_DIR_OPTION] = project.build.testOutputDirectory
}
if (KspCliOption.JAVA_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.JAVA_OUTPUT_DIR_OPTION] = path(buildDir, "generated-test-sources", "ksp-java")
}
if (KspCliOption.KOTLIN_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KOTLIN_OUTPUT_DIR_OPTION] = path(buildDir, "generated-test-sources", "ksp")
}
if (KspCliOption.RESOURCE_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.RESOURCE_OUTPUT_DIR_OPTION] = project.build.testOutputDirectory
}
if (KspCliOption.CACHES_DIR_OPTION !in userOptions) {
options[KspCliOption.CACHES_DIR_OPTION] = path(buildDir, "ksp-caches")
}
if (KspCliOption.PROJECT_BASE_DIR_OPTION !in userOptions) {
options[KspCliOption.PROJECT_BASE_DIR_OPTION] = baseDir
}
if (KspCliOption.KSP_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KSP_OUTPUT_DIR_OPTION] = path(buildDir, "ksp-test")
}
if (KspCliOption.PROCESSOR_CLASSPATH_OPTION !in userOptions) {
options[KspCliOption.PROCESSOR_CLASSPATH_OPTION] = processorClasspath(project, execution)
}
if (KspCliOption.WITH_COMPILATION_OPTION !in userOptions) {
options[KspCliOption.WITH_COMPILATION_OPTION] = "true"
}
return options
}

val apOptions = userOptions[KspCliOption.PROCESSING_OPTIONS_OPTION] ?: emptyList()
if (apOptions.none { it.startsWith("ktorm.ktlintExecutable=") }) {
options[KspCliOption.PROCESSING_OPTIONS_OPTION] = "ktorm.ktlintExecutable=${ktlintExecutable(project)}"
}
private fun buildTestPluginOptions(
project: MavenProject, execution: MojoExecution, userOptions: Map<KspCliOption, List<String>>
): Map<KspCliOption, String> {
val baseDir = project.basedir.path
val buildDir = project.build.directory
val options = LinkedHashMap<KspCliOption, String>()

if (KspCliOption.CLASS_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.CLASS_OUTPUT_DIR_OPTION] = project.build.testOutputDirectory
}
if (KspCliOption.JAVA_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.JAVA_OUTPUT_DIR_OPTION] = path(buildDir, "generated-test-sources", "ksp-java")
}
if (KspCliOption.KOTLIN_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KOTLIN_OUTPUT_DIR_OPTION] = path(buildDir, "generated-test-sources", "ksp")
}
if (KspCliOption.RESOURCE_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.RESOURCE_OUTPUT_DIR_OPTION] = project.build.testOutputDirectory
}
if (KspCliOption.CACHES_DIR_OPTION !in userOptions) {
options[KspCliOption.CACHES_DIR_OPTION] = path(buildDir, "ksp-caches")
}
if (KspCliOption.PROJECT_BASE_DIR_OPTION !in userOptions) {
options[KspCliOption.PROJECT_BASE_DIR_OPTION] = baseDir
}
if (KspCliOption.KSP_OUTPUT_DIR_OPTION !in userOptions) {
options[KspCliOption.KSP_OUTPUT_DIR_OPTION] = path(buildDir, "ksp-test")
}
if (KspCliOption.PROCESSOR_CLASSPATH_OPTION !in userOptions) {
options[KspCliOption.PROCESSOR_CLASSPATH_OPTION] = processorClasspath(project, execution)
}
if (KspCliOption.WITH_COMPILATION_OPTION !in userOptions) {
options[KspCliOption.WITH_COMPILATION_OPTION] = "true"
}

val apOptions = userOptions[KspCliOption.PROCESSING_OPTIONS_OPTION] ?: emptyList()
if (apOptions.none { it.startsWith("ktorm.ktlintExecutable=") }) {
options[KspCliOption.PROCESSING_OPTIONS_OPTION] = "ktorm.ktlintExecutable=${ktlintExecutable(project)}"
}

return options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ internal class StandaloneKtLintCodeFormatter(val environment: SymbolProcessorEnv
private val command = buildCommand()

init {
environment.logger.info("[ktorm-ksp-compiler] init standalone ktlint code formatter with command: $command")
environment.logger.info("[ktorm-ksp-compiler] init ktlint formatter with command: ${command.joinToString(" ")}")
}

override fun format(code: String): String {
try {
val p = Runtime.getRuntime().exec(command)
val p = ProcessBuilder(command).start()
p.outputStream.bufferedWriter(Charsets.UTF_8).use { it.write(preprocessCode(code)) }
p.waitFor()

Expand All @@ -45,20 +45,19 @@ internal class StandaloneKtLintCodeFormatter(val environment: SymbolProcessorEnv
}
}

private fun buildCommand(): String {
val isJava8 =
try { Class.forName("java.lang.reflect.InaccessibleObjectException"); false }
catch (_: ClassNotFoundException) { true }
private fun buildCommand(): List<String> {
val n = "java.lang.reflect.InaccessibleObjectException"
val isJava8 = try { Class.forName(n); false } catch (_: ClassNotFoundException) { true }

val java = findJavaExecutable()
val ktlint = environment.options["ktorm.ktlintExecutable"]!!
val config = createEditorConfigFile()

if (isJava8) {
return "$java -jar $ktlint --format --stdin --log-level=none --editorconfig=$config"
return listOf(java, "-jar", ktlint, "-F", "--stdin", "--log-level=none", "--editorconfig=$config")
} else {
val jvmArgs = "--add-opens java.base/java.lang=ALL-UNNAMED"
return "$java $jvmArgs -jar $ktlint --format --stdin --log-level=none --editorconfig=$config"
val jvmArgs = arrayOf("--add-opens", "java.base/java.lang=ALL-UNNAMED")
return listOf(java, *jvmArgs, "-jar", ktlint, "-F", "--stdin", "--log-level=none", "--editorconfig=$config")
}
}

Expand Down

0 comments on commit be2e63d

Please sign in to comment.