-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.clj
116 lines (84 loc) · 3.37 KB
/
build.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(require '[cljs.build.api :as api]
'[clojure.java.shell :as shell]
'[clojure.string :as string])
;;; Configuration.
(def source-dir "src")
(def test-dir "test")
(def compiler-config {:main 'my-app.core
:output-to "target/my-app/main.js"
:output-dir "target/my-app/main"
:target :nodejs
:optimizations :simple
:source-map "target/my-app/main.js.map"})
(def test-config {:main 'my-app.test-runner
:output-to "target/test.js"
:output-dir "target/test"
:target :nodejs
:optimizations :none
:source-map true})
(def test-environment {:SOME_ENV_VAR "some-env-value"})
(def dev-config (merge compiler-config
{:optimizations :none
:source-map true}))
;;; Tasks mechanism.
(defmulti task first)
(defmethod task :default
[args]
(let [all-tasks (-> task methods (dissoc :default) keys sort (->> (interpose ", ") (apply str)))]
(println "unknown or missing task argument. Choose one of:" all-tasks)
(System/exit 1)))
;;; Helper functions.
(defn run-node-tests []
(let [{:keys [out err exit]} (shell/sh "node" "target/test.js" :env test-environment)]
(println out err)
(= exit 0)))
(defn try-require [ns-sym]
(try (require ns-sym) true (catch Exception e false)))
(defmacro with-namespaces
[namespaces & body]
(if (every? try-require namespaces)
`(do ~@body)
`(do (println "task not available - required dependencies not found")
(System/exit 1))))
;;; Compiling task.
(defn compile-once []
(api/build source-dir compiler-config))
(defn compile-refresh []
(api/watch source-dir compiler-config))
(defmethod task "compile" [[_ type]]
(case type
(nil "once") (compile-once)
"watch" (compile-refresh)
(do (println "Unknown argument to compile task:" type)
(System/exit 1))))
;;; Testing task
(defn test-once []
(api/build (api/inputs source-dir test-dir) test-config)
(let [success? (run-node-tests)]
(System/exit (if success? 0 1))))
(defn test-refresh []
(api/watch (api/inputs source-dir test-dir)
(assoc test-config :watch-fn run-node-tests)))
(defmethod task "test" [[_ type]]
(case type
(nil "once") (test-once)
"watch" (test-refresh)
(do (println "Unknown argument to test task:" type)
(System/exit 1))))
;;; Figwheeling task
(defmethod task "figwheel" [[_ port]]
(with-namespaces [figwheel-sidecar.repl-api]
(figwheel-sidecar.repl-api/start-figwheel!
{:figwheel-options (cond-> {}
port (merge {:nrepl-port (some-> port Long/parseLong)
:nrepl-middleware ["cider.nrepl/cider-middleware"
"refactor-nrepl.middleware/wrap-refactor"
"cemerick.piggieback/wrap-cljs-repl"]}))
:all-builds [{:id "dev"
:figwheel true
:source-paths [source-dir]
:compiler dev-config}]})
(when-not port
(figwheel-sidecar.repl-api/cljs-repl))))
;;; Build script entrypoint.
(task *command-line-args*)