From 775e08e41d8570befa9d5c359f26895ac95fe4fc Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Tue, 28 Mar 2023 14:36:51 +0200 Subject: [PATCH] [Gradle] Property.awaitFinalValue: Support generic properties ... even when not declared as lifecycle aware ^KT-56654 Verification Pending --- .../kotlin/gradle/plugin/KotlinPluginLifecycle.kt | 12 ++++++++++-- .../gradle/unitTests/LifecycleAwarePropertyTest.kt | 9 +++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt index 81bb92f6ee5..49867f331c6 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt @@ -171,11 +171,19 @@ internal suspend fun Property.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 Property.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 } /** diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt index 668e476df09..ce3a6c52a34 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/LifecycleAwarePropertyTest.kt @@ -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() assertFalse(property.isKotlinPluginLifecycleAware()) - assertFailsWith { property.awaitFinalValue() } + assertEquals(KotlinPluginLifecycle.Stage.EvaluateBuildscript, currentKotlinPluginLifecycle().stage) + assertNull(property.awaitFinalValue()) + assertEquals(KotlinPluginLifecycle.Stage.FinaliseDsl, currentKotlinPluginLifecycle().stage) + assertFails { property.set("x") } } @Test