From 5645d54e3556ee8a0cad5d2feb7bde2d25764124 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 3 Jun 2016 02:06:23 -0800 Subject: [PATCH] If kapt{} detected, run javac before kotlinc. --- .../kobalt/internal/JvmCompilerPlugin.kt | 23 ++++++++++++++++++- .../com/beust/kobalt/plugin/apt/AptPlugin.kt | 6 ++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index 0e7bc9be..94b2edc5 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -16,6 +16,7 @@ import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn import java.io.File +import java.util.* import javax.inject.Inject import javax.inject.Singleton @@ -161,12 +162,32 @@ open class JvmCompilerPlugin @Inject constructor( throw KobaltException("Couldn't find any compiler for project ${project.name}") } else { val allCompilers = compilerContributors.flatMap { it.compilersFor(project, context)}.sorted() - allCompilers.forEach { compiler -> + + /** + * Swap the Java and Kotlin compilers from the list. + */ + fun swapJavaAndKotlin(allCompilers: List): List { + val result = ArrayList(allCompilers) + var ik = -1 + var ij = -1 + allCompilers.withIndex().forEach { wi -> + if (wi.value.sourceSuffixes.contains("java")) ij = wi.index + if (wi.value.sourceSuffixes.contains("kt")) ik = wi.index + } + Collections.swap(result, ik, ij) + return result + } + + // If this project has a kapt{} directive, we want to run the Java compiler first + val hasKapt = project.projectProperties.get("kaptConfig") != null + var finalAllCompilers = if (hasKapt) swapJavaAndKotlin(allCompilers) else allCompilers + finalAllCompilers.forEach { compiler -> val compilerResults = compilerUtils.invokeCompiler(project, context, compiler, sourceDirectories(project, context), isTest) results.addAll(compilerResults.successResults) if (failedResult == null) failedResult = compilerResults.failedResult } + return if (failedResult != null) failedResult!! else if (results.size > 0) results[0] else TaskResult(true) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt index cf2b93d5..9a930ed9 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt @@ -47,6 +47,7 @@ class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, va companion object { const val PLUGIN_NAME = "Apt" + const val KAPT_CONFIG = "kaptConfig" } override val name = PLUGIN_NAME @@ -125,7 +126,10 @@ class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, va private val kaptConfigs: HashMap = hashMapOf() - fun addKaptConfig(project: Project, kapt: KaptConfig) = kaptConfigs.put(project.name, kapt) + fun addKaptConfig(project: Project, kapt: KaptConfig) { + project.projectProperties.put(KAPT_CONFIG, kapt) + kaptConfigs.put(project.name, kapt) + } } class AptConfig(var outputDir: String = "generated/source/apt")