Refactoring: use convention mapping for configuring classpath of kotlin tasks

#KT-12658 fixed
    #KT-12750 fixed
This commit is contained in:
Alexey Tsvetkov
2016-07-25 16:05:32 +03:00
parent d55a8f8054
commit b13e1b451d
3 changed files with 5 additions and 68 deletions
@@ -56,19 +56,6 @@ fun Project.initKapt(
kotlinAfterJavaTask.source(kaptManager.generatedKotlinSourceDir)
kotlinAfterJavaTask.source(kaptManager.aptOutputDir)
val javaDestinationDir = project.files(javaTask.destinationDir)
javaTask.doLast {
kotlinAfterJavaTask.source(kotlinTask.source)
// we don't want kotlinAfterJavaTask to track modifications in generated class
kotlinAfterJavaTask.classpath -= javaDestinationDir
}
kotlinAfterJavaTask.doFirst {
kotlinAfterJavaTask.classpath += javaDestinationDir
}
kotlinAfterJavaTask.doLast {
kotlinAfterJavaTask.classpath -= javaDestinationDir
}
subpluginEnvironment.addSubpluginArguments(this, kotlinAfterJavaTask)
} else {
kotlinAfterJavaTask = null
@@ -108,7 +95,7 @@ private fun Project.createKotlinAfterJavaTask(
tasksProvider: KotlinTasksProvider
): KotlinCompile {
val kotlinAfterJavaTask = with (tasksProvider.createKotlinJVMTask(this, kotlinTask.name + KOTLIN_AFTER_JAVA_TASK_SUFFIX)) {
classpath = kotlinTask.classpath - project.files(javaTask.destinationDir)
conventionMapping.map("classpath") { kotlinTask.classpath }
this
}
@@ -5,6 +5,7 @@ import com.android.build.gradle.BasePlugin
import com.android.build.gradle.api.AndroidSourceSet
import com.android.build.gradle.internal.variant.BaseVariantData
import com.android.build.gradle.internal.variant.BaseVariantOutputData
import com.android.builder.model.SourceProvider
import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.Plugin
@@ -171,14 +172,7 @@ class Kotlin2JvmSourceSetProcessor(
val kaptManager = AnnotationProcessingManager(kotlinTask, javaTask, sourceSetName,
aptConfiguration.resolve(), aptOutputDir, aptWorkingDir)
kotlinAfterJavaTask = project.initKapt(kotlinTask, javaTask, kaptManager,
sourceSetName, null, subpluginEnvironment, tasksProvider)
if (kotlinAfterJavaTask != null) {
javaTask.doFirst {
kotlinAfterJavaTask!!.classpath = project.files(kotlinTask.classpath, javaTask.destinationDir)
}
}
kotlinAfterJavaTask = project.initKapt(kotlinTask, javaTask, kaptManager, sourceSetName, null, subpluginEnvironment, tasksProvider)
}
configureJavaTask(kotlinTask, javaTask, kotlinAfterJavaTask, logger)
@@ -369,11 +363,10 @@ open class KotlinAndroidPlugin @Inject constructor(val scriptHandler: ScriptHand
// store kotlin classes in separate directory. They will serve as class-path to java compiler
kotlinTask.destinationDir = File(project.buildDir, "tmp/kotlin-classes/$variantDataName")
kotlinTask.description = "Compiles the ${variantDataName} kotlin."
kotlinTask.description = "Compiles the $variantDataName kotlin."
kotlinTask.setDependsOn(javaTask.dependsOn)
val aptFiles = arrayListOf<File>()
for (provider in variantData.sourceProviders) {
val aptConfiguration = aptConfigurations[(provider as AndroidSourceSet).name]
// Ignore if there's only an annotation processor wrapper in dependencies (added by default)
@@ -384,21 +377,7 @@ open class KotlinAndroidPlugin @Inject constructor(val scriptHandler: ScriptHand
}
subpluginEnvironment.addSubpluginArguments(project, kotlinTask)
// should not be evaluated until right before compileKotlin evaluation since android can change
// java classpath during project evaluation (see prepareComAndroidSupportSupportV42311Library)
val fullClasspath = lazy {
val androidRT = project.files(AndroidGradleWrapper.getRuntimeJars(androidPlugin, androidExt))
(javaTask.classpath + androidRT) - project.files(kotlinTask.destinationDir)
}
kotlinTask.classpath = javaTask.classpath
kotlinTask.updateClasspathBeforeTask { fullClasspath.value }
kotlinTask.doFirst {
for (task in project.getTasksByName(kotlinTaskName + KOTLIN_AFTER_JAVA_TASK_SUFFIX, false)) {
(task as AbstractCompile).classpath = project.files(fullClasspath.value, javaTask.destinationDir)
}
}
kotlinTask.conventionMapping.map("classpath") { javaTask.classpath + project.files(AndroidGradleWrapper.getRuntimeJars(androidPlugin, androidExt)) }
val (aptOutputDir, aptWorkingDir) = project.getAptDirsForSourceSet(variantDataName)
variantData.addJavaSourceFoldersToModel(aptOutputDir)
var kotlinAfterJavaTask: KotlinCompile? = null
@@ -7,35 +7,6 @@ import org.gradle.api.tasks.compile.AbstractCompile
import java.io.File
import kotlin.reflect.KProperty
/*
* Adding fake up-to-date check to do something before execution, and before input snapshots are taken.
* Note, this hack is sensitive to up-to-date checks order, so it should be added before any custom up-to-date checks
* that could return false.
*
* Should be used only for compileKotlinTask.
*
* // todo: could add configureKotlinTask that always executes before compileKotlin instead
*/
internal fun AbstractCompile.updateClasspathBeforeTask(newClassPath: (oldClassPath: FileCollection) -> FileCollection) {
// Gradle can take inputs snapshots before task run (normally) or after task run (--rerun-tasks or some up-to-date when returned false).
// Fake up-to-date check to update classpath dynamically before inputs snapshot is taken.
// Won't be called in case of some other up-to-date check returned false before this one evaluation
// Won't be called in case of --rerun-tasks
var classpathIsUpdated = false
outputs.upToDateWhen {
classpath = newClassPath(classpath)
classpathIsUpdated = true
true
}
// in case fake up-to-date check was not called (see comment above)
doFirst {
if (!classpathIsUpdated) {
classpath = newClassPath(classpath)
}
}
}
internal fun AbstractCompile.appendClasspathDynamically(file: File) {
var added = false