~subsetpark/bagatto

ref: 37b87a808c37b933f5d830bff18d352130f64c06 bagatto/src/env.janet -rw-r--r-- 1.3 KiB
37b87a80 — Zach Smith Add thread init test 4 months ago
                                                                                
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
(import temple)

(def required-checks @{})
(def exec-blacklist @{})

(defmacro defrequire
  ```
  Define a function, preceded by the name of an external program. If
  this program is not present when Bagatto is run, this function will
  raise an error.
  ```
  [program name args & body]
  ~((put env/required-checks ,program true)
     (defn ,name
       ,args
       (when (in (dyn :executable-blacklist) ,program)
         (error (string "Required executable not installed: " ,program)))
       ,;body)))

(defn- on-path?
  [prog]
  (let [f (file/open "/dev/null" :w)]
    (case (os/execute ["which" prog] :p {:out f :err f})
      0 true
      false)))

(defn- prepare-syspath! []
  (match (os/getenv "JANET_PATH")
    nil :ok
    janet-path (put root-env :syspath janet-path)))

(defn- populate-exec-blacklist!
  ```
  Iterate through all the executables declared with `defrequire` and
  check for their existence in the path. Add a blacklist of all
  executables that are absent from the path to the given environment.
  ```
  []
  (loop [check :keys required-checks]
    (unless (on-path? check)
      (put exec-blacklist check true))))

(defn setdyn-blacklist! [] (setdyn :executable-blacklist exec-blacklist))

(defn prepare-root-env!
  []
  (prepare-syspath!)
  (temple/add-loader)
  (populate-exec-blacklist!))