diff --git a/ktorm-ksp-compiler-maven-plugin/src/main/kotlin/org/ktorm/ksp/compiler/maven/KtormKspMavenPluginExtension.kt b/ktorm-ksp-compiler-maven-plugin/src/main/kotlin/org/ktorm/ksp/compiler/maven/KtormKspMavenPluginExtension.kt index e5cf1e96..f278e76d 100644 --- a/ktorm-ksp-compiler-maven-plugin/src/main/kotlin/org/ktorm/ksp/compiler/maven/KtormKspMavenPluginExtension.kt +++ b/ktorm-ksp-compiler-maven-plugin/src/main/kotlin/org/ktorm/ksp/compiler/maven/KtormKspMavenPluginExtension.kt @@ -49,18 +49,26 @@ public class KtormKspMavenPluginExtension : KotlinMavenPluginExtension { override fun getPluginOptions(project: MavenProject, execution: MojoExecution): List { 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> { @@ -82,74 +90,80 @@ public class KtormKspMavenPluginExtension : KotlinMavenPluginExtension { val buildDir = project.build.directory val options = LinkedHashMap() - 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> + ): Map { + val baseDir = project.basedir.path + val buildDir = project.build.directory + val options = LinkedHashMap() + + 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 diff --git a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/StandaloneKtLintCodeFormatter.kt b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/StandaloneKtLintCodeFormatter.kt index 4c714ea7..d494d915 100644 --- a/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/StandaloneKtLintCodeFormatter.kt +++ b/ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/StandaloneKtLintCodeFormatter.kt @@ -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() @@ -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 { + 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") } }