install.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. PATH sanity. Two ways this install can fail to be the codegraph that runs:
  79. # 1. $BIN_DIR isn't on PATH at all.
  80. # 2. A *different* codegraph sits earlier on PATH and shadows ours — most
  81. # often a stale `npm i -g @colbymchenry/codegraph`, whose launcher keeps
  82. # running its own version-pinned bundle, so `codegraph --version` disagrees
  83. # with what we just installed (issue #1071).
  84. # Walk PATH once: note whether $BIN_DIR is present and which codegraph wins.
  85. on_path=0
  86. winner=""
  87. oldifs="$IFS"; IFS=:
  88. for dir in $PATH; do
  89. [ -n "$dir" ] || continue
  90. if [ "$dir" = "$BIN_DIR" ]; then on_path=1; fi
  91. if [ -z "$winner" ] && [ -x "$dir/codegraph" ] && [ ! -d "$dir/codegraph" ]; then
  92. winner="$dir/codegraph"
  93. fi
  94. done
  95. IFS="$oldifs"
  96. if [ "$on_path" -eq 0 ]; then
  97. echo ""
  98. echo "$BIN_DIR is not on your PATH. Add it:"
  99. echo " export PATH=\"$BIN_DIR:\$PATH\""
  100. elif [ -n "$winner" ] && [ "$winner" != "$BIN_DIR/codegraph" ]; then
  101. echo ""
  102. echo "Warning: another codegraph is earlier on your PATH and will run instead:"
  103. echo " $winner"
  104. echo " (this install: $BIN_DIR/codegraph)"
  105. echo "If 'codegraph --version' shows an unexpected version, remove the other copy"
  106. echo "(e.g. 'npm rm -g @colbymchenry/codegraph') or put $BIN_DIR first on PATH."
  107. fi
  108. echo ""
  109. echo "Done. Run: codegraph --help"