google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.81k stars 264 forks source link

KSP read classpath resource #431

Open oooplz opened 3 years ago

oooplz commented 3 years ago

Using the Java annotation processing API. We can be using javax.annotation.processing.Filer.getResource method reading files.

Can SymbolProcessor API does the same work?

I searched the code playground, but not found read resource file API.

And can you provide a code example of read resource file API?

ting-yuan commented 3 years ago

There is no such function currently. We'll evaluate and provide a solution.

oooplz commented 3 years ago

There is no such function currently. We'll evaluate and provide a solution.

Thanks you very much for replying. The issue should be closed?

ting-yuan commented 3 years ago

Can we keep this open to track the task?

micHar commented 1 year ago

Any updates here? It hasn't been touched for quite a while.

Edger commented 3 months ago

Any update? SPI really needs this support.

kycqdhl3c commented 1 month ago

Any update?

brizzbuzz commented 1 month ago

curious to hear from someone familiar with the project... is this more of a technical blocker than it seems? it doesn't seem like a big ask, but seeing as this issue is 3 years old, wondering if there is a technical limitation here.

ForteScarlet commented 1 month ago

Any news here?

kycqdhl3c commented 4 days ago

I try with the code, and it work.

class XSpiProcessor(private val env: SymbolProcessorEnvironment, private val fileName: String) :
    SymbolProcessor {
    override fun process(resolver: Resolver): List<KSAnnotated> {
        val symbols = resolver.getSymbolsWithAnnotation(StartupNode::class.qualifiedName!!)
        val providers = symbols.filterIsInstance<KSClassDeclaration>().map { symbol ->
            symbol.qualifiedName?.asString().orEmpty()
        }.toList()
        generateConfigFiles(providers)
        return emptyList()
    }

    private fun generateConfigFiles(list: List<String>) {
        if (list.isEmpty()) {
            return
        }
        val resourceStream = env.codeGenerator.createNewFile(
            Dependencies.ALL_FILES,
            "META-INF/services",
            fileName,
            ""
        )
        resourceStream.bufferedWriter().use { writer ->
            list.forEach { providerImplementer ->
                writer.write(providerImplementer)
                writer.newLine()
            }
        }
    }
}