Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

import

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
commit a5a8f862faa5d16078af41e394996e5e44226586 0 parents
Chris Granger authored
6 .gitignore
... ... @@ -0,0 +1,6 @@
  1 +pom.xml
  2 +*jar
  3 +/lib/
  4 +/classes/
  5 +.lein-failures
  6 +.lein-deps-sum
24 README.md
Source Rendered
... ... @@ -0,0 +1,24 @@
  1 +# CLJS watcher
  2 +
  3 +A simple shell script that watched for changes to CLJS files and recompiles them for you.
  4 +
  5 +## Usage
  6 +
  7 +Put `cljs-watch` on your $PATH (such as in /usr/local/bin) and then simply run it from a terminal:
  8 +
  9 +```bash
  10 +cljs-watch
  11 +
  12 +#it can also take a directory and compile options
  13 +cljs-watch cljs-src/ {:optimizations :none}
  14 +```
  15 +
  16 +## Notes
  17 +* by default output-to is set to `resources/public/cljs/bootstrap.js`
  18 +* it will add the local `lib/` to your classpath when you run it, allowing you to have other cljs deps in that folder
  19 +
  20 +## License
  21 +
  22 +Copyright (C) 2011 Chris Granger
  23 +
  24 +Distributed under the Eclipse Public License, the same as Clojure.
97 cljs-watch
... ... @@ -0,0 +1,97 @@
  1 +#!/bin/sh
  2 +
  3 +CLJS_LIB=$CLOJURESCRIPT_HOME/lib/*
  4 +CLJS_SRC=$CLOJURESCRIPT_HOME/src/clj
  5 +CLJS_CLJS=$CLOJURESCRIPT_HOME/src/cljs
  6 +
  7 +exec java -server -Xmx2G -Xms2G -Xmn256m -cp "${CLJS_LIB}:${CLJS_SRC}:${CLJS_CLJS}:lib/*" clojure.main -e \
  8 +"
  9 +(use '[clojure.java.io :only [file]])
  10 +(require '[cljs.closure :as cljsc])
  11 +
  12 +(do
  13 +
  14 +(def default-opts {:optimizations :simple
  15 + :pretty-print true
  16 + :output-dir \"resources/public/cljs/\"
  17 + :output-to \"resources/public/cljs/bootstrap.js\"})
  18 +
  19 +(def ANSI-CODES
  20 + {:reset \"[0m\"
  21 + :default \"[39m\"
  22 + :white \"[37m\"
  23 + :black \"[30m\"
  24 + :red \"[31m\"
  25 + :green \"[32m\"
  26 + :blue \"[34m\"
  27 + :yellow \"[33m\"
  28 + :magenta \"[35m\"
  29 + :cyan \"[36m\"
  30 + })
  31 +
  32 +(defn ansi
  33 + [code]
  34 + (str \u001b (get ANSI-CODES code (:reset ANSI-CODES))))
  35 +
  36 +(defn style
  37 + [s & codes]
  38 + (str (apply str (map ansi codes)) s (ansi :reset)))
  39 +
  40 +(def last-compiled (atom 0))
  41 +
  42 +(defn ext-filter [coll ext]
  43 + (filter (fn [f]
  44 + (let [fname (.getName f)
  45 + fext (subs fname (inc (.lastIndexOf fname \".\")))]
  46 + (and (not= \. (first fname)) (.isFile f) (= fext ext))))
  47 + coll))
  48 +
  49 +(defn find-cljs [dir]
  50 + (let [dir-files (-> dir file file-seq)]
  51 + (ext-filter dir-files \"cljs\")))
  52 +
  53 +(defn compile-cljs [src-dir opts]
  54 + (cljsc/build src-dir opts)
  55 + (reset! last-compiled (System/currentTimeMillis)))
  56 +
  57 +(defn newer? [f]
  58 + (let [last-modified (.lastModified f)]
  59 + (> last-modified @last-compiled)))
  60 +
  61 +(defn files-updated? [dir]
  62 + (some newer? (find-cljs dir)))
  63 +
  64 +(defn watcher-print [& text]
  65 + (print (style \":: watcher :: \" :magenta))
  66 + (apply print text)
  67 + (flush))
  68 +
  69 +(defn status-print [text]
  70 + (print \" \" (style text :green) \"\n\")
  71 + (flush))
  72 +
  73 +(defn transform-cl-args
  74 + [args]
  75 + (let [source (first args)
  76 + opts-string (apply str (interpose \" \" (rest args)))
  77 + options (when (> (count opts-string) 1)
  78 + (try (read-string opts-string)
  79 + (catch Exception e (println e))))]
  80 + {:source source :options options}))
  81 +
  82 +(let [{:keys [source options]} (transform-cl-args *command-line-args*)
  83 + src-dir (or source \"src/\")
  84 + opts (merge default-opts options)]
  85 + (.mkdirs (file (:output-to opts)))
  86 + (watcher-print \"Building ClojureScript files in ::\" src-dir)
  87 + (compile-cljs src-dir opts)
  88 + (status-print \"[done]\")
  89 + (watcher-print \"Waiting for changes\n\")
  90 + (while true
  91 + (Thread/sleep 1000)
  92 + (when (files-updated? src-dir)
  93 + (watcher-print \"Compiling updated files...\")
  94 + (compile-cljs src-dir opts)
  95 + (status-print \"[done]\")))))"
  96 +
  97 +
5 project.clj
... ... @@ -0,0 +1,5 @@
  1 +(defproject cljs-watch "1.0.0-SNAPSHOT"
  2 + :description "FIXME: write description"
  3 + :main cljs-watch.core
  4 + :dependencies [[org.clojure/clojure "1.3.0-beta1"]
  5 + [cljs-compiler-jar "0.1.0-SNAPSHOT"]])
87 src/cljs_watch/core.clj
... ... @@ -0,0 +1,87 @@
  1 +(use '[clojure.java.io :only [file]])
  2 +(require '[cljs.closure :as cljsc])
  3 +
  4 +(do
  5 +
  6 + (def default-opts {:optimizations :simple
  7 + :pretty-print true
  8 + :output-dir "resources/public/cljs/"
  9 + :output-to "resources/public/cljs/bootstrap.js"})
  10 +
  11 + (def ANSI-CODES
  12 + {:reset "[0m"
  13 + :default "[39m"
  14 + :white "[37m"
  15 + :black "[30m"
  16 + :red "[31m"
  17 + :green "[32m"
  18 + :blue "[34m"
  19 + :yellow "[33m"
  20 + :magenta "[35m"
  21 + :cyan "[36m"
  22 + })
  23 +
  24 + (defn ansi
  25 + [code]
  26 + (str \u001b (get ANSI-CODES code (:reset ANSI-CODES))))
  27 +
  28 + (defn style
  29 + [s & codes]
  30 + (str (apply str (map ansi codes)) s (ansi :reset)))
  31 +
  32 + (def last-compiled (atom 0))
  33 +
  34 + (defn ext-filter [coll ext]
  35 + (filter (fn [f]
  36 + (let [fname (.getName f)
  37 + fext (subs fname (inc (.lastIndexOf fname ".")))]
  38 + (and (not= \. (first fname)) (.isFile f) (= fext ext))))
  39 + coll))
  40 +
  41 + (defn find-cljs [dir]
  42 + (let [dir-files (-> dir file file-seq)]
  43 + (ext-filter dir-files "cljs")))
  44 +
  45 + (defn compile-cljs [src-dir opts]
  46 + (cljsc/build src-dir opts)
  47 + (reset! last-compiled (System/currentTimeMillis)))
  48 +
  49 + (defn newer? [f]
  50 + (let [last-modified (.lastModified f)]
  51 + (> last-modified @last-compiled)))
  52 +
  53 + (defn files-updated? [dir]
  54 + (some newer? (find-cljs dir)))
  55 +
  56 + (defn watcher-print [& text]
  57 + (print (style ":: watcher :: " :magenta))
  58 + (apply print text)
  59 + (flush))
  60 +
  61 + (defn status-print [text]
  62 + (print " " (style text :green) "\n")
  63 + (flush))
  64 +
  65 + (defn transform-cl-args
  66 + [args]
  67 + (let [source (first args)
  68 + opts-string (apply str (interpose " " (rest args)))
  69 + options (when (> (count opts-string) 1)
  70 + (try (read-string opts-string)
  71 + (catch Exception e (println e))))]
  72 + {:source source :options options}))
  73 +
  74 + (let [{:keys [source options]} (transform-cl-args *command-line-args*)
  75 + src-dir (or source "src/")
  76 + opts (merge default-opts options)]
  77 + (.mkdirs (file (:output-to opts)))
  78 + (watcher-print "Building ClojureScript files in ::" src-dir)
  79 + (compile-cljs src-dir opts)
  80 + (status-print "[done]")
  81 + (watcher-print "Waiting for changes\n")
  82 + (while true
  83 + (Thread/sleep 1000)
  84 + (when (files-updated? src-dir)
  85 + (watcher-print "Compiling updated files...")
  86 + (compile-cljs src-dir opts)
  87 + (status-print "[done]")))))
6 test/cljs_watch/test/core.clj
... ... @@ -0,0 +1,6 @@
  1 +(ns cljs-watch.test.core
  2 + (:use [cljs-watch.core])
  3 + (:use [clojure.test]))
  4 +
  5 +(deftest replace-me ;; FIXME: write
  6 + (is false "No tests have been written."))

0 comments on commit a5a8f86

Please sign in to comment.
Something went wrong with that request. Please try again.