https://kotlinlang.org logo
Title
g

Grégory Lureau

04/14/2022, 8:32 PM
Hi! Is there some way to retrieve the name of the current module being processed? (I'm generating documentation on a multi-module project.)
t

Ting-Yuan Huang

04/14/2022, 9:57 PM
If you are talking about Gradle modules, not that I'm aware of in KSP directly. Maybe try passing it via processor options in the
ksp{}
extension in build scripts?
g

Grégory Lureau

04/14/2022, 10:25 PM
Yes, thanks. I need to operate on a lot of modules, using the
originFile
path for now to deduce the module name, quite limited indeed. I suppose I could add a gradle plugin to pass automatically the value from gradle to ksp 🤔 I was hoping something from KSP environment, not sure if it makes sense.
a

Anton Popov

06/07/2022, 12:40 PM
I found, that
ResolverImpl
contains this info, but it is not exposed through the
Resolver
API. Maybe it is possible to enhance the API by adding
moduleName
property to
Resolver
? @Jiaxiang @Ting-Yuan Huang
For now, this super bad solution seems to work
fun moduleName(resolver: Resolver): String {
    val moduleDescriptor = resolver::class.java
        .getDeclaredField("module")
        .apply { isAccessible = true }
        .get(resolver)
    val rawName = moduleDescriptor::class.java
        .getMethod("getName")
        .invoke(moduleDescriptor)
        .toString()
    return rawName
        .removeSurrounding("<", ">")
        .removeSuffix("_debug")
        .removeSuffix("_release")
}
j

Jiaxiang

06/07/2022, 5:12 PM
I would recommend to not rely on reflection. I think this
moduleName
thing was considered long ago, and I can’t think of any objections to this, but as we are approaching to our next release, this probably won’t be shipped until next next release.
a

Anton Popov

06/07/2022, 5:16 PM
Can I create github enhancement issue?
j

Jiaxiang

06/07/2022, 5:17 PM
sure, that’s even better!
a

Anton Popov

06/07/2022, 5:27 PM
Done!
:thank-you: 2