I’ve been toying around with Clojure recently. In order to start clojure easily, I created a startup script similar to the ones found in the Wikibooks “Getting Started” page for Clojure. Except I used Ruby, because it started to get too complicated for pure shell script.

First, it assumes that you want to run from the latest Clojure and clojure-contrib sources, so it’ll prepend both clojure.jar and clojure-contrib.jar to the CLASSPATH.

It will also update the CLASSPATH, so you can simply drop new jars into ~/classes and the script automatically picks them up if they aren’t already in the CLASSPATH.

It supports both rlwrap and JLine, although you probably shouldn’t use both at the same time. Since rlwrap doesn’t compile under Snow Leopard anyway, I only use JLine.

It also detects if you have Processing installed, and includes all the jars you need to get clj-processing running. Also, this part is OS X specific, since it assumes all the p5 stuff is inside Processing.app, located under /Applications. However, if you don’t have it installed, or aren’t running on OS X, it simply won’t include it.

To use it yourself, you’ll probably want to update the CLOJURE_DIR and CLOJURE_CONTRIB_DIR to point at your clones. I use Git for tracking their respective sources. Rich Hickey has mirrors of you can find the repo for both Clojure and clojure-contrib on GitHub:

And if you put your own jars somewhere else, simply update the custom_classes_path to point to that directory.

#!/usr/bin/env ruby
#
# Released under MIT License
# Copyright (c) 2009 Markus Prinz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'shellwords'
# For rlwrap
BREAK_CHARS=%q/(){}[],^%$#@"";:''|\\/
# Update Java Classpath
CLASSPATH = ENV["CLASSPATH"] ? ENV["CLASSPATH"].split(':') : []
custom_classes_path = File.expand_path("~/classes")
Dir[File.join(custom_classes_path, "*.jar")].each do |jar|
CLASSPATH << jar unless CLASSPATH.include?(jar)
end if File.exists?(custom_classes_path)
# Add processing stuff, if it is installed
PROCESSING_DIR = "/Applications/Processing.app/Contents/Resources/Java"
processing_available = false
if File.exists?(PROCESSING_DIR)
%w(
core.jar
libraries/opengl/library/gluegen-rt.jar
libraries/opengl/library/jogl.jar
libraries/opengl/library/opengl.jar
).each do |jar_name|
jar = File.join(PROCESSING_DIR, jar_name)
CLASSPATH << jar if File.exists?(jar) && !CLASSPATH.include?(jar)
end
processing_available = true
end
CLOJURE_DIR = File.expand_path("~/Code/clojure")
CLOJURE_JAR = File.join(CLOJURE_DIR, "clojure.jar")
CLOJURE_CONTRIB_DIR = File.expand_path("~/Code/clojure-contrib")
CLOJURE_CONTRIB_JAR = File.join(CLOJURE_CONTRIB_DIR, "clojure-contrib.jar")
run_clojure = "java "
run_clojure << "-Djava.library.path=#{PROCESSING_DIR}/libraries/opengl/library " if processing_available
run_clojure << "-cp #{CLOJURE_JAR}:#{CLOJURE_CONTRIB_JAR}:#{CLASSPATH.join(":")} "
if ARGV.empty?
run_repl = if system("which rlwrap")
"rlwrap --remember --complete-filenames --break-chars=#{BREAK_CHARS.shellescape} --file=#{File.expand_path("~/.clj_completions")} "
else
""
end
run_repl << run_clojure
run_clojure << "jline.ConsoleRunner " if CLASSPATH.find{ |jar| jar =~ /jline-\d+\.\d+\.\d+\.jar/ }
run_repl << "clojure.main"
exec run_repl
else
exec "#{run_clojure} clojure.lang.Script #{ARGV[0]} -- #{ARGV[1..-1].shelljoin}"
end
view raw gistfile1.rb hosted with ❤ by GitHub