src/tool/world.bend
ezx@1.11.0.0 · 232 lines · 37 definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import Base
18import ezx@1.11.0.0/src/lock/up.bend as Up
19import ezx@1.11.0.0/src/ledger/manifest.bend as M
20import ezx@1.11.0.0/src/pkg/path.bend as P
21
22
23
24type Act is Data:
25 Run{args: List<&2, String>}
26 Install{}
27 Upgrade{}
28
29
30type Env is Data:
31 Env{tool_bin: String, xdg_bin: String, xdg_cache: String, home: String, path: String}
32
33
34type Ask is Data:
35 Read{path: String}
36 Exec{path: String}
37 Locate{path: String}
38
39
40
41
42
43type Answer is Data:
44 Plain{got: Maybe<&2, String>}
45 Is{yes: Bool}
46 Located{abs: String, top: String, head: String, ok: Bool, status: String}
47 Miss{why: String}
48
49
50type Reply is Data:
51 Reply{ask: Ask, answer: Answer}
52
53
54type World is Data:
55 World{act: Act, target: String, want: String, here: String, env: Env, bend: String,
56 ledger: Maybe<&2, String>, lock: Maybe<&2, String>, replies: List<&2, Reply>,
57 ups: List<&2, Up.Reply>}
58
59
60def act(world: World) -> Act:
61 World{a, _t, _want, _here, _env, _bend, _ledger, _lock, _rs, _ups} = world
62 a
63
64
65def target(world: World) -> String:
66 World{_a, t, _want, _here, _env, _bend, _ledger, _lock, _rs, _ups} = world
67 t
68
69
70def want(world: World) -> String:
71 World{_a, _t, w, _here, _env, _bend, _ledger, _lock, _rs, _ups} = world
72 w
73
74
75def here(world: World) -> String:
76 World{_a, _t, _want, h, _env, _bend, _ledger, _lock, _rs, _ups} = world
77 h
78
79
80def env(world: World) -> Env:
81 World{_a, _t, _want, _here, e, _bend, _ledger, _lock, _rs, _ups} = world
82 e
83
84
85def bend(world: World) -> String:
86 World{_a, _t, _want, _here, _env, b, _ledger, _lock, _rs, _ups} = world
87 b
88
89
90def ledger(world: World) -> Maybe<&2, String>:
91 World{_a, _t, _want, _here, _env, _bend, l, _lock, _rs, _ups} = world
92 l
93
94
95def lock(world: World) -> Maybe<&2, String>:
96 World{_a, _t, _want, _here, _env, _bend, _ledger, l, _rs, _ups} = world
97 l
98
99
100def replies(world: World) -> List<&2, Reply>:
101 World{_a, _t, _want, _here, _env, _bend, _ledger, _lock, rs, _ups} = world
102 rs
103
104
105def ups(world: World) -> List<&2, Up.Reply>:
106 World{_a, _t, _want, _here, _env, _bend, _ledger, _lock, _rs, us} = world
107 us
108
109def env.tool_bin(vars: Env) -> String:
110 Env{b, _x, _c, _h, _p} = vars
111 b
112
113def env.xdg_bin(vars: Env) -> String:
114 Env{_b, x, _c, _h, _p} = vars
115 x
116
117def env.xdg_cache(vars: Env) -> String:
118 Env{_b, _x, c, _h, _p} = vars
119 c
120
121def env.home(vars: Env) -> String:
122 Env{_b, _x, _c, h, _p} = vars
123 h
124
125def env.path(vars: Env) -> String:
126 Env{_b, _x, _c, _h, p} = vars
127 p
128
129
130def home(world: World) -> String:
131 env.home(env(world))
132
133
134
135
136
137def bin.home(+home: String) -> Maybe<&2, String>:
138 Bool.pick(Maybe<&2, String>, String.is_empty(home), None{}, Some{home ++ "/.local/bin"})
139
140
141def bin.xdg(+xdg: String, +home: String) -> Maybe<&2, String>:
142 Bool.pick(Maybe<&2, String>, String.is_empty(xdg), bin.home(home), Some{xdg})
143
144
145
146
147def bin.dir(+ez: String, +xdg: String, +home: String) -> Maybe<&2, String>:
148 Bool.pick(Maybe<&2, String>, String.is_empty(ez), bin.xdg(xdg, home), Some{ez})
149
150
151def bin.of(vars: Env) -> Maybe<&2, String>:
152 Env{+b, +x, _c, +h, _p} = vars
153 bin.dir(b, x, h)
154
155
156
157
158def cache.home(+here: String, +home: String) -> Maybe<&2, String>:
159 Bool.pick(Maybe<&2, String>, String.is_empty(home), None{},
160 Some{P.anchor(here, home) ++ "/.cache/ez/tool"})
161
162def cache.dir(+here: String, +xdg: String, +home: String) -> Maybe<&2, String>:
163 Bool.pick(Maybe<&2, String>, String.is_empty(xdg), cache.home(here, home),
164 Some{P.anchor(here, xdg) ++ "/ez/tool"})
165
166
167def cache.of(world: World) -> Maybe<&2, String>:
168 World{_a, _t, _want, +here, vars, _bend, _ledger, _lock, _rs, _ups} = world
169 Env{_b, _x, +c, +h, _p} = vars
170 cache.dir(here, c, h)
171
172
173def text.or(got: Maybe<&2, String>) -> String:
174 match got:
175 case None{}:
176 ""
177 case Some{t}:
178 t
179
180
181
182
183
184
185def project.of(got: Maybe<&2, String>) -> M.Read:
186 match got:
187 case None{}:
188 M.Good{M.Manifest{"", "", "", "", "", "", [], []}}
189 case Some{text}:
190 M.parse(text)
191
192
193def project(world: World) -> M.Read:
194 project.of(ledger(world))
195
196
197
198
199
200def key(ask: Ask) -> String:
201 match ask:
202 case Read{path}:
203 "read\n" ++ path
204 case Exec{path}:
205 "exec\n" ++ path
206 case Locate{path}:
207 "locate\n" ++ path
208
209
210type Heard is Data:
211 Heard{answer: Answer}
212 Open{}
213
214
215def look.step(hit: Bool, answer: Answer, rest: Unit -> Heard) -> Heard:
216 match hit:
217 case True{}:
218 Heard{answer}
219 case False{}:
220 rest(Unit{})
221
222
223def look(rs: List<&2, Reply>, +want: String) -> Heard:
224 match rs:
225 case []:
226 Open{}
227 case Reply{ask, answer} <> t:
228 look.step(String.eq(key(ask), want), answer, _u => look(t, want))
229
230
231def heard(+world: World, ask: Ask) -> Heard:
232 look(replies(world), key(ask))