Friday, February 6, 2015

Blazing Fast Clojure Command Line Tools

Clojure's a cutting edge lispy language, but has shared a common problem with Java. In the past, it took too long starting  up to use Clojure for creation of command line tools. When you type

ls -la

tail -f

cd /

or any other command, you expect immediate gratification, not ten seconds of painful lag. Clojure's finally on the command line train.

Now, I'm not the first coder to make fast command line apps with Clojure, but I'm still excited to share what I learned. So hold on tight. I'm going to give a very quick overview on how to create extremely fast command line tools with Clojure.


These instructions work with OS X. They should work for any of the varieties of *nix out there. You'll need to modify the instructions to pull off Windows, but it shouldn't be too horrible to get working on Windows. The basic steps are all the same.

The first trick is using ClojureScript. But, doesn't that compile down to ... *BIG GASP* ... JavaScript? Yes. Yes it does. You could compile vanilla Clojure apps down to JavaScript, too, but using ClojureScript makes life easier and you still get the standard functional Clojure lispy goodness.

The first step to any Clojure installation is lein, more formally known as Leiningen. Leiningen provides commands (with templates) for creation of base Clojure and ClojureScript projects. It has a whole bunch of plugins that it very happily downloads and installs with simple modifications of property files. Even the CounterClockwise Clojure plugin for Eclipse uses Leiningen.

Visit leiningen.org to grab the lein script or lein.bat for your *nix or Windows OS. There are very nice installation instructions on the lein home page.

Once you have lein running from your command line, you want a nice ClojureScript template project that compiles your clojure files down to a nice compact JavaScript file. What you want is mies. Check out the project over on github.

The mies template actually generates a web page with associated *.js files. All you want is the *.min.js file that it generates when it compiles.

Create a nice ClojureScript project with the following command. (Leiningen handles the mies installation for you.)

lein new mies hello-world

lein is the tool. new tells lein that you want a new project. mies indicates that you want to use the mies template for creating the new project. hello-world is the name of the new project.

Once you cd into your new project, you can track down and modify your *.cljs source file. Notice it is in a *.cljs instead of a *.clj file. Your source file will be at:

src/hello_world/core.cljs

To build your project, issue the following command from the project's root directory—the hello-world directory.

lein cljsbuild once

or

lein cljsbuild auto

The build is a little slow. That's because of all the optimization that is going on when compiling the Clojure down to JavaScript. If you open up the resulting hello_world.min.js with a text editor, you'll see this is not your father's JavaScript. This is extremely lightweight JavaScript.

While your in the the hello_world.min.js file add the following line to the beginning of the file. It should be on it's own line, not sharing a line with the previous first line of code.

#!/usr/bin/env node

You will need to modify that slightly for Windows, but essentially, you want the node command called at the beginning of your file so the rest of the file is interpreted by Node.js.

Save and exit the file. Rename the *.min.js file to hello or for Windows users, hello.bat. No file extension is needed on *nix. Make the file executable with a command like,

chmod u+x hello

or

chmod 755 hello

Now if you're like me, you don't have Node.js installed and running. Node.js is built on Google's V8 JavaScript Engine. That's the one used by Chrome. It's so fast, no other JavaScript engine even compares.

Even though the site claims it's out of date, I like the node installation instructions here. For OS X users, you need to install the Xcode and git apps, and then run git, configure, make and make install the node project.

Once you have Node.js up and running with the node command, you're done. That's all there is to getting everything ready. Now if you put your hello file in the path, you should have a very fast command line responding with Hello World!

No comments:

Post a Comment