Skip to content

Commit d7facce

Browse files
sellmairSpace Team
authored and
Space Team
committed
[Gradle] KotlinPluginLifecycle: Implement .toString for better diagnostics
^KT-59446 In Progress
1 parent 46ea908 commit d7facce

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycle.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ internal interface KotlinPluginLifecycle {
296296

297297
val stage: Stage
298298

299+
val isStarted: Boolean
300+
301+
val isFinished: Boolean
302+
299303
fun launch(block: suspend KotlinPluginLifecycle.() -> Unit)
300304

301305
suspend fun await(stage: Stage)

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPluginLifecycleImpl.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
2424
KotlinPluginLifecycle.Stage.values().associateWith { ArrayDeque() }
2525

2626
private val loopRunning = AtomicBoolean(false)
27-
private val isStarted = AtomicBoolean(false)
27+
private val isStartedImpl = AtomicBoolean(false)
2828
private val isFinishedSuccessfully = AtomicBoolean(false)
2929
private val isFinishedWithFailures = AtomicBoolean(false)
3030

3131

3232
override var stage: KotlinPluginLifecycle.Stage = KotlinPluginLifecycle.Stage.values.first()
3333

34+
override val isStarted: Boolean
35+
get() = isStartedImpl.get()
36+
37+
override val isFinished: Boolean
38+
get() = isFinishedSuccessfully.get() || isFinishedWithFailures.get()
39+
3440
fun start() {
35-
check(!isStarted.getAndSet(true)) {
41+
check(!isStartedImpl.getAndSet(true)) {
3642
"${KotlinPluginLifecycle::class.java.name} already started"
3743
}
3844

@@ -103,13 +109,13 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
103109

104110
private fun finishWithFailures(failures: List<Throwable>) {
105111
assert(failures.isNotEmpty())
106-
assert(isStarted.get())
112+
assert(isStartedImpl.get())
107113
assert(!isFinishedWithFailures.getAndSet(true))
108114
configurationResult.complete(ProjectConfigurationResult.Failure(failures))
109115
}
110116

111117
private fun finishSuccessfully() {
112-
assert(isStarted.get())
118+
assert(isStartedImpl.get())
113119
assert(!isFinishedSuccessfully.getAndSet(true))
114120
configurationResult.complete(ProjectConfigurationResult.Success)
115121
}
@@ -140,7 +146,7 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
140146

141147
enqueuedActions.getValue(stage).addLast(action)
142148

143-
if (stage == KotlinPluginLifecycle.Stage.EvaluateBuildscript && isStarted.get()) {
149+
if (stage == KotlinPluginLifecycle.Stage.EvaluateBuildscript && isStartedImpl.get()) {
144150
loopIfNecessary()
145151
}
146152
}
@@ -168,6 +174,14 @@ internal class KotlinPluginLifecycleImpl(override val project: Project) : Kotlin
168174
}
169175
}
170176
}
177+
178+
override fun toString(): String = buildString {
179+
append("Kotlin Plugin Lifecycle: (${project.displayName})")
180+
if (!isStarted) append(" *not started*")
181+
else append(" stage '$stage'")
182+
if (isFinishedSuccessfully.get()) append(" *finished successfully*")
183+
if (isFinishedWithFailures.get()) append(" *finished with failures*")
184+
}
171185
}
172186

173187
private class KotlinPluginLifecycleCoroutineContextElement(

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/utils/Future.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal fun CompletableFuture<Unit>.complete() = complete(Unit)
6464
* @param name: The name of the extras key being used to store the future (see [extrasLazyProperty])
6565
*/
6666
internal inline fun <Receiver, reified T> futureExtension(
67-
name: String? = null, noinline block: suspend Receiver.() -> T
67+
name: String? = null, noinline block: suspend Receiver.() -> T,
6868
): ExtrasLazyProperty<Receiver, Future<T>> where Receiver : HasMutableExtras, Receiver : HasProject {
6969
return extrasLazyProperty<Receiver, Future<T>>(name) {
7070
project.future { block() }
@@ -97,7 +97,7 @@ internal fun <T> CompletableFuture(): CompletableFuture<T> {
9797

9898
private class FutureImpl<T>(
9999
private val deferred: Completable<T> = Completable(),
100-
private val lifecycle: KotlinPluginLifecycle? = null
100+
private val lifecycle: KotlinPluginLifecycle? = null,
101101
) : CompletableFuture<T>, Serializable {
102102
fun completeWith(result: Result<T>) = deferred.completeWith(result)
103103

@@ -111,7 +111,7 @@ private class FutureImpl<T>(
111111

112112
override fun getOrThrow(): T {
113113
return if (deferred.isCompleted) deferred.getCompleted() else throw IllegalLifecycleException(
114-
"Future was not completed yet" + if (lifecycle != null) " (stage '${lifecycle.stage}') (${lifecycle.project.displayName})"
114+
"Future was not completed yet" + if (lifecycle != null) " '$lifecycle'"
115115
else ""
116116
)
117117
}
@@ -128,7 +128,7 @@ private class FutureImpl<T>(
128128
}
129129

130130
private class LenientFutureImpl<T>(
131-
private val future: Future<T>
131+
private val future: Future<T>,
132132
) : LenientFuture<T>, Serializable {
133133
override suspend fun await(): T {
134134
return future.await()
@@ -181,7 +181,7 @@ private class LazyFutureImpl<T>(private val future: Lazy<Future<T>>) : Future<T>
181181
* Simple, Single Threaded, replacement for kotlinx.coroutines.CompletableDeferred.
182182
*/
183183
private class Completable<T>(
184-
private var value: Result<T>? = null
184+
private var value: Result<T>? = null,
185185
) {
186186
constructor(value: T) : this(Result.success(value))
187187

0 commit comments

Comments
 (0)