From 5ed74f2df49c93ed1617520a935078b59ad7e195 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 1 Sep 2024 22:21:19 +0200 Subject: Add per compat project event listeners [no changelog] --- .../kotlin/process/SubscribeAnnotationProcessor.kt | 26 +++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'symbols') diff --git a/symbols/src/main/kotlin/process/SubscribeAnnotationProcessor.kt b/symbols/src/main/kotlin/process/SubscribeAnnotationProcessor.kt index 307f5c3..e6fcd89 100644 --- a/symbols/src/main/kotlin/process/SubscribeAnnotationProcessor.kt +++ b/symbols/src/main/kotlin/process/SubscribeAnnotationProcessor.kt @@ -1,4 +1,3 @@ - package moe.nea.firmament.annotations.process import com.google.auto.service.AutoService @@ -16,23 +15,24 @@ import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.symbol.Nullability import com.google.devtools.ksp.validate -import java.text.SimpleDateFormat -import java.util.Date import moe.nea.firmament.annotations.Subscribe class SubscribeAnnotationProcessor( val logger: KSPLogger, val codeGenerator: CodeGenerator, + val sourceSetName: String, ) : SymbolProcessor { override fun finish() { subscriptions.sort() + if (subscriptions.isEmpty()) return val subscriptionSet = subscriptions.mapTo(mutableSetOf()) { it.parent.containingFile!! } val dependencies = Dependencies( aggregating = true, *subscriptionSet.toTypedArray()) + val generatedFileName = "AllSubscriptions${sourceSetName.replaceFirstChar { it.uppercaseChar() }}" val subscriptionsFile = codeGenerator - .createNewFile(dependencies, "moe.nea.firmament.annotations.generated", "AllSubscriptions") + .createNewFile(dependencies, "moe.nea.firmament.annotations.generated", generatedFileName) .bufferedWriter() subscriptionsFile.apply { appendLine("// This file is @generated by SubscribeAnnotationProcessor") @@ -44,8 +44,8 @@ class SubscribeAnnotationProcessor( appendLine() appendLine("import moe.nea.firmament.events.subscription.*") appendLine() - appendLine("object AllSubscriptions {") - appendLine(" fun provideSubscriptions(addSubscription: (Subscription<*>) -> Unit) {") + appendLine("class $generatedFileName : SubscriptionList {") + appendLine(" override fun provideSubscriptions(addSubscription: (Subscription<*>) -> Unit) {") for (subscription in subscriptions) { val owner = subscription.parent.qualifiedName!!.asString() val method = subscription.child.simpleName.asString() @@ -53,12 +53,20 @@ class SubscribeAnnotationProcessor( appendLine(" addSubscription(Subscription<$type>(") appendLine(" ${owner},") appendLine(" ${owner}::${method},") - appendLine(" ${type}))") + appendLine(" ${type},") + appendLine(" \"${method}\"))") } appendLine(" }") appendLine("}") } subscriptionsFile.close() + val metaInf = codeGenerator.createNewFileByPath( + Dependencies(false), + "META-INF/services/moe.nea.firmament.events.subscription.SubscriptionList", extensionName = "") + .bufferedWriter() + metaInf.append("moe.nea.firmament.annotations.generated.") + metaInf.appendLine(generatedFileName) + metaInf.close() } data class Subscription( @@ -121,6 +129,8 @@ class SubscribeAnnotationProcessor( @AutoService(SymbolProcessorProvider::class) class SubscribeAnnotationProcessorProvider : SymbolProcessorProvider { override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { - return SubscribeAnnotationProcessor(environment.logger, environment.codeGenerator) + return SubscribeAnnotationProcessor(environment.logger, + environment.codeGenerator, + environment.options["firmament.sourceset"] ?: "main") } } -- cgit