Build with current MoonBit toolchain

Migrate to moon.mod/moon.pkg, update suberror and Json construction
syntax, import @json, annotate FFI params #borrow, and clear all
deprecation warnings. Builds cleanly, tests pass, and the CLI was
smoke-tested against a local registry:2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhm28fFg1zAegJABvu
This commit is contained in:
Claude committed 2026-11-27 01:33:28 +00:00
1 parent efeaa326c8
commit 5a91e87d64
11 files changed
+236 -131

No files matched your search

+67 -24
View File
@@ -1,5 +1,6 @@
// Planning and applying installs, removals, upgrades and rollbacks.
///|
/// One package in an install plan.
struct Step {
name : String
@@ -30,10 +31,15 @@ fn manifest_of_installed(p : Installed) -> Manifest {
}
}
///|
/// Every constraint that currently applies to `name`: the request itself,
/// the user's own constraint if they installed it explicitly, and what each
/// other package requires (using planned manifests for packages being replaced).
fn constraints_for(p : Planner, name : String, requested : String) -> Array[String] {
fn constraints_for(
p : Planner,
name : String,
requested : String,
) -> Array[String] {
let cons : Array[String] = [requested]
match p.ctx.state.get(name) {
Some(inst) =>
@@ -56,7 +62,7 @@ fn constraints_for(p : Planner, name : String, requested : String) -> Array[Stri
}
}
for other, st in p.steps {
if other != name && not(p.ctx.state.contains(other)) {
if other != name && !(p.ctx.state.contains(other)) {
match st.manifest.depends.get(name) {
Some(c) => cons.push(c)
None => ()
@@ -67,10 +73,15 @@ fn constraints_for(p : Planner, name : String, requested : String) -> Array[Stri
}
///|
fn visit(p : Planner, name : String, requested : String, explicit : Bool) -> Unit raise OrbitError {
fn visit(
p : Planner,
name : String,
requested : String,
explicit : Bool,
) -> Unit raise OrbitError {
match p.steps.get(name) {
Some(st) => {
if not(satisfies_all(st.version, [requested])) {
if !(satisfies_all(st.version, [requested])) {
raise OrbitError(
"conflict: \{name} \{st.version} is planned but '\{requested}' is required",
)
@@ -87,14 +98,18 @@ fn visit(p : Planner, name : String, requested : String, explicit : Bool) -> Uni
let upgrading = p.upgrade.contains(name)
match p.ctx.state.get(name) {
Some(inst) =>
if not(upgrading) && satisfies_all(inst.version, cons) {
if !(upgrading) && satisfies_all(inst.version, cons) {
let st = Step::{
name,
version: inst.version,
digest: inst.digest,
manifest: manifest_of_installed(inst),
explicit: explicit || inst.explicit,
constraint: if explicit { requested } else { inst.constraint },
constraint: if explicit {
requested
} else {
inst.constraint
},
keep: true,
}
p.steps[name] = st
@@ -147,10 +162,11 @@ fn visit(p : Planner, name : String, requested : String, explicit : Bool) -> Uni
p.order.push(st)
}
///|
/// "name" or "name@constraint"
fn parse_spec(spec : String) -> (String, String) raise OrbitError {
let (name, c) = split_first(spec, '@')
if not(valid_name(name)) {
if !(valid_name(name)) {
raise OrbitError("invalid package name '\{name}'")
}
(name, if c == "" { "*" } else { c })
@@ -176,17 +192,28 @@ fn unlink_bins(ctx : Ctx, bins : Array[String]) -> Unit {
}
///|
fn link_bins(ctx : Ctx, dir : String, bins : Array[String]) -> Unit raise OrbitError {
fn link_bins(
ctx : Ctx,
dir : String,
bins : Array[String],
) -> Unit raise OrbitError {
sh_ok("mkdir -p " + sq(bin_dir(ctx)))
for b in bins {
sh_ok(
"ln -sfn " + sq(dir + "/" + b) + " " + sq(bin_dir(ctx) + "/" + basename(b)),
"ln -sfn " +
sq(dir + "/" + b) +
" " +
sq(bin_dir(ctx) + "/" + basename(b)),
)
}
}
///|
fn check_bin_conflicts(ctx : Ctx, name : String, bins : Array[String]) -> Unit raise OrbitError {
fn check_bin_conflicts(
ctx : Ctx,
name : String,
bins : Array[String],
) -> Unit raise OrbitError {
for b in bins {
match owner_of_bin(ctx, basename(b)) {
Some(other) =>
@@ -200,6 +227,7 @@ fn check_bin_conflicts(ctx : Ctx, name : String, bins : Array[String]) -> Unit r
}
}
///|
/// Switch `name` to a fetched build, keeping the old one for rollback.
fn activate(ctx : Ctx, st : Step) -> Unit raise OrbitError {
let dir = fetch_into_store(ctx, st.name, st.digest)
@@ -227,7 +255,9 @@ fn activate(ctx : Ctx, st : Step) -> Unit raise OrbitError {
inst.depends = st.manifest.depends
inst.explicit = st.explicit
inst.constraint = st.constraint
println(" \{st.name}: \{inst.history[inst.history.length() - 1].version} -> \{st.version}")
println(
" \{st.name}: \{inst.history[inst.history.length() - 1].version} -> \{st.version}",
)
}
None => {
ctx.state[st.name] = Installed::{
@@ -273,7 +303,7 @@ fn apply_plan(ctx : Ctx, p : Planner) -> Unit raise OrbitError {
///|
fn new_planner(ctx : Ctx, upgrade : Array[String]) -> Planner {
Planner::{ ctx, steps: Map::new(), order: [], upgrade }
Planner::{ ctx, steps: Map([]), order: [], upgrade, }
}
///|
@@ -289,6 +319,7 @@ fn cmd_install(ctx : Ctx, specs : Array[String]) -> Unit raise OrbitError {
apply_plan(ctx, p)
}
///|
/// Upgrade the named packages (or everything) to the newest versions their
/// constraints allow. Dependencies are moved only when required.
fn cmd_upgrade(ctx : Ctx, names : Array[String]) -> Unit raise OrbitError {
@@ -299,7 +330,7 @@ fn cmd_upgrade(ctx : Ctx, names : Array[String]) -> Unit raise OrbitError {
}
} else {
for n in names {
if not(ctx.state.contains(n)) {
if !(ctx.state.contains(n)) {
raise OrbitError("\{n} is not installed")
}
targets.push(n)
@@ -339,6 +370,7 @@ fn remove_one(ctx : Ctx, name : String) -> Unit raise OrbitError {
}
}
///|
/// Remove dependencies nobody needs any more.
fn autoremove(ctx : Ctx) -> Unit raise OrbitError {
let mut changed = true
@@ -346,7 +378,7 @@ fn autoremove(ctx : Ctx) -> Unit raise OrbitError {
changed = false
let orphans : Array[String] = []
for name, p in ctx.state {
if not(p.explicit) && reverse_deps(ctx, name).length() == 0 {
if !(p.explicit) && reverse_deps(ctx, name).length() == 0 {
orphans.push(name)
}
}
@@ -358,23 +390,27 @@ fn autoremove(ctx : Ctx) -> Unit raise OrbitError {
}
///|
fn cmd_remove(ctx : Ctx, names : Array[String], force : Bool) -> Unit raise OrbitError {
fn cmd_remove(
ctx : Ctx,
names : Array[String],
force : Bool,
) -> Unit raise OrbitError {
if names.length() == 0 {
raise OrbitError("usage: orbit remove <name>...")
}
for n in names {
if not(ctx.state.contains(n)) {
if !(ctx.state.contains(n)) {
raise OrbitError("\{n} is not installed")
}
}
for n in names {
let users : Array[String] = []
for u in reverse_deps(ctx, n) {
if not(names.contains(u)) {
if !(names.contains(u)) {
users.push(u)
}
}
if users.length() > 0 && not(force) {
if users.length() > 0 && !(force) {
raise OrbitError(
"\{n} is required by \{join(users, ", ")} (use --force to remove anyway)",
)
@@ -387,6 +423,7 @@ fn cmd_remove(ctx : Ctx, names : Array[String], force : Bool) -> Unit raise Orbi
save_state(ctx)
}
///|
/// Swap the current build with the previous one (run twice to undo).
fn cmd_rollback(ctx : Ctx, name : String) -> Unit raise OrbitError {
let inst = match ctx.state.get(name) {
@@ -398,8 +435,10 @@ fn cmd_rollback(ctx : Ctx, name : String) -> Unit raise OrbitError {
None => raise OrbitError("\{name} has no previous version to roll back to")
}
let dir = store_dir(ctx, name, prev.digest)
if not(exists(dir)) {
raise OrbitError("previous build of \{name} (\{prev.version}) is no longer in the store")
if !(exists(dir)) {
raise OrbitError(
"previous build of \{name} (\{prev.version}) is no longer in the store",
)
}
unlink_bins(ctx, inst.bins)
inst.history.push(Gen::{
@@ -420,7 +459,10 @@ fn cmd_rollback(ctx : Ctx, name : String) -> Unit raise OrbitError {
let missing : Array[String] = []
for dep, c in inst.depends {
match ctx.state.get(dep) {
Some(d) => if not(satisfies_all(d.version, [c])) { missing.push(dep + "@" + c) }
Some(d) =>
if !(satisfies_all(d.version, [c])) {
missing.push(dep + "@" + c)
}
None => missing.push(dep + "@" + c)
}
}
@@ -431,10 +473,11 @@ fn cmd_rollback(ctx : Ctx, name : String) -> Unit raise OrbitError {
}
}
///|
/// Delete store entries that are neither current nor kept for rollback.
fn cmd_gc(ctx : Ctx) -> Unit raise OrbitError {
let store = ctx.root + "/store"
if not(exists(store)) {
if !(exists(store)) {
println("store is empty")
return
}
@@ -451,7 +494,7 @@ fn cmd_gc(ctx : Ctx) -> Unit raise OrbitError {
keep.push(store_id(g.digest))
}
for id in lines(capture("ls -1 " + sq(store + "/" + pkg))) {
if not(keep.contains(id)) {
if !(keep.contains(id)) {
sh_ok("rm -rf " + sq(store + "/" + pkg + "/" + id))
freed = freed + 1
}
@@ -466,7 +509,7 @@ fn cmd_gc(ctx : Ctx) -> Unit raise OrbitError {
///|
fn cmd_list(ctx : Ctx) -> Unit {
if ctx.state.size() == 0 {
if ctx.state.length() == 0 {
println("no packages installed")
return
}