aboutsummaryrefslogtreecommitdiff
path: root/symbols/src/main/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'symbols/src/main/kotlin')
-rw-r--r--symbols/src/main/kotlin/process/SubscribeAnnotationProcessor.kt26
1 files changed, 18 insertions, 8 deletions
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")
}
}