[Gradle] Introduce lazyFuture and futureExtension methods

^KT-34662 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-03-13 16:26:52 +01:00
committed by Space Team
parent 7192b4c5af
commit 7c65d00f29
3 changed files with 32 additions and 13 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.sources.withDependsOnClosure
import org.jetbrains.kotlin.gradle.targets.native.internal.CInteropCommonizerTask.CInteropCommonizerDependencies
import org.jetbrains.kotlin.gradle.targets.native.internal.CInteropCommonizerTask.CInteropGist
import org.jetbrains.kotlin.gradle.tasks.CInteropProcess
import org.jetbrains.kotlin.gradle.utils.*
@@ -32,6 +33,9 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
import java.io.File
import javax.inject.Inject
private typealias GroupedCommonizerDependencies = Map<CInteropCommonizerGroup, List<CInteropCommonizerDependencies>>
@CacheableTask
internal open class CInteropCommonizerTask
@Inject constructor(
@@ -121,8 +125,8 @@ internal open class CInteropCommonizerTask
* For Gradle Configuration Cache support the Group-to-Dependencies relation should be pre-cached.
* It is used during execution phase.
*/
private val groupedCommonizerDependencies: Future<Map<CInteropCommonizerGroup, List<CInteropCommonizerDependencies>>> = project.future {
val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@future emptyMap()
private val groupedCommonizerDependencies: Future<GroupedCommonizerDependencies> by project.lazyFuture {
val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@lazyFuture emptyMap()
val sourceSetsByTarget = multiplatformExtension.sourceSets.groupBy { sourceSet -> sourceSet.commonizerTarget.getOrThrow() }
val sourceSetsByGroup = multiplatformExtension.sourceSets.groupBy { sourceSet ->
@@ -228,7 +232,7 @@ internal open class CInteropCommonizerTask
}
@get:Internal
internal val allInteropGroups: Future<Set<CInteropCommonizerGroup>> = project.future {
internal val allInteropGroups: Future<Set<CInteropCommonizerGroup>> by project.lazyFuture {
val dependents = allDependents.await()
val allScopeSets = dependents.map { it.scopes }.toSet()
val rootScopeSets = allScopeSets.filter { scopeSet ->
@@ -263,8 +267,8 @@ internal open class CInteropCommonizerTask
return suitableGroups.firstOrNull()
}
private val allDependents: Future<Set<CInteropCommonizerDependent>> = project.future {
val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@future emptySet()
private val allDependents: Future<Set<CInteropCommonizerDependent>> by project.lazyFuture {
val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@lazyFuture emptySet()
val fromSharedNativeCompilations = multiplatformExtension
.targets.flatMap { target -> target.compilations }
@@ -18,20 +18,20 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.utils.Future
import org.jetbrains.kotlin.gradle.utils.lazyFuture
import org.jetbrains.kotlin.gradle.utils.future
import org.jetbrains.kotlin.gradle.utils.futureExtension
import org.jetbrains.kotlin.tooling.core.UnsafeApi
internal val KotlinSourceSet.commonizerTarget: Future<CommonizerTarget?> by lazyFuture {
internal val KotlinSourceSet.commonizerTarget: Future<CommonizerTarget?> by futureExtension {
await(KotlinPluginLifecycle.Stage.AfterFinaliseRefinesEdges)
@OptIn(UnsafeApi::class)
inferCommonizerTarget(this)
}
internal val KotlinSourceSet.sharedCommonizerTarget: Future<SharedCommonizerTarget?> by lazyFuture {
commonizerTarget.await() as? SharedCommonizerTarget
}
internal val KotlinSourceSet.sharedCommonizerTarget: Future<SharedCommonizerTarget?>
get() = project.future { commonizerTarget.await() as? SharedCommonizerTarget }
internal val KotlinCompilation<*>.commonizerTarget: Future<CommonizerTarget?> by lazyFuture {
internal val KotlinCompilation<*>.commonizerTarget: Future<CommonizerTarget?> by futureExtension {
await(KotlinPluginLifecycle.Stage.AfterFinaliseRefinesEdges)
@OptIn(UnsafeApi::class)
inferCommonizerTarget(this)
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.tooling.core.ExtrasLazyProperty
import org.jetbrains.kotlin.tooling.core.HasMutableExtras
import org.jetbrains.kotlin.tooling.core.extrasLazyProperty
import java.io.Serializable
import kotlin.reflect.KProperty
/**
* See [KotlinPluginLifecycle]:
@@ -54,7 +53,13 @@ internal interface CompletableFuture<T> : Future<T> {
internal fun CompletableFuture<Unit>.complete() = complete(Unit)
internal inline fun <Receiver, reified T> lazyFuture(
/**
* Extend a given [Receiver] with data produced by [block]:
* This uses the [HasMutableExtras] infrastructure to store/share the produced future entity to the given [Receiver]
* Note: The [block] will be lazily launched on first access to this extension!
* @param name: The name of the extras key being used to store the future (see [extrasLazyProperty])
*/
internal inline fun <Receiver, reified T> futureExtension(
name: String? = null, noinline block: suspend Receiver.() -> T
): ExtrasLazyProperty<Receiver, Future<T>> where Receiver : HasMutableExtras, Receiver : HasProject {
return extrasLazyProperty<Receiver, Future<T>>(name) {
@@ -64,6 +69,16 @@ internal inline fun <Receiver, reified T> lazyFuture(
internal fun <T> Project.future(block: suspend Project.() -> T): Future<T> = kotlinPluginLifecycle.future { block() }
/**
* Shortcut for
* ```kotlin
* lazy { future { block() } }
* ```
*
* basically creating a future, which is launched lazily
*/
internal fun <T> Project.lazyFuture(block: suspend Project.() -> T): Lazy<Future<T>> = lazy { future(block) }
internal fun <T> KotlinPluginLifecycle.future(block: suspend () -> T): Future<T> {
return FutureImpl<T>(CompletableDeferred()).also { future ->
launch { future.completeWith(runCatching { block() }) }