Skip to content

Commit

Permalink
[Gradle] KotlinPluginLifecycle: Implement .toString for better diagno…
Browse files Browse the repository at this point in the history
…stics

^KT-59446 In Progress

(cherry picked from commit d7facce)
  • Loading branch information
sellmair authored and qodana-bot committed Jun 21, 2023
1 parent 3a65c0c commit 54b11e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ internal interface KotlinPluginLifecycle {

fun enqueue(stage: Stage, action: KotlinPluginLifecycle.() -> Unit)

val isStarted: Boolean

fun launch(block: suspend KotlinPluginLifecycle.() -> Unit)

suspend fun await(stage: Stage)
Expand Down Expand Up @@ -370,13 +372,16 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
Stage.values().associateWith { ArrayDeque() }

private val loopRunning = AtomicBoolean(false)
private val isStarted = AtomicBoolean(false)
private val isStartedImpl = AtomicBoolean(false)
private val isFinished = AtomicBoolean(false)

override val isStarted: Boolean
get() = isStartedImpl.get()

private val properties = WeakHashMap<Property<*>, WeakReference<LifecycleAwareProperty<*>>>()

fun start() {
check(!isStarted.getAndSet(true)) {
check(!isStartedImpl.getAndSet(true)) {
"${KotlinPluginLifecycle::class.java.name} already started"
}

Expand Down Expand Up @@ -444,7 +449,7 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP

enqueuedActions.getValue(stage).addLast(action)

if (stage == Stage.EvaluateBuildscript && isStarted.get()) {
if (stage == Stage.EvaluateBuildscript && isStartedImpl.get()) {
loopIfNecessary()
}
}
Expand Down Expand Up @@ -498,6 +503,13 @@ private class KotlinPluginLifecycleImpl(override val project: Project) : KotlinP
return property.orNull
}
}

override fun toString(): String = buildString {
append("Kotlin Plugin Lifecycle: (${project.displayName})")
if (!isStarted) append(" *not started*")
else append(" stage '$stage'")
if (isFinished.get()) append(" *finished*")
}
}

private class KotlinPluginLifecycleCoroutineContextElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -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)

Expand All @@ -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 ""
)
}
Expand All @@ -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()
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 54b11e3

Please sign in to comment.