I'm trying to build a system to run kts scripts at...
# scripting
a
I'm trying to build a system to run kts scripts at runtime, ideally with some sort of environment context (i.e access to the full classpath, for some backend stuff, personal project) - is this possible with the current state of kotlin scripting?
a
Do you want to run a Kotlin script from another JVM process?
a
Yeah! The idea is to have sort of scripts i can call to run from some arbitrary method
a
Okay, cool. Try taking a look at the answer here https://stackoverflow.com/questions/76484985/is-there-a-way-to-instantinate-kts-script-engine-in-the-gradle-kts/76505455 It explains how to launch a .kts from a Gradle task - but because Gradle is JVM based, it'll contain a lot of similar info!
a
Hm, I'm not really sure how to use this - but it requires Configurations, I don't know if i can use them at runtime? i've never tried before
The idea here for anyone else who may see it - I want to be able to effectively call and run any arbitrary script an admin user wants to run - also allowing "automatically" ran scripts to be hotfixed should an issue arise without having to take the backend down
h
which ide you are using
a
Sorry, that answer might be a bit confusing if you're not familiar with Gradle. I'll re-explain :) The main points are the required dependencies: •
org.jetbrains.kotlin:kotlin-compiler:$kotlinVersion
org.jetbrains.kotlin:kotlin-scripting-compiler:$kotlinVersion
org.jetbrains.kotlin:kotlin-main-kts:$kotlinVersion
And then import
org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
to compile and run the scripts. It's got a
main(args)
function, that you can just call and pass in some args. Most of the args are pretty standard. The trickier part is passing in the runtime classpath in the
-classpath
arg.