CrystalScript - a Crystal binary caching CLI

I wanted to be able to use Crystal as a scripting language, meaning I could run source files without them having to be systematically compiled.

It’s already possible by doing crystal my_script.cr, but I wondered if I could get rid of that systematic half-second compilation time every time I run the script.

So I built a CLI named crystalscript (I was not very inspired for a name). It’s basically a wrapper that will compile your script and cache the binary the first time it’s executed. After that, it will only replace the cached binary when the source changes.

The best way to use it is with shebangs, for example lets make a simple say_hello.cr script

#!/usr/bin/env crystalscript

puts "Hello"

Make it executable

chmod u+x say_hello.cr

Now it’s possible to call it like this

./say_hello.cr

I also made it so there are different optimization mode for the binaries.

For example, when I’m creating a new script, it will change often and I want the compilation to be as fast as possible. In this case I can use this shebang.

#!/usr/bin/env -S crystalscript --mode dev

When I’m done I can change the mode to normal (default) or release to have a faster and lighter binary.

Those modes are just presets using the crystal compiler args like -O, --release, --no-debug, etc. More info on the compilation modes for CrystalScript.

On a side note, I found a similar project called scriptisto which works with many languages, but I still wanted to try to build one myself for Crystal.

3 Likes