May 2020: The state of Kotlin Scripting

Mentmore Regent fountain pen by Athanasius

What is Kotlin Scripting?

Kotlin Scripting is explained by a KEEP (Kotlin Enhancement and Evolution Process) document. The KEEP contains the whole proposal in a whooping 900 lines of markdown. You should read it, but we’ll sum it up as:

  • A way to easily compile Kotlin source files…
  • … with a given context, i.e. a set of variables/methods accessible to the script.

Kscript

Kscript is the de facto solution. It’s been there since 2016. There is a great KotlinConf video that explains why it was done (spoiler: initially for data science!). Kscript allows you to take any file, name it *.kts and make it executable:

// hello.kts
#!/usr/bin/env kscript
println("my args are ${args.joinToString(",")}")
$ chmod +x hello.kts$ ./hello.kts a few arguments
my args are a,few,arguments
Autocomplete in your Kotlin scripts
  • Scripts caching: Running the same script will be way faster the second time.
  • Maven dependencies: If you want to use OkHttp in your scripts, just drop @file:DependsOn("com.squareup.okhttp3:okhttp:4.7.2") and say goodbye to curl/wget.
  • IntelliJ support: Kscript generates an intelliJ project to edit your scripts with autocomplete.
  • Bootstrap header: To make you script install kscript automagically.
  • And a lot of other ones: See the kscript repo for the list.
  • It requires a local Kscript install on your machine. This is pretty easy to do thanks to SdkMan but still annoying when distributing scripts or trying to run them in your CI.
  • Dependency resolution doesn’t work with Java11. You will be stuck with Java8. Again, you can have multiple JDKs installed on your machine but it’s still annoying.

Kotlin-main-kts

Kotlin-main-kts is the contender and is the first party solution from the Kotlin team. Kotlin1.3.70 has largely improved the support for scripting as described in the 1.3.70 blog post.

// hello.main.kts
#!/usr/bin/env kotlin
println("my args are ${args.joinToString(",")}")
Autocomplete your *.main.kts files directly in IntelliJ
  • First party support.
  • Directly edit in intelliJ. No need for a wrapper Gradle project.
  • Only Kotlin is required, not need to install anything else.
  • No builtin bootstrap header and other nice helpers from Kscript.
  • Dependency resolution is limited.

Conclusion

Whether you’d like the extra features of Kscript or the nice integration and first party support of Kotlin-main-kts, writing Kotlin scripts has never been this easy. Kotlin can easily start new processes, do redirections and text processing, all that with the autocompletion and the strong typing we all love. For a few examples, you can check this script or this other. And Kotlin 1.4 should make it even easier!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store