-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
api.clj
691 lines (570 loc) · 20.2 KB
/
api.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
(ns shadow.cljs.devtools.api
(:refer-clojure :exclude (compile test))
(:require
[clojure.core.async :as async :refer (go <! >! >!! <!! alt! alt!!)]
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[clojure.pprint :refer (pprint)]
[clojure.java.browse :refer (browse-url)]
[clojure.string :as str]
[shadow.runtime.services :as rt]
[shadow.build :as build]
[shadow.build.api :as build-api]
[shadow.build.node :as node]
[shadow.build.npm :as npm]
[shadow.build.classpath :as cp]
[shadow.build.babel :as babel]
[shadow.cljs.devtools.server.worker :as worker]
[shadow.cljs.devtools.server.util :as util]
[shadow.cljs.devtools.server.common :as common]
[shadow.cljs.devtools.config :as config]
[shadow.cljs.devtools.errors :as e]
[shadow.cljs.devtools.server.supervisor :as super]
[shadow.cljs.devtools.server.repl-impl :as repl-impl]
[shadow.cljs.devtools.server.runtime :as runtime])
(:import [java.net Inet4Address NetworkInterface]))
;; nREPL support
(def ^:dynamic *nrepl-cljs* nil)
(def ^:dynamic *nrepl-clj-ns* nil)
(def ^:dynamic *nrepl-active* false)
(def ^:dynamic *nrepl-quit-signal* nil)
(defn get-worker
[id]
{:pre [(keyword? id)]}
(let [{:keys [out supervisor] :as app}
(runtime/get-instance!)]
(super/get-worker supervisor id)
))
(defn make-runtime []
(let [config (-> (config/load-cljs-edn!)
;; just in case someone gets the idea to put :server-runtime true into their config
(dissoc :server-runtime))]
(log/debug "starting runtime instance")
(-> {::started (System/currentTimeMillis)
:config config}
(rt/init (common/get-system-config config))
(rt/start-all)
)))
(defmacro with-runtime [& body]
;; not using binding since there should only ever be one runtime instance per JVM
`(let [body-fn#
(fn []
~@body)]
(if (runtime/get-instance)
(body-fn#)
;; start/stop instance when not running in server context
(let [runtime# (make-runtime)]
(try
(runtime/set-instance! runtime#)
(body-fn#)
(finally
(runtime/reset-instance!)
(rt/stop-all runtime#)))))))
(defn get-build-config [build-id]
(config/get-build build-id))
(defn get-build-ids []
(-> (config/load-cljs-edn)
(get :builds)
(keys)
(into #{})))
(defn get-runtime! []
(runtime/get-instance!))
(defn- get-or-start-worker [build-config opts]
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)]
(if-let [worker (super/get-worker supervisor (:build-id build-config))]
worker
(super/start-worker supervisor build-config)
)))
(defn- start-worker [build-config opts]
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)]
(super/start-worker supervisor build-config)
))
(defn worker-running? [build-id]
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)]
(contains? (super/active-builds supervisor) build-id)
))
(defn watch-compile!
"manually trigger a recompile for a watch when {:autobuild false} is used"
[build-id]
(let [worker (get-worker build-id)]
(if-not worker
:watch-not-running
(do (-> worker
(worker/compile)
(worker/sync!))
;; avoid returning the worker state because it will blow up the REPL
:ok))))
(defn watch-compile-all!
"call watch-compile! for all running builds"
[]
(let [{:keys [supervisor]}
(runtime/get-instance!)
active-builds
(super/active-builds supervisor)]
(doseq [id active-builds]
(watch-compile! id))
:ok))
(defn watch-set-autobuild!
"starts/stops autobuild for a watch worker by id
(watch-set-autobuild :app true|false)"
[build-id toggle]
(let [worker (get-worker build-id)]
(cond
(nil? worker)
:watch-not-running
toggle
(-> worker
(worker/start-autobuild)
(worker/sync!))
:else
(-> worker
(worker/stop-autobuild)
(worker/sync!)))
:ok))
(defn watch*
([{:keys [build-id] :as build-config} {:keys [autobuild verbose sync] :as opts}]
(let [out
(util/stdout-dump verbose)
build-config
(if (map? build-id)
build-id
(config/get-build! build-id))]
(-> (start-worker build-config opts)
(worker/watch out true)
(cond->
(not (false? autobuild))
(worker/start-autobuild)
(false? autobuild)
(worker/compile)
(not (false? sync))
(worker/sync!))
))))
(defn watch
"starts a dev worker process for a given :build-id
opts defaults to {:autobuild true}"
([build-id]
(watch build-id {}))
([build-id opts]
(if (worker-running? build-id)
:already-watching
(let [build-config
(if (map? build-id)
build-id
(config/get-build! build-id))]
(watch* build-config opts)
:watching
))))
(defn active-builds []
(let [{:keys [supervisor]}
(runtime/get-instance!)]
(super/active-builds supervisor)))
(defn compiler-env [build-id]
(let [{:keys [supervisor]}
(runtime/get-instance!)]
(when-let [worker (super/get-worker supervisor build-id)]
(-> worker
:state-ref
(deref)
:build-state
:compiler-env))))
(comment
(watch :browser)
(stop-worker :browser))
(defn stop-worker [build-id]
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)]
(super/stop-worker supervisor build-id)
:stopped))
(defn get-config []
(or (when-let [inst (runtime/get-instance)]
(:config inst))
(config/load-cljs-edn)))
(defn get-build-config [id]
{:pre [(keyword? id)]}
(config/get-build! id))
(defn build-finish [{::build/keys [build-info] :as state} config]
(util/print-build-complete build-info config)
(let [rt (::runtime state)]
(when (::once rt)
(let [{:keys [babel npm]} rt]
(babel/stop babel)
(npm/stop npm))))
state)
(defn compile* [build-config opts]
(util/print-build-start build-config)
(-> (util/new-build build-config :dev opts)
(build/configure :dev build-config)
(build/compile)
(build/flush)
(build-finish build-config)))
(defn compile!
"do not use at the REPL, will return big pile of build state, will blow up REPL by printing too much"
[build opts]
(with-runtime
(let [build-config (config/get-build! build)]
(compile* build-config opts))))
(defn compile
([build]
(compile build {}))
([build opts]
(try
(compile! build opts)
:done
(catch Exception e
(e/user-friendly-error e)))))
(defn once
"deprecated: use compile"
([build]
(compile build {}))
([build opts]
(compile build opts)))
(defn release*
[build-config {:keys [debug source-maps pseudo-names] :as opts}]
(util/print-build-start build-config)
(-> (util/new-build build-config :release opts)
(build/configure :release build-config)
(cond->
(or debug source-maps)
(build-api/enable-source-maps)
(or debug pseudo-names)
(build-api/with-compiler-options
{:pretty-print true
:pseudo-names true}))
(build/compile)
(build/optimize)
(build/flush)
(build-finish build-config)))
(defn release
([build]
(release build {}))
([build opts]
(try
(with-runtime
(let [build-config (config/get-build! build)]
(release* build-config opts)))
:done
(catch Exception e
(e/user-friendly-error e)))))
(defn check* [{:keys [id] :as build-config} opts]
;; FIXME: pretend release mode so targets don't need to account for extra mode
;; in most cases we want exactly :release but not sure that is true for everything?
(-> (util/new-build build-config :release opts)
(build/configure :release build-config)
(as-> {:keys [cache-dir] :as X}
(assoc X
;; using another dir because of source maps
;; not sure :release builds want to enable source maps by default
;; so running check on the release dir would cause a recompile which is annoying
;; but check errors are really useless without source maps
:cache-dir (io/file cache-dir "check")
;; always override :output-dir since check output should never be used
;; only generates output for source maps anyways
:output-dir (io/file cache-dir "check-out")))
(build-api/enable-source-maps)
(update-in [:compiler-options :closure-warnings] merge {:check-types :warning})
(build/compile)
(build/check)
(build-finish build-config))
:done)
(defn check
([build]
(check build {}))
([build opts]
(try
(with-runtime
(let [build-config (config/get-build! build)]
(check* build-config opts)))
(catch Exception e
(e/user-friendly-error e)))))
(defn repl-client-connected? [worker]
(not (empty? (-> worker :state-ref deref :eval-clients))))
(defn nrepl-select
([id]
(nrepl-select id {}))
([id {:keys [skip-repl-out] :as opts}]
(let [worker (get-worker id)]
(cond
(nil? worker)
[:no-worker id]
:else
(do (set! *nrepl-cljs* id)
;; doing this to make cider prompt not show "user" as prompt after calling this
(set! *nrepl-clj-ns* *ns*)
(let [repl-ns (some-> worker :state-ref deref :build-state :repl-state :current :ns)]
(set! *ns* (create-ns (or repl-ns 'cljs.user))))
;; calling (node-repl) in a REPL will cause the stdout and the repl prn forward
;; to both print which is not what we want
(when-not skip-repl-out
;; nrepl doesn't have a clear way to signal the end of a session
;; so this keeps running even is the nrepl is disconnected
;; I hope the print just fails and kills the loop?
(when-let [quit *nrepl-quit-signal*]
(let [chan (async/chan 100)]
(go (try
(loop []
(alt! :priority true
chan
([msg]
(when (some? msg)
(case (:type msg)
:repl/out
(do (println (:text msg))
(flush))
:repl/err
(binding [*out* *err*]
(println (:text msg))
(flush))
:ignored)
(recur)
))
quit
([_] :quit)))
(log/debug ::nrepl-print-loop-end)
(catch Exception e
(log/debug e ::nrepl-print-loop-ex)
(async/close! chan))))
(worker/watch worker chan true))))
;; need to set the var immediately since some cider middleware needs it to
;; switch REPL type to cljs. can't get to the nrepl session from here.
(when-let [pvar (find-var 'cemerick.piggieback/*cljs-compiler-env*)]
(.set pvar
(reify
clojure.lang.IDeref
(deref [_]
(when-let [worker (get-worker id)]
(-> worker :state-ref deref :build-state :compiler-env))))))
;; Cursive uses this to switch repl type to cljs
(println "To quit, type: :cljs/quit")
[:selected id])))))
(defn select-cljs-runtime [worker]
(let [all (-> worker :state-ref deref :runtimes vals vec)]
(println (format "There are %d connected runtimes, please select one by typing the number and pressing enter." (count all)))
(println "Type x or q to quit")
(doseq [[idx runtime] (->> all
(map :runtime-info)
(map-indexed vector))]
(prn [idx runtime]))
(let [num (read)]
(cond
(number? num)
(get all num)
(= num 'x)
{:quit true}
(= num 'q)
{:quit true}
:else
(recur worker)
))))
(defn repl-next
([build-id]
(repl-next build-id {}))
([build-id {:keys [stop-on-eof] :as opts}]
(if *nrepl-active*
(nrepl-select build-id opts)
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)
worker
(super/get-worker supervisor build-id)]
(if-not worker
:no-worker
(let [current-runtimes
(-> worker :state-ref deref :runtimes vals)
{:keys [quit runtime-id runtime-info] :as runtime}
(if (= 1 (count current-runtimes))
(-> current-runtimes first)
(select-cljs-runtime worker))]
(when-not quit
(when runtime-id
(println (str "Connecting to REPL runtime: " runtime-id)))
;; " " (pr-str runtime-info)
(repl-impl/stdin-takeover! worker app runtime-id)
(when stop-on-eof
(super/stop-worker supervisor build-id)))))))))
(defn repl
([build-id]
(repl build-id {}))
([build-id {:keys [stop-on-eof] :as opts}]
(if *nrepl-active*
(nrepl-select build-id opts)
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)
worker
(super/get-worker supervisor build-id)]
(if-not worker
:no-worker
(do (repl-impl/stdin-takeover! worker app nil)
(when stop-on-eof
(super/stop-worker supervisor build-id))))))))
;; FIXME: should maybe allow multiple instances
(defn node-repl
([]
(node-repl {}))
([opts]
(let [{:keys [supervisor] :as app}
(runtime/get-instance!)
was-running?
(worker-running? node-repl)
worker
(or (super/get-worker supervisor :node-repl)
(repl-impl/node-repl* app opts))]
(repl :node-repl {:skip-repl-out (not was-running?)})
)))
;; FIXME: should maybe allow multiple instances
(defn start-browser-repl* [{:keys [config supervisor] :as app}]
(let [cfg
{:build-id :browser-repl
:target :browser
:output-dir (str (:cache-root config) "/builds/browser-repl/js")
:asset-path "/cache/browser-repl/js"
:modules
{:repl {:entries '[shadow.cljs.devtools.client.browser-repl]}}
:devtools
{:autoload false}}]
(super/start-worker supervisor cfg)))
(defn browser-repl
([]
(browser-repl {}))
([{:keys [verbose open] :as opts}]
(let [{:keys [supervisor config http] :as app}
(runtime/get-instance!)
worker
(or (super/get-worker supervisor :browser-repl)
(let [worker
(start-browser-repl* app)
out-chan
(-> (async/sliding-buffer 10)
(async/chan))]
(go (loop []
(when-some [msg (<! out-chan)]
(try
(util/print-worker-out msg verbose)
(catch Exception e
(prn [:print-worker-out-error e])))
(recur)
)))
(worker/watch worker out-chan)
(worker/compile! worker)
(browse-url (str "http" (when (:ssl http) "s") "://localhost:" (:port http) "/browser-repl-js"))
worker))]
(repl :browser-repl)
)))
(defn dev*
[{:keys [build-id] :as build-config} {:keys [autobuild verbose] :as opts}]
(if (worker-running? build-id)
:already-watching
(let [config
(config/load-cljs-edn)
{:keys [supervisor] :as app}
(runtime/get-instance!)
out
(util/stdout-dump verbose)
worker
(-> (start-worker build-config opts)
(worker/watch out false)
(cond->
(not (false? autobuild))
(-> (worker/start-autobuild)
(worker/sync!))))]
;; for normal REPL loops we wait for the CLJS loop to end
(when-not *nrepl-active*
(repl-impl/stdin-takeover! worker app nil)
(super/stop-worker supervisor (:build-id build-config)))
:done)))
(defn dev
([build]
(dev build {}))
([build opts]
(try
(let [build-config (config/get-build! build)]
(dev* build-config opts))
(catch Exception e
(e/user-friendly-error e)))))
(defn help []
(-> (slurp (io/resource "shadow/txt/repl-help.txt"))
(println)))
(defn find-resources-using-ns [ns]
(let [{:keys [classpath]} (get-runtime!)]
(->> (cp/find-resources-using-ns classpath ns)
(map :ns)
(into #{}))))
(defn test []
(println "TBD"))
(defn release-snapshot [& args]
(println "release-snapshot was moved!")
(println "from the CLI use:" )
(println "\tshadow-cljs run shadow.cljs.build-report <build-id> some.html")
(println "from the REPL use:")
(println "\t(require '[shadow.cljs.build-report :as r])")
(println "\t(r/generate :build-id {:report-file \"some.html\"})")
(println "both commands generate a standalone \"some.html\" file with the full report"))
(comment
(release-snapshot :browser {})
(defn node-execute! [node-args file]
(let [script-args
["node"]
pb
(doto (ProcessBuilder. script-args)
(.directory nil))]
;; not using this because we only get output once it is done
;; I prefer to see progress
;; (prn (apply shell/sh script-args))
(let [node-proc (.start pb)]
(.start (Thread. (bound-fn [] (util/pipe node-proc (.getInputStream node-proc) *out*))))
(.start (Thread. (bound-fn [] (util/pipe node-proc (.getErrorStream node-proc) *err*))))
(let [out (.getOutputStream node-proc)]
(io/copy (io/file file) out)
(.close out))
;; FIXME: what if this doesn't terminate?
(let [exit-code (.waitFor node-proc)]
exit-code))))
(defn test-all []
(-> (build/configure :dev '{:build-id :shadow-build-api/test
:target :node-script
:main shadow.test-runner/main
:output-to "target/shadow-test-runner.js"
:hashbang false})
(node/make-test-runner)
(build/compile)
(build/flush))
(node-execute! [] "target/shadow-test-runner.js")
::test-all)
(defn test-affected
[source-names]
{:pre [(seq source-names)
(not (string? source-names))
(every? string? source-names)]}
(-> (test-setup)
(node/execute-affected-tests! source-names))
::test-affected))
(defn- find-local-addrs []
(for [ni (enumeration-seq (NetworkInterface/getNetworkInterfaces))
:when (not (.isLoopback ni))
:when (.isUp ni)
:let [display-name (.getDisplayName ni)]
:when
(and (not (str/includes? display-name "VirtualBox"))
(not (str/includes? display-name "vboxnet"))
(not (str/includes? display-name "utun"))
(not (re-find #"tun\d+" display-name)))
addr (enumeration-seq (.getInetAddresses ni))
;; probably don't need ipv6 for dev
:when (instance? Inet4Address addr)]
[ni addr]))
;; (prn (find-local-addrs))
(defn get-server-addr []
(let [addrs (find-local-addrs)]
(when (seq addrs)
(let [[ni addr] (first addrs)]
(comment
(println (format "shadow-cljs - Using IP \"%s\" from Interface \"%s\"" (.getHostAddress addr) (.getDisplayName ni)))
(when (not= 1 (count addrs))
(log/infof "Found multiple IPs, might be using the wrong one. Please report all interfaces should the chosen one be incorrect.")
(doseq [[ni addr] addrs]
(log/infof "Found IP:%s Interface:%s" (.getHostAddress addr) (.getDisplayName ni)))))
;; would be neat if we could just use InetAddress/getLocalHost
;; but that returns my VirtualBox Adapter for some reason
;; which is incorrect and doesn't work
(.getHostAddress addr)))))