-
Notifications
You must be signed in to change notification settings - Fork 93
/
proc.cljc
66 lines (56 loc) · 1.99 KB
/
proc.cljc
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
(ns ^:no-doc etaoin.impl.proc
(:require
[babashka.fs :as fs]
#?@(:bb [[babashka.process :as p]]
:clj [[babashka.process :as p]
[babashka.process.pprint]]) ;; to support exception rendering in REPL
[clojure.string :as str]))
(set! *warn-on-reflection* true)
(def windows? (str/starts-with? (System/getProperty "os.name") "Windows"))
(defn- get-null-file ^java.io.File
[]
(if windows?
(fs/file "NUL")
(fs/file "/dev/null")))
(defn- redirect-opts [val std-key std-file-key]
(cond-> {}
(= :inherit val)
(assoc std-key :inherit)
(string? val)
(assoc std-key :write
std-file-key (fs/file val))
(nil? val)
(assoc std-key :write
std-file-key (fs/file (get-null-file)))))
(defn run
([args] (run args {}))
([args {:keys [log-stdout log-stderr env]}]
(let [p-opts (cond-> (redirect-opts log-stdout :out :out-file)
:always (merge (redirect-opts log-stderr :err :err-file))
env (assoc :extra-env env))
binary (first args)
user-guide-link "https://github.com/clj-commons/etaoin/blob/master/doc/01-user-guide.adoc#installing-the-browser-webdrivers"]
(try
(p/process args p-opts)
(catch Throwable e
;; not sure if folks will see this helpful message...
(throw (ex-info
(format "Failed to launch WebDriver binary `%s`.
Please ensure you have the driver installed.
If it is not on the PATH specify its location.
For driver installation, check out the Etaoin user guide: %s" binary user-guide-link)
{:args args} e)))))))
(defn kill
"Ask `p` to die. Use [[result]] to get exit code if you need it."
[p]
(p/destroy-tree p)
@p)
(defn result
"Call after killing to get result of `p`.
If you call before killing you'll wait until p dies naturally, which a WebDriver should not do, so don't do that."
[p]
@p)
(defn alive?
"Check if `p` has died unexpectedly, use [[result]] to get result."
[p]
(p/alive? p))