Created
January 26, 2012 11:27
-
-
Save jedahu/1682343 to your computer and use it in GitHub Desktop.
browser repl for clojurescript projects
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
#!/bin/sh | |
PORT=9000 | |
OUT_DIR='out' | |
while getopts ":f:p:d:" opt; do | |
case $opt in | |
f) | |
HTML="$OPTARG" | |
;; | |
p) | |
PORT="$OPTARG" | |
;; | |
d) | |
OUT_DIR="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
OUT_FILE="$OUT_DIR/srepl.js" | |
shift $((OPTIND-1)) | |
BROWSER="$1" | |
[ "$BROWSER" ] || BROWSER='phantomjs' | |
SCRIPT=$(cat << EOF | |
(require '[cljs.repl :as repl] | |
'[cljs.repl.browser :as browser] | |
'[cljs.closure :as closure]) | |
(import '[java.io File]) | |
(defn html [out-dir out-file] | |
(let [tmp (File. (if (empty? "$HTML") "srepl.html" "$HTML"))] | |
(spit | |
tmp | |
(str "<html><head><meta charset='UTF-8'>" | |
"<body><script src='" out-dir "/goog/base.js'></script>" | |
"<script src='" out-file "'></script>" | |
"<script>goog.require('srepl.browser');</script></body></html>" | |
)) | |
tmp)) | |
(defn script [html] | |
(let [tmp (File. "$OUT_DIR/phantom-srepl.js")] | |
(spit | |
tmp | |
(str "new WebPage().open('" | |
(.getAbsolutePath html) | |
"', function() {});")) | |
tmp)) | |
(defn start-browser [command html] | |
(.exec | |
(Runtime/getRuntime) | |
(into-array | |
[command | |
(.getPath | |
(if (some #{"phantom" "phantomjs"} #{command}) | |
(script html) | |
html))]))) | |
(defn start-repl [port] | |
(repl/repl (browser/repl-env :port port))) | |
(defn build-browser-js [out-dir out-file port] | |
(closure/build | |
'[(ns srepl.browser | |
(:require [clojure.browser.repl :as brepl])) | |
(brepl/connect | |
(str "http://localhost:$PORT/repl"))] | |
{:optimizations nil | |
:pretty-print true | |
:output-dir out-dir | |
:output-to out-file})) | |
(let [out-dir (.getAbsolutePath (File. "$OUT_DIR")) | |
out-file (.getAbsolutePath (File. "$OUT_FILE")) | |
port $PORT | |
browser "$BROWSER"] | |
(build-browser-js out-dir out-file port) | |
(future | |
(Thread/sleep 1000) | |
(start-browser | |
browser | |
(html out-dir out-file))) | |
(start-repl port)) | |
EOF | |
) | |
export LEIN_JVM_OPTS=-XX:+TieredCompilation | |
exec rlwrap java -server -cp "lib/*:lib/dev/*:src" \ | |
clojure.main -e "$SCRIPT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment