All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkmc.processor.Resource.kt Maven / Gradle / Ivy

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)
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy