Running Clojure in CodeRunner
This morning, I really wanted to see if I could get Clojure running in CodeRunner. It's a pretty nice lightweight development environment which is really targeted at scripting or prototyping ideas. The docs say it can do a full IDE for web development, and that's certainly possible - but I'm not sure that I'd use it for that, as there are likely far more dependencies - like the RESTful service, that need to be included, and that's just too much.
While CodeRunner has a lot of language support built-in, it doesn't handle Clojure, and that's the one language that I really wish they would have. So much so that I emailed the author to ask about adding it. When I didn't get a response, I decided to see if I couldn't do it on my own.
Turns out, it's not that hard.
Start by having Leiningen installed, and that you can get from Homebrew - which is a good idea to have installed on a Mac anyway. Assuming you have Homebrew installed, you simply need to say:
$ brew install leiningen
and it'll be downloaded and ready to go.
Then you can look at the lein-exec GitHub page for the rest of the instructions as to how to get it going. You need to update your ~/.lein/profiles.clj file to include:
{:user {:plugins [[lein-exec "0.3.7"]]}}
and then in your ~/bin directory, or really any place in your PATH you need to create the following two files - copied from the GitHub repo. First is lein-exec containing:
#!/bin/bash if [[ $1 =~ ^[~\/] ]] then # its already an absolute path lein exec "$@" else # This is a relative path, so make it absolute, # using the current directory as the base. lein exec "`pwd`/$@" fi
and the second is lein-exec-p, which contains:
#!/bin/bash if [[ $1 =~ ^[~\/] ]] then # its already an absolute path lein exec -p "$@" else # This is a relative path, so make it absolute, # using the current directory as the base. lein exec -p "`pwd`/$@" fi
At this point, you can make a script that looks like:
#!/bin/bash lein-exec (prn (* 4 6))
and when you run it, you will get 24 on the console. Not bad.
In order to get this running in CodeRunner, we need to add a new language to the preferences. I chose to duplicate Perl as that's very close to what I needed. I changed the settings to look like:
and the Templates I deleted, and the Docs I added the Dash docs, as I have that installed on my mac, and it is very good:
Once I had that, I could create a script and run it:
While not perfect, it's exactly what I wanted to be able to do, and it does it perfectly.