-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backend: Auto load annotation (#1904)
Co-authored-by: Brady <thatgravyboat@gmail.com>
- Loading branch information
1 parent
2698b9d
commit a20da5a
Showing
11 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
68 changes: 68 additions & 0 deletions
68
...tation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/ModuleProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
annotation-processors/src/main/kotlin/at/hannibal2/skyhanni/skyhannimodule/SkyHanniModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package at.hannibal2.skyhanni.skyhannimodule | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.SOURCE) | ||
annotation class SkyHanniModule |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
at.hannibal2.skyhanni.skyhannimodule.ModuleProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
loom.platform=forge | ||
org.gradle.jvmargs=-Xmx2g | ||
org.gradle.jvmargs=-Xmx2g | ||
org.gradle.parallel=true | ||
org.gradle.caching=true | ||
kotlin.incremental.useClasspathSnapshot=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters