[Gradle] KotlinSourceSetConvention: KotlinSourceSet registration logic

Separate the retrieval and registration of KotlinSourceSets in the getValue function of KotlinSourceSetConvention. Add a custom Trace class to be stored in the isRegisteredByKotlinSourceSetConventionAt property for better readability and debugging purposes.
This commit is contained in:
Sebastian Sellmair
2023-06-21 14:34:05 +02:00
committed by Space Team
parent 0b57dac738
commit 2a4f9b19f0
@@ -39,12 +39,17 @@ import kotlin.reflect.KProperty
@ExperimentalKotlinGradlePluginApi
object KotlinSourceSetConvention :
ReadOnlyProperty<NamedDomainObjectContainer<KotlinSourceSet>, NamedDomainObjectProvider<KotlinSourceSet>> {
internal class Trace : Throwable()
override fun getValue(
thisRef: NamedDomainObjectContainer<KotlinSourceSet>, property: KProperty<*>,
): NamedDomainObjectProvider<KotlinSourceSet> {
val name = property.name
return if (name in thisRef.names) thisRef.named(name) else thisRef.register(name) { sourceSet ->
sourceSet.isRegisteredByKotlinSourceSetConventionAt = Throwable()
if (name in thisRef.names) return thisRef.named(name)
val trace = Trace()
return thisRef.register(name) { sourceSet ->
sourceSet.isRegisteredByKotlinSourceSetConventionAt = trace
}
}
@@ -53,7 +58,7 @@ object KotlinSourceSetConvention :
* This will be null if SourceSet already existed and was referenced using the convention, or of no convention was used at all.
*/
@Suppress("UnusedReceiverParameter") // Diagnostic is wrong
internal var KotlinSourceSet.isRegisteredByKotlinSourceSetConventionAt: Throwable?
internal var KotlinSourceSet.isRegisteredByKotlinSourceSetConventionAt: Trace?
by extrasReadWriteProperty("isRegisteredByKotlinSourceSetConvention")
private set
}