#!/usr/bin/env nu # Build independent commands and a committed source snapshot of their repository. def main [destination?: path] { let here = $env.FILE_PWD let destination = ($destination | default ($here | path join .. target userland bundle) | path expand) let bin = ($destination | path join bin) let object = ($here | path join cwd-init init.o) let sources = [($here | path join cwd-init init.rs) ($here | path join cwd-init exit.wit) ($here | path join cwd-init build.nu)] if not ($object | path exists) or ($sources | any {|source| (ls $source | get modified.0) > (ls $object | get modified.0) }) { ^nu ($here | path join cwd-init build.nu) if $env.LAST_EXIT_CODE != 0 { error make {msg: 'cwd-init build failed'} } } let metadata = (do { cd $here; ^cargo metadata --no-deps --format-version 1 } | from json) do { cd $here; ^cargo build --workspace --release --locked } if $env.LAST_EXIT_CODE != 0 { error make {msg: 'userland build failed'} } ^nu ($here | path join brush build.nu) if $env.LAST_EXIT_CODE != 0 { error make {msg: 'brush build failed'} } mkdir $bin for package in ($metadata.packages | where {|p| $p.id in $metadata.workspace_members }) { let tools = ($package.manifest_path | path dirname | path join tools.txt) for binary in ($package.targets | where {|t| 'bin' in $t.kind }) { let source = ($metadata.target_directory | path join wasm32-wasip2 release $'($binary.name).wasm') let names = if ($tools | path exists) { open --raw $tools | lines | where {|s| ($s | str trim) != '' } } else { [$binary.name] } for name in $names { ^cp --remove-destination $source ($bin | path join $'($name).wasm') if $env.LAST_EXIT_CODE != 0 { error make {msg: $'bundle copy failed: ($name)'} } } } } let brush_metadata = (^cargo metadata --manifest-path ($here | path join brush source Cargo.toml) --no-deps --format-version 1 | from json) let brush = ($brush_metadata.target_directory | path join wasm32-wasip2 release brush.wasm) for name in [sh bash] { ^cp --remove-destination $brush ($bin | path join $'($name).wasm') if $env.LAST_EXIT_CODE != 0 { error make {msg: 'brush copy failed'} } } let repo = ($here | path join .. | path expand) let revision = (^git -C $repo rev-parse HEAD | str trim) let snapshot = ($destination | path join src sausage) if ($snapshot | path exists) { rm --recursive --force $snapshot } mkdir $snapshot let archive = ($destination | path join source.tar) ^git -C $repo archive --output $archive HEAD host vfs userland Cargo.toml Cargo.lock flake.nix flake.lock README.md AGENTS.md .gitignore if $env.LAST_EXIT_CODE != 0 { error make {msg: 'source archive failed'} } ^tar -xf $archive -C $snapshot if $env.LAST_EXIT_CODE != 0 { error make {msg: 'source extraction failed'} } rm $archive ^git -C $snapshot init --quiet --initial-branch=main if $env.LAST_EXIT_CODE != 0 { error make {msg: 'source git init failed'} } ^git -C $snapshot add host vfs userland Cargo.toml Cargo.lock flake.nix flake.lock README.md AGENTS.md .gitignore if $env.LAST_EXIT_CODE != 0 { error make {msg: 'source git add failed'} } with-env { GIT_AUTHOR_NAME: 'Sausage', GIT_AUTHOR_EMAIL: 'sausage@localhost', GIT_COMMITTER_NAME: 'Sausage', GIT_COMMITTER_EMAIL: 'sausage@localhost', GIT_AUTHOR_DATE: '2000-01-01T00:00:00+0000', GIT_COMMITTER_DATE: '2000-01-01T00:00:00+0000' } { ^git -C $snapshot commit --no-gpg-sign --quiet -m $'Bundle source ($revision)' if $env.LAST_EXIT_CODE != 0 { error make {msg: 'source git commit failed'} } } print $destination }