Repository navigation
Expand file tree
/
Copy pathtool.mbt
More file actions
702 lines (673 loc) · 18.6 KB
/
Copy pathtool.mbt
File metadata and controls
702 lines (673 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
///|
/// Tool management, the `mb tool` command group.
///
/// A "tool" is a mooncakes.io module that publishes at least one executable
/// package. `mb tool install` downloads the module source, compiles it with a
/// managed (or system) MoonBit toolchain and copies the resulting native
/// binaries into `~/.local/bin`.
///|
pub const MOONCAKES_API : String = "https://mooncakes.io"
///|
pub const MOONCAKES_DOWNLOAD : String = "https://download.mooncakes.io"
///|
/// Persisted metadata for one installed tool version. `bin` records where the
/// executables were copied; it is optional because records written before it
/// existed only have `bins`.
#derive(ToJson, FromJson)
pub struct ToolMeta {
name : String
version : String
bins : Array[String]
bin : String?
}
///|
pub extend ToolMeta with ToJson::{to_json}
///|
pub extend ToolMeta with @json.FromJson::{from_json}
///|
/// Root directory holding all installed tools.
pub fn tool_root() -> String raise MbError {
path_join(mb_home(), "tool")
}
///|
fn tool_key(name : String) -> String {
name.replace(old="/", new="-")
}
///|
/// Directory for one tool version.
pub fn tool_dir(name : String, version : String) -> String raise MbError {
path_join(path_join(tool_root(), tool_key(name)), version)
}
///|
fn split_spec(spec : String) -> (String, String?) raise MbError {
match spec.split_once("@") {
Some((m, v)) => (m.to_owned(), Some(v.to_owned()))
None =>
if spec == "" {
raise MbError(
"expected a package name in the form <author>/<module>[@<version>]",
)
} else {
(spec, None)
}
}
}
///|
/// Fetch the registry manifest for a module, optionally pinned to a version.
///
/// Returns `None` when the module is simply not in the registry (HTTP 404), and
/// raises when the registry could not be reached, so a network failure is not
/// mistaken for "this name is a GitHub repository".
fn fetch_manifest_opt(name : String, version : String?) -> Json? raise MbError {
let url = match version {
Some(v) => MOONCAKES_API + "/api/v0/manifest/" + name + "@" + v
None => MOONCAKES_API + "/api/v0/manifest/" + name
}
let (code, body) = http_get_status(url)
if code == 404 {
return None
}
if code == 0 {
raise MbError("could not reach mooncakes.io for '" + name + "'")
}
if code >= 400 {
raise MbError(
"mooncakes.io returned HTTP " + code.to_string() + " for '" + name + "'",
)
}
let j = @json.parse(body) catch {
_ => raise MbError("mooncakes.io returned invalid JSON for '" + name + "'")
}
match json_object(j, "metadata") {
Some(_) => Some(j)
None =>
raise MbError("mooncakes.io returned no metadata for '" + name + "'")
}
}
///|
/// Fetch the registry manifest for a module, optionally pinned to a version.
pub fn fetch_manifest(name : String, version : String?) -> Json raise MbError {
match fetch_manifest_opt(name, version) {
Some(j) => j
None => raise MbError("module '" + name + "' was not found on mooncakes.io")
}
}
///|
/// Discover native executables produced by `moon build` under `src`.
///
/// MoonBit writes native binaries as `<name>.exe`; we additionally verify the
/// file magic so non-executable artefacts are ignored.
pub fn discover_binaries(src : String) -> Array[(String, String)] raise MbError {
let out : Array[(String, String)] = []
let build = path_join(src, "_build/native/release/build")
if !dir_exists(build) {
return out
}
for rel in walk_files(build) {
let abs = path_join(build, rel)
if !is_executable_file(abs) {
continue
}
let raw = base_name(rel)
let tool_name = if raw.has_suffix(".exe") {
raw[:raw.length() - 4].to_owned()
} else {
raw
}
// Several packages can share a binary name; keep the first one.
let mut dup = false
for (n, _) in out {
if n == tool_name {
dup = true
}
}
if !dup {
out.push((tool_name, abs))
}
}
out
}
///|
/// Refuse to clobber an executable that belongs to something else unless
/// `force` is set, mirroring `uv tool install`. A symlink pointing back into
/// `dir` is one of ours and may be replaced. The check runs before anything is
/// copied, so a conflict leaves the existing files untouched.
fn ensure_bin_free(
bin : String,
names : Array[String],
dir : String,
force : Bool,
) -> Unit raise MbError {
if force {
return
}
for name in names {
let dest = path_join(bin, name)
match readlink(dest) {
Some(target) =>
if !shim_points_into(target, dir) {
raise Coded(
"executable already exists: " + name + " (use --force to overwrite)",
2,
)
}
None =>
if path_exists(dest) {
raise Coded(
"executable already exists: " + name + " (use --force to overwrite)",
2,
)
}
}
}
}
///|
/// Remove a tool version's staged binaries. Called before (re)staging so a
/// reinstall that no longer ships some executable does not keep it around.
fn reset_staged(dir : String) -> Unit {
try_remove(path_join(dir, "bin"))
}
///|
/// Copy a built or downloaded executable into `<dir>/bin/<name>`, so the bin
/// directory only ever holds symlinks into the tool directory.
fn stage_binary(
dir : String,
name : String,
src : String,
) -> Unit raise MbError {
let tbin = path_join(dir, "bin")
mkdir_p(tbin)
let dest = path_join(tbin, name)
copy_file(src, dest)
chmod_exec(dest)
}
///|
/// Point `bin/<name>` at `<dir>/bin/<name>` for each installed executable.
fn link_binaries(
bin : String,
dir : String,
names : Array[String],
) -> Unit raise MbError {
mkdir_p(bin)
let tbin = path_join(dir, "bin")
prune_links(bin, dir, names)
for name in names {
let dest = path_join(bin, name)
if !run_ok(
"ln -sfn " +
shell_quote(path_join(tbin, name)) +
" " +
shell_quote(dest),
) {
raise MbError("failed to link " + dest)
}
}
}
///|
/// Remove links in `bin` that point into `dir` but are not part of `names`,
/// so reinstalling a tool with fewer executables (e.g. `-i`) does not leave
/// stale entry points behind.
fn prune_links(bin : String, dir : String, names : Array[String]) -> Unit {
if !dir_exists(bin) {
return
}
for entry in list_dir_unsafe(bin) {
if in_array(names, entry) {
continue
}
let link = path_join(bin, entry)
match readlink(link) {
Some(target) =>
if shim_points_into(target, dir) {
run_ok("rm -f -- " + shell_quote(link)) |> ignore
}
None => ()
}
}
}
///|
/// Print where each installed executable was linked.
fn report_installed(bin : String, names : Array[String]) -> Unit {
for name in names {
println(" installed " + path_join(bin, name))
}
}
///|
/// Compile the extracted source at `src` with `tc` and return the discovered
/// binaries.
pub fn build_tool(
src : String,
tc : ToolchainEnv,
) -> Array[(String, String)] raise MbError {
let prefix = env_prefix(tc)
let cmd = "cd " +
shell_quote(src) +
" && " +
prefix +
shell_quote(tc.moon) +
" build --target native --release"
run_checked(cmd) catch {
_ => raise MbError("failed to build the tool in " + src)
}
let bins = discover_binaries(src)
if bins.is_empty() {
raise MbError(
"no native executable was produced; this module does not publish a runnable package",
)
}
bins
}
///|
/// Install one tool from a `<author>/<module>[@<version>]` spec.
pub fn tool_install(
spec : String,
toolchain_override : String,
bin_override : String,
force : Bool,
interactive : Bool,
) -> Unit raise MbError {
ensure_tools()
let bin_dir = if bin_override != "" { bin_override } else { bin_dir() }
let (name, requested) = split_spec(spec)
println("Resolving " + name + " ...")
// A name that is not on mooncakes.io is tried as a GitHub repository
// (`owner/repo` or a URL) and installed from its releases.
let manifest = match fetch_manifest_opt(name, requested) {
Some(m) => m
None => return install_github_tool(spec, bin_dir, force, interactive)
}
let meta = match json_object(manifest, "metadata") {
Some(m) => m
None =>
raise MbError("mooncakes.io returned no metadata for '" + name + "'")
}
let resolved_name = match json_string(meta, "name") {
Some(n) => n
None => name
}
let version = match json_string(meta, "version") {
Some(v) => v
None => raise MbError("could not determine a version for '" + name + "'")
}
let checksum = match json_string(meta, "checksum") {
Some(c) => c
None => raise MbError("could not determine a checksum for '" + name + "'")
}
let dir = tool_dir(resolved_name, version)
if file_exists(path_join(dir, "tool.json")) && !force {
println(
resolved_name +
" " +
version +
" is already installed; use --force to reinstall",
)
return
}
// Prefer a prebuilt binary from the module's GitHub release when one matches
// this platform; otherwise fall through to building from source.
let repository = match json_string(meta, "repository") {
Some(r) => r
None => ""
}
if repository != "" {
let released = try_install_release(
resolved_name, version, repository, requested, bin_dir, dir, force, interactive,
) catch {
e =>
// A conflict is fatal; only an unusable asset falls back to source.
if e is Coded(_, _) {
raise e
} else {
eprintln("warning: " + e.message() + "; building from source instead")
None
}
}
match released {
Some(bins) => {
let record : ToolMeta = {
name: resolved_name,
version,
bins,
bin: Some(bin_dir),
}
write_text(
path_join(dir, "tool.json"),
@json.to_json(record).stringify(indent=2),
)
println("Installed " + resolved_name + " " + version + " (prebuilt)")
return
}
None => ()
}
}
// Download and verify the source archive.
let url = MOONCAKES_DOWNLOAD +
"/user/" +
resolved_name +
"/" +
version +
".zip"
println("Downloading " + url + " ...")
let zip_path = temp_path()
download(url, zip_path, true)
let actual = sha256_hex(read_bytes(zip_path))
if actual != checksum {
rm_rf(zip_path) catch {
_ => ()
}
raise MbError("checksum mismatch for " + resolved_name + " " + version)
}
let src = path_join(dir, "src")
rm_rf(src) catch {
_ => ()
}
extract_zip(read_bytes(zip_path), src)
rm_rf(zip_path) catch {
_ => ()
}
// Build with a toolchain.
let tc = resolve_toolchain(toolchain_override)
println("Building " + resolved_name + " " + version + " ...")
let built = build_tool(src, tc)
// Install the binaries, refusing to clobber unrelated executables. With
// `-i`, let the user pick a subset when the module has several executables.
let all_names : Array[String] = []
for (n, _) in built {
all_names.push(n)
}
let names = choose_names(all_names, interactive)
ensure_bin_free(bin_dir, names, dir, force)
reset_staged(dir)
for (tool_name, path) in built {
if in_array(names, tool_name) {
stage_binary(dir, tool_name, path)
}
}
link_binaries(bin_dir, dir, names)
report_installed(bin_dir, names)
let record : ToolMeta = {
name: resolved_name,
version,
bins: names,
bin: Some(bin_dir),
}
write_text(
path_join(dir, "tool.json"),
@json.to_json(record).stringify(indent=2),
)
println("Installed " + resolved_name + " " + version)
}
///|
/// Read the metadata of every installed tool version.
pub fn installed_tools() -> Array[ToolMeta] raise MbError {
let out : Array[ToolMeta] = []
let root = tool_root()
if !dir_exists(root) {
return out
}
for key in list_dir_unsafe(root) {
let versions_dir = path_join(root, key)
if !dir_exists(versions_dir) {
continue
}
for version in list_dir_unsafe(versions_dir) {
let meta_path = path_join(path_join(versions_dir, version), "tool.json")
if !file_exists(meta_path) {
continue
}
let text = read_text(meta_path) catch { _ => continue }
let meta : ToolMeta = @json.from_json(
@json.parse(text) catch {
_ => continue
},
) catch {
_ => continue
}
out.push(meta)
}
}
out
}
///|
/// Where a tool's executables live: its recorded install directory, or the
/// current default for records written before that was tracked.
pub fn tool_bin_dir(meta : ToolMeta) -> String raise MbError {
match meta.bin {
Some(b) => b
None => bin_dir()
}
}
///|
/// The `mb tool list` command. With `outdated`, consult mooncakes.io and mark
/// tools that have a newer version available.
pub fn tool_list(outdated : Bool) -> Unit raise MbError {
let tools = installed_tools()
let root = tool_root()
if tools.is_empty() {
println("No tools installed (looked in " + root + ").")
return
}
println("Installed tools in " + root + ":")
for t in tools {
let mut line = " " + t.name + " " + t.version
if outdated {
let latest = match latest_tool_version_any(t.name) {
Some(v) => v
None => ""
}
if latest == "" {
line = line + " (could not check for updates)"
} else if latest != t.version {
line = line + " (newer: " + latest + ")"
}
}
println(line)
let bin = tool_bin_dir(t)
if t.bins.is_empty() {
println(" no binaries recorded")
} else {
let entries = t.bins.map(b => {
if file_exists(path_join(bin, b)) {
b
} else {
b + " (missing)"
}
})
println(" " + entries.join(", ") + " in " + bin)
}
}
}
///|
/// The `mb tool uninstall <module>` command.
pub fn tool_uninstall(spec : String) -> Unit raise MbError {
let (name, requested) = split_spec(spec)
let tools = installed_tools()
let mut removed = false
for t in tools {
if t.name != name {
continue
}
if requested is Some(v) && v != t.version {
continue
}
// Remove the symlinks that point into this version's directory. A link
// taken over by another tool (or a foreign file) is left alone, so the
// link target decides ownership.
let bin = tool_bin_dir(t)
let dir = tool_dir(t.name, t.version)
for b in t.bins {
let dest = path_join(bin, b)
match readlink(dest) {
Some(target) =>
if shim_points_into(target, dir) {
run_ok("rm -f -- " + shell_quote(dest)) |> ignore
}
None => ()
}
}
rm_rf(tool_dir(t.name, t.version)) catch {
_ => ()
}
// Drop the per-tool directory once its last version is gone.
let name_dir = path_join(tool_root(), tool_key(t.name))
if dir_exists(name_dir) && list_dir_unsafe(name_dir).is_empty() {
rm_rf(name_dir) catch {
_ => ()
}
}
println("Uninstalled " + t.name + " " + t.version)
removed = true
}
if !removed {
raise MbError("no installed tool matches '" + spec + "'")
}
}
///|
/// The `mb tool upgrade` command. With `all`, upgrade every installed tool;
/// otherwise upgrade the named tools (any version specifier is ignored and the
/// latest version is installed). Each tool is rebuilt into the directory it was
/// originally installed into.
pub fn tool_upgrade(
names : Array[String],
all : Bool,
toolchain_override : String,
) -> Unit raise MbError {
ensure_tools()
let tools = installed_tools()
if tools.is_empty() {
println("No tools installed.")
return
}
let wanted : Array[ToolMeta] = []
for t in tools {
let mut pick = all
for n in names {
if fst(split_spec(n)) == t.name {
pick = true
}
}
if pick {
// One entry per tool name, even when several versions are installed.
let mut dup = false
for w in wanted {
if w.name == t.name {
dup = true
}
}
if !dup {
wanted.push(t)
}
}
}
for t in wanted {
let latest = match latest_tool_version_any(t.name) {
Some(v) => v
None => {
eprintln("warning: could not check " + t.name + " for updates")
continue
}
}
if latest == t.version {
println(t.name + " " + t.version + " is already up to date")
} else {
println("Upgrading " + t.name + " " + t.version + " -> " + latest)
tool_install(
t.name + "@" + latest,
toolchain_override,
tool_bin_dir(t),
true,
false,
)
rm_rf(tool_dir(t.name, t.version)) catch {
_ => ()
}
}
}
// Warn about names that matched no installed tool.
for n in names {
let name = fst(split_spec(n))
let mut found = false
for t in tools {
if t.name == name {
found = true
}
}
if !found {
eprintln("warning: '" + name + "' is not installed")
}
}
}
///|
/// The `mb tool uninstall` command: remove the named tools, or every installed
/// tool with `all`.
pub fn tool_uninstall_many(
specs : Array[String],
all : Bool,
) -> Unit raise MbError {
if all {
let tools = installed_tools()
if tools.is_empty() {
println("No tools installed.")
return
}
let done : Array[String] = []
for t in tools {
let mut seen = false
for d in done {
if d == t.name {
seen = true
}
}
if seen {
continue
}
done.push(t.name)
tool_uninstall(t.name)
}
return
}
for s in specs {
tool_uninstall(s)
}
}
///|
fn fst(pair : (String, String?)) -> String {
let (a, _) = pair
a
}
///|
/// Latest version of an installed tool, whether it came from mooncakes.io or
/// straight from a GitHub repository. `None` when neither source has a version.
fn latest_tool_version_any(name : String) -> String? {
let from_registry = latest_tool_version(name) catch { _ => "" }
if from_registry != "" {
return Some(from_registry)
}
match parse_github_repo(name) {
Some(repo) => latest_github_tag(repo)
None => None
}
}
///|
/// Latest published version of a module.
pub fn latest_tool_version(name : String) -> String raise MbError {
let manifest = fetch_manifest(name, None)
match json_string(manifest, "latest_version") {
Some(v) => v
None =>
match json_object(manifest, "metadata") {
Some(m) =>
match json_string(m, "version") {
Some(v) => v
None =>
raise MbError(
"could not determine the latest version of '" + name + "'",
)
}
None =>
raise MbError(
"could not determine the latest version of '" + name + "'",
)
}
}
}