src/ez/key.bend
ezx@1.5.0.0 · 67 lines · 0 definitions
1# ez/key: what a cached tool checkout and binary were made from. A checkout
2# is its commit. A binary is not: a pin's `bin` or `entry` builds another file
3# from the same commit, and another bend builds the same file differently. So
4# the binary's record is all three, and it is reused only when every one of
5# them is the one about to be built.
6import Base
7
8# the commit, the file handed to bend, and the bend that built it
9type Stamp is Data:
10 Stamp{rev: String, file: String, bend: String}
11
12# the record that matches nothing: an empty commit is never reused
13def none() -> Stamp:
14 Stamp{"", "", ""}
15
16# the record as it is written, one field to a line
17def show(key: Stamp) -> String:
18 match key:
19 case Stamp{+r, +f, +b}:
20 r ++ "\n" ++ f ++ "\n" ++ b ++ "\n"
21
22# exactly three lines, or no record. A cache written before the file and the
23# bend were recorded holds the commit alone, and that reads as no record.
24def read.third(+rev: String, +file: String, ls: List<&2, String>) -> Stamp:
25 match ls:
26 case []:
27 none()
28 case +b <> t:
29 match t:
30 case []:
31 Stamp{rev, file, b}
32 case _h <> _t:
33 none()
34
35def read.second(+rev: String, ls: List<&2, String>) -> Stamp:
36 match ls:
37 case []:
38 none()
39 case +f <> t:
40 read.third(rev, f, t)
41
42def read.lines(ls: List<&2, String>) -> Stamp:
43 match ls:
44 case []:
45 none()
46 case +r <> t:
47 read.second(r, t)
48
49# a record's text, as `show` wrote it and a file read trimmed it
50def read(+text: String) -> Stamp:
51 read.lines(String.split(text, '\n'))
52
53# the checkout is this commit, and there is a commit at all. The checkout's
54# record is the commit alone: the file and the bend are the binary's business.
55def checkout(+was: String, +now: String) -> Bool:
56 Bool.and(Bool.not(String.is_empty(now)), String.eq(was, now))
57
58# the three fields the same, and a commit at all. An empty commit is a dirty
59# tree or a directory that is not a checkout, and those are built every time.
60def reuse(was: Stamp, now: Stamp) -> Bool:
61 match was:
62 case Stamp{+r0, +f0, +b0}:
63 match now:
64 case Stamp{+r1, +f1, +b1}:
65 Bool.and(Bool.not(String.is_empty(r1)),
66 Bool.and(String.eq(r0, r1),
67 Bool.and(String.eq(f0, f1), String.eq(b0, b1))))