src/ledger/upgrade.bend
ezx@1.11.0.0 · 223 lines · 28 definitions
1
2
3
4
5
6
7
8
9
10import Base
11import ezx@1.11.0.0/src/ledger/manifest.bend as M
12
13
14type Aim is Data:
15 Hold{}
16 Follow{+ref: String}
17 Forward{}
18
19
20type Verdict is Data:
21 Keep{}
22 Drift{}
23 Off{}
24 Advance{rev: String, tag: String}
25
26
27
28type Swap is Data:
29 Swap{old: String, new: String}
30
31
32def chosen.go(all: Bool, +only: String, +name: String) -> Bool:
33 match all:
34 case True{}:
35 True{}
36 case False{}:
37 String.eq(only, name)
38
39
40def chosen(+only: String, +name: String) -> Bool:
41 chosen.go(String.is_empty(only), only, name)
42
43
44def known.step(hit: Bool, rest: Unit -> Bool) -> Bool:
45 match hit:
46 case True{}:
47 True{}
48 case False{}:
49 rest(Unit{})
50
51
52def known.in(ds: List<&2, M.Dep>, +name: String) -> Bool:
53 match ds:
54 case []:
55 False{}
56 case +h <> t:
57 known.step(String.eq(M.dep.name(h), name), _u => known.in(t, name))
58
59
60def known.go(all: Bool, ds: List<&2, M.Dep>, +name: String) -> Bool:
61 match all:
62 case True{}:
63 True{}
64 case False{}:
65 known.in(ds, name)
66
67
68def known(+only: String, ds: List<&2, M.Dep>) -> Bool:
69 known.go(String.is_empty(only), ds, only)
70
71
72def aim.tag(bare: Bool, +tag: String) -> Aim:
73 match bare:
74 case True{}:
75 Forward{}
76 case False{}:
77 Follow{tag}
78
79
80def aim.src(source: M.Source) -> Aim:
81 match source:
82 case M.Hub{_named}:
83 Hold{}
84 case M.Git{_url, _rev, +tag, _root, _nar, _vend}:
85 aim.tag(String.is_empty(tag), tag)
86 case M.Member{_path, _named}:
87 Hold{}
88
89
90
91def aim(sel: Bool, source: M.Source) -> Aim:
92 match sel:
93 case False{}:
94 Hold{}
95 case True{}:
96 aim.src(source)
97
98
99def judge.same(agree: Bool) -> Verdict:
100 match agree:
101 case True{}:
102 Keep{}
103 case False{}:
104 Drift{}
105
106
107def judge.move(onward: Bool, +rev: String, +tag: String) -> Verdict:
108 match onward:
109 case True{}:
110 Advance{rev, tag}
111 case False{}:
112 Off{}
113
114
115
116
117
118def judge(same: Bool, onward: Bool, agree: Bool, rev: String, tag: String) -> Verdict:
119 match same:
120 case True{}:
121 judge.same(agree)
122 case False{}:
123 judge.move(onward, rev, tag)
124
125
126def agree.nar(same_nar: Bool, same_root: Bool) -> Bool:
127 match same_nar:
128 case False{}:
129 False{}
130 case True{}:
131 same_root
132
133
134def agree.go(same_hash: Bool, same_nar: Bool, same_root: Bool) -> Bool:
135 match same_hash:
136 case False{}:
137 False{}
138 case True{}:
139 agree.nar(same_nar, same_root)
140
141
142def agree(+oh: String, +nh: String, +onar: String, +nnar: String, +oroot: String, +nroot: String) -> Bool:
143 agree.go(String.eq(oh, nh), String.eq(onar, nnar), String.eq(oroot, nroot))
144
145
146
147def hub.bad(+why: String) -> Bool:
148 String.contains(why, "does not hash")
149
150
151
152def retarget.go(
153 +name: String,
154 +entry: String,
155 source: M.Source,
156 +rev: String,
157 +hash: String,
158 +root: String,
159 +nar: String
160) -> M.Dep:
161 match source:
162 case M.Hub{named}:
163 M.Dep{name, hash, entry, M.Hub{named}}
164 case M.Git{url, _rev, tag, _root, _nar, vend}:
165 M.Dep{name, hash, entry, M.Git{url, rev, tag, root, nar, vend}}
166 case M.Member{path, named}:
167 M.Dep{name, hash, entry, M.Member{path, named}}
168
169
170def retarget(dep: M.Dep, +rev: String, +hash: String, +root: String, +nar: String) -> M.Dep:
171 M.Dep{n, _h, e, s} = dep
172 retarget.go(n, e, s, rev, hash, root, nar)
173
174
175
176
177
178def reimport.head(+hash: String) -> String:
179 String.append("import ", String.append(hash, "/"))
180
181
182def reimport.hit(empty: Bool, +line: String, +old: String) -> Bool:
183 match empty:
184 case True{}:
185 False{}
186 case False{}:
187 String.starts_with(line, reimport.head(old))
188
189
190def reimport.line.go(hit: Bool, +line: String, +old: String, +new: String) -> String:
191 match hit:
192 case True{}:
193 String.append(reimport.head(new),
194 String.drop(line, String.length(reimport.head(old))))
195 case False{}:
196 line
197
198
199def reimport.line(+line: String, +old: String, +new: String) -> String:
200 reimport.line.go(reimport.hit(String.is_empty(old), line, old), line, old, new)
201
202
203def reimport.many.line(ss: List<&2, Swap>, +line: String) -> String:
204 match ss:
205 case []:
206 line
207 case Swap{+old, +new} <> t:
208 reimport.many.line(t, reimport.line(line, old, new))
209
210
211def reimport.many.lines(+ss: List<&2, Swap>, ls: List<&2, String>) -> List<&2, String>:
212 match ls:
213 case []:
214 []
215 case +h <> t:
216 reimport.many.line(ss, h) <> reimport.many.lines(ss, t)
217
218
219
220
221
222def reimport.many(+ss: List<&2, Swap>, +text: String) -> String:
223 String.join(reimport.many.lines(ss, String.lines(text)), "\n")