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 | |
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')
5 files changed, 112 insertions, 0 deletions
diff --git a/annotation-processors/build.gradle.kts b/annotation-processors/build.gradle.kts new file mode 100644 index 000000000..c091ba6e3 --- /dev/null +++ b/annotation-processors/build.gradle.kts @@ -0,0 +1,27 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + idea + kotlin("jvm") + java +} + +repositories { + mavenCentral() +} + +dependencies { + implementation(kotlin("stdlib-jdk8")) + implementation("com.google.devtools.ksp:symbol-processing-api:1.8.0-1.0.8") +} + +tasks.withType<JavaCompile> { + if (JavaVersion.current().isJava9Compatible) { + options.release.set(8) + } +} + +val compileKotlin: KotlinCompile by tasks +compileKotlin.kotlinOptions { + jvmTarget = "1.8" +} 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 diff --git a/annotation-processors/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider b/annotation-processors/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider new file mode 100644 index 000000000..5f0d42dc4 --- /dev/null +++ b/annotation-processors/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider @@ -0,0 +1 @@ +at.hannibal2.skyhanni.skyhannimodule.ModuleProvider |