#!/usr/bin/env bash case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) printf '\n Windows detected — graff runs natively; install it from PowerShell:\n\n' printf ' irm https://codegraff.com/install.ps1 | iex\n\n' exit 0 ;; esac # POSIX sh compatible: `curl ... | sh` runs this under dash on Debian/Ubuntu, # which rejects `set -o pipefail`. Enable it only where the shell supports it. set -eu if (set -o pipefail) 2>/dev/null; then set -o pipefail; fi # Git Bash / MSYS / Cygwin: graff runs natively, through the PowerShell # installer. This check must run here, at top level: inside detect_platform, # which main() runs in a $(...) subshell, `exit` only left the subshell and # the script went on to clone and build. case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) printf '\n Windows detected — graff runs natively; install it from PowerShell:\n\n' printf ' irm https://codegraff.com/install.ps1 | iex\n\n' exit 0 ;; esac # graff-harness installer. Prefers a prebuilt binary from the latest GitHub # release; falls back to building from source with Zig. Styled after the # codedb/codegraff installers. # # curl -fsSL https://raw.githubusercontent.com/justrach/codegraff/main/install.sh | bash # # or, from a checkout: ./install.sh # # Env overrides: HARNESS_REPO (source repo), HARNESS_DIR (install dir), # HARNESS_BUILD=source (skip the release download and compile), # HARNESS_NO_GRAFF=1 (skip the codedb/zigrep companion suite), # HARNESS_NO_CODEDB=1 (leave codedb alone), # HARNESS_NO_PATH=1 (do not append the install dir to ~/.zshrc / ~/.bashrc). REPO="${HARNESS_REPO:-https://github.com/justrach/codegraff}" INSTALL_DIR="${HARNESS_DIR:-$HOME/bin}" BIN="graff" ZIG_VERSION="0.17.0" # Colors R='\033[0;31m' G='\033[0;32m' Y='\033[0;33m' C='\033[0;36m' W='\033[1;37m' D='\033[0;90m' N='\033[0m' detect_platform() { local os arch os="$(uname -s)"; arch="$(uname -m)" case "$os" in Darwin) os="darwin" ;; Linux) os="linux" ;; *) printf " ${R}unsupported OS: $os${N}\n" >&2; exit 1 ;; esac case "$arch" in arm64|aarch64) arch="arm64" ;; x86_64|amd64) arch="x86_64" ;; *) printf " ${R}unsupported arch: $arch${N}\n" >&2; exit 1 ;; esac echo "${os}-${arch}" } need_zig() { if ! command -v zig >/dev/null 2>&1; then printf "\n ${R}error: zig not found${N} — harness needs Zig $ZIG_VERSION.\n" printf " install it with: ${C}zigup $ZIG_VERSION${N}\n\n" >&2 exit 1 fi local v; v="$(zig version 2>/dev/null || echo '?')" case "$v" in "$ZIG_VERSION") ;; # exact reproducible toolchain *) printf " ${Y}warning:${N} zig $v detected; harness targets $ZIG_VERSION — run 'zigup $ZIG_VERSION'.\n" ;; esac printf " ${D}zig${N} $v\n" } # Apple Silicon SIGKILLs a binary whose signature no longer matches the # kernel cache for that path. Leave a notarized Developer ID alone (re-signing # strips the staple). Do NOT Developer-ID-sign a local zig build: unnotarized # Developer ID + hardened runtime is `SIGKILL (Code Signature Invalid)` once # --yolo pages in more than --version. Ad-hoc is the correct local signature. darwin_sign() { [ "$(uname -s)" = Darwin ] || return 0 local bin="$1" identifier="${2:-graff}" [ -f "$bin" ] || return 0 if codesign --verify --strict "$bin" >/dev/null 2>&1 \ && codesign -dv --verbose=2 "$bin" 2>&1 | grep -q 'Authority=Developer ID Application'; then return 0 fi codesign -s - --force --identifier "$identifier" "$bin" >/dev/null 2>&1 || true } # macOS release tarballs carry nanocua.app, graff's computer use (ADR 0314). # ~/Applications needs no administrator, and Launch Services finds it there. A # tarball without it changes nothing. It runs nothing until an agent needs it. place_nanocua() { [ "$(uname -s)" = Darwin ] || return 0 local app stage dest old app="$(find "$1" -maxdepth 2 -type d -name nanocua.app 2>/dev/null | head -1)" [ -n "$app" ] || return 0 mkdir -p "$HOME/Applications" || return 1 dest="$HOME/Applications/nanocua.app" stage="$HOME/Applications/.nanocua.app.new" old="$HOME/Applications/.nanocua.app.old" rm -rf "$stage" "$old" cp -R "$app" "$stage" || { rm -rf "$stage"; return 1; } if [ -e "$dest" ]; then mv "$dest" "$old" || { rm -rf "$stage"; return 1; }; fi if ! mv "$stage" "$dest"; then [ -e "$old" ] && mv "$old" "$dest" 2>/dev/null || true rm -rf "$stage" return 1 fi rm -rf "$old" } # Drop the binary onto a *fresh inode*: overwriting a running macOS binary in # place gets the replacement SIGKILLed by the kernel's signature cache. place_bin() { mkdir -p "$INSTALL_DIR" rm -f "$INSTALL_DIR/$BIN" install -m 0755 "$1" "$INSTALL_DIR/$BIN" darwin_sign "$INSTALL_DIR/$BIN" # Overwrite any *other* graff already on PATH (e.g. an older copy in # ~/.local/bin from the codegraff stack) so the binary we just installed # actually wins instead of being shadowed by a stale one earlier on PATH. # Fresh inode each time (rm then install) — same reason as above. Only # touch writable copies; never error on a protected/system path. oldifs="$IFS"; IFS=: for d in $PATH; do [ -n "$d" ] || continue [ "$d" = "$INSTALL_DIR" ] && continue if [ -f "$d/$BIN" ] && [ -w "$d/$BIN" ]; then if rm -f "$d/$BIN" && install -m 0755 "$1" "$d/$BIN" 2>/dev/null; then darwin_sign "$d/$BIN" printf " ${D}│${N} %-10s ${G}✓${N} (overwrote stale $d/$BIN)\n" "replace" fi fi done IFS="$oldifs" } # Try the latest GitHub release for this platform. Plain curl is the fast path # now that the repo is public; gh is only a fallback for private forks. fetch_release() { local platform="$1" target asset tmpd case "$platform" in darwin-arm64) target="aarch64-macos" ;; darwin-x86_64) target="x86_64-macos" ;; linux-arm64) target="aarch64-linux" ;; linux-x86_64) target="x86_64-linux" ;; *) return 1 ;; esac # graff-named assets first; harness-named for releases that predate the # rename (the tarball's inner dir/binary follow the asset name). local stem for stem in graff harness; do asset="$stem-$target.tar.gz" tmpd="$(mktemp -d)" # Public repo: plain curl is the fast path (no auth, and it can't hang the # way `gh release download` has been observed to on some machines). Fall # back to gh only if curl fails — e.g. a private fork — and gh is present. if curl -fsSL --max-time 60 "$REPO/releases/latest/download/$asset" -o "$tmpd/$asset" 2>/dev/null; then : elif command -v gh >/dev/null 2>&1 \ && gh release download --repo "${REPO#https://github.com/}" --pattern "$asset" --dir "$tmpd" >/dev/null 2>&1; then : else rm -rf "$tmpd"; continue fi break done [ -f "$tmpd/$asset" ] || { rm -rf "$tmpd"; return 1; } tar -xzf "$tmpd/$asset" -C "$tmpd" 2>/dev/null || { rm -rf "$tmpd"; return 1; } # Locate the extracted binary. CI (release.yml) nests it under # graff-/, but hand-repacked macOS tarballs (the local notarize # flow) ship it flat at the root. Handle both, then fall back to a search # so a future layout change can't silently break the install again. local binpath="" if [ -f "$tmpd/$stem" ]; then binpath="$tmpd/$stem" elif [ -f "$tmpd/$stem-$target/$stem" ]; then binpath="$tmpd/$stem-$target/$stem" else binpath="$(find "$tmpd" -type f -name "$stem" 2>/dev/null | head -1)" fi [ -n "$binpath" ] && [ -f "$binpath" ] || { rm -rf "$tmpd"; return 1; } place_bin "$binpath" place_nanocua "$tmpd" rm -rf "$tmpd" # Fail closed: if the binary didn't actually land, return non-zero so the # caller falls back to a source build instead of falsely reporting success. # (set -e is suppressed inside the `if fetch_release` guard, so the install # error above does not abort on its own.) [ -x "$INSTALL_DIR/$BIN" ] || return 1 } # codedb backs graff's codedb tool. Install it when missing and bring an # existing copy up to codedb's latest release, so `graff update` keeps it # current too. Only a download that matches the release's checksums.sha256 # and runs `--version` is installed. Prints current, installed or failed. CODEDB_REPO="${CODEDB_REPO:-https://github.com/justrach/codedb}" update_codedb() { local platform="$1" asset="codedb-$1" tag latest current dest tmpd staged tag="$(curl -fsSLI --max-time 30 -o /dev/null -w '%{url_effective}' "$CODEDB_REPO/releases/latest" 2>/dev/null)" || { echo failed; return 0; } latest="${tag##*/v}" case "$latest" in ''|*/*) echo failed; return 0 ;; esac dest="$(command -v codedb 2>/dev/null || true)" if [ -n "$dest" ]; then current="$("$dest" --version 2>/dev/null | awk '{print $NF}')" [ "$current" = "$latest" ] && { echo current; return 0; } fi tmpd="$(mktemp -d)" if ! curl -fsSL --max-time 120 "$CODEDB_REPO/releases/download/v$latest/$asset" -o "$tmpd/$asset" \ || ! curl -fsSL --max-time 30 "$CODEDB_REPO/releases/download/v$latest/checksums.sha256" -o "$tmpd/checksums.sha256"; then rm -rf "$tmpd"; echo failed; return 0 fi if command -v sha256sum >/dev/null 2>&1; then (cd "$tmpd" && grep " $asset\$" checksums.sha256 | sha256sum -c - >/dev/null 2>&1) else (cd "$tmpd" && grep " $asset\$" checksums.sha256 | shasum -a 256 -c - >/dev/null 2>&1) fi || { rm -rf "$tmpd"; echo failed; return 0; } chmod 0755 "$tmpd/$asset" "$tmpd/$asset" --version >/dev/null 2>&1 || { rm -rf "$tmpd"; echo failed; return 0; } # Replace the codedb on PATH when we may write it; otherwise install next to graff. if [ -z "$dest" ] || [ ! -w "$dest" ]; then dest="$INSTALL_DIR/codedb"; fi mkdir -p "$(dirname "$dest")" || { rm -rf "$tmpd"; echo failed; return 0; } # Stage a fresh inode beside the destination, then replace it atomically. # A failed install or signature must leave the existing binary untouched. staged="$(mktemp "${dest}.XXXXXX")" || { rm -rf "$tmpd"; echo failed; return 0; } if ! install -m 0755 "$tmpd/$asset" "$staged" \ || ! darwin_sign "$staged" codedb \ || ! { [ "$(uname -s)" != Darwin ] || codesign --verify --strict "$staged" >/dev/null 2>&1; } \ || ! mv -f "$staged" "$dest"; then rm -f "$staged"; rm -rf "$tmpd"; echo failed; return 0 fi rm -rf "$tmpd" echo "installed $latest" } build_from_source() { local src tmp="" need_zig # Source: build in place if we're in the repo, else clone it. if [ -f build.zig ] && grep -qE '"(graff|harness)"' build.zig 2>/dev/null; then src="$(pwd)" printf " ${D}│${N} %-10s ${G}✓${N} (current checkout)\n" "source" else command -v git >/dev/null 2>&1 || { printf " ${R}error: git not found${N}\n" >&2; exit 1; } tmp="$(mktemp -d)"; src="$tmp/codegraff" printf " ${D}│${N} %-10s " "clone" git clone --depth 1 "$REPO" "$src" >/dev/null 2>&1 \ && printf "${G}✓${N}\n" \ || { printf "${R}failed${N}\n\n ${R}could not clone $REPO${N} (private repo — check your GitHub access)\n\n" >&2; exit 1; } fi printf " ${D}│${N} %-10s " "build" ( cd "$src" && zig build -Doptimize=ReleaseSafe -Dlearn=false ) >/dev/null 2>&1 \ && printf "${G}✓${N}\n" \ || { printf "${R}failed${N}\n\n ${R}zig build failed${N} — run it manually in $src to see the error\n\n" >&2; exit 1; } # post-rename checkouts build zig-out/bin/graff; older ones harness if [ -f "$src/zig-out/bin/graff" ]; then place_bin "$src/zig-out/bin/graff" else place_bin "$src/zig-out/bin/harness" fi [ -n "$tmp" ] && rm -rf "$tmp" } main() { local platform platform="$(detect_platform)" printf "\n ${W}graff-harness${N} ${D}installer${N}\n\n" printf " ${D}platform${N} $platform\n" printf " ${D}install${N} $INSTALL_DIR\n\n" # Kick off the companion suite install in the background so it downloads in # parallel with the graff binary below — it's the slow part (a full curl|sh of # codedb-pro + the zigrep/zigread/zigpatch suite). Reaped before the summary. # Empty suite_pid = already present or opted out (HARNESS_NO_GRAFF) → nothing to wait on. # Tools installer = codegraff.com/install.sh; --max-time bounds it; override via GRAFF_SUITE_URL. GRAFF_SUITE_URL="${GRAFF_SUITE_URL:-https://codegraff.com/install.sh}" suite_pid=""; suite_log="" if [ -z "${HARNESS_NO_GRAFF:-}" ] && ! { command -v codedb-pro >/dev/null 2>&1 && command -v zigpatch >/dev/null 2>&1; }; then suite_log="$(mktemp)" ( curl -fsSL --max-time 120 "$GRAFF_SUITE_URL" | sh >/dev/null 2>&1 && echo ok || echo fail ) >"$suite_log" 2>&1 & suite_pid=$! fi if [ "${HARNESS_BUILD:-release}" != "source" ] && fetch_release "$platform"; then printf " ${D}│${N} %-10s ${G}✓${N} (prebuilt release)\n" "download" else build_from_source fi printf " ${D}│${N} %-10s ${G}✓${N}\n\n" "install" printf " ${G}installed${N} ${D}→ $INSTALL_DIR/$BIN${N}\n" # Compat: the command used to be `harness` — the SDKs' fallback and old # shell habits keep working through a symlink. ln -sf "$INSTALL_DIR/$BIN" "$INSTALL_DIR/harness" # Reap the companion suite install that was kicked off in parallel at the top # of main() (muonry, zigrep/zigread/zigpatch, codedb — they back the @ picker, # the codedb tool, and zigpatch's atomic edit_file splices; the harness works # fully without it, premium paths just stay dormant). Skip with HARNESS_NO_GRAFF=1. if [ -z "${HARNESS_NO_GRAFF:-}" ]; then printf " ${D}│${N} %-10s " "suite" if [ -z "$suite_pid" ]; then printf "${G}✓${N} (already present)\n" else wait "$suite_pid" 2>/dev/null || true if [ "$(cat "$suite_log" 2>/dev/null)" = ok ]; then printf "${G}✓${N} (muonry + zigrep suite)\n" else printf "${Y}skipped${N} ${D}(companion install unavailable — harness still fully functional)${N}\n" fi rm -f "$suite_log" fi fi # After the suite, which may install codedb itself: keep codedb current. if [ -z "${HARNESS_NO_GRAFF:-}" ] && [ -z "${HARNESS_NO_CODEDB:-}" ]; then printf " ${D}│${N} %-10s " "codedb" case "$(update_codedb "$platform")" in current) printf "${G}✓${N} (up to date)\n" ;; installed*) printf "${G}✓${N} (updated)\n" ;; *) printf "${Y}skipped${N} ${D}(codedb release unavailable — graff still works)${N}\n" ;; esac fi ensure_path if [ "${GRAFF_NO_MCP:-}" != "1" ]; then "$INSTALL_DIR/$BIN" mcp install || printf 'MCP setup incomplete. Retry with: graff mcp install\n' >&2 fi printf "\n ${W}done!${N} run ${C}$BIN${N} to start, or ${C}$BIN --help${N}\n\n" } # Persist $INSTALL_DIR on PATH so the next terminal finds `graff`. # curl|sh often runs under bash while the login shell is zsh (macOS default), # which is why people saw "graff: command not found" after a successful # install — we only printed an export they never pasted. Write every rc that # exists, and create ~/.zshrc / ~/.bashrc when that is the login shell. # HARNESS_NO_PATH=1 skips. Idempotent via the marker comment. ensure_path() { if [ -n "${HARNESS_NO_PATH:-}" ]; then printf " ${D}PATH skipped (HARNESS_NO_PATH)${N}\n" return 0 fi if [ -z "${HOME:-}" ]; then printf " ${Y}HOME unset — could not persist PATH${N}\n" return 0 fi local marker="# codegraff PATH (install.sh) — do not duplicate" local export_line="export PATH=\"${INSTALL_DIR}:\$PATH\"" local fish_line="fish_add_path -m \"${INSTALL_DIR}\"" local wrote=0 local shell_name zshrc bashrc profile fishrc shell_name="$(basename "${SHELL:-sh}")" zshrc="${ZDOTDIR:-$HOME}/.zshrc" bashrc="$HOME/.bashrc" profile="$HOME/.profile" fishrc="$HOME/.config/fish/config.fish" append_rc() { local file="$1" local payload="$2" mkdir -p "$(dirname "$file")" 2>/dev/null || return 0 if [ -f "$file" ] && grep -F "$marker" "$file" >/dev/null 2>&1; then return 0 fi if ! printf "\n%s\n%s\n" "$marker" "$payload" >> "$file" 2>/dev/null; then return 0 fi wrote=1 printf " ${D}│${N} %-10s ${G}✓${N} (PATH → %s)\n" "PATH" "$file" } case "$shell_name" in zsh) touch "$zshrc" 2>/dev/null || true append_rc "$zshrc" "$export_line" ;; bash) touch "$bashrc" 2>/dev/null || true append_rc "$bashrc" "$export_line" ;; fish) mkdir -p "$(dirname "$fishrc")" 2>/dev/null || true touch "$fishrc" 2>/dev/null || true append_rc "$fishrc" "$fish_line" ;; *) touch "$profile" 2>/dev/null || true append_rc "$profile" "$export_line" ;; esac if [ "$shell_name" != zsh ] && [ -f "$zshrc" ]; then append_rc "$zshrc" "$export_line" fi if [ "$shell_name" != bash ] && [ -f "$bashrc" ]; then append_rc "$bashrc" "$export_line" fi if [ "$wrote" = 1 ]; then printf "\n ${Y}open a new terminal${N} or ${C}source${N} the rc file above — otherwise ${C}$BIN${N} is not on PATH yet.\n" else case ":$PATH:" in *":$INSTALL_DIR:"*) ;; *) printf "\n ${Y}add to PATH:${N}\n" printf " ${C}export PATH=\"$INSTALL_DIR:\$PATH\"${N} ${D}(add to ~/.zshrc or ~/.bashrc)${N}\n" ;; esac fi } if [ "${1:-}" = "--path-only" ]; then ensure_path exit 0 fi main