-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
cli.clj
260 lines (221 loc) · 7.85 KB
/
cli.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
(ns shadow.cljs.devtools.cli
(:gen-class)
(:require [clojure.string :as str]
[clojure.main :as main]
[shadow.cljs.devtools.config :as config]
[shadow.cljs.devtools.cli-opts :as opts]
[shadow.cljs.devtools.api :as api]
[shadow.cljs.devtools.server :as server]
[shadow.cljs.devtools.errors :as errors]
[shadow.cljs.devtools.server.npm-deps :as npm-deps]
[shadow.build.api :as cljs]
[shadow.build.node :as node]
[shadow.cljs.devtools.server.socket-repl :as socket-repl]
[shadow.cljs.devtools.server.env :as env]
[shadow.cljs.devtools.server.runtime :as runtime])
(:import (clojure.lang LineNumberingPushbackReader)
(java.io StringReader)))
(defn do-build-command [{:keys [action options] :as opts} build-config]
(try
(case action
:release
(api/release* build-config options)
:check
(api/check* build-config options)
:compile
(api/compile* build-config options))
:success
(catch Exception e
e)))
(defn maybe-rethrow-exceptions [ex]
(case (count ex)
0 :success
1 (throw (first ex))
(throw (ex-info "multiple builds failed" {:exceptions ex}))
))
(defn do-build-commands
[{:keys [builds] :as config} {:keys [action options] :as opts} build-ids]
;; FIXME: this should start classpath/npm services so builds can use it
;; if this is not a server instance
(->> build-ids
(map (fn [build-id]
(or (get builds build-id)
(do (println (str "No config for build \"" (name build-id) "\" found."))
;; FIXME: don't repeat this
(println (str "Available builds are: " (->> builds
(keys)
(map name)
(str/join ", "))))))))
(remove nil?)
(map #(future (do-build-command opts %)))
(into []) ;; force lazy seq
(map deref)
(remove #{:success})
(into []) ;; force again
;; need to throw exceptions to ensure that cli commands can exit with proper exit codes
(maybe-rethrow-exceptions)))
(defn do-clj-eval [config {:keys [arguments options] :as opts}]
(let [in
(if (:stdin options)
*in*
(-> (str/join " " arguments)
(StringReader.)
(LineNumberingPushbackReader.)))]
(binding [*in* in]
(main/repl
:init #(socket-repl/repl-init {:print false})
:prompt (fn [])))
))
(defn do-clj-run [config {:keys [arguments options] :as opts}]
(let [[main & args]
arguments
main-sym
(symbol main)
main-sym
(if (nil? (namespace main-sym))
(symbol main "-main")
main-sym)
main-ns
(namespace main-sym)]
(try
(require (symbol main-ns))
(catch Exception e
(throw (ex-info
(format "failed to load namespace: %s" main-ns)
{:tag ::clj-run-load
:main-ns main-ns
:main-sym main-sym} e))))
(let [main-var (find-var main-sym)]
(if-not main-var
(println (format "could not find %s" main-sym))
(let [main-meta (meta main-var)]
(try
(cond
;; running in server
(runtime/get-instance)
(apply main-var args)
;; new jvm. task fn wants to run watch
(:shadow/requires-server main-meta)
(do (server/start!)
(apply main-var args)
(server/wait-for-stop!))
;; new jvm. task fn doesn't need watch
:else
(api/with-runtime
(apply main-var args)))
(catch Exception e
(throw (ex-info
(format "failed to run function: %s" main-sym)
{:tag ::clj-run
:main-sym main-sym}
e)))))))))
(defn blocking-action
[config {:keys [action builds options] :as opts}]
(binding [*in* *in*]
(cond
(= :clj-eval action)
(api/with-runtime
(do-clj-eval config opts))
(or (= :clj-run action)
(= :run action))
(do-clj-run config opts)
(contains? #{:watch :node-repl :browser-repl :cljs-repl :clj-repl :server} action)
(server/from-cli action builds options)
)))
(defn main [& args]
(let [{:keys [action builds options summary errors] :as opts}
(opts/parse args)
config
(config/load-cljs-edn!)]
;; always install since its a noop if everything is in package.json
;; and a server restart is not required for them to be picked up
(npm-deps/main config opts)
;; FIXME: need cleaner with-runtime logic
;; don't like that some actions implicitely start a server
;; while others don't
;; I think server should be a dedicated action but that only makes sense once we have a UI
(cond
;;
;; actions that do a thing and exit
;;
(:version options)
(println "TBD")
(or (:help options) (seq errors))
(opts/help opts)
;;(= action :test)
;;(api/test-all)
(= :npm-deps action)
(println "npm-deps done.")
(contains? #{:compile :check :release} action)
(api/with-runtime
(do-build-commands config opts builds))
;; MVP, really not sure where to take this
(= :test action)
(api/with-runtime
(api/test))
;;
;; actions that may potentially block
;;
(contains? #{:watch :node-repl :browser-repl :cljs-repl :clj-repl :server :clj-eval :clj-run :run} action)
(blocking-action config opts)
:else
(println "Unknown action.")
)))
(defn print-main-error [e]
(try
(errors/user-friendly-error e)
(catch Exception ignored
;; print failed, don't attempt to print anything again
)))
(defn print-token
"attempts to print the given token to stdout which may be a socket
if the client already closed the socket that would cause a SocketException
so we ignore any errors since the client is already gone"
[token]
(try
(println token)
(catch Exception e
;; CTRL+D closes socket so we can't write to it anymore
)))
(defn from-remote
"the CLI script calls this with 2 extra token (uuids) that will be printed to notify
the script whether or not to exit with an error code, it also causes the client to
disconnect instead of us forcibly dropping the connection"
[complete-token error-token args]
(try
(if (env/restart-required?)
(do (println "SERVER INSTANCE OUT OF DATE!\nPlease restart.")
(print-token error-token))
(do (apply main "--via" "remote" args)
(print-token complete-token)))
(catch Exception e
(print-main-error e)
(println error-token))))
;; direct launches don't need to mess with tokens
(defn -main [& args]
(try
(apply main "--via" "main" args)
(shutdown-agents)
(catch Exception e
(print-main-error e)
(System/exit 1))))
(comment
(defn autotest
"no way to interrupt this, don't run this in nREPL"
[]
(-> (api/test-setup)
(cljs/watch-and-repeat!
(fn [state modified]
(-> state
(cond->
;; first pass, run all tests
(empty? modified)
(node/execute-all-tests!)
;; only execute tests that might have been affected by the modified files
(not (empty? modified))
(node/execute-affected-tests! modified))
)))))
(defn test-all []
(api/test-all))
(defn test-affected [test-ns]
(api/test-affected [(cljs/ns->cljs-file test-ns)])))