363 lines
12 KiB
MoonBit
363 lines
12 KiB
MoonBit
// orbit: a package manager whose repository is any OCI registry.
|
|
|
|
///|
|
|
/// Orbit's version. A test keeps it equal to the one in moon.mod.
|
|
let orbit_version : String = "0.1.0"
|
|
|
|
///|
|
|
let usage : String =
|
|
#|orbit - packages as OCI artifacts
|
|
#|
|
|
#|usage (project workflow, when orbit.toml is present):
|
|
#| orbit new [dir] create a project orbit.toml
|
|
#| orbit add [channel::]<name>[@constraint]...
|
|
#| solve, pin in orbit.toml, lock & sync
|
|
#| orbit install [--locked | --frozen] sync .orbit/ from orbit.toml / orbit.lock
|
|
#| orbit sync [--locked | --frozen] same as `orbit install` in a project
|
|
#| orbit lock [--update] resolve and write orbit.lock
|
|
#| orbit update [name...] upgrade locked packages within constraints
|
|
#| orbit remove <name>... remove from orbit.toml, re-lock & sync
|
|
#| orbit run [--locked | --frozen] <cmd|task> [args...]
|
|
#| run a command or [tasks] entry in .orbit
|
|
#| orbit list show locked project packages
|
|
#| orbit env print PATH for .orbit/bin + global bin
|
|
#|
|
|
#|usage (global & registry commands; add -g/--global inside a project):
|
|
#| orbit init [--project] create ~/.orbit (or --project for orbit.toml)
|
|
#| orbit login <host> [-u <user>] [-p <pass> | --password-stdin] [--plain-http]
|
|
#| log in to an OCI registry
|
|
#| orbit logout <host> remove stored registry credentials
|
|
#| orbit channel list channels, highest priority first
|
|
#| orbit channel add <name> <url> [--plain-http]
|
|
#| orbit publish <dir> [--channel <name>] [--no-latest] [--no-index]
|
|
#| push <dir>/orbit.json + <dir>/files/
|
|
#| orbit index [--channel <name>] [name...]
|
|
#| rebuild the channel's package index
|
|
#| orbit install [-g] [channel::]<name>[@constraint]...
|
|
#| install globally with SAT dependency solving
|
|
#| orbit remove [-g] <name>... [--force] remove, then drop unused dependencies
|
|
#| orbit upgrade [-g] [name...] jointly upgrade within constraints
|
|
#| orbit rollback <name> switch to the previous global build
|
|
#| orbit list [-g] installed global packages
|
|
#| orbit info [channel::]<name> installed package details (local only)
|
|
#| orbit versions <name> published versions
|
|
#| orbit search [term] list packages (channel index or catalog API)
|
|
#| orbit gc delete unused builds from the store
|
|
#| orbit env [-g] print the PATH line for your shell
|
|
#|
|
|
#|flags: -g, --global --manifest-path <path> --locked --frozen
|
|
#| -h, --help -V, --version
|
|
#|constraints: 1.2.3 =1.2.3 ==1.2.3 1.2.* ^1.2.0 ~1.2.0 >=1.2 <2.0 !=1.3.0 *
|
|
#| (comma-separated clauses are ANDed, e.g. ">=1.2.0, <2.0.0")
|
|
#|config: <orbit root>/config.toml
|
|
#|credentials: <orbit root>/auth.json (falls back to ~/.docker/config.json)
|
|
#|environment: ORBIT_ROOT (default ~/.orbit); ORBIT_REGISTRY and
|
|
#| ORBIT_PLAIN_HTTP=1 override the built-in main channel
|
|
|
|
///|
|
|
/// Create the directories, and a config.toml if there is no config yet.
|
|
async fn cmd_init() -> Unit raise OrbitError {
|
|
let root = orbit_root()
|
|
sh_ok(
|
|
"mkdir -p " +
|
|
sq(root + "/bin") +
|
|
" " +
|
|
sq(root + "/store") +
|
|
" " +
|
|
sq(root + "/tmp"),
|
|
)
|
|
let config = root + "/config.toml"
|
|
// A legacy config.json is still honored, so don't hide it with a new file.
|
|
if !exists(config) && !exists(root + "/config.json") {
|
|
write_text_atomic(config, config_template)
|
|
}
|
|
println("orbit root: \{root}")
|
|
println("channels:")
|
|
print_channels(load_ctx().channels)
|
|
println("add to your shell profile:")
|
|
println(" export PATH=\"\{root}/bin:$PATH\"")
|
|
}
|
|
|
|
///|
|
|
fn need(pos : Array[String], n : Int32, what : String) -> Unit raise OrbitError {
|
|
if pos.length() < n {
|
|
raise OrbitError("usage: orbit \{what}")
|
|
}
|
|
}
|
|
|
|
///|
|
|
async fn resolve_project_or_none(
|
|
force_global : Bool,
|
|
manifest_path : String?,
|
|
) -> String? raise OrbitError {
|
|
if force_global {
|
|
if manifest_path is Some(_) {
|
|
raise OrbitError("--global cannot be used with --manifest-path")
|
|
}
|
|
None
|
|
} else {
|
|
find_project_manifest(manifest_path)
|
|
}
|
|
}
|
|
|
|
///|
|
|
async fn require_project_manifest(
|
|
manifest_path : String?,
|
|
) -> String raise OrbitError {
|
|
match find_project_manifest(manifest_path) {
|
|
Some(mf) => mf
|
|
None =>
|
|
raise OrbitError(
|
|
"no orbit.toml found in current or parent directories (run `orbit new` to create one)",
|
|
)
|
|
}
|
|
}
|
|
|
|
///|
|
|
async fn dispatch(args : Array[String]) -> Unit raise OrbitError {
|
|
if args.length() == 0 {
|
|
println(usage)
|
|
return
|
|
}
|
|
let cmd = args[0]
|
|
let pos : Array[String] = []
|
|
let flags : Array[String] = []
|
|
let mut publish_to = MAIN_CHANNEL
|
|
let mut username : String? = None
|
|
let mut password : String? = None
|
|
let mut force_global = false
|
|
let mut manifest_path : String? = None
|
|
let mut i = 1
|
|
while i < args.length() {
|
|
if cmd == "run" && pos.length() > 0 {
|
|
// Forward all flags/args after the command/task name verbatim.
|
|
pos.push(args[i])
|
|
i = i + 1
|
|
continue
|
|
}
|
|
let a = args[i]
|
|
if a == "--" {
|
|
i = i + 1
|
|
while i < args.length() {
|
|
pos.push(args[i])
|
|
i = i + 1
|
|
}
|
|
break
|
|
} else if a == "-g" || a == "--global" {
|
|
force_global = true
|
|
} else if a == "--manifest-path" {
|
|
if i + 1 >= args.length() {
|
|
raise OrbitError("usage: --manifest-path <path>")
|
|
}
|
|
manifest_path = Some(args[i + 1])
|
|
i = i + 1
|
|
} else if has_prefix(a, "--manifest-path=") {
|
|
manifest_path = Some(drop_chars(a, 16))
|
|
} else if a == "--channel" {
|
|
if (cmd != "publish" && cmd != "index") || i + 1 >= args.length() {
|
|
raise OrbitError("usage: orbit publish <dir> | index --channel <name>")
|
|
}
|
|
publish_to = args[i + 1]
|
|
i = i + 1
|
|
} else if a == "-u" || a == "--username" {
|
|
if cmd != "login" || i + 1 >= args.length() {
|
|
raise OrbitError(
|
|
"usage: orbit login <host> [-u <user>] [-p <pass> | --password-stdin] [--plain-http]",
|
|
)
|
|
}
|
|
username = Some(args[i + 1])
|
|
i = i + 1
|
|
} else if has_prefix(a, "--username=") {
|
|
if cmd != "login" {
|
|
raise OrbitError("unknown flag '\{a}'")
|
|
}
|
|
username = Some(drop_chars(a, 11))
|
|
} else if a == "-p" || a == "--password" {
|
|
if cmd != "login" || i + 1 >= args.length() {
|
|
raise OrbitError(
|
|
"usage: orbit login <host> [-u <user>] [-p <pass> | --password-stdin] [--plain-http]",
|
|
)
|
|
}
|
|
password = Some(args[i + 1])
|
|
i = i + 1
|
|
} else if has_prefix(a, "--password=") {
|
|
if cmd != "login" {
|
|
raise OrbitError("unknown flag '\{a}'")
|
|
}
|
|
password = Some(drop_chars(a, 11))
|
|
} else if has_prefix(a, "--") {
|
|
flags.push(a)
|
|
} else {
|
|
pos.push(a)
|
|
}
|
|
i = i + 1
|
|
}
|
|
match cmd {
|
|
"help" | "-h" | "--help" => println(usage)
|
|
"-V" | "--version" => println("orbit \{orbit_version}")
|
|
"new" => {
|
|
if force_global {
|
|
raise OrbitError("--global cannot be used with `orbit new`")
|
|
}
|
|
cmd_project_init(if pos.length() > 0 { Some(pos[0]) } else { None })
|
|
}
|
|
"init" =>
|
|
if flags.contains("--project") {
|
|
cmd_project_init(if pos.length() > 0 { Some(pos[0]) } else { None })
|
|
} else {
|
|
if pos.length() > 0 {
|
|
raise OrbitError(
|
|
"init takes no arguments; use `orbit channel add <name> <url>` or `orbit new <dir>`",
|
|
)
|
|
}
|
|
cmd_init()
|
|
}
|
|
"login" => {
|
|
need(
|
|
pos,
|
|
1,
|
|
"login <host> [-u <user>] [-p <pass> | --password-stdin] [--plain-http]",
|
|
)
|
|
cmd_login(
|
|
pos[0],
|
|
username,
|
|
password,
|
|
flags.contains("--password-stdin"),
|
|
flags.contains("--plain-http"),
|
|
)
|
|
}
|
|
"logout" => {
|
|
need(pos, 1, "logout <host>")
|
|
cmd_logout(pos[0])
|
|
}
|
|
"channel" => {
|
|
need(pos, 1, "channel list|add")
|
|
match pos[0] {
|
|
"list" => print_channels(load_ctx().channels)
|
|
"add" => {
|
|
need(pos, 3, "channel add <name> <url> [--plain-http]")
|
|
cmd_channel_add(
|
|
orbit_root(),
|
|
pos[1],
|
|
pos[2],
|
|
flags.contains("--plain-http"),
|
|
)
|
|
}
|
|
other => raise OrbitError("unknown channel command '\{other}'")
|
|
}
|
|
}
|
|
"lock" => {
|
|
if force_global {
|
|
raise OrbitError("--global cannot be used with `orbit lock`")
|
|
}
|
|
let mf = require_project_manifest(manifest_path)
|
|
cmd_project_lock(load_ctx(), mf, flags.contains("--update"))
|
|
}
|
|
"sync" => {
|
|
if force_global {
|
|
raise OrbitError("--global cannot be used with `orbit sync`")
|
|
}
|
|
let mf = require_project_manifest(manifest_path)
|
|
cmd_project_sync(
|
|
load_ctx(),
|
|
mf,
|
|
flags.contains("--locked"),
|
|
flags.contains("--frozen"),
|
|
)
|
|
}
|
|
"run" => {
|
|
if force_global {
|
|
raise OrbitError("--global cannot be used with `orbit run`")
|
|
}
|
|
let mf = require_project_manifest(manifest_path)
|
|
need(pos, 1, "run [--locked|--frozen] <command|task> [args...]")
|
|
cmd_project_run(
|
|
load_ctx(),
|
|
mf,
|
|
pos,
|
|
flags.contains("--locked"),
|
|
flags.contains("--frozen"),
|
|
)
|
|
}
|
|
"env" =>
|
|
match resolve_project_or_none(force_global, manifest_path) {
|
|
Some(mf) => cmd_project_env(load_ctx(), mf)
|
|
None => println("export PATH=\"\{orbit_root()}/bin:$PATH\"")
|
|
}
|
|
"publish" => {
|
|
need(pos, 1, "publish <dir>")
|
|
let ch = find_channel(load_ctx(), publish_to)
|
|
publish(
|
|
ch,
|
|
pos[0],
|
|
!flags.contains("--no-latest"),
|
|
update_index=!flags.contains("--no-index"),
|
|
)
|
|
}
|
|
"index" => cmd_index(find_channel(load_ctx(), publish_to), pos)
|
|
"install" =>
|
|
match resolve_project_or_none(force_global, manifest_path) {
|
|
Some(mf) =>
|
|
if pos.length() == 0 {
|
|
cmd_project_sync(
|
|
load_ctx(),
|
|
mf,
|
|
flags.contains("--locked"),
|
|
flags.contains("--frozen"),
|
|
)
|
|
} else {
|
|
raise OrbitError(
|
|
"in a project (found \{mf}), use `orbit add <pkg>...` to add dependencies or `orbit install -g <pkg>...` to install globally",
|
|
)
|
|
}
|
|
None => cmd_install(load_ctx(), pos)
|
|
}
|
|
"add" =>
|
|
match resolve_project_or_none(force_global, manifest_path) {
|
|
Some(mf) => cmd_project_add(load_ctx(), mf, pos)
|
|
None => cmd_install(load_ctx(), pos)
|
|
}
|
|
"remove" | "rm" =>
|
|
match resolve_project_or_none(force_global, manifest_path) {
|
|
Some(mf) => cmd_project_remove(load_ctx(), mf, pos)
|
|
None => cmd_remove(load_ctx(), pos, flags.contains("--force"))
|
|
}
|
|
"upgrade" | "up" | "update" =>
|
|
match resolve_project_or_none(force_global, manifest_path) {
|
|
Some(mf) => cmd_project_update(load_ctx(), mf, pos)
|
|
None => cmd_upgrade(load_ctx(), pos)
|
|
}
|
|
"rollback" => {
|
|
need(pos, 1, "rollback <name>")
|
|
cmd_rollback(load_ctx(), pos[0])
|
|
}
|
|
"list" | "ls" =>
|
|
match resolve_project_or_none(force_global, manifest_path) {
|
|
Some(mf) => cmd_project_list(load_ctx(), mf)
|
|
None => cmd_list(load_ctx())
|
|
}
|
|
"info" => {
|
|
need(pos, 1, "info [channel::]<name>")
|
|
cmd_info(load_ctx(), pos[0])
|
|
}
|
|
"versions" => {
|
|
need(pos, 1, "versions <name>")
|
|
cmd_versions(load_ctx(), pos[0])
|
|
}
|
|
"search" =>
|
|
cmd_search(load_ctx(), if pos.length() > 0 { pos[0] } else { "" })
|
|
"gc" => cmd_gc(load_ctx())
|
|
_ => raise OrbitError("unknown command '\{cmd}' (try `orbit help`)")
|
|
}
|
|
}
|
|
|
|
///|
|
|
async fn main {
|
|
dispatch(cli_args()) catch {
|
|
OrbitError(msg) => {
|
|
println("orbit: error: \{msg}")
|
|
c_exit(1)
|
|
}
|
|
}
|
|
}
|