Added genref and messages modules
This commit is contained in:
1 parent
ff6dc22d00
commit
1d45c1b718
30 files changed
+1395
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package me.redtea.carcadex.genref.plugin
|
||||
|
||||
import com.google.devtools.ksp.KspExperimental
|
||||
import com.google.devtools.ksp.getAnnotationsByType
|
||||
import com.google.devtools.ksp.processing.CodeGenerator
|
||||
import com.google.devtools.ksp.processing.KSPLogger
|
||||
import com.google.devtools.ksp.symbol.KSFile
|
||||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
|
||||
import com.google.devtools.ksp.symbol.KSVisitorVoid
|
||||
import com.squareup.kotlinpoet.*
|
||||
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
|
||||
import com.squareup.kotlinpoet.ksp.writeTo
|
||||
import me.redtea.carcadex.genref.plugin.annotations.OnDisable
|
||||
import me.redtea.carcadex.genref.plugin.annotations.Plugin
|
||||
import tech.carcadex.kotlinbukkitkit.architecture.KotlinPlugin
|
||||
|
||||
@OptIn(KotlinPoetKspPreview::class)
|
||||
class PluginVisitor(private val codeGenerator: CodeGenerator,
|
||||
private val logger: KSPLogger
|
||||
) : KSVisitorVoid() {
|
||||
|
||||
var funcs: MutableList<FunSpec> = mutableListOf()
|
||||
|
||||
override fun visitFile(file: KSFile, data: Unit) {
|
||||
file.declarations.filter { it is KSFunctionDeclaration }.forEach { it.accept(this, Unit) }
|
||||
|
||||
val packageName = file.packageName.asString()
|
||||
val className = file.fileName.replace(
|
||||
".kt",
|
||||
""
|
||||
) + "Plugin"
|
||||
|
||||
val fileSpec = FileSpec.builder(packageName, className).apply {
|
||||
addProperty(
|
||||
PropertySpec.builder(
|
||||
"plugin",
|
||||
ClassName(packageName, className)
|
||||
)
|
||||
.mutable(true)
|
||||
.setter(FunSpec.builder("set()").addModifiers(KModifier.PRIVATE).build())
|
||||
.addModifiers(
|
||||
listOf(
|
||||
KModifier.LATEINIT
|
||||
)
|
||||
)
|
||||
.build()
|
||||
)
|
||||
addType(
|
||||
TypeSpec.Companion.classBuilder(className).primaryConstructor(
|
||||
FunSpec.constructorBuilder().build()
|
||||
)
|
||||
.addFunctions(funcs)
|
||||
.superclass(KotlinPlugin::class)
|
||||
.build()
|
||||
)
|
||||
}.build()
|
||||
try {
|
||||
fileSpec.writeTo(codeGenerator, true)
|
||||
} catch (ignored: FileAlreadyExistsException) {
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(KspExperimental::class)
|
||||
override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: Unit) {
|
||||
if (function.getAnnotationsByType(OnDisable::class).any()) disable(function)
|
||||
|
||||
if(function.getAnnotationsByType(Plugin::class).any()) enable(function)
|
||||
|
||||
|
||||
function.returnType!!.accept(this, Unit)
|
||||
}
|
||||
|
||||
fun disable(function: KSFunctionDeclaration) {
|
||||
funcs.add(FunSpec.builder("onPluginDisable")
|
||||
.addCode(CodeBlock.of("""
|
||||
${function.simpleName.getShortName()}()
|
||||
""".trimIndent()))
|
||||
.returns(Unit::class)
|
||||
.addModifiers(KModifier.OVERRIDE)
|
||||
.build())
|
||||
|
||||
}
|
||||
|
||||
fun enable(function: KSFunctionDeclaration) {
|
||||
funcs.add(FunSpec.builder("onPluginEnable")
|
||||
.addCode(CodeBlock.of("""
|
||||
plugin = this
|
||||
${function.simpleName.getShortName()}()
|
||||
|
||||
""".trimIndent()))
|
||||
.returns(Unit::class)
|
||||
.addModifiers(KModifier.OVERRIDE)
|
||||
.build())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package me.redtea.carcadex.genref.plugin.annotations
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class OnDisable()
|
||||
@@ -0,0 +1,5 @@
|
||||
package me.redtea.carcadex.genref.plugin.annotations
|
||||
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class Plugin()
|
||||
@@ -0,0 +1,29 @@
|
||||
package me.redtea.carcadex.genref.processor
|
||||
|
||||
import com.google.devtools.ksp.KspExperimental
|
||||
import com.google.devtools.ksp.isAnnotationPresent
|
||||
import com.google.devtools.ksp.processing.CodeGenerator
|
||||
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.KSAnnotated
|
||||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
|
||||
import com.google.devtools.ksp.validate
|
||||
import me.redtea.carcadex.genref.plugin.PluginVisitor
|
||||
import me.redtea.carcadex.genref.plugin.annotations.Plugin
|
||||
|
||||
class GenRefProcessor(private val codeGenerator: CodeGenerator,
|
||||
private val logger: KSPLogger) : SymbolProcessor {
|
||||
@OptIn(KspExperimental::class)
|
||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||
val files = resolver.getAllFiles().filter { it.declarations.any { it is KSFunctionDeclaration && it.validate()
|
||||
&& it.isAnnotationPresent(Plugin::class)} }
|
||||
|
||||
files.let { files -> files.forEach {
|
||||
it.accept(PluginVisitor(codeGenerator, logger), Unit)
|
||||
}
|
||||
}
|
||||
return files.filter { it.validate() }.toList()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package me.redtea.carcadex.genref.processor
|
||||
|
||||
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
|
||||
import com.google.devtools.ksp.processing.SymbolProcessorProvider
|
||||
|
||||
class ProcessorProvider : SymbolProcessorProvider {
|
||||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
|
||||
return GenRefProcessor(environment.codeGenerator, environment.logger)
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
me.redtea.carcadex.genref.processor.ProcessorProvider
|
||||
Reference in new issue
Block a user