src/fetch/run.bend
ezx@1.11.0.0 · 128 lines · 11 definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import Base
17import snap@1.4.0.0/main.bend as R
18import ezx@1.11.0.0/src/io/file.bend as F
19import ezx@1.11.0.0/src/lock/run.bend as Run
20import ezx@1.11.0.0/src/lock/world.bend as W
21import ezx@1.11.0.0/src/lock/up.bend as Up
22import ezx@1.11.0.0/src/fetch/world.bend as FW
23import ezx@1.11.0.0/src/fetch/plan.bend as FP
24
25
26
27def laid.seen(got: Maybe<&2, String>, +dir: String) -> IO(W.Answer):
28 match got:
29 case None{}:
30 IO.pure(W.Answer, W.Miss{"ez: " ++ dir ++ " is not there"})
31 case Some{text}:
32 Run.answer.lib(dir, text)
33
34
35
36def laid(+lib: String, +hash: String) -> IO(W.Answer):
37 +dir = lib ++ "/" ++ hash
38 do IO<W.Answer>:
39 m : Maybe<&2, String> <- F.read(dir ++ "/manifest")
40 laid.seen(m, dir)
41
42
43def answer(+lib: String, ask: FW.Ask) -> IO(FW.Answer):
44 match ask:
45 case FW.Laid{hash}:
46 do IO<FW.Answer>:
47 a : W.Answer <- laid(lib, hash)
48 return FW.Read{a}
49 case FW.Git{_hash, q}:
50 do IO<FW.Answer>:
51 a : Up.Answer <- Run.answer.up(lib, q)
52 return FW.Said{a}
53 case FW.Hub{q}:
54 do IO<FW.Answer>:
55 a : W.Answer <- Run.answer(lib, q)
56 return FW.Read{a}
57 case FW.Named{nv}:
58 do IO<FW.Answer>:
59 m : Maybe<&2, String> <- F.read(lib ++ "/names/" ++ nv)
60 return FW.Held{m}
61 case FW.Tree{_hash, _entry}:
62 do IO<FW.Answer>:
63 l : W.Listing <- Run.read.listing()
64 return FW.Seen{l}
65
66
67def answer.all(+lib: String, asks: List<&2, FW.Ask>) -> IO(List<&2, FW.Reply>):
68 match asks:
69 case []:
70 IO.pure(List<&2, FW.Reply>, [])
71 case +h <> t:
72 do IO<List<&2, FW.Reply>>:
73 a : FW.Answer <- answer(lib, h)
74 rest : List<&2, FW.Reply> <- answer.all(lib, t)
75 return FW.Reply{h, a} <> rest
76
77
78def more(world: FW.World, rs: List<&2, FW.Reply>) -> FW.World:
79 FW.World{here, ledger, lock, replies} = world
80 FW.World{here, ledger, lock, List.append(&2, FW.Reply, replies, rs)}
81
82
83
84def loop.step(st: FP.Step, +lib: String, world: FW.World, go: FW.World -> IO(Unit)) -> IO(Unit):
85 match st:
86 case FP.Asking{asks}:
87 do IO<Unit>:
88 rs : List<&2, FW.Reply> <- answer.all(lib, asks)
89 go(more(world, rs))
90 case FP.Run{pl}:
91 Run.exec.plan(lib, pl)
92
93
94
95
96
97def rounds() -> Nat:
98 U32.to_nat(10)
99
100
101
102
103def loop(fuel: Nat, +lib: String, +world: FW.World) -> IO(Unit):
104 match fuel:
105 case 0n:
106 IO.die(Unit, 1, "ez: ez fetch kept asking questions")
107 case 1n+f:
108 loop.step(FP.step(world), lib, world, w => loop(f, lib, w))
109
110
111def at(+here: String, +file: String) -> String:
112 here ++ "/" ++ file
113
114
115
116
117def fill(+here: String, +lib: String) -> IO(Unit):
118 do IO<Unit>:
119 led : Maybe<&2, String> <- F.read(at(here, FW.toml()))
120 lk : Maybe<&2, String> <- F.read(at(here, FW.lockfile()))
121 loop(rounds(), lib, FW.World{here, Maybe.is_some(&2, String, led), lk, []})
122
123
124def run() -> IO(Unit):
125 do IO<Unit>:
126 lib : String <- Run.bend.lib()
127 +pwd : String <- R.exec(["pwd"])
128 fill(String.trim(R.text(pwd)), lib)