~/bend-docscommunity

src/ws/up.bend source

src/ws/up.bend on the hub · documented module

# ws/up: `ez lock --upgrade` at a workspace root# (laws/WorkspaceOnePinPerSource.bend). The upgrade is planned, as the lock# is, on the joint ledger (ws/joint.bend), so a source a name declares moves# once for the whole workspace. What it writes back is not the joint ledger,# which no file holds: the one write of ez.toml the upgrade plans is# replaced by a write of every ledger that declares a moved name, the root's# and each member's, each dependency and tool taking its new pin from the# joint ledger the upgrade leaves while keeping the source as that ledger# records it, relative to its own directory. Every other effect of the plan# is kept as it was.import Baseimport ../ledger/manifest.bend as Mimport ../ledger/render.bend as Rendimport ../toml/toml.bend as Timport ../lock/world.bend as LWimport ../lock/plan.bend as LPimport ../pkg/pkg.bend as Kimport ./workspace.bend as Wimport ./joint.bend as Jimport ./moved.bend as Mv# what the write-back needs: nothing, for a project of its own, or the# root's ledger text, what its `[workspace]` table says, every other ez.toml# git lists under the root, and the text of the lock being rewritten, "" for# none, which records each member's hash before it moved# (laws/HashedMemberMovesCascade.bend)type Back is Data:  Alone{}  Back{text: String, spec: W.Spec, found: List<&2, J.Found>, lock: String}# ---------------------------------------------------------------------------# a pin moved back into a ledger# a source with the pin the joint ledger now gives it: the joint's commit,# tag, root and narHash, and this ledger's own url and vendor bitdef source.back(own: M.Source, joint: M.Source) -> M.Source:  match own:    case M.Hub{nv}:      M.Hub{nv}    case M.Git{url, _rev, _tag, _root, _nar, vend}:      match joint:        case M.Hub{_jn}:          M.Git{url, _rev, _tag, _root, _nar, vend}        case M.Git{_ju, jr, jt, jo, jn, _jv}:          M.Git{url, jr, jt, jo, jn, vend}        case M.Member{_jp, _jm}:          M.Git{url, _rev, _tag, _root, _nar, vend}    case M.Member{path, named}:      M.Member{path, named}# a dependency with the pin the joint ledger gives its name, by its own# source: a workspace member's section is kept as it is written, since the# hash and entry the joint ledger gives it are no ledger's to recorddef dep.back.src(own: M.Source, +nn: String, +hh: String, +ee: String, joint: M.Dep) -> M.Dep:  match own:    case M.Member{path, nv}:      M.Dep{nn, hh, ee, M.Member{path, nv}}    case M.Hub{nv}:      M.Dep{_jn, jh, je, js} = joint      M.Dep{nn, jh, je, source.back(M.Hub{nv}, js)}    case M.Git{url, rev, tag, root, nar, vend}:      M.Dep{_jn, jh, je, js} = joint      M.Dep{nn, jh, je, source.back(M.Git{url, rev, tag, root, nar, vend}, js)}# a dependency with the pin the joint ledger gives its namedef dep.back.at(own: M.Dep, joint: M.Dep) -> M.Dep:  M.Dep{nn, hh, ee, os} = own  dep.back.src(os, nn, hh, ee, joint)# a dependency of a ledger, after the upgradedef dep.back(+own: M.Dep, +js: List<&2, M.Dep>) -> M.Dep:  dep.back.at(own, M.find(js, M.dep.name(own)))# every dependency of a ledger, after the upgradedef deps.back(ds: List<&2, M.Dep>, +js: List<&2, M.Dep>) -> List<&2, M.Dep>:  match ds:    case []:      []    case +hd <> tl:      dep.back(hd, js) <> deps.back(tl, js)# a tool with the pin the joint ledger gives its namedef tool.back.at(own: M.Cli, joint: M.Cli) -> M.Cli:  M.Cli{nn, ee, bb, os} = own  M.Cli{_jn, _je, _jb, js} = joint  M.Cli{nn, ee, bb, source.back(os, js)}# a tool of a ledger, after the upgradedef tool.back(+own: M.Cli, +ts: List<&2, M.Cli>) -> M.Cli:  tool.back.at(own, M.tool.find(ts, M.tool.name(own)))# every tool of a ledger, after the upgradedef tools.back(ts: List<&2, M.Cli>, +js: List<&2, M.Cli>) -> List<&2, M.Cli>:  match ts:    case []:      []    case +hd <> tl:      tool.back(hd, js) <> tools.back(tl, js)# a ledger after the upgrade, every pin taken from the joint ledgerdef ledger.back(mm: M.Manifest, joint: M.Manifest) -> M.Manifest:  M.Manifest{nn, ee, bb, hh, pa, pv, ds, ts} = mm  M.Manifest{_jn, _je, _jb, _jh, _jpa, _jpv, +jd, +jt} = joint  M.Manifest{nn, ee, bb, hh, pa, pv, deps.back(ds, jd), tools.back(ts, jt)}# ---------------------------------------------------------------------------# the root's ledger, written back with its `[workspace]` table# texts written as a TOML array of stringsdef array.items(ts: List<&2, String>) -> String:  match ts:    case []:      ""    case +hd <> []:      T.quote(hd)    case +hd <> tl:      T.quote(hd) ++ ", " ++ array.items(tl)# every entry as it was writtendef entries.text(es: List<&2, W.Entry>) -> List<&2, String>:  match es:    case []:      []    case hd <> tl:      W.entry.text(hd) <> entries.text(tl)# every path as it was writtendef paths.text(ps: List<&2, List<&2, String>>) -> List<&2, String>:  match ps:    case []:      []    case hd <> tl:      W.segs.text(hd) <> paths.text(tl)# the `[workspace]` table, as it says what it saysdef spec.text(spec: W.Spec) -> String:  W.Spec{es, ex} = spec  "[workspace]\nmembers = [" ++ array.items(entries.text(es)) ++ "]\nexclude = [" ++    array.items(paths.text(ex)) ++ "]\n"# the root's ledger as a file: its package table when it has one, which a# virtual root does not, its dependencies and tools, and its `[workspace]`def root.text(+mm: M.Manifest, +spec: W.Spec, +root: W.Root) -> String:  +body = Rend.text(mm)  T.normal(Bool.pick(String, W.root.member(root), body, String.drop(body, 10n)) ++ spec.text(spec))# ---------------------------------------------------------------------------# the writes# a ledger written back when the upgrade changed what it holds, and left as# it was otherwisedef write.if(+mm: M.Manifest, +joint: M.Manifest, +at: String, +text: String) -> List<&2, LP.Effect>:  Bool.pick(List<&2, LP.Effect>, String.eq(Rend.show(mm), Rend.show(ledger.back(mm, joint))),    [], [LP.Write{at, text}])# a member's ledger, written back at its directory's ez.tomldef member.write(rd: M.Read, +ds: List<&2, String>, +joint: M.Manifest) -> List<&2, LP.Effect>:  match rd:    case M.Bad{_why}:      []    case M.Good{+mm}:      write.if(mm, joint, W.segs.text(ds) ++ "/ez.toml", Rend.show(ledger.back(mm, joint)))# a member's ledger written back, or, for another's, nothingdef found.pick(hit: Bool, +text: String, +ds: List<&2, String>, +joint: M.Manifest) -> List<&2, LP.Effect>:  match hit:    case True{}:      member.write(M.parse(text), ds, joint)    case False{}:      []# one ez.toml git listed, written back when it is a member'sdef found.write(  lg: Maybe<&2, W.Ledger>,  +text: String,  +dirs: List<&2, List<&2, String>>,  +joint: M.Manifest) -> List<&2, LP.Effect>:  match lg:    case None{}:      []    case Some{ll}:      +ds = W.ledger.dir(ll)      found.pick(W.has(dirs, ds), text, ds, joint)# every member's ledger the upgrade changed, written backdef members.write(  fs: List<&2, J.Found>,  +dirs: List<&2, List<&2, String>>,  +joint: M.Manifest) -> List<&2, LP.Effect>:  match fs:    case []:      []    case J.Found{+pp, +tt} <> tl:      List.append(&2, LP.Effect, found.write(W.ledger.of(pp, tt), tt, dirs, joint),        members.write(tl, dirs, joint))# the root's ledger, written back when the upgrade changed itdef root.write(rd: M.Read, +spec: W.Spec, +root: W.Root, +joint: M.Manifest) -> List<&2, LP.Effect>:  match rd:    case M.Bad{_why}:      []    case M.Good{+mm}:      write.if(mm, joint, "ez.toml", root.text(ledger.back(mm, joint), spec, root))# every ledger the upgrade changed, the root's first, once the member set is# knowndef writes.members(  ms: W.Members,  +text: String,  +spec: W.Spec,  +fs: List<&2, J.Found>,  +joint: M.Manifest) -> List<&2, LP.Effect>:  match ms:    case W.Refused{_why}:      []    case W.Members{_root, +dirs}:      List.append(&2, LP.Effect, root.write(M.parse(text), spec, W.root.of(text), joint),        members.write(fs, dirs, joint))# every ledger the upgrade changed, once the joint ledger it leaves was readdef writes.joint(joint: M.Read, +text: String, +spec: W.Spec, +fs: List<&2, J.Found>) -> List<&2, LP.Effect>:  match joint:    case M.Bad{_why}:      []    case M.Good{+jm}:      writes.members(W.members(spec, W.root.of(text), J.ledgers(fs)), text, spec, fs, jm)# every ledger the upgrade changed, from the joint ledger it leavesdef writes(back: Back, joint: M.Read) -> List<&2, LP.Effect>:  match back:    case Alone{}:      []    case Back{+text, +spec, +fs, _lock}:      writes.joint(joint, text, spec, fs)# a write kept, or, when it is of ez.toml, the ledgers' in its placedef effect.write(  hit: Bool,  at: String,  text: String,  ws: List<&2, LP.Effect>,  rest: List<&2, LP.Effect>) -> List<&2, LP.Effect>:  match hit:    case True{}:      List.append(&2, LP.Effect, ws, rest)    case False{}:      LP.Write{at, text} <> rest# one effect of the plan: the write of ez.toml replaced by the ledgers'def effect.back(effect: LP.Effect, +ws: List<&2, LP.Effect>, rest: List<&2, LP.Effect>) -> List<&2, LP.Effect>:  match effect:    case LP.Write{+at, text}:      effect.write(String.eq(at, "ez.toml"), at, text, ws, rest)    case LP.Lay{pl, hash, files}:      LP.Lay{pl, hash, files} <> rest    case LP.Drop{hash}:      LP.Drop{hash} <> rest    case LP.Say{text}:      LP.Say{text} <> rest    case LP.Name{nv, hash}:      LP.Name{nv, hash} <> rest# every effect, with the write of ez.toml replaceddef effects.back(es: List<&2, LP.Effect>, +ws: List<&2, LP.Effect>) -> List<&2, LP.Effect>:  match es:    case []:      []    case hd <> tl:      rest = effects.back(tl, ws)      effect.back(hd, ws, rest)# the plan with its write of ez.toml replaced by the ledgers'def plan.back(pl: LP.Plan, +ws: List<&2, LP.Effect>) -> LP.Plan:  LP.Plan{es, outcome} = pl  LP.Plan{effects.back(es, ws), outcome}# ---------------------------------------------------------------------------# members imported by their hash (laws/HashedMemberMovesCascade.bend)# the World an upgrade plans on, and the files it rewritestype Up is Data:  Up{world: LW.World, edits: List<&2, LP.Effect>}# the committed files a World listsdef files.of(world: LW.World) -> List<&2, K.Source>:  LW.World{_args, _ledger, listing, _replies, _ups} = world  J.files(listing)# an upgrade at a workspace root, once its moves are known: the World with# every committed file rewritten by them, and the writes of the files they# changedef up.moves(+ms: List<&2, Mv.Shift>, +text: String, +fs: List<&2, J.Found>, +world: LW.World) -> Up:  +files = files.of(world)  +moved = Mv.rewrite(Mv.swaps(ms), files)  Up{Mv.world.moved(J.joint(text, fs, moved), moved, world), Mv.edits(Mv.swaps(ms), files)}# an upgrade at a workspace root: every member it moves (ws/moved.bend), its# import lines rewritten, until none moves furtherdef up.of(+text: String, +fs: List<&2, J.Found>, +lock: String, +world: LW.World) -> Up:  up.moves(Mv.cascade(Mv.ctx(text, fs, files.of(world), lock, LW.args.only(world))), text, fs, world)# the World an upgrade plans ondef up.world(got: Up) -> LW.World:  Up{w2, _eds} = got  w2# the World `ez lock` plans on at a workspace root: a plain lock's as it is,# and an upgrade's with every moved member's import lines rewrittendef world.at(up: Bool, +text: String, +fs: List<&2, J.Found>, +lock: String, +world: LW.World) -> LW.World:  match up:    case True{}:      up.world(up.of(text, fs, lock, world))    case False{}:      world# the World `ez lock` plans ondef plans.on(back: Back, +world: LW.World) -> LW.World:  match back:    case Alone{}:      world    case Back{+text, _spec, +fs, +lock}:      world.at(LW.args.upgrade(world), text, fs, lock, world)# why a plain lock at a workspace root refuses before it asks anything: a# committed line names the hash the lock being rewritten records for a member# that has moved since. "" when it does not, and always for an upgrade, which# rewrites the line, and for a project of its own.def stale.at(up: Bool, +lock: String, +world: LW.World) -> String:  match up:    case True{}:      ""    case False{}:      Mv.stale.world(lock, world)# and at the root as the command found itdef stale(back: Back, +world: LW.World) -> String:  match back:    case Alone{}:      ""    case Back{_text, _spec, _fs, +lock}:      stale.at(LW.args.upgrade(world), lock, world)# the plan that refuses, writing nothingdef refused(+why: String) -> LP.Plan:  LP.Plan{[], LP.Refused{why}}# the files an upgrade rewrites, written ahead of the plan, unless it refusesdef edits.put(refuses: Bool, eds: List<&2, LP.Effect>, pl: LP.Plan) -> LP.Plan:  match refuses:    case True{}:      pl    case False{}:      LP.Plan{es, outcome} = pl      LP.Plan{List.append(&2, LP.Effect, eds, es), outcome}# an upgrade's plan on the World it rewrote: the files it rewrote written# first, so a source the lock planner rewrites too is left as the planner# writes it, and then the planner's plan with the ledgers written backdef plan.up(got: Up, +back: Back) -> LP.Plan:  Up{+w2, eds} = got  edits.put(LP.refuses(w2), eds, plan.back(LP.plan(w2), writes(back, LP.ledger.next(w2))))# a plain lock's plan, once it is known whether a line is staledef plan.plain(fine: Bool, +why: String, +world: LW.World) -> LP.Plan:  match fine:    case True{}:      LP.plan(world)    case False{}:      refused(why)# what `ez lock` runs at a workspace root, under `--upgrade` or notdef plan.at(up: Bool, +back: Back, +world: LW.World) -> LP.Plan:  match up:    case True{}:      match back:        case Alone{}:          LP.plan(world)        case Back{+text, +spec, +fs, +lock}:          plan.up(up.of(text, fs, lock, world), Back{text, spec, fs, lock})    case False{}:      match back:        case Alone{}:          LP.plan(world)        case Back{_text, _spec, _fs, +lock}:          +why = Mv.stale.world(lock, world)          plan.plain(String.is_empty(why), why, world)# what `ez lock` runs: for a project of its own, the plan the lock planner# makes; at a workspace root, the plan it makes on the joint ledger, with,# under `--upgrade`, every moved member's import lines rewritten and every# ledger the upgrade changed written back in place of the joint one. A plain# lock writes no ledger, so its plan is as it was, unless a line names a# moved member's old hash, which it refuses.def plan(back: Back, +world: LW.World) -> LP.Plan:  match back:    case Alone{}:      LP.plan(world)    case Back{+text, +spec, +fs, +lock}:      plan.at(LW.args.upgrade(world), Back{text, spec, fs, lock}, world)# the step a plain lock takes, once it is known whether a line is staledef step.plain(fine: Bool, +why: String, +world: LW.World) -> LP.Step:  match fine:    case True{}:      LP.step(world)    case False{}:      LP.Run{refused(why)}# the lock planner's step on the World `ez lock` plans on: its questions, or# its plan. A plain lock with a stale line asks nothing and refuses.def step(+back: Back, +world: LW.World) -> LP.Step:  +why = stale(back, world)  step.plain(String.is_empty(why), why, plans.on(back, world))