To wrap up the 2022, here's my final release of th...
# feed
b
To wrap up the 2022, here's my final release of the year - introducing ktx@0.0.1 πŸŽ‰
ktx
is
npx
built for kotlin ecosystem. Currently it only supports running & installing kotlin scripts, but I do plan to add maven jar support in the future.
0.0.1
is the first developer preview release, so I'd very much appreciate any feedback I could get about its API, usage or just general usefulness. Here's a one-liner to get it onto your unix system
Copy code
curl <https://raw.githubusercontent.com/mpetuska/ktx/master/scripts/install.sh> | bash
πŸ‘ 5
πŸ‘ 2
πŸ‘πŸΎ 2
πŸš€ 8
m
Would be nice to have an usage example on the readme
πŸ’‘ 3
On mobile right now but looking forward to testing this. I've been looking for something like that for a long time
b
Good idea! Let me know if you have any useful kotlin scripts to use for such examples
Otherwise I'll mock up some sample scripts myself
m
Nothing at the top of my head. Simple sample script is πŸ‘πŸ‘
b
Ah, then there's nothing special. As mentioned in the usage section it's as simple as
ktx run <https://server.io/url/to/script.kts|https://server.io/url/to/script.kts>
(doesn't even need to have .kts extension as long as content is kotlin script)
Caching is rudimentary at the moment and ignores etags
m
A few more feedbacks: β€’ on my M1 mac + zsh, ktx is installed to
/Users/mbonnin/.local/share/ktx/
but
/Users/mbonnin/.ktx/
is sourced β€’ I'm guessing it requires a JVM installed, right?
Looking further, what about something that can run any maven dependency as long as it's a jar with a main class?
Copy code
# default uses latest version
ktx run com.example:my-cli
# version can be specified explicitely
ktx run com.example:my-cli:1.0
b
Running jars is the next thing on my todo list. Should land this month.
πŸ™Œ 1
m
I guess the hard part is the dependency resolution, right?
Not sure what Kotlin scripting uses these days, maybe that can be reused
b
A few more feedbacks:
on my M1 mac + zsh, ktx is installed to /Users/mbonnin/.local/share/ktx/ but /Users/mbonnin/.ktx/ is sourced
I'm guessing it requires a JVM installed, right?
That's expected. I'm trying to keep ktx itself and it's state separate. However the layout is not final yet, still experimenting with it. Also jvm is the only prerequisite, should probably mention it in the readme πŸ˜€
πŸ‘ 1
Re: jars It's actually super easy, barely an inconvenience πŸ˜‚ I'm planning to generate a simple script that declares a dependency on the jar and invokes its main method. After that kotlin script engine will resolve the dependencies as per usual
m
Right, that works too!
So looks like
kotlin
is a dependency too? In addition to a JVM?
Or do you install
kotlin
if not found?
b
Nope, it uses embedded scripting
So just the jvm
πŸ‘ 1
m
Ah I see
"installed part of ktx"
b
Btw, what do you mean by "`/Users/mbonnin/.ktx/` is sourced"? It should source
/Users/mbonnin/.local/share/ktx/.ktxrc
m
Copy code
$ cat /Users/mbonnin/.local/share/ktx/.ktxrc
#!/usr/bin/env bash

if [[ -z "$KTX_HOME" ]]; then
  KTX_HOME="$HOME/.ktx"
  PATH="$KTX_HOME/bin:$PATH"
fi%
b
So it does source the right file. .ktxrc just appends to path
m
Yea right but
KTX_HOME
is wrong
(inside the sourced file)
I was expecting
Copy code
if [[ -z "$KTX_HOME" ]]; then
  KTX_HOME="$HOME/.local/share/ktx"
  PATH="$KTX_HOME/bin:$PATH"
fi%
(or somthing at ~/.ktx but somehow I don't have it there?)
Copy code
$ ls ~/.ktx
ls: /Users/mbonnin/.ktx: No such file or directory
b
And that's fine, it gets created as needed
So on the first run/install of a script
Let me know if that's not the case on osx
m
first run/install of a script
Thing is I can't run anything because
ktx
is not in my PATH by default
I had to do
export PATH=$HOME/.local/share/ktx/bin:$PATH
manually
b
Ah, that's probably because you don't have ~/.local/bin on your path (ktx is symlinked to that dir)
Unix paths are hard...
I guess just adding ktx bin to path directly should be more reliable since we're sourcing shit anyways
m
I have it:
Copy code
$ ls -al ~/.local/bin/ktx
lrwxr-xr-x  1 mbonnin  staff  39 Jan  2 13:47 /Users/mbonnin/.local/bin/ktx -> /Users/mbonnin/.local/share/ktx/bin/ktx
But it's not in my PATH (for some reason 🀷 )
I guess just adding ktx bin to path directly should be more reliable since we're sourcing shit anyways
+1. I don't think you can expect anything besides /usr/bin to be in $PATH
b
Anyways that's exactly why I wanted someone besides me to tinker with it - lots of good feedback to improve upon! Plus I'm testing on linux so some osx tinkering was useful too!
πŸ‘ 1
c
Any benefits compared to something like
kscript
?
b
It's purpose is entirely different. Kscript is kotlin script runner, whereas ktx is script (and soon executable jar) manager. It's purpose is to make it trivial to install or just executive arbitrary kotlin scripts/jars off the web, just like npx for js.
Although there's some overlap since ktx is also able to execute local scripts
πŸ‘€ 1
m
kscript
is also able to run download and cache remote scripts: https://github.com/kscripting/kscript#url-usage
πŸ‘€ 1
But not arbitrary jars
c
Running arbitrary JARs is just a
wget
+`java -jar` call, though
m
Dependency resolution is the hard ℒ️ thing
c
Ah, does KTX have a solution to that? That's interesting
m
If jars are fat jars, it's fine but other times it's not
c
I tried using Kscript with Maven dependencies but it didn't work well
m
It's working okay-ish these days as long as you use maven publications
The catch is more and more publication are actually Gradle publications that rely on a
.module
file
b
Jar support is in the works, should land by the end of the week
m
So for all multiplatform stuff, you have to append
-jvm
for an example
c
m
(I'm actually talking about the resolver used by
*.main.kts
, i.e. first party resolver. Maybe
kscript
is a bit different)
@CLOVIS use
*.main.kts
, it's working really well for me
c
I'll try that, thanks
b
Another differentiating factor for ktx is that it's able to "install" remote jars and scripts that are then usable like any other cli command from the terminal. E.g.
Copy code
ktx install <https://host.com/my-script.main.kts|https://host.com/my-script.main.kts>

my-script arg1 arg2 #just works
https://kotlinlang.slack.com/archives/C0BJ0GTE2/p1673400313715129 No jar support as promised, but it delivers the next best thing - maven package support. Although I'm not sure how useful direct jar execution support would be? Curious to hear your thoughts and use-cases for that.
πŸš€ 1