src/ws/hop.bend
ezx@1.11.0.0 · 113 lines · 12 definitions
1
2
3
4
5
6
7import Base
8import ezx@1.11.0.0/src/pkg/path.bend as P
9import ezx@1.11.0.0/src/ws/workspace.bend as W
10import ezx@1.11.0.0/src/ws/root.bend as R
11
12
13
14type Hop is Data:
15 Here{}
16 Hop{dir: String}
17 Stop{why: String}
18
19
20def hop.within(+top: String, root: List<&2, String>, rel: List<&2, String>) -> Hop:
21 match rel:
22 case []:
23 Here{}
24 case _hd <> _tl:
25 Hop{P.join(top, W.segs.text(root))}
26
27
28
29def hop(+top: String, found: R.Found) -> Hop:
30 match found:
31 case R.Alone{}:
32 Here{}
33 case R.Within{root, rel}:
34 hop.within(top, root, rel)
35 case R.Broken{why}:
36 Stop{why}
37
38
39
40
41def program(+pwd: String, +prog: String) -> String:
42 Bool.pick(String, String.contains(prog, "/"), P.anchor(pwd, prog), prog)
43
44
45
46
47def line(+dir: String, +pwd: String, +prog: String, words: List<&2, String>) -> List<&2, String>:
48 List.append(&2, String,
49 ["sh", "-c", "cd -- \"$1\" && shift && exec \"$@\"", "sh", dir, program(pwd, prog)], words)
50
51
52
53
54
55
56
57type Lib is Data:
58 Keep{}
59 Set{lib: String}
60
61
62def lib.within(+top: String, root: List<&2, String>, rel: List<&2, String>) -> Lib:
63 match rel:
64 case []:
65 Keep{}
66 case _hd <> _tl:
67 Set{P.join(P.join(top, W.segs.text(root)), ".ez/lib")}
68
69
70def lib.found(set: Bool, +top: String, found: R.Found) -> Lib:
71 match set:
72 case True{}:
73 Keep{}
74 case False{}:
75 match found:
76 case R.Alone{}:
77 Keep{}
78 case R.Within{root, rel}:
79 lib.within(top, root, rel)
80 case R.Broken{_why}:
81 Keep{}
82
83
84
85def lib(+top: String, found: R.Found, +env: String) -> Lib:
86 lib.found(Bool.not(String.is_empty(env)), top, found)
87
88
89def lib.line(+lib: String, +pwd: String, +prog: String, words: List<&2, String>) -> List<&2, String>:
90 List.append(&2, String,
91 ["sh", "-c", "BEND_LIB=\"$1\"; export BEND_LIB; shift; exec \"$@\"", "sh", lib, program(pwd, prog)], words)
92
93
94
95
96
97def lock.within(+top: String, root: List<&2, String>, rel: List<&2, String>) -> String:
98 match rel:
99 case []:
100 "ez.lock.toml"
101 case _hd <> _tl:
102 P.join(P.join(top, W.segs.text(root)), "ez.lock.toml")
103
104
105
106def lock(+top: String, found: R.Found) -> String:
107 match found:
108 case R.Alone{}:
109 "ez.lock.toml"
110 case R.Within{root, rel}:
111 lock.within(top, root, rel)
112 case R.Broken{_why}:
113 "ez.lock.toml"