Created
December 3, 2009 04:59
-
-
Save brentonashworth/247899 to your computer and use it in GitHub Desktop.
Ruby Script to run Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home" | |
JAVA="#{JAVA_HOME}/bin/java" | |
GIT_ROOT=ENV['GIT_ROOT'] | |
CLJ="/opt/clojure" | |
LIB="#{GIT_ROOT}/library" | |
classpath=".:src:test:classes" + | |
":#{GIT_ROOT}/formpluslogic/fpl-clojure-util/fpl-clojure-util.jar" + | |
":#{CLJ}/swank-clojure/src/" | |
JLINE="#{LIB}/bin-tools/jline-0.9.94.jar" | |
clojure_classpath=":#{CLJ}/clojure/clojure.jar"+ | |
":#{CLJ}/clojure-contrib/clojure-contrib.jar" | |
java_opts="-Xms500M -Xmx1024M" | |
class String | |
def ends_with?(str) | |
str = str.to_str | |
tail = self[-str.length, str.length] | |
tail == str | |
end | |
def starts_with?(str) | |
str = str.to_str | |
head = self[0, str.length] | |
head == str | |
end | |
end | |
def find_root(d, repl_config) | |
Dir.chdir(d) | |
if Dir.pwd == "/" | |
nil | |
else | |
if Dir.glob("#{repl_config}").size == 1 | |
Dir.pwd | |
else | |
find_root("../", repl_config) | |
end | |
end | |
end | |
def ensure_clj(script_name) | |
unless script_name.ends_with? ".clj" | |
script_name += ".clj" | |
end | |
script_name | |
end | |
def get_clj_args(list) | |
new_list = list.find_all{|item| !(item.starts_with? "-X") } | |
if new_list.size > 0 | |
" -- #{new_list.join(" ")}" | |
else | |
"" | |
end | |
end | |
def get_java_opts(list) | |
new_list = list.find_all{|item| item.starts_with? "-X" } | |
if new_list.size > 0 | |
(" " + new_list.join(" ")).gsub(/-Xjmx/, "-Dcom.sun.management.jmxremote") | |
else | |
"" | |
end | |
end | |
repl_config=".cljrc.clj" | |
dir = Dir.pwd | |
if find_root(dir, repl_config) | |
puts "Running in #{Dir.pwd}" | |
else | |
Dir.chdir(dir) | |
puts "Running in #{Dir.pwd}" | |
puts "Using default #{repl_config}" | |
repl_config="~/.cljrc.clj" | |
end | |
lib_has_clojure = false | |
if File.directory? "lib" | |
Dir.foreach("lib") do |x| | |
if x =~ /.*.jar$/ | |
if x.starts_with? "clojure-" | |
lib_has_clojure = true | |
end | |
classpath = classpath + ":lib/#{x}" | |
end | |
end | |
end | |
unless lib_has_clojure | |
classpath = classpath + clojure_classpath | |
end | |
puts classpath | |
if ARGV[0] | |
file_name = ensure_clj(ARGV.shift) | |
clojure_args = "" | |
puts "Running Clojure Script... #{file_name}" | |
if ARGV.size > 0 | |
clojure_args = get_clj_args(ARGV) | |
java_opts += get_java_opts(ARGV) | |
end | |
system("#{JAVA} #{java_opts} -cp #{classpath} " + | |
"clojure.lang.Script #{file_name}#{clojure_args}") | |
else | |
puts "Starting Clojure REPL..." | |
java_opts += "-Dcom.sun.management.jmxremote" | |
system("#{JAVA} #{java_opts} -cp #{classpath}:#{JLINE} jline.ConsoleRunner " + | |
"clojure.lang.Repl #{repl_config}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment