Hello everyone. Is there a way to "register" scrip...
# scripting
m
Hello everyone. Is there a way to "register" scripts that get compiled into some sort of META-INF file? Let me explain. I have my regular Kotlin sources that contain my good ol' .kt files, along with some
.custom.kts
files. I would like to retrieve, at runtime, a list of all of the classes that were compiled from said
.custom.kts
files, e.g. with their FQN in a file somewhere within META-INF (similar to (I believe?) service providers in Java). Is there a way to do this with just the script compilation configs that are offered? I'd like to do this without compiler plugins or KSP in order to keep my build process simple. Is there a way to do this? (There are multiple use cases on my end, such as doing some sort of "feature discovery" at runtime by looking at all the classes that got generated from a
.feature.kts
file of some sort)
i
I cannot think of any way to achieve this without a compiler plugin or some post-processing. But the idea seems quite interesting and may also relate to some other planned features of the scripting. Could you please, create a feature request in the YT. I think that the explanation that you gave here can serve as a feature description quite well.
m
t
If you want something like ServiceLoaders but for scripts, you can probably just implement something similar:
Copy code
fun loadScripts(classLoader: ClassLoader) {
  classLoader.getResources("META-INF/myScripts").asSequence()
  .flatMap { it.openStream().reader().readLines() }
  .map { /* load it as script source */ }
}
When you write a script in
resources/path/to/myScript.kts
, you register it by editing
resources/META-INF/myScripts
and adding the line
path/to/myScript.kts
. And then you can load it dynamically at run time.