install.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/sh
  2. #
  3. # CodeGraph standalone installer.
  4. #
  5. # Downloads a self-contained bundle (a vendored Node runtime + the app) from
  6. # GitHub Releases. No Node.js, no build tools, no npm required — ideal for a
  7. # fresh Linux VPS over SSH.
  8. #
  9. # curl -fsSL https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.sh | sh
  10. #
  11. # Upgrade: run `codegraph upgrade` (or just re-run the same command).
  12. # Uninstall: curl -fsSL .../install.sh | sh -s -- --uninstall
  13. #
  14. # Environment:
  15. # CODEGRAPH_VERSION release tag to install (default: latest)
  16. # CODEGRAPH_INSTALL_DIR bundle location (default: ~/.codegraph)
  17. # CODEGRAPH_BIN_DIR symlink location (default: ~/.local/bin)
  18. set -eu
  19. REPO="colbymchenry/codegraph"
  20. INSTALL_DIR="${CODEGRAPH_INSTALL_DIR:-$HOME/.codegraph}"
  21. BIN_DIR="${CODEGRAPH_BIN_DIR:-$HOME/.local/bin}"
  22. if [ "${1:-}" = "--uninstall" ]; then
  23. rm -f "$BIN_DIR/codegraph"
  24. rm -rf "$INSTALL_DIR"
  25. echo "CodeGraph uninstalled (removed $INSTALL_DIR and $BIN_DIR/codegraph)."
  26. exit 0
  27. fi
  28. # 1. Detect platform → target triple matching the release archives.
  29. os="$(uname -s)"
  30. arch="$(uname -m)"
  31. case "$os" in
  32. Darwin) os="darwin" ;;
  33. Linux) os="linux" ;;
  34. *) echo "codegraph: unsupported OS '$os'." >&2; exit 1 ;;
  35. esac
  36. case "$arch" in
  37. arm64|aarch64) arch="arm64" ;;
  38. x86_64|amd64) arch="x64" ;;
  39. *) echo "codegraph: unsupported architecture '$arch'." >&2; exit 1 ;;
  40. esac
  41. target="${os}-${arch}"
  42. # 2. Resolve the version (latest release unless pinned).
  43. #
  44. # Resolve "latest" from the releases/latest *web* redirect, not the GitHub API:
  45. # the unauthenticated API is rate-limited to 60 requests/hour per IP and returns
  46. # 403 once exhausted — routine on shared/cloud hosts and CI (issue #325). The
  47. # redirect (github.com/<repo>/releases/latest -> .../releases/tag/vX.Y.Z) has no
  48. # such limit. Fall back to the API if the redirect can't be read.
  49. version="${CODEGRAPH_VERSION:-}"
  50. if [ -z "$version" ]; then
  51. version="$(curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest" \
  52. | sed -n 's#.*/releases/tag/##p')"
  53. fi
  54. if [ -z "$version" ]; then
  55. version="$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" \
  56. | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -n1)"
  57. fi
  58. [ -n "$version" ] || { echo "codegraph: could not resolve latest version; set CODEGRAPH_VERSION (e.g. CODEGRAPH_VERSION=v0.9.4)." >&2; exit 1; }
  59. # Release tags are vX.Y.Z; accept a bare X.Y.Z in CODEGRAPH_VERSION too.
  60. case "$version" in v*) ;; *) version="v$version" ;; esac
  61. # 3. Download + extract the bundle.
  62. url="https://github.com/$REPO/releases/download/$version/codegraph-${target}.tar.gz"
  63. echo "Installing CodeGraph $version ($target)..."
  64. tmp="$(mktemp -d)"
  65. trap 'rm -rf "$tmp"' EXIT
  66. curl -fsSL "$url" -o "$tmp/cg.tar.gz" || { echo "codegraph: download failed: $url" >&2; exit 1; }
  67. dest="$INSTALL_DIR/versions/$version"
  68. rm -rf "$dest"
  69. mkdir -p "$dest"
  70. # Archives contain a top-level codegraph-<target>/ dir; strip it.
  71. tar -xzf "$tmp/cg.tar.gz" -C "$dest" --strip-components=1
  72. # 4. Symlink the launcher onto PATH and mark the current version.
  73. mkdir -p "$BIN_DIR"
  74. ln -sf "$dest/bin/codegraph" "$BIN_DIR/codegraph"
  75. ln -sfn "$dest" "$INSTALL_DIR/current"
  76. echo "Installed to $dest"
  77. echo "Linked $BIN_DIR/codegraph"
  78. # 5. Prune older bundles so they don't pile up across upgrades (issue #1074).
  79. # Each release lives in its own versions/<v> dir (~50 MB with the vendored Node
  80. # runtime). `codegraph upgrade` re-runs this script, which drops in a new dir
  81. # and re-points `current` + the launcher — but it never removed the old dirs, so
  82. # they accumulated indefinitely. Keep only what we just installed ($dest) and
  83. # delete the rest. Safe even if a daemon is still executing an older bundle: on
  84. # POSIX the inode stays alive until that process exits, so removing the dir can't
  85. # break a running process. (Windows installs overwrite a single dir in place and
  86. # never reach this.) The markers below let a unit test run this exact block.
  87. # >>> CODEGRAPH_PRUNE_OLD_VERSIONS
  88. pruned=0
  89. if [ -d "$INSTALL_DIR/versions" ]; then
  90. for d in "$INSTALL_DIR/versions"/*; do
  91. [ -d "$d" ] || continue
  92. if [ "$d" != "$dest" ]; then
  93. if rm -rf "$d"; then
  94. pruned=$((pruned + 1))
  95. fi
  96. fi
  97. done
  98. fi
  99. if [ "$pruned" -gt 0 ]; then
  100. echo "Removed $pruned older version(s)"
  101. fi
  102. # <<< CODEGRAPH_PRUNE_OLD_VERSIONS
  103. # 6. PATH sanity. Two ways this install can fail to be the codegraph that runs:
  104. # 1. $BIN_DIR isn't on PATH at all.
  105. # 2. A *different* codegraph sits earlier on PATH and shadows ours — most
  106. # often a stale `npm i -g @colbymchenry/codegraph`, whose launcher keeps
  107. # running its own version-pinned bundle, so `codegraph --version` disagrees
  108. # with what we just installed (issue #1071).
  109. # Walk PATH once: note whether $BIN_DIR is present and which codegraph wins.
  110. on_path=0
  111. winner=""
  112. oldifs="$IFS"; IFS=:
  113. for dir in $PATH; do
  114. [ -n "$dir" ] || continue
  115. if [ "$dir" = "$BIN_DIR" ]; then on_path=1; fi
  116. if [ -z "$winner" ] && [ -x "$dir/codegraph" ] && [ ! -d "$dir/codegraph" ]; then
  117. winner="$dir/codegraph"
  118. fi
  119. done
  120. IFS="$oldifs"
  121. if [ "$on_path" -eq 0 ]; then
  122. echo ""
  123. echo "$BIN_DIR is not on your PATH. Add it:"
  124. echo " export PATH=\"$BIN_DIR:\$PATH\""
  125. elif [ -n "$winner" ] && [ "$winner" != "$BIN_DIR/codegraph" ]; then
  126. echo ""
  127. echo "Warning: another codegraph is earlier on your PATH and will run instead:"
  128. echo " $winner"
  129. echo " (this install: $BIN_DIR/codegraph)"
  130. echo "If 'codegraph --version' shows an unexpected version, remove the other copy"
  131. echo "(e.g. 'npm rm -g @colbymchenry/codegraph') or put $BIN_DIR first on PATH."
  132. fi
  133. echo ""
  134. echo "Done. Run: codegraph --help"