walkmc.processor.Resource.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 kotlinx.serialization.*
import walkmc.annotation.*
import walkmc.serializer.*
/**
* A symbol processor for processing [AutoResource] annotations.
*
* This processor is responsable to create the `services.json` and
* inserts the values inside of them.
*/
class ResourceProcessor(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.AutoResource")
.filterIsInstance()
.toList()
if (symbols.isEmpty())
return emptyList()
val symbolSources = symbols
.map { it.containingFile!! }
.toTypedArray()
val values = symbols
.filter { it.validate() }
.map {
val annotation = it.findAnnotation()
ResourceInfo(it.qualifiedName!!.asString(), annotation?.priority ?: 0)
}
generator.createNewFile(Dependencies(true, *symbolSources), "", "resources", "json")
.writer()
.use { it.write(Json.encodeToString(values)) }
invoked = true
return emptyList()
}
}
/**
* The processor provider of [ResourceProcessor].
*/
@AutoService(SymbolProcessorProvider::class)
class ResourceProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return ResourceProcessor(environment.codeGenerator)
}
}