diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-06-01 20:47:08 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-01 12:47:08 +0200 |
commit | a20da5a79b5b05839862c8e8fd165b18368729c1 (patch) | |
tree | ef0b258d90d839867eba8c002f02e1c823b049ef /annotation-processors/src/main/kotlin | |
parent | 2698b9d13de081be2e72e63faa213a74cafb6a6b (diff) | |
download | skyhanni-a20da5a79b5b05839862c8e8fd165b18368729c1.tar.gz skyhanni-a20da5a79b5b05839862c8e8fd165b18368729c1.tar.bz2 skyhanni-a20da5a79b5b05839862c8e8fd165b18368729c1.zip |
Backend: Auto load annotation (#1904)
Co-authored-by: Brady <thatgravyboat@gmail.com>
Diffstat (limited to 'annotation-processors/src/main/kotlin')
3 files changed, 84 insertions, 0 deletions
diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt new file mode 100644 index 000000000..bc7d262b4 --- /dev/null +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt @@ -0,0 +1,68 @@ +package at.hannibal2.skyhanni.skyhannimodule + +import com.google.devtools.ksp.processing.CodeGenerator +import com.google.devtools.ksp.processing.Dependencies +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.symbol.ClassKind +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.validate +import java.io.OutputStreamWriter + +class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logger: KSPLogger) : SymbolProcessor { + + override fun process(resolver: Resolver): List<KSAnnotated> { + + val symbols = resolver.getSymbolsWithAnnotation(SkyHanniModule::class.qualifiedName!!).toList() + val validSymbols = symbols.mapNotNull { validateSymbol(it) } + + if (validSymbols.isNotEmpty()) { + generateFile(validSymbols) + } + + return emptyList() + } + + private fun validateSymbol(symbol: KSAnnotated): KSClassDeclaration? { + if (!symbol.validate()) { + logger.warn("Symbol is not valid: $symbol") + return null + } + + if (symbol !is KSClassDeclaration) { + logger.error("@SkyHanniModule is only valid on class declarations", symbol) + return null + } + + if (symbol.classKind != ClassKind.OBJECT) { + logger.error("@SkyHanniModule is only valid on kotlin objects", symbol) + return null + } + + return symbol + } + + private fun generateFile(symbols: List<KSClassDeclaration>) { + val dependencies = symbols.mapNotNull { it.containingFile }.toTypedArray() + val deps = Dependencies(true, *dependencies) + + val file = codeGenerator.createNewFile(deps, "at.hannibal2.skyhanni.skyhannimodule", "LoadedModules") + + OutputStreamWriter(file).use { + it.write("package at.hannibal2.skyhanni.skyhannimodule\n\n") + it.write("object LoadedModules {\n") + it.write(" val modules: List<Any> = listOf(\n") + + symbols.forEach { symbol -> + it.write(" ${symbol.qualifiedName!!.asString()},\n") + } + + it.write(" )\n") + it.write("}\n") + } + + logger.warn("Generated LoadedModules file with ${symbols.size} modules") + } +} diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProvider.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProvider.kt new file mode 100644 index 000000000..47d03655f --- /dev/null +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProvider.kt @@ -0,0 +1,11 @@ +package at.hannibal2.skyhanni.skyhannimodule + +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.processing.SymbolProcessorEnvironment +import com.google.devtools.ksp.processing.SymbolProcessorProvider + +class ModuleProvider : SymbolProcessorProvider { + override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { + return ModuleProcessor(environment.codeGenerator, environment.logger) + } +} diff --git a/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt new file mode 100644 index 000000000..cb6b0eae4 --- /dev/null +++ b/annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt @@ -0,0 +1,5 @@ +package at.hannibal2.skyhanni.skyhannimodule + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class SkyHanniModule |