Skip to content

Commit

Permalink
add resilience to compilation exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information
ibdknox committed Aug 24, 2011
1 parent 9513f3e commit 03e40d8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 74 deletions.
148 changes: 75 additions & 73 deletions cljs-watch
Expand Up @@ -4,84 +4,87 @@ CLJS_LIB=$CLOJURESCRIPT_HOME/lib/*
CLJS_SRC=$CLOJURESCRIPT_HOME/src/clj
CLJS_CLJS=$CLOJURESCRIPT_HOME/src/cljs

exec java -server -Xmx2G -Xms2G -Xmn256m -cp "${CLJS_LIB}:${CLJS_SRC}:${CLJS_CLJS}:lib/*" clojure.main -e \
exec java -server -Xmx2G -Xms2G -Xmn256m -cp "${CLJS_LIB}:${CLJS_SRC}:${CLJS_CLJS}:lib/*:src/" clojure.main -e \
"
(use '[clojure.java.io :only [file]])
(require '[cljs.closure :as cljsc])
(do
(def default-opts {:optimizations :simple
:pretty-print true
:output-dir \"resources/public/cljs/\"
:output-to \"resources/public/cljs/bootstrap.js\"})
(def ANSI-CODES
{:reset \"[0m\"
:default \"[39m\"
:white \"[37m\"
:black \"[30m\"
:red \"[31m\"
:green \"[32m\"
:blue \"[34m\"
:yellow \"[33m\"
:magenta \"[35m\"
:cyan \"[36m\"
})
(defn ansi
[code]
(str \u001b (get ANSI-CODES code (:reset ANSI-CODES))))
(defn style
[s & codes]
(def default-opts {:optimizations :simple
:pretty-print true
:output-dir \"resources/public/cljs/\"
:output-to \"resources/public/cljs/bootstrap.js\"})
(def ANSI-CODES
{:reset \"[0m\"
:default \"[39m\"
:white \"[37m\"
:black \"[30m\"
:red \"[31m\"
:green \"[32m\"
:blue \"[34m\"
:yellow \"[33m\"
:magenta \"[35m\"
:cyan \"[36m\"
})
(defn ansi
[code]
(str \u001b (get ANSI-CODES code (:reset ANSI-CODES))))
(defn style
[s & codes]
(str (apply str (map ansi codes)) s (ansi :reset)))
(def last-compiled (atom 0))
(defn ext-filter [coll ext]
(filter (fn [f]
(let [fname (.getName f)
fext (subs fname (inc (.lastIndexOf fname \".\")))]
(and (not= \. (first fname)) (.isFile f) (= fext ext))))
coll))
(defn find-cljs [dir]
(let [dir-files (-> dir file file-seq)]
(ext-filter dir-files \"cljs\")))
(defn compile-cljs [src-dir opts]
(cljsc/build src-dir opts)
(reset! last-compiled (System/currentTimeMillis)))
(defn newer? [f]
(let [last-modified (.lastModified f)]
(> last-modified @last-compiled)))
(defn files-updated? [dir]
(some newer? (find-cljs dir)))
(defn watcher-print [& text]
(print (style \":: watcher :: \" :magenta))
(apply print text)
(flush))
(defn status-print [text]
(print \" \" (style text :green) \"\n\")
(flush))
(defn transform-cl-args
[args]
(let [source (first args)
opts-string (apply str (interpose \" \" (rest args)))
options (when (> (count opts-string) 1)
(try (read-string opts-string)
(catch Exception e (println e))))]
{:source source :options options}))
(let [{:keys [source options]} (transform-cl-args *command-line-args*)
src-dir (or source \"src/\")
opts (merge default-opts options)]
(def last-compiled (atom 0))
(defn ext-filter [coll ext]
(filter (fn [f]
(let [fname (.getName f)
fext (subs fname (inc (.lastIndexOf fname \".\")))]
(and (not= \. (first fname)) (.isFile f) (= fext ext))))
coll))
(defn find-cljs [dir]
(let [dir-files (-> dir file file-seq)]
(ext-filter dir-files \"cljs\")))
(defn compile-cljs [src-dir opts]
(try
(cljsc/build src-dir opts)
(catch Throwable e
(.printStackTrace e)))
(reset! last-compiled (System/currentTimeMillis)))
(defn newer? [f]
(let [last-modified (.lastModified f)]
(> last-modified @last-compiled)))
(defn files-updated? [dir]
(some newer? (find-cljs dir)))
(defn watcher-print [& text]
(print (style \":: watcher :: \" :magenta))
(apply print text)
(flush))
(defn status-print [text]
(print \" \" (style text :green) \"\n\")
(flush))
(defn transform-cl-args
[args]
(let [source (first args)
opts-string (apply str (interpose \" \" (rest args)))
options (when (> (count opts-string) 1)
(try (read-string opts-string)
(catch Exception e (println e))))]
{:source source :options options}))
(let [{:keys [source options]} (transform-cl-args *command-line-args*)
src-dir (or source \"src/\")
opts (merge default-opts options)]
(.mkdirs (file (:output-to opts)))
(watcher-print \"Building ClojureScript files in ::\" src-dir)
(compile-cljs src-dir opts)
Expand All @@ -92,6 +95,5 @@ exec java -server -Xmx2G -Xms2G -Xmn256m -cp "${CLJS_LIB}:${CLJS_SRC}:${CLJS_CLJ
(when (files-updated? src-dir)
(watcher-print \"Compiling updated files...\")
(compile-cljs src-dir opts)
(status-print \"[done]\")))))"


(status-print \"[done]\")))))
"
5 changes: 4 additions & 1 deletion src/cljs_watch/core.clj
Expand Up @@ -43,7 +43,10 @@
(ext-filter dir-files "cljs")))

(defn compile-cljs [src-dir opts]
(cljsc/build src-dir opts)
(try
(cljsc/build src-dir opts)
(catch Throwable e
(.printStackTrace e)))
(reset! last-compiled (System/currentTimeMillis)))

(defn newer? [f]
Expand Down

0 comments on commit 03e40d8

Please sign in to comment.