https://kotlinlang.org logo
#ksp
Title
# ksp
m

Melvin Biamont

11/07/2023, 12:02 AM
Hi, I'm learning a bit ksp and I would like to know how could I pass a file to my
SymbolProcessorProvider
? I tried to pass its relative path as ksp argument like this:
Copy code
ksp {
    arg("the-file", "src/main/resources/my-file.txt")
}
But then, is there a way to get the root directory of the gradle module which called my ksp processor?
m

mbonnin

11/07/2023, 12:22 AM
Only way I've found to do this is to burn the file in the processor jar. Example here
m

Melvin Biamont

11/07/2023, 12:25 AM
Hmmmm, Right now, I have an ugly solution like this
Copy code
val SymbolProcessorEnvironment.moduleDirectory: File?
    get() {
        codeGenerator.createNewFileByPath(Dependencies(false), "codegen", "txt").close()
        val file = codeGenerator.generatedFile.firstOrNull { it.name == "codegen.txt" } ?: return null

        var counter = 0
        var buildDirectory: File? = null
        var currentDirectory: File? = file

        while(buildDirectory == null && counter < 7 && currentDirectory != null) {
            if(currentDirectory.name == "build") {
                buildDirectory = currentDirectory
            }

            currentDirectory = currentDirectory.parentFile
            counter++
        }

        return buildDirectory?.parentFile
    }
But that´s very sad there is no existing solution 😞
e

ephemient

11/07/2023, 12:48 AM
I imagine you'll want to declare the file as an input or output to the ksp task in Gradle so that it can do proper tracking, and then setting the full path as arg shouldn't be much more work than that
m

Melvin Biamont

11/07/2023, 1:14 AM
This works and gives the right file path:
Copy code
ksp {
    arg("the-file", project.file("src/main/resources/my-file.txt").path)
}
e

ephemient

11/07/2023, 1:20 AM
Gradle won't know to invalidate the task cache when the file changes that way though
1
🫠 2
that's why androidx.room introduced its own Gradle plugin for example, https://developer.android.com/jetpack/androidx/releases/room#2.6.0-alpha02
2 Views