Use moonbitlang/async for process and file IO
Process execution and file access go through @process and @fs. The spinner is a background task in a task group, and the C stub keeps only getenv, exit, isatty, symlink and stderr output. orbit info reads installed state only, and it records and restores package descriptions. just install adds Orbit's bin directory to ~/.bashrc once. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
65e5dc966f
commit
ec21287549
11 files changed
+236
-213
No files matched your search
+78
-47
@@ -1,31 +1,10 @@
|
||||
// System access through the C stub, plus small string helpers.
|
||||
// Helpers are written against `for c in s` only, to stay independent of
|
||||
// String APIs that have changed between MoonBit releases.
|
||||
// System access through process execution, MoonBit async fs/process, and small helpers.
|
||||
|
||||
///|
|
||||
suberror OrbitError {
|
||||
OrbitError(String)
|
||||
}
|
||||
|
||||
///|
|
||||
#borrow(cmd)
|
||||
extern "C" fn c_run(cmd : Bytes) -> Int32 = "orbit_run"
|
||||
|
||||
///|
|
||||
#borrow(cmd)
|
||||
extern "C" fn c_capture(cmd : Bytes) -> Bytes = "orbit_capture"
|
||||
|
||||
///|
|
||||
extern "C" fn c_last_status() -> Int32 = "orbit_last_status"
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_read_file(path : Bytes) -> Bytes = "orbit_read_file"
|
||||
|
||||
///|
|
||||
#borrow(path, data)
|
||||
extern "C" fn c_write_file(path : Bytes, data : Bytes) -> Int32 = "orbit_write_file"
|
||||
|
||||
///|
|
||||
#borrow(name)
|
||||
extern "C" fn c_getenv(name : Bytes) -> Bytes = "orbit_getenv"
|
||||
@@ -33,6 +12,17 @@ extern "C" fn c_getenv(name : Bytes) -> Bytes = "orbit_getenv"
|
||||
///|
|
||||
extern "C" fn c_exit(code : Int32) = "orbit_exit"
|
||||
|
||||
///|
|
||||
extern "C" fn c_isatty(fd : Int32) -> Int32 = "isatty"
|
||||
|
||||
///|
|
||||
#borrow(msg)
|
||||
extern "C" fn c_print_err(msg : Bytes) -> Unit = "orbit_print_err"
|
||||
|
||||
///|
|
||||
#borrow(target, linkpath)
|
||||
extern "C" fn c_symlink(target : Bytes, linkpath : Bytes) -> Int32 = "orbit_symlink"
|
||||
|
||||
// ---------------------------------------------------------------- UTF-8
|
||||
|
||||
///|
|
||||
@@ -266,15 +256,19 @@ fn sq(s : String) -> String {
|
||||
sb.to_string()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- process / fs
|
||||
// ---------------------------------------------------------------- process / fs (async)
|
||||
|
||||
///|
|
||||
fn sh(cmd : String) -> Int32 {
|
||||
c_run(utf8_encode(cmd))
|
||||
async fn sh(cmd : String) -> Int32 noraise {
|
||||
try {
|
||||
@process.run("sh", ["-c", cmd])
|
||||
} catch {
|
||||
_ => 128
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn sh_ok(cmd : String) -> Unit raise OrbitError {
|
||||
async fn sh_ok(cmd : String) -> Unit raise OrbitError {
|
||||
let code = sh(cmd)
|
||||
if code != 0 {
|
||||
raise OrbitError("command failed (exit \{code}): \{cmd}")
|
||||
@@ -282,39 +276,72 @@ fn sh_ok(cmd : String) -> Unit raise OrbitError {
|
||||
}
|
||||
|
||||
///|
|
||||
/// Run a command and return its stdout; stderr goes to the terminal.
|
||||
fn capture(cmd : String) -> String raise OrbitError {
|
||||
let out = utf8_decode(c_capture(utf8_encode(cmd)))
|
||||
let code = c_last_status()
|
||||
if code != 0 {
|
||||
raise OrbitError("command failed (exit \{code}): \{cmd}")
|
||||
/// Return stdout; stderr goes to the terminal unless captured. A label enables delayed progress spinner.
|
||||
async fn capture(cmd : String, progress? : String = "") -> String raise OrbitError {
|
||||
let is_tty = c_isatty(2) != 0 && getenv("TERM") != "dumb"
|
||||
let res : Result[String, Error] = try {
|
||||
let out = @async.with_task_group((group) => {
|
||||
if is_tty && progress != "" {
|
||||
group.spawn_bg(async fn() {
|
||||
@async.sleep(250)
|
||||
let frames = ["|", "/", "-", "\\"]
|
||||
let mut i = 0
|
||||
while true {
|
||||
c_print_err(utf8_encode("\r\u001B[2K" + frames[i % 4] + " " + progress))
|
||||
i = i + 1
|
||||
@async.sleep(100)
|
||||
}
|
||||
})
|
||||
}
|
||||
let (code, stdout, _) = @process.collect_output("sh", ["-c", cmd])
|
||||
if is_tty && progress != "" {
|
||||
c_print_err(utf8_encode("\r\u001B[2K"))
|
||||
}
|
||||
if code != 0 {
|
||||
raise OrbitError("command failed (exit \{code}): \{cmd}")
|
||||
}
|
||||
stdout.text()
|
||||
})
|
||||
Ok(out)
|
||||
} catch {
|
||||
e => Err(e)
|
||||
}
|
||||
match res {
|
||||
Ok(s) => s
|
||||
Err(OrbitError(msg)) => raise OrbitError(msg)
|
||||
Err(e) => raise OrbitError(e.to_string())
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
fn exists(path : String) -> Bool {
|
||||
sh("test -e " + sq(path)) == 0
|
||||
async fn exists(path : String) -> Bool noraise {
|
||||
try {
|
||||
@fs.exists(path)
|
||||
} catch {
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn read_text(path : String) -> String? {
|
||||
let b = c_read_file(utf8_encode(path))
|
||||
if c_last_status() != 0 {
|
||||
None
|
||||
} else {
|
||||
Some(utf8_decode(b))
|
||||
async fn read_text(path : String) -> String? noraise {
|
||||
try {
|
||||
let data = @fs.read_file(path)
|
||||
Some(data.text())
|
||||
} catch {
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Write via a temp file and rename, so readers never see a half-written file.
|
||||
fn write_text_atomic(path : String, text : String) -> Unit raise OrbitError {
|
||||
async fn write_text_atomic(path : String, text : String) -> Unit raise OrbitError {
|
||||
let tmp = path + ".tmp"
|
||||
if c_write_file(utf8_encode(tmp), utf8_encode(text)) != 0 {
|
||||
raise OrbitError("cannot write \{tmp}")
|
||||
try {
|
||||
@fs.write_file(tmp, text, create_mode=CreateOrTruncate)
|
||||
@fs.rename(tmp, path)
|
||||
} catch {
|
||||
e => raise OrbitError("cannot write " + path + ": " + e.to_string())
|
||||
}
|
||||
sh_ok("mv -f " + sq(tmp) + " " + sq(path))
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -324,8 +351,12 @@ fn getenv(name : String) -> String {
|
||||
|
||||
///|
|
||||
/// Command-line arguments without the program name (Linux: /proc/self/cmdline).
|
||||
fn cli_args() -> Array[String] {
|
||||
let raw = c_read_file(utf8_encode("/proc/self/cmdline"))
|
||||
async fn cli_args() -> Array[String] noraise {
|
||||
let raw = try {
|
||||
@fs.read_file("/proc/self/cmdline").binary()
|
||||
} catch {
|
||||
_ => Default::default()
|
||||
}
|
||||
let out : Array[String] = []
|
||||
let cur : Array[Byte] = []
|
||||
for i = 0; i < raw.length(); i = i + 1 {
|
||||
|
||||
Reference in new issue
Block a user