~/bend-docscommunity

src/add/relock.bend source

src/add/relock.bend on the hub · documented module

# add/relock: `ez add` as it runs, the add and the lock in one plan# (laws/AddRemoveRelock.bend). The add is planned as before (`AP.command`,# add/plan.bend); once it is known to succeed, the plain `ez lock` planner# is run over the World it leaves (`R.world`, relock/relock.bend): the# ledger the add writes, the `.bend` files git lists, and the lock's# answers. The package the add walked is handed to the lock as the answer# for its hash, read as a tree under BEND_LIB, which is where the add lays# it, so it is not read twice; a hub package's answers are the ones the add# was given. Every other package the lock asks about is answered by the# lock's interpreter as `ez lock` answers it.## The plan, when both succeed: the add's effects, a note when the lock does# not hold the package added (laws/AddNotesUnlockedPackage.bend), then the# lock's effects, the lock last. When either refuses, a refusal with no# effect at all.import Baseimport ./world.bend as Aimport ./plan.bend as APimport ./hub.bend as Himport ../lock/plan.bend as Pimport ../lock/world.bend as Wimport ../lock/up.bend as Upimport ../lock/lock.bend as Limport ../ledger/manifest.bend as Mimport ../pkg/pkg.bend as Kimport ../relock/relock.bend as R# what `ez add` reads: the add's World, the `.bend` files git lists, and the# answers to the lock's questionstype World is Data:  World{add: A.World, listing: W.Listing, locks: List<&2, W.Reply>}# the add's Worlddef add(world: World) -> A.World:  World{a, _listing, _locks} = world  a# ---------------------------------------------------------------------------# the lock's World# this file's text when it is the one at the path, else the rest's. The rest# arrives as a thunk, so the first hit ends the scan.def found.pick(hit: Bool, body: String, rest: Unit -> String) -> String:  match hit:    case True{}:      body    case False{}:      rest(Unit{})# the text of the file at a path among a walk's files, "" when none is theredef text.at(fs: List<&2, K.Source>, +at: String) -> String:  match fs:    case []:      ""    case K.Source{+p, body} <> t:      found.pick(String.eq(p, at), body, _u => text.at(t, at))# the text of each file a manifest names, in its orderdef texts(+fs: List<&2, K.Source>, items: List<&2, K.Item>) -> List<&2, String>:  match items:    case []:      []    case K.Item{at, _sum} <> t:      text.at(fs, at) <> texts(fs, t)# the package a walk made, as the answer to the lock's question about its# hash: its manifest and the text of each file it names, read as a tree under# BEND_LIB, which is where the add lays it. The lock checks it as it checks# any tree there. A walk that made no package answers nothing.def seed.of(walk: K.Walked) -> List<&2, W.Reply>:  match walk:    case K.Walked{h, _root, +fs, ss}:      +manifest = K.manifest_of(ss)      [W.Reply{W.Pkg{h, L.Hub{}, ""},        W.Got{W.Lib{}, manifest, texts(fs, L.manifest.files(String.lines(manifest)))}}]    case K.Asks{_at}:      []    case K.Refused{_why}:      []# the answers the add already has for the lock: a hub package's are those the# hub gave the add, and a git package's is the package its walk madedef seeds.of(named: Bool, +aw: A.World) -> List<&2, W.Reply>:  match named:    case True{}:      A.hubs(aw)    case False{}:      seed.of(AP.walked(aw))# the answers the add already has for the lock, on the add's Worlddef seeds(+aw: A.World) -> List<&2, W.Reply>:  seeds.of(A.named(aw), aw)# the World a plain `ez lock` reads once the add has run: the ledger it# writes, the `.bend` files git lists, and the answers it was given, the add's# firstdef lock.world(world: World) -> W.World:  World{+a, listing, locks} = world  R.world(A.here(a), R.ledger(AP.command(a), A.ledger(a)), listing,    List.append(&2, W.Reply, seeds(a), locks))# the plan of that lockdef lock(world: World) -> P.Plan:  P.plan(lock.world(world))# ---------------------------------------------------------------------------# the note (laws/AddNotesUnlockedPackage.bend)# the hash of the package the add recordsdef added.of(named: Bool, +aw: A.World) -> String:  match named:    case True{}:      H.hash(aw)    case False{}:      AP.hash(aw)# the hash of the package the add records, on the add's Worlddef added(+aw: A.World) -> String:  added.of(A.named(aw), aw)# whether the lock holds a package under the hashdef held(ps: List<&2, L.Pack>, +hash: String) -> Bool:  match ps:    case []:      False{}    case h <> t:      L.any.step(String.eq(L.pack.hash(h), hash), _u => held(t, hash))# the line that says the lock does not hold itdef unlocked(+hash: String) -> String:  "ez: no .bend file git lists imports " ++ hash ++ " yet, so ez.lock.toml does " ++    "not hold it. Import it, `git add` the file, and run `ez lock`."# the note: the line when the lock does not hold the package, none when it doesdef notes.of(held: Bool, +hash: String) -> List<&2, P.Effect>:  match held:    case True{}:      []    case False{}:      [P.Say{unlocked(hash)}]# the note on a Worlddef notes(+world: World) -> List<&2, P.Effect>:  notes.of(held(P.packs(lock.world(world)), added(add(world))), added(add(world)))# ---------------------------------------------------------------------------# the plan# what is added after the lock's reason when the lock refuses: that nothing# was written, and, when the add moves a dependency's hash, where the old one# may still be imported fromdef moved.of(moved: Bool, +name: String, +old: String, +new: String) -> String:  match moved:    case True{}:      "\nez: ez add moves " ++ name ++ " from " ++ old ++ " to " ++ new ++        "; a .bend file that imports " ++ old ++ " has to name " ++ new ++        " first, or `ez lock --upgrade --package " ++ name ++ "` moves it"    case False{}:      ""def tail.dep(+dep: M.Dep, +old: M.Dep) -> String:  "\nez: ez add wrote nothing, since it writes ez.toml and ez.lock.toml together" ++    moved.of(Bool.and(Bool.not(String.is_empty(M.dep.hash(old))),      Bool.not(String.eq(M.dep.hash(old), M.dep.hash(dep)))),      M.dep.name(dep), M.dep.hash(old), M.dep.hash(dep))# the dependency the add recordsdef dep.of(named: Bool, +aw: A.World) -> M.Dep:  match named:    case True{}:      H.added(aw)    case False{}:      AP.added(aw)# what is added after the lock's reason, on the add's Worlddef tail(+aw: A.World) -> String:  +dep = dep.of(A.named(aw), aw)  tail.dep(dep, M.find(A.deps(aw), M.dep.name(dep)))# `ez add` over a World: the add, then the lock over what it leavesdef plan(world: World) -> P.Plan:  World{+a, +listing, +locks} = world  R.join(AP.command(a), lock(World{a, listing, locks}), notes(World{a, listing, locks}),    tail(a))# whether `ez add` refuses on a Worlddef refuses(world: World) -> Bool:  R.refuses(plan(world))# ---------------------------------------------------------------------------# the step# what the interpreter does next: answer git's questions or the hub's for the# add, answer the lock's, or run the plantype Next is Data:  AskGit{asks: List<&2, Up.Ask>}  AskHub{asks: List<&2, W.Ask>}  AskLock{asks: List<&2, W.Ask>}  Planned{plan: P.Plan}# the lock's step, once the add is planned and succeedsdef next.lock(st: P.Step, +world: World) -> Next:  match st:    case P.Asking{as, ups, ns}:      AskLock{R.asks(P.Asking{as, ups, ns})}    case P.Run{_pl}:      Planned{plan(world)}# the add's plan made: refused, it is the plan; otherwise the lock is nextdef next.made(refused: Bool, +world: World) -> Next:  match refused:    case True{}:      Planned{plan(world)}    case False{}:      next.lock(P.step(lock.world(world)), world)def next.add(nx: AP.Next, +world: World) -> Next:  match nx:    case AP.AskGit{asks}:      AskGit{asks}    case AP.AskHub{asks}:      AskHub{asks}    case AP.Planned{pl}:      next.made(R.refuses(pl), world)# what the interpreter does next on a Worlddef next(+world: World) -> Next:  next.add(AP.next(add(world)), world)