src/pub/run.bend
ezx@1.5.0.0 · 112 lines · 0 definitions
1# pub/run: the interpreter for `ez publish`. It reads the directory the
2# project is in and the ledger's text into a World, then loops: it asks the
3# planner (pub/plan.bend) what is still open, answers each question, and adds
4# the answers to the World, until nothing is left to ask. Then it runs the
5# planner's plan with the lock's effect runner (`Run.exec.plan`), which prints
6# what a publish that agreed says, or exits 1 with the reason.
7#
8# What git says of the project and a file's text are answered as the tool
9# commands answer them (`ToolRun.answer`). What git tracks is `git ls-files
10# -v` in the project, with paths written as they are rather than quoted. The
11# upload is `bend <entry> --publish`, followed by the `<name>@<version>` the
12# planner put in the question when the ledger names one, with the project's
13# packages on BEND_LIB, the way every other `bend` ez starts has them; the
14# planner asks for it last, once every other check has passed. Just before it
15# runs, the line the question carries, the hub description, goes to stderr,
16# where bend's own notices go, so stdout stays the hash and the import line.
17# Before that, the lock is read as a file, and a url under the hub is asked
18# for with the lock's own GET (`Web.get`), whose answer is a status line and
19# the text: whether the hub answered 200, and the body or why not.
20# It decides nothing; that it reads and executes faithfully is EZ-TRUST-2.
21import Base
22import snap@1.1.0.0/main.bend as R
23import ezx@1.5.0.0/src/io/file.bend as F
24import ezx@1.5.0.0/src/lock/run.bend as Run
25import ezx@1.5.0.0/src/tool/run.bend as ToolRun
26import ezx@1.5.0.0/src/tool/world.bend as TW
27import ezx@1.5.0.0/src/hub/hub.bend as Web
28import ezx@1.5.0.0/src/hub/get.bend as Net
29import ezx@1.5.0.0/src/share/env.bend as Env
30import ezx@1.5.0.0/src/pub/world.bend as PW
31import ezx@1.5.0.0/src/pub/plan.bend as PP
32
33# what a program printed, and whether it exited 0
34def ran(+out: String) -> PW.Answer:
35 PW.Ran{R.ok(out), R.text(out)}
36
37# one question, answered
38def answer(+lib: String, ask: PW.Ask) -> IO(PW.Answer):
39 match ask:
40 case PW.Tool{q}:
41 do IO<PW.Answer>:
42 a : TW.Answer <- ToolRun.answer(q)
43 return PW.Told{a}
44 case PW.Tracked{}:
45 do IO<PW.Answer>:
46 +out : String <- R.exec(["git", "-c", "core.quotePath=false", "ls-files", "-v"])
47 return ran(out)
48 case PW.Upload{+entry, +named, says}:
49 do IO<PW.Answer>:
50 IO.print_err(says)
51 +out : String <- R.exec(Env.line(lib, PP.upload.line(entry, named)))
52 return ran(out)
53 case PW.Lock{}:
54 do IO<PW.Answer>:
55 m : Maybe<&2, String> <- F.read(PW.lockfile())
56 return PW.Told{TW.Text{m}}
57 case PW.Hub{+url}:
58 do IO<PW.Answer>:
59 +out : String <- Net.get(url)
60 return ran(out)
61
62# every question, answered
63def answer.all(+lib: String, asks: List<&2, PW.Ask>) -> IO(List<&2, PW.Reply>):
64 match asks:
65 case []:
66 IO.pure(List<&2, PW.Reply>, [])
67 case +h <> t:
68 do IO<List<&2, PW.Reply>>:
69 a : PW.Answer <- answer(lib, h)
70 rest : List<&2, PW.Reply> <- answer.all(lib, t)
71 return PW.Reply{h, a} <> rest
72
73# the World with more answers in it
74def more(world: PW.World, rs: List<&2, PW.Reply>) -> PW.World:
75 PW.World{here, ledger, replies} = world
76 PW.World{here, ledger, List.append(&2, PW.Reply, replies, rs)}
77
78# the planner's step: its questions answered and the loop gone round again,
79# or its plan run
80def loop.step(st: PP.Step, +lib: String, world: PW.World, go: PW.World -> IO(Unit)) -> IO(Unit):
81 match st:
82 case PP.Asking{asks}:
83 do IO<Unit>:
84 rs : List<&2, PW.Reply> <- answer.all(lib, asks)
85 go(more(world, rs))
86 case PP.Run{pl}:
87 Run.exec.plan(lib, pl)
88
89# the rounds the loop may take. The walk asks for one file a round and cannot
90# know how many the package holds until it has read them, so this is a guard
91# against a planner that keeps asking, not a limit on a package: running out
92# of it dies loudly.
93def rounds() -> Nat:
94 U32.to_nat(100000)
95
96# the loop, under fuel. The walk asks for one file a round, so the fuel is a
97# guard against a planner that keeps asking, not a limit on a package.
98def loop(fuel: Nat, +lib: String, +world: PW.World) -> IO(Unit):
99 match fuel:
100 case 0n:
101 IO.die(Unit, 1, "ez: ez publish kept asking questions")
102 case 1n+f:
103 loop.step(PP.step(world), lib, world, w => loop(f, lib, w))
104
105# `ez publish`: the package the project's ledger names, checked, sent, and
106# the hash bend answered checked against ez's own
107def run() -> IO(Unit):
108 do IO<Unit>:
109 lib : String <- Env.lib()
110 +pwd : String <- R.exec(["pwd"])
111 led : Maybe<&2, String> <- F.read(PW.toml())
112 loop(rounds(), lib, PW.World{String.trim(R.text(pwd)), led, []})