domain/architecture/system/release/ops.bend

near-v-framework@0.3.0.2 · 156 lines · 0 definitions

1import Base
2import near-v-framework@0.3.0.2/domain/architecture/system/release/type.bend as Rl
3
4# ---- the hub's rules: a name is 12 to 64 characters of a-z, 0-9 and -; a version is four numbers ----
5def digits_only(cs: List<&2, Char>) -> Bool:
6  match cs:
7    case []:
8      True{}
9    case c <> rest:
10      Char.is_digit(c) && digits_only(rest)
11
12def all_numbers(xs: List<&2, String>) -> Bool:
13  match xs:
14    case []:
15      True{}
16    case +x <> rest:
17      Bool.not(String.is_empty(x)) && digits_only(String.to_list(x)) && all_numbers(rest)
18
19def name_chars(cs: List<&2, Char>) -> Bool:
20  match cs:
21    case []:
22      True{}
23    case +c <> rest:
24      (Char.is_lower(c) || Char.is_digit(c) || Char.is_eq(c, '-')) && name_chars(rest)
25
26def package_ok(p: Rl.Package) -> Bool:
27  match p:
28    case Rl.Package{+n, +v, _, _}:
29      Nat.is_ge(String.length(n), 12n) && Nat.is_le(String.length(n), 64n) && name_chars(String.to_list(n)) &&
30      Nat.is_eq(List.length(&2, String, String.split(v, '.')), 4n) && all_numbers(String.split(v, '.'))
31
32def packages_ok(ps: List<&2, Rl.Package>) -> Bool:
33  match ps:
34    case []:
35      True{}
36    case p <> rest:
37      package_ok(p) && packages_ok(rest)
38
39def packages(r: Rl.Release) -> List<&2, Rl.Package>:
40  match r:
41    case Rl.Release{_, ps}:
42      ps
43
44def repository(r: Rl.Release) -> Rl.Repository:
45  match r:
46    case Rl.Release{repo, _}:
47      repo
48
49def is_public(r: Rl.Release) -> Bool:
50  match r:
51    case Rl.Release{repo, _}:
52      match repo:
53        case Rl.Repository{_, p}:
54          p
55
56# Every component any package ships.
57def shipped_components(ps: List<&2, Rl.Package>) -> List<&2, String>:
58  match ps:
59    case []:
60      []
61    case p <> rest:
62      match p:
63        case Rl.Package{_, _, _, cs}:
64          List.append(&2, String, cs, shipped_components(rest))
65
66# Every package's entry file.
67def entries(ps: List<&2, Rl.Package>) -> List<&2, String>:
68  match ps:
69    case []:
70      []
71    case p <> rest:
72      match p:
73        case Rl.Package{_, _, e, _}:
74          e <> entries(rest)
75
76# ---- Bend package sources: Bend with C or JavaScript effectors ----
77# Bend, or a foreign effect in C or JavaScript (Bend's two lanes).
78def is_source(+f: String) -> Bool:
79  String.ends_with(f, ".bend") || String.ends_with(f, ".c") || String.ends_with(f, ".js")
80
81def base_name(f: String) -> String:
82  Maybe.default(&2, String, List.head(&2, String, List.reverse(&2, String, String.split(f, '/'))), "")
83
84# A file that is neither Bend source nor an effect: only licenses, the readme and the ignore list may be.
85def allowed(+f: String) -> Bool:
86  is_source(f) || String.eq(base_name(f), "LICENSE") || String.eq(f, "README.md") || String.eq(f, ".gitignore")
87
88def bend_only(fs: List<&2, String>) -> Bool:
89  match fs:
90    case []:
91      True{}
92    case +f <> rest:
93      allowed(f) && bend_only(rest)
94
95# Every source file is reachable from an entry: a package's, or the system's own.
96def no_dead_code(fs: List<&2, String>, +reached: List<&2, String>) -> Bool:
97  match fs:
98    case []:
99      True{}
100    case +f <> rest:
101      (Bool.not(is_source(f)) || List.contains(~String, ~String.eq, reached, f)) && no_dead_code(rest, reached)
102
103# The first source file nothing reaches, "" when there is none.
104def dead(fs: List<&2, String>, +reached: List<&2, String>) -> String:
105  match fs:
106    case []:
107      ""
108    case +f <> rest:
109      Bool.pick(String, is_source(f) && Bool.not(List.contains(~String, ~String.eq, reached, f)), f, dead(rest, reached))
110
111# The first file that is not Bend, "" when there is none.
112def not_bend(fs: List<&2, String>) -> String:
113  match fs:
114    case []:
115      ""
116    case +f <> rest:
117      Bool.pick(String, allowed(f), not_bend(rest), f)
118
119# ---- the hub's path rule: every segment is [A-Za-z_][A-Za-z0-9_]* (the file name also ends in .bend, .c or
120# .js, or is a LICENSE), under 256 characters ----
121def name_char(+c: Char, first: Bool) -> Bool:
122  Char.is_lower(c) || Char.is_upper(c) || Char.is_eq(c, '_') || (Bool.not(first) && Char.is_digit(c))
123
124def name_chars_ok(cs: List<&2, Char>, first: Bool) -> Bool:
125  match cs:
126    case []:
127      Bool.not(first)
128    case +c <> rest:
129      name_char(c, first) && name_chars_ok(rest, False{})
130
131def stem(+f: String) -> String:
132  Bool.pick(String, String.ends_with(f, ".bend"), String.take(f, Nat.sub(String.length(f), 5n)),
133    Bool.pick(String, String.ends_with(f, ".js"), String.take(f, Nat.sub(String.length(f), 3n)),
134      Bool.pick(String, String.ends_with(f, ".c"), String.take(f, Nat.sub(String.length(f), 2n)), f)))
135
136def segments_ok(ss: List<&2, String>) -> Bool:
137  match ss:
138    case []:
139      True{}
140    case +s <> rest:
141      match rest:
142        case []:
143          String.eq(s, "LICENSE") || (is_source(s) && name_chars_ok(String.to_list(stem(s)), True{}))
144        case _ <> _:
145          name_chars_ok(String.to_list(s), True{}) && segments_ok(rest)
146
147def hub_path_ok(+f: String) -> Bool:
148  Nat.is_lt(String.length(f), 256n) && segments_ok(String.split(f, '/'))
149
150# The first file the hub would refuse, "" when there is none.
151def hub_refused(fs: List<&2, String>) -> String:
152  match fs:
153    case []:
154      ""
155    case +f <> rest:
156      Bool.pick(String, hub_path_ok(f), hub_refused(rest), f)