walkmc.processor.Module.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resources-addon Show documentation
Show all versions of resources-addon Show documentation
A resources API/Addon with compatibility with others plugins
The newest version!
package walkmc.processor
import com.google.auto.service.*
import com.google.devtools.ksp.*
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*
import org.yaml.snakeyaml.*
import walkmc.annotation.*
/**
* An symbol processor for processing [Module] annotation.
*
* This processor is responsable to create the `plugin.yml` and
* inserts the values inside of them.
*/
class ModuleProcessor(val generator: CodeGenerator) : SymbolProcessor {
/**
* Checker for verifying if this processor is already executed.
* Evict various call/freeze.
*/
var invoked = false
override fun process(resolver: Resolver): List {
if (invoked)
return emptyList()
val symbols = resolver
.getSymbolsWithAnnotation("walkmc.annotation.Module")
.filterIsInstance()
.toList()
if (symbols.isEmpty())
return emptyList()
val main = symbols.firstOrNull { it.validate() } ?: return emptyList()
val annotation = main.findAnnotation() ?: return emptyList()
val info = ModuleInfo(main, annotation)
generator.createNewFile(Dependencies(true, main.containingFile!!), "", "plugin", "yml")
.writer()
.use {
Yaml().dump(info.toMap(), it)
it.flush()
}
invoked = true
return emptyList()
}
}
/**
* The processor provider of [ModuleProcessor].
*/
@AutoService(SymbolProcessorProvider::class)
class ModuleProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return ModuleProcessor(environment.codeGenerator)
}
}