[Gradle][Minor] KotlinPluginLifecycle: Remove enqueuedStages field

KT-34662
This commit is contained in:
Sebastian Sellmair
2023-03-09 13:22:04 +01:00
committed by Space Team
parent 656ce4aead
commit cb97beb3d6
@@ -117,6 +117,12 @@ internal interface KotlinPluginLifecycle {
Finalised; Finalised;
val previousOrFirst: Stage get() = previousOrNull ?: values.first()
val previousOrNull: Stage? get() = values.getOrNull(ordinal - 1)
val nextOrNull: Stage? get() = values.getOrNull(ordinal + 1)
val nextOrLast: Stage get() = nextOrNull ?: values.last()
val nextOrThrow: Stage get() = values[ordinal + 1]
operator fun rangeTo(other: Stage): Set<Stage> { operator fun rangeTo(other: Stage): Set<Stage> {
return values.subList(this.ordinal, other.ordinal + 1).toSet() return values.subList(this.ordinal, other.ordinal + 1).toSet()
} }
@@ -124,6 +130,9 @@ internal interface KotlinPluginLifecycle {
companion object { companion object {
val values = values().toList() val values = values().toList()
fun upTo(stage: Stage): Set<Stage> = values.first()..stage fun upTo(stage: Stage): Set<Stage> = values.first()..stage
fun until(stage: Stage): Set<Stage> {
return upTo(stage.previousOrNull ?: return emptySet())
}
} }
} }
@@ -157,11 +166,10 @@ Implementation
*/ */
private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinPluginLifecycle { private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinPluginLifecycle {
private val enqueuedStages: ArrayDeque<Stage> = ArrayDeque(Stage.values)
private val enqueuedActions: Map<Stage, ArrayDeque<KotlinPluginLifecycle.() -> Unit>> = private val enqueuedActions: Map<Stage, ArrayDeque<KotlinPluginLifecycle.() -> Unit>> =
Stage.values().associateWith { ArrayDeque() } Stage.values().associateWith { ArrayDeque() }
private var configureLoopRunning = AtomicBoolean(false) private var loopRunning = AtomicBoolean(false)
private var isStarted = AtomicBoolean(false) private var isStarted = AtomicBoolean(false)
private var isFinished = AtomicBoolean(false) private var isFinished = AtomicBoolean(false)
@@ -177,18 +185,17 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
} }
project.whenEvaluated { project.whenEvaluated {
executeStage(project, enqueuedStages.removeFirst()) assert(enqueuedActions.getValue(stage).isEmpty()) { "Expected empty queue from '$stage'" }
executeStage(project, stage.nextOrThrow)
} }
} }
private fun executeStage(project: Project, stage: Stage) { private fun executeStage(project: Project, stage: Stage) {
this.stage = stage this.stage = stage
val queue = enqueuedActions.getValue(stage)
do { loopIfNecessary()
val action = queue.removeFirstOrNull()
action?.invoke(this) val nextStage = stage.nextOrNull ?: run {
} while (action != null)
val nextStage = enqueuedStages.removeFirstOrNull() ?: run {
isFinished.set(true) isFinished.set(true)
return return
} }
@@ -198,21 +205,20 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
} }
} }
private fun startConfigureLoopIfNecessary() { private fun loopIfNecessary() {
check(stage == Stage.Configure) { "Cannot start 'configure loop' on stage '$stage'" } if (loopRunning.getAndSet(true)) return
if (configureLoopRunning.getAndSet(true)) return
try { try {
val queue = enqueuedActions.getValue(Stage.Configure) val queue = enqueuedActions.getValue(stage)
do { do {
val action = queue.removeFirstOrNull() val action = queue.removeFirstOrNull()
action?.invoke(this) action?.invoke(this)
} while (action != null) } while (action != null)
} finally { } finally {
configureLoopRunning.set(false) loopRunning.set(false)
} }
} }
override var stage: Stage = enqueuedStages.removeFirst() override var stage: Stage = Stage.values.first()
override fun enqueue(stage: Stage, action: KotlinPluginLifecycle.() -> Unit) { override fun enqueue(stage: Stage, action: KotlinPluginLifecycle.() -> Unit) {
if (stage < this.stage) { if (stage < this.stage) {
@@ -222,7 +228,7 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
enqueuedActions.getValue(stage).addLast(action) enqueuedActions.getValue(stage).addLast(action)
if (stage == Stage.Configure) { if (stage == Stage.Configure) {
startConfigureLoopIfNecessary() loopIfNecessary()
} }
} }