src/share/env.bend

ezx@1.8.0.0 · 44 lines · 0 definitions

1# share/env: the environment ez hands to the programs it runs. Vendored packages
2# live with the project rather than in ~/.bend/lib, so every `bend` ez starts
3# has to be told where they are. Base has `get_env` and no `set_env`, and the
4# one foreign effect execvp's what it is given, so the variable rides in front
5# of the command as `env BEND_LIB=... bend ...`. `env` is coreutils and is
6# execvp'd like any other program, so no shell is involved.
7import Base
8import ezx@1.8.0.0/src/share/args.bend as Args
9
10# a value, or the fallback when the variable was unset or empty
11def or_else(+value: String, +alt: String) -> String:
12  Bool.pick(String, String.is_empty(value), alt, value)
13
14# what a variable holds, with a default for when it holds nothing
15def var(name: String, +alt: String) -> IO(String):
16  do IO<String>:
17    r : Result<&1, &1, U32 & String, String> <- IO.get_env(name)
18    return or_else(Args.text_of(r), alt)
19
20# where this project's packages live
21def lib() -> IO(String):
22  var("BEND_LIB", ".ez/lib")
23
24# the command with the project's BEND_LIB in front of it
25def line(at: String, args: List<&2, String>) -> List<&2, String>:
26  List.append(&2, String, ["env", "BEND_LIB=" ++ at], args)
27
28# the directory `ez build` writes into: `bin` when it was not told where
29def dirs.build(build: Bool, bare: Bool) -> List<&2, String>:
30  match build:
31    case True{}:
32      Bool.pick(List<&2, String>, bare, ["bin"], [])
33    case False{}:
34      []
35
36# the directories bend writes into for a command and does not make itself:
37# `.ez` for `ez check`'s JS, and `bin` for `ez build`'s default output. They
38# are made just before bend starts, so a check or build that is refused makes
39# none. Every other command makes what it writes into when it writes, and
40# `ez help`, `doctor` and the rest write nothing here, so they leave a
41# directory as they found it.
42def dirs(+cmd: String, +out: String) -> List<&2, String>:
43  Bool.pick(List<&2, String>, String.eq(cmd, "check"), [".ez"],
44    dirs.build(String.eq(cmd, "build"), String.is_empty(out)))