[Gradle] LifecycleAwareProperty: Allow lazy creation even in 'finaliseIn' or later

^KT-58209 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-04-24 17:46:47 +02:00
committed by Space Team
parent 16369bc008
commit bc1abb2e6a
2 changed files with 39 additions and 6 deletions
@@ -137,6 +137,16 @@ internal suspend fun Stage.await() {
currentKotlinPluginLifecycle().await(this)
}
/**
* See [newProperty]
*/
internal inline fun <reified T : Any> Project.newKotlinPluginLifecycleAwareProperty(
finaliseIn: Stage = Stage.FinaliseDsl, initialValue: T? = null
): LifecycleAwareProperty<T> {
return kotlinPluginLifecycle.newProperty(T::class.java, finaliseIn, initialValue)
}
/**
* See [LifecycleAwareProperty]
* Will create a new [LifecycleAwareProperty] which is going to finalise its value in stage [finaliseIn]
@@ -152,10 +162,10 @@ internal suspend fun Stage.await() {
* }
* ```
*/
internal inline fun <reified T : Any> Project.newKotlinPluginLifecycleAwareProperty(
internal inline fun <reified T : Any> KotlinPluginLifecycle.newProperty(
finaliseIn: Stage = Stage.FinaliseDsl, initialValue: T? = null
): LifecycleAwareProperty<T> {
return kotlinPluginLifecycle.newLifecycleAwareProperty(T::class.java, finaliseIn, initialValue)
return newProperty(T::class.java, finaliseIn, initialValue)
}
/**
@@ -323,7 +333,7 @@ internal interface KotlinPluginLifecycle {
suspend fun await(stage: Stage)
fun <T : Any> newLifecycleAwareProperty(
fun <T : Any> newProperty(
type: Class<T>, finaliseIn: Stage, initialValue: T?
): LifecycleAwareProperty<T>
@@ -419,7 +429,7 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
override fun enqueue(stage: Stage, action: KotlinPluginLifecycle.() -> Unit) {
if (stage < this.stage) {
throw IllegalLifecycleException("Cannot enqueue Action for stage '${this.stage}' in current stage '${this.stage}'")
throw IllegalLifecycleException("Cannot enqueue Action for stage '${stage}' in current stage '${this.stage}'")
}
/*
@@ -463,10 +473,11 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
}
}
override fun <T : Any> newLifecycleAwareProperty(type: Class<T>, finaliseIn: Stage, initialValue: T?): LifecycleAwareProperty<T> {
override fun <T : Any> newProperty(type: Class<T>, finaliseIn: Stage, initialValue: T?): LifecycleAwareProperty<T> {
val property = project.objects.property(type)
if (initialValue != null) property.set(initialValue)
enqueue(finaliseIn) { property.finalizeValue() }
if (finaliseIn <= stage) property.finalizeValue()
else enqueue(finaliseIn) { property.finalizeValue() }
val lifecycleAwareProperty = LifecycleAwarePropertyImpl(finaliseIn, property)
properties[property] = WeakReference(lifecycleAwareProperty)
return lifecycleAwareProperty
@@ -61,4 +61,26 @@ class LifecycleAwarePropertyTest {
assertFailsWith<IllegalStateException> { property.set(2) }
}
}
@Test
fun `test - creating a property - after finaliseIn stage already passed`() = project.runLifecycleAwareTest {
launchInStage(KotlinPluginLifecycle.Stage.last) {
val property by project.newKotlinPluginLifecycleAwareProperty<String>(Companion.first)
/* Property was finalised immediately */
assertFailsWith<IllegalStateException> { property.set("Expected to fail") }
assertNull(property.orNull)
}
}
@Test
fun `test - creating a property - in finaliseIn stage`() = project.runLifecycleAwareTest {
launchInStage(KotlinPluginLifecycle.Stage.FinaliseDsl) {
val property by project.newKotlinPluginLifecycleAwareProperty<String>(FinaliseDsl)
/* Property was finalised immediately */
assertFailsWith<IllegalStateException> { property.set("Expected to fail") }
assertNull(property.orNull)
}
}
}