-
Notifications
You must be signed in to change notification settings - Fork 0
/
cirru_sepal.clj
69 lines (59 loc) · 1.94 KB
/
cirru_sepal.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(ns leiningen.cirru-sepal
(:require [cirru.parser.core :as parser]
[cirru.sepal :as sepal]
[clojure.java.io :as io]
[clojure.string :refer (replace-first)]
[hawk.core :as hawk])
(:import (java.io File)))
(def cwd (str (System/getenv "PWD") "/"))
(def compile-from "source")
(def compile-to "src")
(defn replace-extension [source-path]
(replace-first source-path ".cirru" ""))
(defn replace-filename [source-path]
(-> source-path
(replace-first "cirru-" "")
(replace-first ".cirru" "")))
(defn compile-code [code]
(sepal/make-code (parser/pare code "")))
(defn compile-file [filename]
(println (str "Compiling: " filename))
(let
[result (compile-code (slurp filename))]
(with-open [wrtr (io/writer (replace-extension filename))]
(.write wrtr result))
(io/make-parents (replace-filename filename))
(.renameTo
(File. (replace-extension filename))
(File. (replace-filename filename)))))
(defn is-cirru [f]
(some? (re-matches #".*\.cirru" (.getName f))))
(defn compile-all [paths]
(println "Start compiling files.")
(doall (map
(fn [path] (compile-file path))
(filter is-cirru
(apply concat
(map (fn [path] (file-seq (io/file path))) paths))))))
(defn listen-file [event]
(if (is-cirru (:file event))
(let
[ filename (.getAbsolutePath (:file event))
relativePath (clojure.string/replace filename cwd "")]
(compile-file relativePath))))
(defn watch-all [paths]
(println "Start watching files.")
(hawk/watch! [{:paths paths
:handler (fn [context event]
(listen-file event)
context)}])
(loop []
(Thread/sleep 400)
(recur)))
(defn cirru-sepal [project & args]
(let
[configurations (:cirru-sepal project)
paths (:paths configurations)]
(if (= (first args) "watch")
(watch-all paths)
(compile-all paths))))