[Gradle] KotlinPluginLifecycle: Remove unnecessary before stages

^KT-34662 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-03-16 17:41:31 +01:00
committed by Space Team
parent 4a896a2579
commit e76119b5eb
5 changed files with 50 additions and 78 deletions
@@ -241,18 +241,8 @@ internal interface KotlinPluginLifecycle {
* Configure Phase of Gradle: No .afterEvaluate {} listeners have been called yet, * Configure Phase of Gradle: No .afterEvaluate {} listeners have been called yet,
* the buildscript is still evaluated! * the buildscript is still evaluated!
*/ */
Configure, EvaluateBuildscript,
AfterEvaluateBuildscript,
/**
* The buildscript has been evaluated, first .afterEvaluate {} listeners get executed.
* This can include .afterEvaluates from other plugins as well as other users!
*/
AfterEvaluate,
/**
* Before [FinaliseDsl]
*/
BeforeFinaliseDsl,
/** /**
* Last changes are allowed to be done to the DSL. * Last changes are allowed to be done to the DSL.
@@ -260,42 +250,22 @@ internal interface KotlinPluginLifecycle {
* disallow further changes * disallow further changes
*/ */
FinaliseDsl, FinaliseDsl,
/**
* After [FinaliseDsl]
*/
AfterFinaliseDsl, AfterFinaliseDsl,
/**
* Before [FinaliseRefinesEdges]
*/
BeforeFinaliseRefinesEdges,
/** /**
* All refines edges ([KotlinSourceSet.dependsOn]) have to be finalised here. * All refines edges ([KotlinSourceSet.dependsOn]) have to be finalised here.
* Adding edges after this stage is forbidden and will throw an exception! * Adding edges after this stage is forbidden and will throw an exception!
*/ */
FinaliseRefinesEdges, FinaliseRefinesEdges,
/**
* After [FinaliseRefinesEdges]
*/
AfterFinaliseRefinesEdges, AfterFinaliseRefinesEdges,
/**
* Before [FinaliseCompilations]
*/
BeforeFinaliseCompilations,
/** /**
* [KotlinCompilation] instances have to finalised: Creating compilations after this stage is forbidden. * [KotlinCompilation] instances have to finalised: Creating compilations after this stage is forbidden.
* Values and configuration of compilations also shall be finalised already * Values and configuration of compilations also shall be finalised already
*/ */
FinaliseCompilations, FinaliseCompilations,
/**
* After [FinaliseCompilations]
*/
AfterFinaliseCompilations, AfterFinaliseCompilations,
/** /**
@@ -305,6 +275,8 @@ internal interface KotlinPluginLifecycle {
val previousOrFirst: Stage get() = previousOrNull ?: values.first() val previousOrFirst: Stage get() = previousOrNull ?: values.first()
val previousOrNull: Stage? get() = values.getOrNull(ordinal - 1) val previousOrNull: Stage? get() = values.getOrNull(ordinal - 1)
val previousOrThrow: Stage
get() = previousOrNull ?: throw IllegalArgumentException("'$this' does not have a next ${Stage::class.simpleName}")
val nextOrNull: Stage? get() = values.getOrNull(ordinal + 1) val nextOrNull: Stage? get() = values.getOrNull(ordinal + 1)
val nextOrLast: Stage get() = nextOrNull ?: values.last() val nextOrLast: Stage get() = nextOrNull ?: values.last()
val nextOrThrow: Stage val nextOrThrow: Stage
@@ -447,7 +419,7 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
enqueuedActions.getValue(stage).addLast(action) enqueuedActions.getValue(stage).addLast(action)
if (stage == Stage.Configure && isStarted.get()) { if (stage == Stage.EvaluateBuildscript && isStarted.get()) {
loopIfNecessary() loopIfNecessary()
} }
} }
@@ -183,7 +183,7 @@ class KotlinMetadataTargetConfigurator :
private fun createMetadataCompilationsForCommonSourceSets( private fun createMetadataCompilationsForCommonSourceSets(
target: KotlinMetadataTarget, target: KotlinMetadataTarget,
allMetadataJar: TaskProvider<out Jar> allMetadataJar: TaskProvider<out Jar>
) = target.project.launchInStage(KotlinPluginLifecycle.Stage.BeforeFinaliseCompilations) { ) = target.project.launchInStage(KotlinPluginLifecycle.Stage.AfterFinaliseDsl) {
withRestrictedStages(KotlinPluginLifecycle.Stage.upTo(KotlinPluginLifecycle.Stage.FinaliseCompilations)) { withRestrictedStages(KotlinPluginLifecycle.Stage.upTo(KotlinPluginLifecycle.Stage.FinaliseCompilations)) {
// Do this after all targets are configured by the user build script // Do this after all targets are configured by the user build script
@@ -44,7 +44,7 @@ class KotlinAndroidTargetVariantTypeDslImplTest {
@Test @Test
fun `test - module - cannot be set after FinaliseDsl`() = project.runLifecycleAwareTest { fun `test - module - cannot be set after FinaliseDsl`() = project.runLifecycleAwareTest {
launchInStage(KotlinPluginLifecycle.Stage.BeforeFinaliseDsl) { launchInStage(KotlinPluginLifecycle.Stage.FinaliseDsl.previousOrThrow) {
dsl.targetHierarchy.module.set(KotlinTargetHierarchy.ModuleName("x")) dsl.targetHierarchy.module.set(KotlinTargetHierarchy.ModuleName("x"))
} }
@@ -28,7 +28,7 @@ class KotlinPluginLifecycleTest {
@Test @Test
fun `test - configure phase is executed right away`() { fun `test - configure phase is executed right away`() {
val invocations = AtomicInteger(0) val invocations = AtomicInteger(0)
lifecycle.enqueue(Configure) { lifecycle.enqueue(EvaluateBuildscript) {
invocations.incrementAndGet() invocations.incrementAndGet()
} }
assertEquals(1, invocations.get(), "Expected one invocation") assertEquals(1, invocations.get(), "Expected one invocation")
@@ -37,8 +37,8 @@ class KotlinPluginLifecycleTest {
@Test @Test
fun `test - launchInState - Configure`() { fun `test - launchInState - Configure`() {
val invocations = AtomicInteger(0) val invocations = AtomicInteger(0)
project.launchInStage(Configure) { project.launchInStage(EvaluateBuildscript) {
assertEquals(Configure, stage) assertEquals(EvaluateBuildscript, stage)
assertEquals(1, invocations.incrementAndGet()) assertEquals(1, invocations.incrementAndGet())
} }
assertEquals(1, invocations.get()) assertEquals(1, invocations.get())
@@ -50,20 +50,20 @@ class KotlinPluginLifecycleTest {
val nestedAInvocations = AtomicInteger(0) val nestedAInvocations = AtomicInteger(0)
val nestedBInvocations = AtomicInteger(0) val nestedBInvocations = AtomicInteger(0)
val nestedCInvocations = AtomicInteger(0) val nestedCInvocations = AtomicInteger(0)
lifecycle.enqueue(Configure) { lifecycle.enqueue(EvaluateBuildscript) {
assertEquals(1, outerInvocations.incrementAndGet()) assertEquals(1, outerInvocations.incrementAndGet())
lifecycle.enqueue(Configure) nestedA@{ lifecycle.enqueue(EvaluateBuildscript) nestedA@{
assertEquals(0, nestedBInvocations.get(), "Expected nestedA to be executed before nestedB") assertEquals(0, nestedBInvocations.get(), "Expected nestedA to be executed before nestedB")
assertEquals(1, nestedAInvocations.incrementAndGet()) assertEquals(1, nestedAInvocations.incrementAndGet())
lifecycle.enqueue(Configure) nestedC@{ lifecycle.enqueue(EvaluateBuildscript) nestedC@{
assertEquals(1, nestedBInvocations.get(), "Expected nestedB to be executed before nestedC") assertEquals(1, nestedBInvocations.get(), "Expected nestedB to be executed before nestedC")
assertEquals(1, nestedCInvocations.incrementAndGet()) assertEquals(1, nestedCInvocations.incrementAndGet())
} }
} }
lifecycle.enqueue(Configure) nestedB@{ lifecycle.enqueue(EvaluateBuildscript) nestedB@{
assertEquals(1, nestedAInvocations.get(), "Expected nestedA to be executed before nestedB") assertEquals(1, nestedAInvocations.get(), "Expected nestedA to be executed before nestedB")
assertEquals(0, nestedCInvocations.get(), "Expected nestedB to be executed before nestedC") assertEquals(0, nestedCInvocations.get(), "Expected nestedB to be executed before nestedC")
assertEquals(1, nestedBInvocations.incrementAndGet()) assertEquals(1, nestedBInvocations.incrementAndGet())
@@ -110,13 +110,13 @@ class KotlinPluginLifecycleTest {
assertEquals(1, action3Invocations.incrementAndGet()) assertEquals(1, action3Invocations.incrementAndGet())
} }
lifecycle.enqueue(AfterEvaluate) action1@{ lifecycle.enqueue(AfterEvaluateBuildscript) action1@{
assertEquals(0, action2Invocations.get(), "Expected action1 to be executed before action2") assertEquals(0, action2Invocations.get(), "Expected action1 to be executed before action2")
assertEquals(0, action3Invocations.get(), "Expected action1 to be executed before action3") assertEquals(0, action3Invocations.get(), "Expected action1 to be executed before action3")
assertEquals(1, action1Invocations.incrementAndGet()) assertEquals(1, action1Invocations.incrementAndGet())
} }
lifecycle.enqueue(AfterEvaluate) action2@{ lifecycle.enqueue(AfterEvaluateBuildscript) action2@{
assertEquals(1, action1Invocations.get(), "Expected action1 to be executed before action2") assertEquals(1, action1Invocations.get(), "Expected action1 to be executed before action2")
assertEquals(0, action3Invocations.get(), "Expected action2 to be executed before action3") assertEquals(0, action3Invocations.get(), "Expected action2 to be executed before action3")
assertEquals(1, action2Invocations.incrementAndGet()) assertEquals(1, action2Invocations.incrementAndGet())
@@ -139,20 +139,20 @@ class KotlinPluginLifecycleTest {
val nestedAInvocations = AtomicInteger(0) val nestedAInvocations = AtomicInteger(0)
val nestedBInvocations = AtomicInteger(0) val nestedBInvocations = AtomicInteger(0)
val nestedCInvocations = AtomicInteger(0) val nestedCInvocations = AtomicInteger(0)
lifecycle.enqueue(AfterEvaluate) { lifecycle.enqueue(AfterEvaluateBuildscript) {
assertEquals(1, outerInvocations.incrementAndGet()) assertEquals(1, outerInvocations.incrementAndGet())
lifecycle.enqueue(AfterEvaluate) nestedA@{ lifecycle.enqueue(AfterEvaluateBuildscript) nestedA@{
assertEquals(0, nestedBInvocations.get(), "Expected nestedA to be executed before nestedB") assertEquals(0, nestedBInvocations.get(), "Expected nestedA to be executed before nestedB")
assertEquals(1, nestedAInvocations.incrementAndGet()) assertEquals(1, nestedAInvocations.incrementAndGet())
lifecycle.enqueue(AfterEvaluate) nestedC@{ lifecycle.enqueue(AfterEvaluateBuildscript) nestedC@{
assertEquals(1, nestedBInvocations.get(), "Expected nestedB to be executed before nestedC") assertEquals(1, nestedBInvocations.get(), "Expected nestedB to be executed before nestedC")
assertEquals(1, nestedCInvocations.incrementAndGet()) assertEquals(1, nestedCInvocations.incrementAndGet())
} }
} }
lifecycle.enqueue(AfterEvaluate) nestedB@{ lifecycle.enqueue(AfterEvaluateBuildscript) nestedB@{
assertEquals(1, nestedAInvocations.get(), "Expected nestedA to be executed before nestedB") assertEquals(1, nestedAInvocations.get(), "Expected nestedA to be executed before nestedB")
assertEquals(0, nestedCInvocations.get(), "Expected nestedB to be executed before nestedC") assertEquals(0, nestedCInvocations.get(), "Expected nestedB to be executed before nestedC")
assertEquals(1, nestedBInvocations.incrementAndGet()) assertEquals(1, nestedBInvocations.incrementAndGet())
@@ -177,7 +177,7 @@ class KotlinPluginLifecycleTest {
val executed = AtomicBoolean(false) val executed = AtomicBoolean(false)
lifecycle.enqueue(ReadyForExecution) { lifecycle.enqueue(ReadyForExecution) {
assertFailsWith<IllegalLifecycleException> { assertFailsWith<IllegalLifecycleException> {
lifecycle.enqueue(AfterEvaluate) { fail("This code shall not be executed!") } lifecycle.enqueue(AfterEvaluateBuildscript) { fail("This code shall not be executed!") }
} }
assertFalse(executed.getAndSet(true)) assertFalse(executed.getAndSet(true))
} }
@@ -202,14 +202,14 @@ class KotlinPluginLifecycleTest {
val action2Invocations = AtomicInteger(0) val action2Invocations = AtomicInteger(0)
val action3Invocations = AtomicInteger(0) val action3Invocations = AtomicInteger(0)
lifecycle.enqueue(Configure) action1@{ lifecycle.enqueue(EvaluateBuildscript) action1@{
assertEquals(0, action2Invocations.get()) assertEquals(0, action2Invocations.get())
assertEquals(0, action3Invocations.get()) assertEquals(0, action3Invocations.get())
assertEquals(1, action1Invocations.incrementAndGet()) assertEquals(1, action1Invocations.incrementAndGet())
} }
lifecycle.enqueue(Configure) action2@{ lifecycle.enqueue(EvaluateBuildscript) action2@{
lifecycle.enqueue(Configure) action3@{ lifecycle.enqueue(EvaluateBuildscript) action3@{
assertEquals(1, action1Invocations.get()) assertEquals(1, action1Invocations.get())
assertEquals(1, action2Invocations.get()) assertEquals(1, action2Invocations.get())
assertEquals(1, action3Invocations.incrementAndGet()) assertEquals(1, action3Invocations.incrementAndGet())
@@ -259,9 +259,9 @@ class KotlinPluginLifecycleTest {
val executionPointB = AtomicBoolean(false) val executionPointB = AtomicBoolean(false)
lifecycle.launch action1@{ lifecycle.launch action1@{
assertFalse(executionPointA.getAndSet(true)) assertFalse(executionPointA.getAndSet(true))
assertEquals(Configure, stage) assertEquals(EvaluateBuildscript, stage)
await(AfterEvaluate) await(AfterEvaluateBuildscript)
assertEquals(AfterEvaluate, stage) assertEquals(AfterEvaluateBuildscript, stage)
assertFalse(executionPointB.getAndSet(true)) assertFalse(executionPointB.getAndSet(true))
} }
@@ -276,9 +276,9 @@ class KotlinPluginLifecycleTest {
fun `test - launch - await - launch`() { fun `test - launch - await - launch`() {
val executedInnerAction = AtomicBoolean(false) val executedInnerAction = AtomicBoolean(false)
lifecycle.launch { lifecycle.launch {
await(AfterEvaluate) await(AfterEvaluateBuildscript)
launch { launch {
assertEquals(AfterEvaluate, stage) assertEquals(AfterEvaluateBuildscript, stage)
await(FinaliseRefinesEdges) await(FinaliseRefinesEdges)
assertEquals(FinaliseRefinesEdges, stage) assertEquals(FinaliseRefinesEdges, stage)
assertFalse(executedInnerAction.getAndSet(true)) assertFalse(executedInnerAction.getAndSet(true))
@@ -303,7 +303,7 @@ class KotlinPluginLifecycleTest {
fun `test - launch - await - exception`() { fun `test - launch - await - exception`() {
val testException = object : Throwable() {} val testException = object : Throwable() {}
lifecycle.launch { lifecycle.launch {
await(AfterEvaluate) await(AfterEvaluateBuildscript)
launch { launch {
throw testException throw testException
} }
@@ -318,7 +318,7 @@ class KotlinPluginLifecycleTest {
@Test @Test
fun `test - require current stage`() = project.runLifecycleAwareTest { fun `test - require current stage`() = project.runLifecycleAwareTest {
launchInStage(AfterEvaluate) { launchInStage(AfterEvaluateBuildscript) {
requireCurrentStage { } // OK requireCurrentStage { } // OK
requireCurrentStage { requireCurrentStage {
@@ -330,10 +330,10 @@ class KotlinPluginLifecycleTest {
@Test @Test
fun `test - launch in required stage`() = project.runLifecycleAwareTest { fun `test - launch in required stage`() = project.runLifecycleAwareTest {
launchInRequiredStage(AfterEvaluate) { launchInRequiredStage(AfterEvaluateBuildscript) {
assertEquals(AfterEvaluate, stage) assertEquals(AfterEvaluateBuildscript, stage)
await(AfterEvaluate) await(AfterEvaluateBuildscript)
assertEquals(AfterEvaluate, stage) assertEquals(AfterEvaluateBuildscript, stage)
assertFailsWith<IllegalLifecycleException> { await(ReadyForExecution) } assertFailsWith<IllegalLifecycleException> { await(ReadyForExecution) }
} }
} }
@@ -342,13 +342,13 @@ class KotlinPluginLifecycleTest {
fun `test - withRestrictedStages`() = project.runLifecycleAwareTest { fun `test - withRestrictedStages`() = project.runLifecycleAwareTest {
launch { launch {
withRestrictedStages(Stage.upTo(FinaliseRefinesEdges)) { withRestrictedStages(Stage.upTo(FinaliseRefinesEdges)) {
assertEquals(Configure, stage) assertEquals(EvaluateBuildscript, stage)
await(AfterEvaluate) await(AfterEvaluateBuildscript)
assertEquals(AfterEvaluate, stage) assertEquals(AfterEvaluateBuildscript, stage)
await(BeforeFinaliseRefinesEdges) await(FinaliseDsl)
assertEquals(BeforeFinaliseRefinesEdges, stage) assertEquals(FinaliseDsl, stage)
await(FinaliseRefinesEdges) await(FinaliseRefinesEdges)
assertEquals(FinaliseRefinesEdges, stage) assertEquals(FinaliseRefinesEdges, stage)
@@ -366,12 +366,12 @@ class KotlinPluginLifecycleTest {
afterEvaluate { afterEvaluate {
launch { launch {
assertEquals(AfterEvaluate.nextOrThrow, currentKotlinPluginLifecycle().stage) assertEquals(AfterEvaluateBuildscript.nextOrThrow, currentKotlinPluginLifecycle().stage)
assertEquals(1, actionInvocations.incrementAndGet()) assertEquals(1, actionInvocations.incrementAndGet())
} }
} }
await(AfterEvaluate.nextOrThrow.nextOrThrow) await(AfterEvaluateBuildscript.nextOrThrow.nextOrThrow)
assertEquals(1, actionInvocations.get()) assertEquals(1, actionInvocations.get())
await(Stage.values.last()) await(Stage.values.last())
} }
@@ -411,13 +411,13 @@ class KotlinPluginLifecycleTest {
@Test @Test
fun `test - Stage - range utils`() { fun `test - Stage - range utils`() {
assertEquals(setOf(BeforeFinaliseDsl, FinaliseDsl, AfterFinaliseDsl), BeforeFinaliseDsl..AfterFinaliseDsl) assertEquals(setOf(AfterEvaluateBuildscript, FinaliseDsl, AfterFinaliseDsl), AfterEvaluateBuildscript..AfterFinaliseDsl)
assertEquals(emptySet(), AfterFinaliseDsl..BeforeFinaliseDsl) assertEquals(emptySet(), AfterFinaliseDsl..AfterFinaliseDsl.previousOrThrow)
assertEquals(setOf(BeforeFinaliseDsl), BeforeFinaliseDsl..BeforeFinaliseDsl) assertEquals(setOf(FinaliseDsl), FinaliseDsl..FinaliseDsl)
assertTrue(FinaliseDsl in Stage.upTo(FinaliseDsl)) assertTrue(FinaliseDsl in Stage.upTo(FinaliseDsl))
assertTrue(Stage.values.first() in Stage.upTo(FinaliseDsl)) assertTrue(Stage.values.first() in Stage.upTo(FinaliseDsl))
assertTrue(BeforeFinaliseDsl in Stage.upTo(FinaliseDsl)) assertTrue(FinaliseDsl.previousOrThrow in Stage.upTo(FinaliseDsl))
assertTrue(FinaliseDsl.nextOrThrow !in Stage.upTo(FinaliseDsl)) assertTrue(FinaliseDsl.nextOrThrow !in Stage.upTo(FinaliseDsl))
assertTrue(FinaliseDsl !in Stage.until(FinaliseDsl)) assertTrue(FinaliseDsl !in Stage.until(FinaliseDsl))
@@ -30,7 +30,7 @@ class LifecycleAwarePropertyTest {
val property by project.newKotlinPluginLifecycleAwareProperty<Int>(AfterFinaliseRefinesEdges) val property by project.newKotlinPluginLifecycleAwareProperty<Int>(AfterFinaliseRefinesEdges)
assertTrue(property.isKotlinPluginLifecycleAware()) assertTrue(property.isKotlinPluginLifecycleAware())
launchInStage(BeforeFinaliseRefinesEdges) { launchInStage(FinaliseRefinesEdges.previousOrThrow) {
property.set(1) property.set(1)
} }
@@ -39,7 +39,7 @@ class LifecycleAwarePropertyTest {
property.set(2) property.set(2)
} }
assertEquals(Configure, currentKotlinPluginLifecycle().stage) assertEquals(EvaluateBuildscript, currentKotlinPluginLifecycle().stage)
assertEquals(2, property.awaitFinalValue()) assertEquals(2, property.awaitFinalValue())
assertEquals(AfterFinaliseRefinesEdges, currentKotlinPluginLifecycle().stage) assertEquals(AfterFinaliseRefinesEdges, currentKotlinPluginLifecycle().stage)
} }
@@ -53,10 +53,10 @@ class LifecycleAwarePropertyTest {
@Test @Test
fun `test - changing value after finalized`() = project.runLifecycleAwareTest { fun `test - changing value after finalized`() = project.runLifecycleAwareTest {
val property by project.newKotlinPluginLifecycleAwareProperty<Int>(AfterEvaluate) val property by project.newKotlinPluginLifecycleAwareProperty<Int>(AfterEvaluateBuildscript)
property.set(1) property.set(1)
launchInStage(AfterEvaluate) { launchInStage(AfterEvaluateBuildscript) {
assertFailsWith<IllegalStateException> { property.set(2) } assertFailsWith<IllegalStateException> { property.set(2) }
} }
} }