install.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/bin/bash
  2. # Set your repository
  3. REPO="HelixDB/helix-db"
  4. # Function to run command with timeout
  5. run_with_timeout() {
  6. local timeout_duration=$1
  7. shift
  8. timeout "$timeout_duration" "$@"
  9. }
  10. # Function to get version from binary safely
  11. get_binary_version() {
  12. local binary_path=$1
  13. if [[ -f "$binary_path" && -x "$binary_path" ]]; then
  14. local version_output
  15. version_output=$(run_with_timeout 10s "$binary_path" --version 2>/dev/null)
  16. if [[ $? -eq 0 && -n "$version_output" ]]; then
  17. echo "$version_output" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1
  18. fi
  19. fi
  20. }
  21. # Fetch the latest release version from GitHub API
  22. VERSION=$(curl --silent "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name"' | sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
  23. if [[ -z "$VERSION" ]]; then
  24. echo "Failed to fetch the latest version. Please check your internet connection or the repository."
  25. exit 1
  26. fi
  27. # Remove 'v' prefix if present for comparison
  28. LATEST_VERSION=${VERSION#v}
  29. echo "Latest available version: $VERSION"
  30. echo "User home directory: $HOME"
  31. # Detect the operating system
  32. OS=$(uname -s)
  33. ARCH=$(uname -m)
  34. INSTALL_DIR="$HOME/.local/bin"
  35. mkdir -p "$INSTALL_DIR"
  36. # Add the installation directory to PATH immediately for this session
  37. export PATH="$INSTALL_DIR:$PATH"
  38. # Check if binary already exists and get its version
  39. EXISTING_BINARY="$INSTALL_DIR/helix"
  40. CURRENT_VERSION=""
  41. if [[ -f "$EXISTING_BINARY" ]]; then
  42. echo "Existing binary found at $EXISTING_BINARY"
  43. CURRENT_VERSION=$(get_binary_version "$EXISTING_BINARY")
  44. if [[ -n "$CURRENT_VERSION" ]]; then
  45. echo "Current installed version: $CURRENT_VERSION"
  46. # Compare versions (simple string comparison should work for semver)
  47. if [[ "$CURRENT_VERSION" == "$LATEST_VERSION" ]]; then
  48. echo "You already have the latest version ($CURRENT_VERSION) installed."
  49. echo "To force reinstall, delete $EXISTING_BINARY and run this script again."
  50. # Still verify it works
  51. echo "Verifying current installation..."
  52. if run_with_timeout 10s helix --version >/dev/null 2>&1; then
  53. echo "Helix CLI is working correctly!"
  54. exit 0
  55. else
  56. echo "Current installation appears to be broken. Proceeding with reinstall..."
  57. fi
  58. else
  59. echo "Updating from version $CURRENT_VERSION to $LATEST_VERSION"
  60. fi
  61. else
  62. echo "Existing binary found but version check failed. Proceeding with reinstall..."
  63. fi
  64. fi
  65. # Ensure that $HOME/.local/bin is in the PATH permanently
  66. if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
  67. echo "Adding $HOME/.local/bin to PATH permanently"
  68. # Determine shell config file
  69. if [[ "$SHELL" == *"bash"* ]]; then
  70. SHELL_CONFIG="$HOME/.bashrc"
  71. elif [[ "$SHELL" == *"zsh"* ]]; then
  72. SHELL_CONFIG="$HOME/.zshrc"
  73. fi
  74. # Add to shell config if not already present
  75. if [[ -f "$SHELL_CONFIG" ]]; then
  76. if ! grep -q 'export PATH="$HOME/.local/bin:$PATH"' "$SHELL_CONFIG"; then
  77. echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$SHELL_CONFIG"
  78. fi
  79. fi
  80. fi
  81. # Determine the appropriate binary to download
  82. if [[ "$OS" == "Linux" && "$ARCH" == "x86_64" ]]; then
  83. FILE="helix-cli-linux-amd64"
  84. elif [[ "$OS" == "Linux" && "$ARCH" == "aarch64" ]]; then
  85. FILE="helix-cli-linux-arm64"
  86. elif [[ "$OS" == "Darwin" && "$ARCH" == "arm64" ]]; then
  87. FILE="helix-cli-macos-arm64"
  88. elif [[ "$OS" == "Darwin" && "$ARCH" == "x86_64" ]]; then
  89. FILE="helix-cli-macos-amd64"
  90. else
  91. echo "Unsupported system: This installer only works on Linux AMD64 and macOS ARM64"
  92. echo "Your system is: $OS $ARCH"
  93. exit 1
  94. fi
  95. # Download the binary
  96. URL="https://github.com/$REPO/releases/download/$VERSION/$FILE"
  97. echo "Downloading from $URL"
  98. # Create a temporary file for download
  99. TEMP_BINARY=$(mktemp)
  100. curl -L "$URL" -o "$TEMP_BINARY"
  101. if [[ $? -ne 0 ]]; then
  102. echo "Failed to download the binary"
  103. rm -f "$TEMP_BINARY"
  104. exit 1
  105. fi
  106. # Make it executable
  107. chmod +x "$TEMP_BINARY"
  108. # Test the downloaded binary with timeout
  109. echo "Testing downloaded binary..."
  110. if run_with_timeout 10s "$TEMP_BINARY" --version &> /dev/null; then
  111. echo "Downloaded binary is working. Installing..."
  112. mv "$TEMP_BINARY" "$INSTALL_DIR/helix"
  113. else
  114. echo "Downloaded binary is incompatible with system or has issues. Falling back to building from source..."
  115. rm -f "$TEMP_BINARY"
  116. # Ensure Rust is installed
  117. if ! command -v cargo &> /dev/null; then
  118. echo "Installing Rust first..."
  119. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
  120. source "$HOME/.cargo/env"
  121. fi
  122. # Clone and build from source
  123. TMP_DIR=$(mktemp -d)
  124. echo "Building from source in $TMP_DIR..."
  125. git clone "https://github.com/$REPO.git" "$TMP_DIR"
  126. if [[ $? -ne 0 ]]; then
  127. echo "Failed to clone repository"
  128. rm -rf "$TMP_DIR"
  129. exit 1
  130. fi
  131. cd "$TMP_DIR"
  132. git checkout "$VERSION"
  133. if [[ $? -ne 0 ]]; then
  134. echo "Failed to checkout version $VERSION"
  135. cd - > /dev/null
  136. rm -rf "$TMP_DIR"
  137. exit 1
  138. fi
  139. echo "Building helix-cli (this may take a while)..."
  140. cargo build --release --bin helix
  141. if [[ $? -ne 0 ]]; then
  142. echo "Failed to build from source"
  143. cd - > /dev/null
  144. rm -rf "$TMP_DIR"
  145. exit 1
  146. fi
  147. # Test the built binary
  148. if run_with_timeout 10s "./target/release/helix" --version &> /dev/null; then
  149. mv "target/release/helix" "$INSTALL_DIR/helix"
  150. echo "Successfully built and installed from source"
  151. else
  152. echo "Built binary failed version check"
  153. cd - > /dev/null
  154. rm -rf "$TMP_DIR"
  155. exit 1
  156. fi
  157. cd - > /dev/null
  158. rm -rf "$TMP_DIR"
  159. fi
  160. # Verify installation and ensure command is available
  161. echo "Verifying installation..."
  162. if run_with_timeout 10s helix --version >/dev/null 2>&1; then
  163. INSTALLED_VERSION=$(get_binary_version "$INSTALL_DIR/helix")
  164. echo "Installation successful!"
  165. echo "Helix CLI version $INSTALLED_VERSION has been installed to $INSTALL_DIR/helix"
  166. echo "The 'helix' command is now available in your current shell"
  167. if [[ -n "$SHELL_CONFIG" ]]; then
  168. echo "For permanent installation, please restart your shell or run:"
  169. echo " source $SHELL_CONFIG"
  170. fi
  171. else
  172. echo "Installation failed - binary is not responding to version check."
  173. exit 1
  174. fi
  175. # Install Rust if needed
  176. echo "Checking dependencies..."
  177. if ! command -v cargo &> /dev/null; then
  178. echo "Rust/Cargo is not installed. Installing Rust..."
  179. if [[ "$OS" == "Linux" ]] || [[ "$OS" == "Darwin" ]]; then
  180. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
  181. source "$HOME/.cargo/env"
  182. elif [[ "$OS" == "Windows_NT" ]]; then
  183. curl --proto '=https' --tlsv1.2 -sSf https://win.rustup.rs -o rustup-init.exe
  184. ./rustup-init.exe -y
  185. rm rustup-init.exe
  186. fi
  187. else
  188. echo "Rust/Cargo is already installed."
  189. fi
  190. # Final verification that helix is working
  191. echo "Final verification..."
  192. if run_with_timeout 10s helix --version; then
  193. echo "Helix CLI is working correctly!"
  194. else
  195. echo "Warning: Helix CLI may not be working correctly."
  196. echo "Please try running 'source $SHELL_CONFIG' or restart your terminal."
  197. exit 1
  198. fi