[Gradle] KotlinPluginLifecycle: Implement .toString for better diagnostics

^KT-59446 In Progress
This commit is contained in:
Sebastian Sellmair
2023-06-20 09:08:10 +02:00
committed by Space Team
parent 46ea908daf
commit d7facce71f
3 changed files with 28 additions and 10 deletions
@@ -296,6 +296,10 @@ internal interface KotlinPluginLifecycle {
val stage: Stage
val isStarted: Boolean
val isFinished: Boolean
fun launch(block: suspend KotlinPluginLifecycle.() -> Unit)
suspend fun await(stage: Stage)
@@ -24,15 +24,21 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
KotlinPluginLifecycle.Stage.values().associateWith { ArrayDeque() }
private val loopRunning = AtomicBoolean(false)
private val isStarted = AtomicBoolean(false)
private val isStartedImpl = AtomicBoolean(false)
private val isFinishedSuccessfully = AtomicBoolean(false)
private val isFinishedWithFailures = AtomicBoolean(false)
override var stage: KotlinPluginLifecycle.Stage = KotlinPluginLifecycle.Stage.values.first()
override val isStarted: Boolean
get() = isStartedImpl.get()
override val isFinished: Boolean
get() = isFinishedSuccessfully.get() || isFinishedWithFailures.get()
fun start() {
check(!isStarted.getAndSet(true)) {
check(!isStartedImpl.getAndSet(true)) {
"${KotlinPluginLifecycle::class.java.name} already started"
}
@@ -103,13 +109,13 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
private fun finishWithFailures(failures: List<Throwable>) {
assert(failures.isNotEmpty())
assert(isStarted.get())
assert(isStartedImpl.get())
assert(!isFinishedWithFailures.getAndSet(true))
configurationResult.complete(ProjectConfigurationResult.Failure(failures))
}
private fun finishSuccessfully() {
assert(isStarted.get())
assert(isStartedImpl.get())
assert(!isFinishedSuccessfully.getAndSet(true))
configurationResult.complete(ProjectConfigurationResult.Success)
}
@@ -140,7 +146,7 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
enqueuedActions.getValue(stage).addLast(action)
if (stage == KotlinPluginLifecycle.Stage.EvaluateBuildscript && isStarted.get()) {
if (stage == KotlinPluginLifecycle.Stage.EvaluateBuildscript && isStartedImpl.get()) {
loopIfNecessary()
}
}
@@ -168,6 +174,14 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
}
}
}
override fun toString(): String = buildString {
append("Kotlin Plugin Lifecycle: (${project.displayName})")
if (!isStarted) append(" *not started*")
else append(" stage '$stage'")
if (isFinishedSuccessfully.get()) append(" *finished successfully*")
if (isFinishedWithFailures.get()) append(" *finished with failures*")
}
}
private class KotlinPluginLifecycleCoroutineContextElement(
@@ -64,7 +64,7 @@ internal fun CompletableFuture<Unit>.complete() = complete(Unit)
* @param name: The name of the extras key being used to store the future (see [extrasLazyProperty])
*/
internal inline fun <Receiver, reified T> futureExtension(
name: String? = null, noinline block: suspend Receiver.() -> T
name: String? = null, noinline block: suspend Receiver.() -> T,
): ExtrasLazyProperty<Receiver, Future<T>> where Receiver : HasMutableExtras, Receiver : HasProject {
return extrasLazyProperty<Receiver, Future<T>>(name) {
project.future { block() }
@@ -97,7 +97,7 @@ internal fun <T> CompletableFuture(): CompletableFuture<T> {
private class FutureImpl<T>(
private val deferred: Completable<T> = Completable(),
private val lifecycle: KotlinPluginLifecycle? = null
private val lifecycle: KotlinPluginLifecycle? = null,
) : CompletableFuture<T>, Serializable {
fun completeWith(result: Result<T>) = deferred.completeWith(result)
@@ -111,7 +111,7 @@ private class FutureImpl<T>(
override fun getOrThrow(): T {
return if (deferred.isCompleted) deferred.getCompleted() else throw IllegalLifecycleException(
"Future was not completed yet" + if (lifecycle != null) " (stage '${lifecycle.stage}') (${lifecycle.project.displayName})"
"Future was not completed yet" + if (lifecycle != null) " '$lifecycle'"
else ""
)
}
@@ -128,7 +128,7 @@ private class FutureImpl<T>(
}
private class LenientFutureImpl<T>(
private val future: Future<T>
private val future: Future<T>,
) : LenientFuture<T>, Serializable {
override suspend fun await(): T {
return future.await()
@@ -181,7 +181,7 @@ private class LazyFutureImpl<T>(private val future: Lazy<Future<T>>) : Future<T>
* Simple, Single Threaded, replacement for kotlinx.coroutines.CompletableDeferred.
*/
private class Completable<T>(
private var value: Result<T>? = null
private var value: Result<T>? = null,
) {
constructor(value: T) : this(Result.success(value))