Hi, I just want to check if I'm in progress of sol...
# announcements
l
Hi, I just want to check if I'm in progress of solving something that does not need solving. I want users to be able to run kotlin scripts out of the box (no pre-installation requirements) with (runtime) maven dependency resolution. So I made https://github.com/markaren/kts yesterday. It allows users to download the
kts
executable and then invoke a
.kts
script with maven dependencies. I know of kscript (unix systems) and the official scripting support (needs kotlin installed). My solution requires no prior software installed and works regardless of platform. Is it valuable?
b
Looks useful. Could you also create a precompiled release for this?
l
I will
It can also be used as a library, with the
invoke(script)
function exposed. It returns an object from the script
But then then naturally, the calling library needs to have that object in the classpath if it's a complex return type
b
Thanks!
I see it has some jar dependencies, so it'd be great to have it as executable fat jar where you can just curl it from the release and execute it straight away
Something to consider for the next release :)
Otherwise i've managed to setup current release on my linux machine to be visible on global PATH via symlink and it works like a charm
And lastly I've noticed you're renaming maven dep keyword to artifact. Wouldn't lib or dependency be better?
l
I gotta admit that I prefer the current distribution method. Then you add it to PATH and invoke it like ./kts rather than java -jar kts.jar. Also I've previously had issues with shadowJar plugin not including the right jars
Wouldn't lib or dependency be better?
hmm.. I dunno
b
No no, that's not what i meant. You can create executable jars (spring boot does that) where you can invoke the jar on bash like you'd invoke a shell script via ./app.jar or just ./app (if you rename it)
And it comes with all dependencies packed
Essentially your zip file, but exexutable without extracting it
l
mhh. I've only used shadowJar for this, which creates just a dumb fatJar. Make an isse for it 😉
But it requires functioning shadow jar
And currently I couldn;t get it to work due to some conflicting dependencies
Best I've managed to come at now is working shadow jar (and shadowJarDistScripts) and most importantly attached executable. It can be used directly as
./kts.jar <yourScript>.kts
For whatever reason, however, if i remove .jar suffix or symlink it it breaks with
Copy code
javax.script.ScriptException: error: unresolved reference: println
println("OK")
^
error: cannot access script base class 'org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate'.
Check your module classpath for missing or conflicting dependencies
println("OK")
^

        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.asJsr223EvalResult(KotlinJsr223J
vmScriptEngineBase.kt:104)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmSc
riptEngineBase.kt:63)
        at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngine
Base.kt:31)
        at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at info.laht.kts.KtsScriptRunner$invokeKts$thread$1.run(KtsScriptRunner.kt:38)
        at java.base/java.lang.Thread.run(Thread.java:834)
SOOO, I've managed to solve the exe jar problem and it now works like a charm! Here's a prebuilt exe (requires bash env to work, so no power shell or cmd :D). Will raise a PR to your repo soon
That attached binary is fully self contained and only requires JRE to be on the PATH. It's also adding kotlin-stdlib and all kotlin scripting dependencies to the script classpath by default
l
Abandoning this project in favour of kotlin-main-kts. Downloading
kotlin
from kotlin-compiler is just as easy as downloading
kts
and you get script caching and autocompletion.
👍 1
b
What about maven deps in scripts?
l
Works also with kotlin-main-kts
I had some issue with transitative deps with main-kts, but got it working using api dependencies
b
Sweet
l
The transitative thingy is actually a big issue, so kts is still viable. I changed the include syntax to use
@file:
so that autocomplete works in IntelliJ, but you must use kts to actually run it
This file is fully editable with autocompletion and import help for external dependencied in intellij, and I can right click to run it with kts
best of both worlds
b
How did you get kts in context menu?
l
settings -> tools -> external tools
then you get it as as external tools -> kts. To have it also directly as kts. Add an action to editor popup menu
👍 1