|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Rebuild ~/coding/hapi-driver from ~/.config/hapi/driver-manifest.yaml |
| 3 | +# |
| 4 | +# ~/coding/hapi-driver is READ-ONLY between rebuilds — this script is the only |
| 5 | +# supported way to change it. Hand-edits and cp-from-other-worktrees are forbidden. |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# hapi-driver-rebuild # rebuild only (no hub restart) |
| 9 | +# hapi-driver-rebuild --build-web # also rebuild web/dist |
| 10 | +# hapi-driver-rebuild --verify # run typecheck + test after merge |
| 11 | +# hapi-driver-rebuild --activate # swing hapi-active + restart hub (DESTRUCTIVE to live sessions) |
| 12 | +# |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +PRIMARY="${HAPI_PRIMARY:-$HOME/coding/hapi}" |
| 16 | +DRIVER="${HAPI_DRIVER:-$HOME/coding/hapi-driver}" |
| 17 | +MANIFEST="${HAPI_DRIVER_MANIFEST:-$HOME/.config/hapi/driver-manifest.yaml}" |
| 18 | +PARSE="$PRIMARY/scripts/tooling/parse-driver-manifest.mjs" |
| 19 | +DRIVER_BRANCH="${HAPI_DRIVER_BRANCH:-driver/integration}" |
| 20 | +BUN="${BUN:-$HOME/.bun/bin/bun}" |
| 21 | + |
| 22 | +BUILD_WEB=0 |
| 23 | +VERIFY=0 |
| 24 | +ACTIVATE=0 |
| 25 | + |
| 26 | +while [[ $# -gt 0 ]]; do |
| 27 | + case "$1" in |
| 28 | + --build-web) BUILD_WEB=1; shift ;; |
| 29 | + --verify) VERIFY=1; shift ;; |
| 30 | + --activate) ACTIVATE=1; shift ;; |
| 31 | + -h|--help) |
| 32 | + sed -n '2,12p' "$0" |
| 33 | + exit 0 |
| 34 | + ;; |
| 35 | + *) echo "Unknown option: $1" >&2; exit 2 ;; |
| 36 | + esac |
| 37 | +done |
| 38 | + |
| 39 | +if [[ ! -f "$MANIFEST" ]]; then |
| 40 | + echo "ERROR: manifest not found: $MANIFEST" >&2 |
| 41 | + echo "Copy example: mkdir -p ~/.config/hapi && cp $PRIMARY/docs/tooling/driver-manifest.example.yaml $MANIFEST" >&2 |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +if [[ ! -x "$PARSE" ]] && [[ ! -f "$PARSE" ]]; then |
| 46 | + echo "ERROR: parser missing: $PARSE" >&2 |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +mkdir -p "$(dirname "$MANIFEST")" |
| 51 | + |
| 52 | +echo "Fetching upstream..." |
| 53 | +git -C "$PRIMARY" fetch upstream |
| 54 | + |
| 55 | +SYNC_SCRIPT="$PRIMARY/scripts/tooling/hapi-sync-fork-main.sh" |
| 56 | +if [[ -x "$SYNC_SCRIPT" ]]; then |
| 57 | + if ! "$SYNC_SCRIPT" --check-only 2>/dev/null; then |
| 58 | + echo "ERROR: fork main is behind upstream/main. Run: hapi-sync-fork-main && git push origin main" >&2 |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +elif git -C "$PRIMARY" show-ref --verify --quiet refs/heads/main; then |
| 62 | + behind_main="$(git -C "$PRIMARY" rev-list --count main..upstream/main 2>/dev/null || echo 0)" |
| 63 | + if [[ "${behind_main:-0}" -gt 0 ]]; then |
| 64 | + echo "ERROR: $PRIMARY main is ${behind_main} commit(s) behind upstream/main" >&2 |
| 65 | + echo " Run: hapi-sync-fork-main" >&2 |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +fi |
| 69 | + |
| 70 | +if [[ -d "$DRIVER" ]] && [[ -n "$(git -C "$DRIVER" status --porcelain)" ]]; then |
| 71 | + echo "WARNING: $DRIVER has local changes — rebuild will reset the tree (stash or commit them elsewhere first)." >&2 |
| 72 | + echo " Only manifest-driven rebuilds belong on the driver tree. See docs/tooling/driver-soup.md" >&2 |
| 73 | +fi |
| 74 | + |
| 75 | +if [[ ! -d "$DRIVER" ]]; then |
| 76 | + echo "Creating driver worktree at $DRIVER (branch $DRIVER_BRANCH)..." |
| 77 | + git -C "$PRIMARY" worktree add -b "$DRIVER_BRANCH" "$DRIVER" upstream/main |
| 78 | +fi |
| 79 | + |
| 80 | +if [[ ! -e "$DRIVER/hub/.env" ]]; then |
| 81 | + echo "Linking hub/.env → ~/.hapi/hub.env" |
| 82 | + ln -s "$HOME/.hapi/hub.env" "$DRIVER/hub/.env" |
| 83 | +fi |
| 84 | + |
| 85 | +manifest_json="$("$BUN" run "$PARSE" "$MANIFEST")" |
| 86 | +base_ref="$(echo "$manifest_json" | jq -r '.base')" |
| 87 | +layer_count="$(echo "$manifest_json" | jq '.layers | length')" |
| 88 | + |
| 89 | +if echo "$manifest_json" | jq -e '.layers[] | select(.ref == "fix/web-scroll-guard-unwrap-race")' >/dev/null; then |
| 90 | + pr722_state="$(gh pr view 722 --repo "${HAPI_PR_REPO:-tiann/hapi}" --json state --jq '.state' 2>/dev/null || true)" |
| 91 | + if [[ "$pr722_state" == "MERGED" ]]; then |
| 92 | + echo "WARNING: upstream PR #722 is merged — drop fix/web-scroll-guard-unwrap-race from $MANIFEST" >&2 |
| 93 | + fi |
| 94 | +fi |
| 95 | + |
| 96 | +if [[ -n "$upstream_tip" && "$base_ref" == "upstream/main" ]]; then |
| 97 | + echo "Base: upstream/main @ $(git -C "$PRIMARY" log -1 --oneline "$upstream_tip")" |
| 98 | +fi |
| 99 | + |
| 100 | +echo "Resetting $DRIVER to $base_ref ($layer_count layer(s))..." |
| 101 | +git -C "$DRIVER" checkout -B "$DRIVER_BRANCH" "$base_ref" |
| 102 | + |
| 103 | +resolve_merge_ref() { |
| 104 | + local type="$1" ref="$2" |
| 105 | + case "$type" in |
| 106 | + branch|integrate) |
| 107 | + if git -C "$PRIMARY" rev-parse --verify "${ref}^{commit}" >/dev/null 2>&1; then |
| 108 | + echo "$ref" |
| 109 | + else |
| 110 | + echo "ERROR: layer ref not found: $ref" >&2 |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + ;; |
| 114 | + pr) |
| 115 | + local head_branch pr_repo="${HAPI_PR_REPO:-tiann/hapi}" |
| 116 | + head_branch="$(gh pr view "$ref" --repo "$pr_repo" --json headRefName --jq '.headRefName' 2>/dev/null || true)" |
| 117 | + if [[ -z "$head_branch" || "$head_branch" == "null" ]]; then |
| 118 | + echo "ERROR: could not resolve PR #$ref via gh (repo: $pr_repo)" >&2 |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + git -C "$PRIMARY" fetch origin "$head_branch" 2>/dev/null || true |
| 122 | + echo "origin/$head_branch" |
| 123 | + ;; |
| 124 | + *) |
| 125 | + echo "ERROR: unknown layer type: $type" >&2 |
| 126 | + exit 1 |
| 127 | + ;; |
| 128 | + esac |
| 129 | +} |
| 130 | + |
| 131 | +for i in $(seq 0 $((layer_count - 1))); do |
| 132 | + type="$(echo "$manifest_json" | jq -r ".layers[$i].type")" |
| 133 | + ref="$(echo "$manifest_json" | jq -r ".layers[$i].ref")" |
| 134 | + merge_ref="$(resolve_merge_ref "$type" "$ref")" |
| 135 | + |
| 136 | + echo "Layer $((i + 1))/$layer_count: merging $merge_ref ..." |
| 137 | + if ! git -C "$DRIVER" merge --no-edit "$merge_ref"; then |
| 138 | + echo "ERROR: merge conflict merging $merge_ref into $DRIVER_BRANCH" >&2 |
| 139 | + echo "Resolve in $DRIVER, commit, or fix manifest order." >&2 |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | +done |
| 143 | + |
| 144 | +echo "Driver HEAD: $(git -C "$DRIVER" log -1 --oneline)" |
| 145 | + |
| 146 | +if [[ "$BUILD_WEB" -eq 1 ]] || [[ ! -f "$DRIVER/web/dist/index.html" ]]; then |
| 147 | + echo "Building web..." |
| 148 | + if [[ ! -d "$DRIVER/node_modules" ]]; then |
| 149 | + echo "Installing dependencies (first driver build)..." |
| 150 | + (cd "$DRIVER" && "$BUN" install) |
| 151 | + fi |
| 152 | + (cd "$DRIVER/web" && "$BUN" run build) |
| 153 | +fi |
| 154 | + |
| 155 | +if [[ "$VERIFY" -eq 1 ]]; then |
| 156 | + echo "Running typecheck..." |
| 157 | + (cd "$DRIVER" && "$BUN" typecheck) |
| 158 | + echo "Running tests..." |
| 159 | + (cd "$DRIVER" && "$BUN" run test) |
| 160 | +fi |
| 161 | + |
| 162 | +echo "" |
| 163 | +echo "Driver rebuild complete: $DRIVER @ $(git -C "$DRIVER" rev-parse --short HEAD)" |
| 164 | +echo "Manifest: $MANIFEST" |
| 165 | +echo "Active hub: $(readlink -f "$HOME/coding/hapi-active" 2>/dev/null || echo '(no symlink)')" |
| 166 | + |
| 167 | +if [[ "$ACTIVATE" -eq 1 ]]; then |
| 168 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 169 | + echo " ACTIVATE: restarts hapi-hub + hapi-runner (kills sessions)" |
| 170 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 171 | + if [[ -t 0 ]]; then |
| 172 | + read -rp "Proceed with hapi-use-worktree $DRIVER? [y/N] " yn |
| 173 | + [[ "${yn,,}" == "y" ]] || { echo "Skipped activate."; exit 0; } |
| 174 | + else |
| 175 | + echo "Non-interactive: skipping activate (re-run with TTY or use hapi-use-worktree manually)." >&2 |
| 176 | + exit 0 |
| 177 | + fi |
| 178 | + exec hapi-use-worktree "$DRIVER" |
| 179 | +fi |
| 180 | + |
| 181 | +echo "" |
| 182 | +echo "To swing live hub (restarts service — kills sessions):" |
| 183 | +echo " hapi-use-worktree $DRIVER" |
0 commit comments