This repository has been archived on 2026-10-10. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
opencode-sandbox/scripts/install-shortcut.sh

86 lines
2.7 KiB
Bash
Raw Normal View History

2026-09-27 20:49:11 +02:00
#!/bin/sh
# Installs a global `opencode-sandbox` shim so that command, run from any
# directory on the host, runs `make opencode` against *this* repo's
# Makefile, mounting whatever directory you're standing in (not this repo)
# as /workspace. Invoked from the Makefile's `install` target as:
# sh scripts/install-shortcut.sh "$(CURDIR)"
#
# POSIX sh only (no bashisms) -- this always runs under Make's own
# $(SHELL), normally /bin/sh, regardless of the user's interactive shell.
set -eu
REPO_DIR=${1:?"usage: $0 <repo-dir>"}
BIN_DIR="$HOME/.local/bin"
SHIM="$BIN_DIR/opencode-sandbox"
mkdir -p "$BIN_DIR"
# -f (not -C): -C would chdir make into the repo before $(CURDIR) is
# evaluated, breaking the "mount whatever directory you're standing in"
# behavior that -v $(CURDIR):/workspace in the Makefile depends on.
cat > "$SHIM" <<EOF
#!/bin/sh
exec make -f "$REPO_DIR/Makefile" opencode "\$@"
EOF
chmod +x "$SHIM"
echo "==> Installed shim: $SHIM"
# Already on PATH? Nothing left to do.
case ":$PATH:" in
*":$BIN_DIR:"*)
echo "==> $BIN_DIR is already on PATH."
exit 0
;;
esac
# Resolve the user's *login* shell via $SHELL, not the shell running this
# script (always Make's $(SHELL)/sh) -- same reason rustup/nvm/pnpm's own
# installers all key off $SHELL instead of process-detecting the caller.
shell_name=$(basename "${SHELL:-}")
if [ "$shell_name" = fish ] && command -v fish >/dev/null 2>&1; then
# fish_add_path persists via a universal variable -- no config file to edit.
fish -c "fish_add_path $BIN_DIR"
echo "==> Added $BIN_DIR to PATH via fish_add_path."
exit 0
fi
case "$shell_name" in
zsh)
# Prefer .zshrc (the standard interactive-shell file); if it doesn't
# exist but .zshenv does, respect that existing setup instead of
# scattering PATH across two files. Only create .zshrc if neither exists.
if [ -f "$HOME/.zshrc" ]; then
rc="$HOME/.zshrc"
elif [ -f "$HOME/.zshenv" ]; then
rc="$HOME/.zshenv"
else
rc="$HOME/.zshrc"
fi
;;
bash)
# macOS Terminal.app runs bash as a login shell, sourcing
# ~/.bash_profile (not ~/.bashrc) unless the user's profile chains to it.
case "$(uname -s)" in
Darwin) rc="$HOME/.bash_profile" ;;
*) rc="$HOME/.bashrc" ;;
esac
;;
*) rc="$HOME/.profile" ;;
esac
marker_begin="# >>> opencode-sandbox >>>"
marker_end="# <<< opencode-sandbox <<<"
if [ -f "$rc" ] && grep -qF "$marker_begin" "$rc"; then
echo "==> $rc already configures PATH for opencode-sandbox; skipping."
else
touch "$rc"
{
printf '\n%s\n' "$marker_begin"
printf 'export PATH="%s:$PATH"\n' "$BIN_DIR"
printf '%s\n' "$marker_end"
} >> "$rc"
echo "==> Added $BIN_DIR to PATH in $rc."
echo "==> Restart your shell (or run: . $rc) to pick it up."
fi