src/add/run.bend source
src/add/run.bend on the hub · documented module
# add/run: the interpreter for `ez add`. It reads the directory it runs in,# HOME, ez.toml, `.gitignore` and the `.bend` files git lists into a World,# then loops: it asks the planner (add/relock.bend) what is still open,# answers each question with the lock's interpreter (`Run.answer.ups`: what# git prints for tags, HEAD or a ref, and a checkout at a rev with its NAR# hash and every file's text; `Run.answer.all`: for a hub package's name,# what the hub says the name names, and the package's manifest and files,# and for the lock that follows the add, each package and name it reads, as# `ez lock` answers them), and adds the answers to the World, until nothing# is left to ask. Then it runs the planner's plan with the lock's effect# runner (`Run.exec.plan`), which exits 1 with the reason when the plan# refuses. It decides nothing; that it reads and executes faithfully is# trusted in ez/app.bend.import Baseimport snap@1.4.0.0/main.bend as Rimport ../io/file.bend as Fimport ../lock/run.bend as Runimport ../lock/up.bend as Upimport ../lock/world.bend as Wimport ./world.bend as Aimport ../ws/run.bend as WsRunimport ./relock.bend as ARimport ../ws/relock.bend as Rel# the planner's step: its questions answered and the loop gone round again,# or its plan rundef loop.step(st: AR.Next, +lib: String, world: AR.World, go: AR.World -> IO(Unit)) -> IO(Unit): match st: case AR.AskGit{asks}: do IO<Unit>: rs : List<&2, Up.Reply> <- Run.answer.ups(lib, asks) go(AR.more(world, rs)) case AR.AskHub{asks}: do IO<Unit>: rs : List<&2, W.Reply> <- Run.answer.all(lib, asks) go(AR.more.hub(world, rs)) case AR.AskLock{asks}: do IO<Unit>: rs : List<&2, W.Reply> <- Run.answer.all(lib, asks) go(AR.more.lock(world, rs)) case AR.Planned{pl}: Run.exec.plan(lib, pl)# the rounds the loop may take. The add asks at most three rounds for a git# target and two for a name; the lock after it asks a round per level of the# import graph, as `ez lock` does (`Run.rounds`). This is only a guard against# a planner that keeps asking, and running out of it dies loudly.def rounds() -> Nat: # noqa: L001 the IO loop's fuel 100000n# the loop, under fuel. Every round adds an answer, since the planner never# asks what the World answers already, so the fuel is only a guard.def loop( # noqa: L001 IO driver: the interpreter's loop fuel: Nat, +lib: String, +ws: Rel.Ws, +world: AR.World) -> IO(Unit): match fuel: case 0n: IO.die(Unit, 1, "ez: ez add kept asking questions") case 1n+f: loop.step(Rel.add.next(ws, world), lib, world, w => loop(f, lib, ws, w))# `ez add <target> [ref] [entry.bend] [--rename NAME]`def run( # noqa: L001 IO entry of the command +target: String, +ref: String, +entry: String, +rename: String) -> IO(Unit): do IO<Unit>: lib : String <- Run.bend.lib() +pwd : String <- R.exec(["pwd"]) h : Result<&1, &1, U32 & String, String> <- IO.get_env("HOME") +home : String <- IO.pure(String, Run.env.text(h)) pinned : String <- WsRun.add.ref(home, target, ref) led : Maybe<&2, String> <- F.read(A.toml()) ig : Maybe<&2, String> <- F.read(A.ignore.file()) ls : W.Listing <- Run.read.listing() ws : Rel.Ws <- WsRun.relock.ws() loop(rounds(), lib, ws, AR.World{A.World{A.Args{target, pinned, entry, rename}, String.trim(R.text(pwd)), home, led, F.text_of(ig), [], []}, ls, []})