[Gradle] Property.awaitFinalValue: Support generic properties

... even when not declared as lifecycle aware

^KT-56654 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-03-28 14:36:51 +02:00
committed by Space Team
parent 2043c9db8e
commit 775e08e41d
2 changed files with 15 additions and 6 deletions
@@ -171,11 +171,19 @@ internal suspend fun <T : Any> Property<T>.findKotlinPluginLifecycleAwarePropert
* Will suspend until the property finalises its value and therefore a final value can returned.
* Note: This only works on properties that are [isKotlinPluginLifecycleAware]
* (e.g. by being created using [newKotlinPluginLifecycleAwareProperty]).
*
* If a property was not created using 'newKotlinPluginLifecycleAwareProperty' then the execution
* will suspend until 'FinaliseDsl' and calls [Property.finalizeValue] before returnign the actual value
*/
internal suspend fun <T : Any> Property<T>.awaitFinalValue(): T? {
val lifecycleAwareProperty = findKotlinPluginLifecycleAwareProperty()
?: throw IllegalArgumentException("Property is not lifecycle aware")
return lifecycleAwareProperty.awaitFinalValue()
if (lifecycleAwareProperty != null) {
return lifecycleAwareProperty.awaitFinalValue()
}
await(Stage.FinaliseDsl)
finalizeValue()
return orNull
}
/**
@@ -18,9 +18,7 @@ import org.jetbrains.kotlin.gradle.util.runLifecycleAwareTest
import org.jetbrains.kotlin.gradle.utils.newProperty
import org.junit.Assert.assertFalse
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
import kotlin.test.*
class LifecycleAwarePropertyTest {
private val project = buildProjectWithMPP()
@@ -48,7 +46,10 @@ class LifecycleAwarePropertyTest {
fun `test - awaitFinalValue - on non lifecycle aware property`() = project.runLifecycleAwareTest {
val property = project.newProperty<String>()
assertFalse(property.isKotlinPluginLifecycleAware())
assertFailsWith<IllegalArgumentException> { property.awaitFinalValue() }
assertEquals(KotlinPluginLifecycle.Stage.EvaluateBuildscript, currentKotlinPluginLifecycle().stage)
assertNull(property.awaitFinalValue())
assertEquals(KotlinPluginLifecycle.Stage.FinaliseDsl, currentKotlinPluginLifecycle().stage)
assertFails { property.set("x") }
}
@Test