Hi. I've got a question: Is there some way to run ...
# scripting
f
Hi. I've got a question: Is there some way to run a
main.kts
script without the filename ending in
.main.kts
? My use-case is that I want to use Kotlin as a script interpreter on GitHub Actions. So would it be possible to make the
kotlin foo
line work without control of the file name? Is there some
--run-as-script
flag?
Copy code
~$ cat foo.main.kts 
#!/usr/bin/env kotlin
println("hello")
~$ kotlin foo.main.kts 
hello
~$ mv foo.main.kts foo
~$ kotlin foo
error: could not find or load main class foo
I've described details of what the issue is here https://github.com/actions/runner/issues/813
v
If it does not work, you could make your
setup-kotlin
action create a
kotlin-script
file that renames the file to the correct naming pattern and then invokes Kotlin
๐Ÿ‘ 1
f
Yes, done that already and it work (the action takes a "script" argument which is written to such a file and then executed), but I tried to make it nicer.
This is mostly driven by my wish to learn about github actions and kotlin, so I don't mind spending time learning more about why it does not work the way I think it should.
v
Not an argument to the action, how would you use multiple? I mean a file kotlin-script that is a bat or sh, that you add to the path. That script will rename the given argument and execute Kotlin. Your users then use
kotlin-script {0}
as configure shell.
f
Yes, considered this too, but I could not get it working for some reason. I tried to implement that in bash, not sure if the syntax was flawed. Could also work, sure, but I'm trying to make it pretty. Also I think if GitHub wants the "custom shell" feature to make sense they have to allow me to define a filename suffix which would solve the problem. They have a hardcoded map between shells and filename suffixes for "known" shells, but they won't allow me (so far) to define my own suffix for my own shell, that's why theat suffix is an empty string. That's what I try to raise awarenes for in the linked issue.
v
Yeah, sure, I'm fully with you. Just tried to suggest a nice (for the user) alternative as long as neither Kotlin allows arbitrary extension (I don't know whether it does) nor GitHub allows to control the extension. ๐Ÿ™‚
n
iirc there was a
-script
option but it might have been a
kotlinc
argument.. i do not have a kotlin binary at hand to test
i
You may try to use `-e`/`-expression` argument and pass the contents of the script as a string. But it will be interpreted as a regular
.kts
script without extensions provided for
.main.kts
scripts. Or you need to create your own runner - you can find almost all you need here - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts-test/src/test/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/test/SimpleMainKtsTest.kt#L21 - just replace the script definition template parameter and add a name with required extension to the
.toScriptSource()
call In any case it seems worth to add an issue to Kotlin YouTrack with a link to the runner issue. It seems like adding such a support is not really difficult.
๐Ÿ‘ 1
๐ŸŽ‰ 1
f
Thanks for your hints, but I've not succeded. I did create a ticket on youtrack https://youtrack.jetbrains.com/issue/KT-43534 I really think a flag for
kotlin
(as opposed to
kotlinc
) would do the trick.
i
@fwilhe do you think it will be sufficient if the possibility to read script from stdin will be supported, so the following usage could be possible (option name is only as an example)?
Copy code
kotlin -i < scriptfile
?
f
I don't think that GitHub allows this
Given the constraints of this "custom shell" feature which I would love to use with Kotlin
they put the script into a file where I don't control the name
but they allow to pass the name and arguments of a interpreter program
v
would be nice if both worked,
kotlin -i scriptfile
and
kotlin -i <scriptfile
or
kotlin -i - <scriptfile
i
ok, I see. Well, we'll think about it and maybe will find some acceptable solution. Thank you for the YT issue!
๐Ÿ‘ 2
f
Thanks for taking care. This whole effort is only there because I really like kotlin and want to use it for scripting everywhere ๐Ÿ™‚
๐Ÿ‘ 1
v
It will probably also allow having a shebang line like
#!/bin/kotlin -i
and then have the script named however you like, for example
/usr/local/bin/my-nifty-script-that-does-not-need-the-suffix