src/ledger/render.bend
ezx@1.4.0.0 · 223 lines · 0 definitions
1# ledger/render: a ledger written back out as ez.toml, and the two edits the
2# command line makes to it. Rendering is from the model, so the file it writes
3# is the file it would read back.
4import Base
5import ezx@1.4.0.0/src/ledger/manifest.bend as M
6import ezx@1.4.0.0/src/toml/toml.bend as T
7
8# a key line, or nothing at all when the value is empty, so an absent tag,
9# entry or bin leaves no trace
10def line(key: String, +val: String) -> String:
11 Bool.pick(String, String.is_empty(val), "", key ++ " = \"" ++ val ++ "\"\n")
12
13# a key line whose value is written bare, as a TOML boolean is, or nothing at
14# all when the value is empty
15def line.bare(key: String, +val: String) -> String:
16 Bool.pick(String, String.is_empty(val), "", key ++ " = " ++ val ++ "\n")
17
18# `true` when the tree is committed, and nothing when it is not, so the key
19# is absent rather than written as an empty string. It is written bare: in
20# TOML `vendor = "true"` is a string, and a reader other than ez, such as
21# nix's `builtins.fromTOML`, would take it for one. ez reads both spellings.
22def vendor.word(vend: Bool) -> String:
23 match vend:
24 case True{}:
25 "true"
26 case False{}:
27 ""
28
29# the keys a source contributes
30def source(src: M.Source) -> String:
31 match src:
32 case M.Hub{named}:
33 line("hub", named)
34 case M.Git{url, rev, tag, root, nar, vend}:
35 line("git", url) ++ line("rev", rev) ++ line("tag", tag) ++
36 line("root", root) ++ line("narHash", nar) ++ line.bare("vendor", vendor.word(vend))
37
38# one `[deps.<name>]` section
39def dep(dependency: M.Dep) -> String:
40 M.Dep{n, h, e, s} = dependency
41 "\n[deps." ++ n ++ "]\n" ++ line("hash", h) ++ source(s) ++ line("entry", e)
42
43# every dependency, in the order the ledger holds them
44def deps(ds: List<&2, M.Dep>) -> String:
45 match ds:
46 case []:
47 ""
48 case h <> t:
49 dep(h) ++ deps(t)
50
51# one `[tools.<name>]` section. No hash: a tool is not a package.
52def tool(pin: M.Tool) -> String:
53 M.Tool{n, e, b, s} = pin
54 "\n[tools." ++ n ++ "]\n" ++ source(s) ++ line("entry", e) ++ line("bin", b)
55
56# every pinned CLI, in the order the ledger holds them
57def tools(ts: List<&2, M.Tool>) -> String:
58 match ts:
59 case []:
60 ""
61 case h <> t:
62 tool(h) ++ tools(t)
63
64# the ledger as ez assembles it, in the TOML ez wrote before eztoml 0.4
65def text(manifest: M.Manifest) -> String:
66 M.Manifest{n, e, b, h, pa, pv, ds, ts} = manifest
67 "[package]\n" ++ line("name", n) ++ line("entry", e) ++ line("bin", b) ++
68 line("hub", h) ++ line("publish-as", pa) ++ line("version", pv) ++
69 deps(ds) ++ tools(ts)
70
71# the ledger as an ez.toml: ez's text, written as eztoml writes it
72def show(manifest: M.Manifest) -> String:
73 T.normal(text(manifest))
74
75# a ledger that was read, as an ez.toml; an unreadable one renders as nothing,
76# so a failed read can never overwrite a good file with a guess
77def render(ledger: M.Read) -> String:
78 match ledger:
79 case M.Bad{_why}:
80 ""
81 case M.Good{m}:
82 show(m)
83
84# a dependency kept or dropped
85def without.put(hit: Bool, dependency: M.Dep, rest: List<&2, M.Dep>) -> List<&2, M.Dep>:
86 match hit:
87 case True{}:
88 rest
89 case False{}:
90 dependency <> rest
91
92# every dependency but the one named
93def without(ds: List<&2, M.Dep>, +name: String) -> List<&2, M.Dep>:
94 match ds:
95 case []:
96 []
97 case +h <> t:
98 without.put(String.eq(M.dep.name(h), name), h, without(t, name))
99
100# the ledger with a dependency added, replacing any of the same name, so adding
101# twice is the same as adding once
102def add(manifest: M.Manifest, +dependency: M.Dep) -> M.Manifest:
103 M.Manifest{n, e, b, h, pa, pv, ds, ts} = manifest
104 M.Manifest{n, e, b, h, pa, pv, List.append(&2, M.Dep, without(ds, M.dep.name(dependency)), [dependency]), ts}
105
106# the ledger with a dependency dropped
107def remove(manifest: M.Manifest, name: String) -> M.Manifest:
108 M.Manifest{n, e, b, h, pa, pv, ds, ts} = manifest
109 M.Manifest{n, e, b, h, pa, pv, without(ds, name), ts}
110
111# a git source with its tree committed
112def vendored(src: M.Source) -> M.Source:
113 match src:
114 case M.Hub{named}:
115 M.Hub{named}
116 case M.Git{url, rev, tag, root, nar, _vend}:
117 M.Git{url, rev, tag, root, nar, True{}}
118
119# a source that keeps a vendor bit the ledger already set, or is left as given
120def revend(keep: Bool, src: M.Source) -> M.Source:
121 match keep:
122 case True{}:
123 vendored(src)
124 case False{}:
125 src
126
127# a dependency added again keeps the `vendor = true` its entry already had.
128# The command line has no way to say it, so a re-add that dropped it would
129# drop the tree from the allowlist without anyone having asked.
130def readd(ds: List<&2, M.Dep>, dependency: M.Dep) -> M.Dep:
131 M.Dep{+n, h, e, s} = dependency
132 M.Dep{n, h, e, revend(M.source.vendor(M.source.dep(M.find(ds, n))), s)}
133
134# the ledger with a dependency added, keeping the vendor bit of the entry it
135# replaces
136def add.keep(manifest: M.Manifest, dependency: M.Dep) -> M.Manifest:
137 M.Manifest{n, e, b, h, pa, pv, +ds, ts} = manifest
138 add(M.Manifest{n, e, b, h, pa, pv, ds, ts}, readd(ds, dependency))
139
140# ---------------------------------------------------------------------------
141# what a ledger may hold to be written
142
143# whether a text holds no copy of a char
144def clear(text: String, +ch: Char) -> Bool:
145 match text:
146 case SNil{}:
147 True{}
148 case SCon{h, t}:
149 rest = clear(t, ch)
150 Bool.and(Bool.not(Char.is_eq(h, ch)), rest)
151
152# whether a name or value can be written as it is: it holds no `"`, no `\`
153# and no newline. The pinned eztoml reader (v0.1.0) neither writes nor reads
154# escapes, a newline would end the line, and a quote or a backslash would make
155# the file mean something else to every other TOML reader, nix's among them.
156def clean(+text: String) -> Bool:
157 Bool.and(clear(text, '"'), Bool.and(clear(text, '\\'), clear(text, '\n')))
158
159# whether a source reads back as itself. A git source names its repo and the
160# directory its paths are written from: the reader takes a section with no
161# `git` for a hub package and one with no `root` for the top of the checkout.
162def source.ok(src: M.Source) -> Bool:
163 match src:
164 case M.Hub{+named}:
165 clean(named)
166 case M.Git{+url, rev, tag, +root, nar, _vend}:
167 Bool.and(Bool.and(Bool.not(String.is_empty(url)), Bool.not(String.is_empty(root))),
168 Bool.and(Bool.and(clean(url), clean(rev)),
169 Bool.and(clean(tag), Bool.and(clean(root), clean(nar)))))
170
171# whether a dependency does: it has a hash, which the reader asks every
172# dependency for, and every name and value is clean
173def dep.ok(dependency: M.Dep) -> Bool:
174 M.Dep{+n, +h, +e, s} = dependency
175 Bool.and(Bool.and(clean(n), Bool.and(Bool.not(String.is_empty(h)), clean(h))),
176 Bool.and(clean(e), source.ok(s)))
177
178# whether every dependency does
179def deps.ok(ds: List<&2, M.Dep>) -> Bool:
180 match ds:
181 case []:
182 True{}
183 case h <> t:
184 rest = deps.ok(t)
185 Bool.and(dep.ok(h), rest)
186
187# whether a pinned CLI's source does. A tool is never imported, so the reader
188# keeps no hub name for one, and one written with a name would read back
189# without it.
190def tool.src.ok(src: M.Source) -> Bool:
191 match src:
192 case M.Hub{named}:
193 String.is_empty(named)
194 case M.Git{url, rev, tag, root, nar, vend}:
195 source.ok(M.Git{url, rev, tag, root, nar, vend})
196
197# whether a pinned CLI does
198def tool.ok(pin: M.Tool) -> Bool:
199 M.Tool{+n, +e, +b, s} = pin
200 Bool.and(clean(n), Bool.and(Bool.and(clean(e), clean(b)), tool.src.ok(s)))
201
202# whether every pinned CLI does
203def tools.ok(ts: List<&2, M.Tool>) -> Bool:
204 match ts:
205 case []:
206 True{}
207 case h <> t:
208 rest = tools.ok(t)
209 Bool.and(tool.ok(h), rest)
210
211# whether a ledger can be written so that it reads back as itself (EZ-LED-4):
212# every name and value is clean, `publish-as` and `version` among them, every
213# dependency has a hash, and every git source names its repo and its root.
214# Every command that writes ez.toml refuses a ledger that is not, so every
215# ledger ez writes is one of these.
216def renderable(manifest: M.Manifest) -> Bool:
217 M.Manifest{+n, +e, +b, +h, +pa, +pv, ds, ts} = manifest
218 Bool.and(Bool.and(Bool.and(clean(n), clean(e)), Bool.and(clean(b), clean(h))),
219 Bool.and(Bool.and(clean(pa), clean(pv)), Bool.and(deps.ok(ds), tools.ok(ts))))
220
221# why a ledger that is not renderable is not written
222def unrenderable() -> String:
223 "ez: ez.toml cannot be written: a name or value holds a quote, a backslash or a newline, a dependency has no hash, or a git source names no repo or no root"