install.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. case ":$PATH:" in
  79. *":$BIN_DIR:"*) ;;
  80. *)
  81. echo ""
  82. echo "$BIN_DIR is not on your PATH. Add it:"
  83. echo " export PATH=\"$BIN_DIR:\$PATH\""
  84. ;;
  85. esac
  86. echo ""
  87. echo "Done. Run: codegraph --help"