Permalink
Browse files

import

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
ibdknox committed Aug 21, 2011
0 parents commit a5a8f862faa5d16078af41e394996e5e44226586
Showing with 225 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +24 −0 README.md
  3. +97 −0 cljs-watch
  4. +5 −0 project.clj
  5. +87 −0 src/cljs_watch/core.clj
  6. +6 −0 test/cljs_watch/test/core.clj
@@ -0,0 +1,6 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-failures
+.lein-deps-sum
@@ -0,0 +1,24 @@
+# CLJS watcher
+
+A simple shell script that watched for changes to CLJS files and recompiles them for you.
+
+## Usage
+
+Put `cljs-watch` on your $PATH (such as in /usr/local/bin) and then simply run it from a terminal:
+
+```bash
+cljs-watch
+
+#it can also take a directory and compile options
+cljs-watch cljs-src/ {:optimizations :none}
+```
+
+## Notes
+* by default output-to is set to `resources/public/cljs/bootstrap.js`
+* it will add the local `lib/` to your classpath when you run it, allowing you to have other cljs deps in that folder
+
+## License
+
+Copyright (C) 2011 Chris Granger
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+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 \
+"
+(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]
+ (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)]
+ (.mkdirs (file (:output-to opts)))
+ (watcher-print \"Building ClojureScript files in ::\" src-dir)
+ (compile-cljs src-dir opts)
+ (status-print \"[done]\")
+ (watcher-print \"Waiting for changes\n\")
+ (while true
+ (Thread/sleep 1000)
+ (when (files-updated? src-dir)
+ (watcher-print \"Compiling updated files...\")
+ (compile-cljs src-dir opts)
+ (status-print \"[done]\")))))"
+
+
@@ -0,0 +1,5 @@
+(defproject cljs-watch "1.0.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :main cljs-watch.core
+ :dependencies [[org.clojure/clojure "1.3.0-beta1"]
+ [cljs-compiler-jar "0.1.0-SNAPSHOT"]])
@@ -0,0 +1,87 @@
+(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]
+ (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)]
+ (.mkdirs (file (:output-to opts)))
+ (watcher-print "Building ClojureScript files in ::" src-dir)
+ (compile-cljs src-dir opts)
+ (status-print "[done]")
+ (watcher-print "Waiting for changes\n")
+ (while true
+ (Thread/sleep 1000)
+ (when (files-updated? src-dir)
+ (watcher-print "Compiling updated files...")
+ (compile-cljs src-dir opts)
+ (status-print "[done]")))))
@@ -0,0 +1,6 @@
+(ns cljs-watch.test.core
+ (:use [cljs-watch.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit a5a8f86

Please sign in to comment.