Basic scripting support for Kotlin


#1

I've developed a little script (see attachment) to turn Kotlin into a scripting language. e.g.

EDIT: This script has now moved to https://github.com/andrewoma/kotlin-script removing the previous limitations. It’s been tested under OS X and Linux (and cygwin with a patched kolinc).

#!/usr/bin/env kotlin
println(“Hello from Kotlin!”)
for (arg in args) {
  println(“arg: $arg”)
}

Features:

  • Supports scripts where the first line is #! and wraps them in a main method
  • Supports standalone scripts with a main function
  • Caches compiled versions of scripts. (“Hello, world” seems to take around 3s to run with compilation and 200ms thereafter)


Limitations:

  • Currently OS X only (although it could easily be ported to any unix/cygwin)
  • Doesn’t support things like kotlinc being a symlink, honouring JAVA_HOME etc


Anyway, I thought others might find it useful while waiting for official scripting support (http://youtrack.jetbrains.com/issue/KT-407)

Cheers,
Andrew

Message was edited by: Andrew O’Malley


#2

Wow!

This is totally awesome! I think it would make a lot of sense to make it a full-featured project, i.e. fix the limitations you mention.

Do you mind to put it up on github so that we will be able to call for Kontributions to the project?

Thanks!


#3

Greetings!

You’re welcome to use it however you like.

I guess the ultimate location for something like this would be along-side kotlinc in the distribution.
Or would you prefer me to put it in my own github repo until it matures?

Cheers,
Andrew


#4

Very interesting work! I installed msys on Windows, downloaded a md5 tool (http://www.fourmilab.ch/md5/md5.zip) and changed the line

  md5=md5 -q "$1"

to

  md5=md5 -n "$1"

and it worked fine on Windows in msys console :slight_smile:


#5

It would be great if you put it in your own repo for now. I'll be happy to help you find collaborators and finally add the tool to the Kotlin family.


#6

Hi Andrey,

I’ve rewritten the script to remove the previous limitations and make it cross-platform (including removing the dependency on md5). I’ve tested it on OS X, Linux and cygwin.

It’s available here: https://github.com/andrewoma/kotlin-script

Cheers,
Andrew


#7

This may be a dumb question, but the docs for kotlin-script says it needs kotlinc in the path.  

I have no idea where to get kotlinc as an executable (I know there’s an ant task…), google hasn’t been any help either.


#8

kotlinc zip contains executable scripts in "bin" directory


#9

What is the kotlinc zip.. do you mean the kotlin zip on github?  There is only a "kotlin" in the bin directory, not kotlinc


#10

You can get the Kotlin builds from Jetbrains' Team City site: http://teamcity.jetbrains.com/project.html?projectId=project67&tab=projectOverview

Under the “Compiler and Plugin” section there’s an “Artifacts” dropdown menu. You can download the kotlin zip file from there (it’s probably best sticking to a version that has been verifed). Within that .zip file there’s the kotlinc scripts (in the bin directory).

Cheers,
Andrew