Convert main task creating helper to lazy API, refactor accordingly

This commit is contained in:
Ilya Chernikov
2019-08-14 15:42:42 +02:00
parent 0f41dc814f
commit 2ed8fa7624
8 changed files with 41 additions and 35 deletions
+4 -1
View File
@@ -12,11 +12,14 @@ import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.SourceSetOutput import org.gradle.api.tasks.SourceSetOutput
import org.gradle.kotlin.dsl.creating import org.gradle.kotlin.dsl.creating
import org.gradle.kotlin.dsl.extra import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.registering
import org.gradle.kotlin.dsl.the import org.gradle.kotlin.dsl.the
import java.io.File import java.io.File
import java.util.concurrent.Callable import java.util.concurrent.Callable
inline fun <reified T : Task> Project.task(noinline configuration: T.() -> Unit) = tasks.creating(T::class, configuration) inline fun <reified T : Task> Project.task(noinline configuration: T.() -> Unit) = tasks.registering(T::class, configuration)
inline fun <reified T : Task> Project.eagerTask(noinline configuration: T.() -> Unit) = tasks.creating(T::class, configuration)
fun Project.callGroovy(name: String, vararg args: Any?): Any? { fun Project.callGroovy(name: String, vararg args: Any?): Any? {
return (property(name) as Closure<*>).call(*args) return (property(name) as Closure<*>).call(*args)
+3 -1
View File
@@ -1,6 +1,7 @@
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.tasks.JavaExec import org.gradle.api.tasks.JavaExec
import org.gradle.jvm.tasks.Jar import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.creating
import org.gradle.kotlin.dsl.task import org.gradle.kotlin.dsl.task
/* /*
@@ -8,7 +9,8 @@ import org.gradle.kotlin.dsl.task
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
fun Project.smartJavaExec(configure: JavaExec.() -> Unit) = task<JavaExec> { // creating class eagerly here: using register causes problems due to quite complicated relationships between these tasks
fun Project.smartJavaExec(configure: JavaExec.() -> Unit) = tasks.creating(JavaExec::class) {
configure() configure()
passClasspathInJar() passClasspathInJar()
} }
+8 -9
View File
@@ -68,16 +68,15 @@ fun Project.configureFormInstrumentation() {
val instrumentedClassesDir = File(project.buildDir, "classes/${sourceSetParam.name}-instrumented") val instrumentedClassesDir = File(project.buildDir, "classes/${sourceSetParam.name}-instrumented")
(sourceSetParam.output.classesDirs as ConfigurableFileCollection).setFrom(instrumentedClassesDir) (sourceSetParam.output.classesDirs as ConfigurableFileCollection).setFrom(instrumentedClassesDir)
val instrumentTask = val instrumentTask =
project.tasks.create(sourceSetParam.getTaskName("instrument", "classes"), IntelliJInstrumentCodeTask::class.java) project.tasks.register(sourceSetParam.getTaskName("instrument", "classes"), IntelliJInstrumentCodeTask::class.java) {
instrumentTask.apply { dependsOn(sourceSetParam.classesTaskName).onlyIf { !classesDirsCopy.isEmpty }
dependsOn(sourceSetParam.classesTaskName).onlyIf { !classesDirsCopy.isEmpty } sourceSet = sourceSetParam
sourceSet = sourceSetParam instrumentationClasspath = instrumentationClasspathCfg
instrumentationClasspath = instrumentationClasspathCfg originalClassesDirs = classesDirsCopy
originalClassesDirs = classesDirsCopy output = instrumentedClassesDir
output = instrumentedClassesDir outputs.dir(instrumentedClassesDir)
} }
instrumentTask.outputs.dir(instrumentedClassesDir)
// Ensure that our task is invoked when the source set is built // Ensure that our task is invoked when the source set is built
sourceSetParam.compiledBy(instrumentTask) sourceSetParam.compiledBy(instrumentTask)
@Suppress("UNUSED_EXPRESSION") @Suppress("UNUSED_EXPRESSION")
@@ -85,7 +85,7 @@ val fullRuntimeSources by task<Sync> {
val reducedRuntimeSources by task<Sync> { val reducedRuntimeSources by task<Sync> {
dependsOn(fullRuntimeSources) dependsOn(fullRuntimeSources)
from(fullRuntimeSources.outputs.files.singleFile) { from(fullRuntimeSources.get().outputs.files.singleFile) {
exclude( exclude(
listOf( listOf(
"libraries/stdlib/unsigned/**", "libraries/stdlib/unsigned/**",
@@ -161,10 +161,10 @@ fun JavaExec.buildKLib(sources: List<String>, dependencies: List<String>, outPat
val fullRuntimeDir = buildDir.resolve("fullRuntime/klib") val fullRuntimeDir = buildDir.resolve("fullRuntime/klib")
val generateFullRuntimeKLib by task<NoDebugJavaExec> { val generateFullRuntimeKLib by eagerTask<NoDebugJavaExec> {
dependsOn(fullRuntimeSources) dependsOn(fullRuntimeSources)
buildKLib(sources = listOf(fullRuntimeSources.outputs.files.singleFile.path), buildKLib(sources = listOf(fullRuntimeSources.get().outputs.files.singleFile.path),
dependencies = emptyList(), dependencies = emptyList(),
outPath = fullRuntimeDir.absolutePath, outPath = fullRuntimeDir.absolutePath,
commonSources = listOf("common", "src", "unsigned").map { "$buildDir/fullRuntime/src/libraries/stdlib/$it" } commonSources = listOf("common", "src", "unsigned").map { "$buildDir/fullRuntime/src/libraries/stdlib/$it" }
@@ -178,11 +178,11 @@ val packFullRuntimeKLib by tasks.registering(Jar::class) {
archiveFileName.set("full-runtime.klib") archiveFileName.set("full-runtime.klib")
} }
val generateReducedRuntimeKLib by task<NoDebugJavaExec> { val generateReducedRuntimeKLib by eagerTask<NoDebugJavaExec> {
dependsOn(reducedRuntimeSources) dependsOn(reducedRuntimeSources)
val outPath = buildDir.resolve("reducedRuntime/klib").absolutePath val outPath = buildDir.resolve("reducedRuntime/klib").absolutePath
buildKLib(sources = listOf(reducedRuntimeSources.outputs.files.singleFile.path), buildKLib(sources = listOf(reducedRuntimeSources.get().outputs.files.singleFile.path),
dependencies = emptyList(), dependencies = emptyList(),
outPath = outPath, outPath = outPath,
commonSources = listOf("common", "src", "unsigned").map { "$buildDir/reducedRuntime/src/libraries/stdlib/$it" } commonSources = listOf("common", "src", "unsigned").map { "$buildDir/reducedRuntime/src/libraries/stdlib/$it" }
@@ -193,7 +193,7 @@ val kotlinTestCommonSources = listOf(
"$rootDir/libraries/kotlin.test/annotations-common/src/main", "$rootDir/libraries/kotlin.test/annotations-common/src/main",
"$rootDir/libraries/kotlin.test/common/src/main" "$rootDir/libraries/kotlin.test/common/src/main"
) )
val generateKotlinTestKLib by task<NoDebugJavaExec> { val generateKotlinTestKLib by eagerTask<NoDebugJavaExec> {
dependsOn(generateFullRuntimeKLib) dependsOn(generateFullRuntimeKLib)
buildKLib( buildKLib(
+3 -1
View File
@@ -69,7 +69,9 @@ fun unzipSdkTask(
} }
} }
} }
prepareSdk.dependsOn(unzipTask) prepareSdk.configure {
dependsOn(unzipTask)
}
additionalConfig?.also { additionalConfig?.also {
dependencies.add(it.name, dependency) dependencies.add(it.name, dependency)
+9 -9
View File
@@ -123,14 +123,14 @@ val reflectShadowJar by task<ShadowJar> {
} }
} }
val stripMetadata by tasks.creating { val stripMetadata by tasks.registering {
dependsOn(reflectShadowJar) dependsOn(reflectShadowJar)
val inputJar = reflectShadowJar.outputs.files.singleFile val inputJar = provider { reflectShadowJar.get().outputs.files.singleFile }
val outputJar = File("$libsDir/kotlin-reflect-stripped.jar") val outputJar = File("$libsDir/kotlin-reflect-stripped.jar")
inputs.file(inputJar) inputs.file(inputJar)
outputs.file(outputJar) outputs.file(outputJar)
doLast { doLast {
stripMetadata(logger, "kotlin/reflect/jvm/internal/impl/.*", inputJar, outputJar) stripMetadata(logger, "kotlin/reflect/jvm/internal/impl/.*", inputJar.get(), outputJar)
} }
} }
@@ -138,10 +138,10 @@ val proguardOutput = "$libsDir/${property("archivesBaseName")}-proguard.jar"
val proguard by task<ProGuardTask> { val proguard by task<ProGuardTask> {
dependsOn(stripMetadata) dependsOn(stripMetadata)
inputs.files(stripMetadata.outputs.files) inputs.files(stripMetadata.get().outputs.files)
outputs.file(proguardOutput) outputs.file(proguardOutput)
injars(mapOf("filter" to "!META-INF/versions/**"), stripMetadata.outputs.files) injars(mapOf("filter" to "!META-INF/versions/**"), stripMetadata.get().outputs.files)
injars(mapOf("filter" to "!META-INF/**,!**/*.kotlin_builtins"), proguardAdditionalInJars) injars(mapOf("filter" to "!META-INF/**,!**/*.kotlin_builtins"), proguardAdditionalInJars)
outjars(proguardOutput) outjars(proguardOutput)
@@ -198,7 +198,7 @@ val result by task<Jar> {
dependsOn(task) dependsOn(task)
from { from {
zipTree(task.outputs.files.singleFile) zipTree(task.get().outputs.files.singleFile)
} }
callGroovy("manifestAttributes", manifest, project, "Main") callGroovy("manifestAttributes", manifest, project, "Main")
@@ -208,7 +208,7 @@ val modularJar by task<Jar> {
dependsOn(proguard) dependsOn(proguard)
archiveClassifier.set("modular") archiveClassifier.set("modular")
from(zipTree(file(proguardOutput))) from(zipTree(file(proguardOutput)))
from(zipTree(reflectShadowJar.archivePath)) { from(zipTree(reflectShadowJar.get().archivePath)) {
include("META-INF/versions/**") include("META-INF/versions/**")
} }
callGroovy("manifestAttributes", manifest, project, "Main", true) callGroovy("manifestAttributes", manifest, project, "Main", true)
@@ -216,14 +216,14 @@ val modularJar by task<Jar> {
val dexMethodCount by task<DexMethodCount> { val dexMethodCount by task<DexMethodCount> {
dependsOn(result) dependsOn(result)
jarFile = result.outputs.files.single() jarFile = result.get().outputs.files.single()
ownPackages = listOf("kotlin.reflect") ownPackages = listOf("kotlin.reflect")
} }
tasks.getByName("check").dependsOn(dexMethodCount) tasks.getByName("check").dependsOn(dexMethodCount)
artifacts { artifacts {
listOf(mainJar.name, "runtime", "archives").forEach { configurationName -> listOf(mainJar.name, "runtime", "archives").forEach { configurationName ->
add(configurationName, result.outputs.files.singleFile) { add(configurationName, result.get().outputs.files.singleFile) {
builtBy(result) builtBy(result)
} }
} }
@@ -73,24 +73,24 @@ val proguard by task<ProGuardTask> {
dependsOn(packJar) dependsOn(packJar)
configuration("main-kts.pro") configuration("main-kts.pro")
injars(mapOf("filter" to "!META-INF/versions/**"), packJar.outputs.files) injars(mapOf("filter" to "!META-INF/versions/**"), packJar.get().outputs.files)
val outputJar = fileFrom(buildDir, "libs", "$jarBaseName-$version-after-proguard.jar") val outputJar = fileFrom(buildDir, "libs", "$jarBaseName-$version-after-proguard.jar")
outjars(outputJar) outjars(outputJar)
inputs.files(packJar.outputs.files.singleFile) inputs.files(packJar.get().outputs.files.singleFile)
outputs.file(outputJar) outputs.file(outputJar)
libraryjars(mapOf("filter" to "!META-INF/versions/**"), proguardLibraryJars) libraryjars(mapOf("filter" to "!META-INF/versions/**"), proguardLibraryJars)
} }
val resultJar = tasks.register<Jar>("resultJar") { val resultJar by task<Jar> {
val pack = if (kotlinBuildProperties.proguard) proguard else packJar val pack = if (kotlinBuildProperties.proguard) proguard else packJar
dependsOn(pack) dependsOn(pack)
setupPublicJar(jarBaseName) setupPublicJar(jarBaseName)
from { from {
zipTree(pack.outputs.files.singleFile) zipTree(pack.get().outputs.files.singleFile)
} }
} }
+3 -3
View File
@@ -246,7 +246,7 @@ val proguard by task<ProGuardTask> {
val outputJar = fileFrom(buildDir, "libs", "$compilerBaseName-after-proguard.jar") val outputJar = fileFrom(buildDir, "libs", "$compilerBaseName-after-proguard.jar")
inputs.files(packCompiler.outputs.files.singleFile) inputs.files(packCompiler.get().outputs.files.singleFile)
outputs.file(outputJar) outputs.file(outputJar)
libraryjars(mapOf("filter" to "!META-INF/versions/**"), proguardLibraries) libraryjars(mapOf("filter" to "!META-INF/versions/**"), proguardLibraries)
@@ -255,7 +255,7 @@ val proguard by task<ProGuardTask> {
// This properties are used by proguard config compiler.pro // This properties are used by proguard config compiler.pro
doFirst { doFirst {
System.setProperty("kotlin-compiler-jar-before-shrink", packCompiler.outputs.files.singleFile.canonicalPath) System.setProperty("kotlin-compiler-jar-before-shrink", packCompiler.get().outputs.files.singleFile.canonicalPath)
System.setProperty("kotlin-compiler-jar", outputJar.canonicalPath) System.setProperty("kotlin-compiler-jar", outputJar.canonicalPath)
} }
} }
@@ -267,7 +267,7 @@ val jar = runtimeJar {
dependsOn(pack) dependsOn(pack)
from { from {
zipTree(pack.outputs.files.singleFile) zipTree(pack.get().outputs.files.singleFile)
} }
manifest.attributes["Class-Path"] = compilerManifestClassPath manifest.attributes["Class-Path"] = compilerManifestClassPath