|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Nanocode Installer |
| 5 | +# Usage: curl -fsSL https://raw.githubusercontent.com/HybridAIOne/nano/main/install.sh | bash |
| 6 | + |
| 7 | +REPO="HybridAIOne/nano" |
| 8 | +BINARY_NAME="nanocode" |
| 9 | +INSTALL_DIR="${NANOCODE_INSTALL_DIR:-$HOME/.local/bin}" |
| 10 | + |
| 11 | +# Colors |
| 12 | +RED='\033[0;31m' |
| 13 | +GREEN='\033[0;32m' |
| 14 | +YELLOW='\033[1;33m' |
| 15 | +NC='\033[0m' # No Color |
| 16 | + |
| 17 | +info() { echo -e "${GREEN}[INFO]${NC} $1"; } |
| 18 | +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } |
| 19 | +error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } |
| 20 | + |
| 21 | +# Detect OS and architecture |
| 22 | +detect_platform() { |
| 23 | + local os arch |
| 24 | + |
| 25 | + case "$(uname -s)" in |
| 26 | + Linux*) os="linux" ;; |
| 27 | + Darwin*) os="darwin" ;; |
| 28 | + *) error "Unsupported OS: $(uname -s)" ;; |
| 29 | + esac |
| 30 | + |
| 31 | + case "$(uname -m)" in |
| 32 | + x86_64|amd64) arch="x86_64" ;; |
| 33 | + aarch64|arm64) arch="aarch64" ;; |
| 34 | + *) error "Unsupported architecture: $(uname -m)" ;; |
| 35 | + esac |
| 36 | + |
| 37 | + echo "${os}-${arch}" |
| 38 | +} |
| 39 | + |
| 40 | +# Get latest release version |
| 41 | +get_latest_version() { |
| 42 | + curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | \ |
| 43 | + grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/' |
| 44 | +} |
| 45 | + |
| 46 | +# Download and install binary |
| 47 | +install_binary() { |
| 48 | + local version="$1" |
| 49 | + local platform="$2" |
| 50 | + local asset_name="${BINARY_NAME}-${platform}.tar.gz" |
| 51 | + local download_url="https://github.com/${REPO}/releases/download/${version}/${asset_name}" |
| 52 | + |
| 53 | + info "Downloading ${BINARY_NAME} ${version} for ${platform}..." |
| 54 | + |
| 55 | + local tmp_dir=$(mktemp -d) |
| 56 | + trap "rm -rf $tmp_dir" EXIT |
| 57 | + |
| 58 | + if ! curl -fsSL "$download_url" -o "$tmp_dir/${BINARY_NAME}.tar.gz" 2>/dev/null; then |
| 59 | + return 1 |
| 60 | + fi |
| 61 | + |
| 62 | + tar -xzf "$tmp_dir/${BINARY_NAME}.tar.gz" -C "$tmp_dir" |
| 63 | + |
| 64 | + mkdir -p "$INSTALL_DIR" |
| 65 | + mv "$tmp_dir/${BINARY_NAME}" "$INSTALL_DIR/${BINARY_NAME}" |
| 66 | + chmod +x "$INSTALL_DIR/${BINARY_NAME}" |
| 67 | + |
| 68 | + return 0 |
| 69 | +} |
| 70 | + |
| 71 | +# Build from source |
| 72 | +build_from_source() { |
| 73 | + info "Building from source..." |
| 74 | + |
| 75 | + if ! command -v cargo &>/dev/null; then |
| 76 | + error "Rust/Cargo not found. Install from https://rustup.rs" |
| 77 | + fi |
| 78 | + |
| 79 | + local tmp_dir=$(mktemp -d) |
| 80 | + trap "rm -rf $tmp_dir" EXIT |
| 81 | + |
| 82 | + git clone --depth 1 "https://github.com/${REPO}.git" "$tmp_dir/${BINARY_NAME}" |
| 83 | + cd "$tmp_dir/${BINARY_NAME}" |
| 84 | + |
| 85 | + cargo build --release |
| 86 | + |
| 87 | + mkdir -p "$INSTALL_DIR" |
| 88 | + mv target/release/${BINARY_NAME} "$INSTALL_DIR/${BINARY_NAME}" |
| 89 | + chmod +x "$INSTALL_DIR/${BINARY_NAME}" |
| 90 | +} |
| 91 | + |
| 92 | +# Check if install dir is in PATH |
| 93 | +check_path() { |
| 94 | + if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then |
| 95 | + warn "Add $INSTALL_DIR to your PATH:" |
| 96 | + echo "" |
| 97 | + echo " export PATH=\"\$PATH:$INSTALL_DIR\"" |
| 98 | + echo "" |
| 99 | + echo "Add this line to your ~/.bashrc, ~/.zshrc, or shell config." |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +main() { |
| 104 | + info "Installing ${BINARY_NAME}..." |
| 105 | + |
| 106 | + local platform=$(detect_platform) |
| 107 | + info "Detected platform: $platform" |
| 108 | + |
| 109 | + local version=$(get_latest_version) |
| 110 | + |
| 111 | + if [ -n "$version" ]; then |
| 112 | + info "Latest version: $version" |
| 113 | + |
| 114 | + if install_binary "$version" "$platform"; then |
| 115 | + info "Successfully installed ${BINARY_NAME} to $INSTALL_DIR/${BINARY_NAME}" |
| 116 | + else |
| 117 | + warn "Pre-built binary not available for $platform" |
| 118 | + build_from_source |
| 119 | + info "Successfully built and installed ${BINARY_NAME} to $INSTALL_DIR/${BINARY_NAME}" |
| 120 | + fi |
| 121 | + else |
| 122 | + warn "Could not determine latest version, building from source" |
| 123 | + build_from_source |
| 124 | + info "Successfully built and installed ${BINARY_NAME} to $INSTALL_DIR/${BINARY_NAME}" |
| 125 | + fi |
| 126 | + |
| 127 | + check_path |
| 128 | + |
| 129 | + echo "" |
| 130 | + info "Installation complete!" |
| 131 | + echo "" |
| 132 | + echo "Don't forget to set your API key:" |
| 133 | + echo " export OPENROUTER_API_KEY=\"your-api-key\"" |
| 134 | + echo " # or" |
| 135 | + echo " export HYBRIDAI_API_KEY=\"your-api-key\"" |
| 136 | + echo "" |
| 137 | + echo "Then run: ${BINARY_NAME}" |
| 138 | + echo "" |
| 139 | +} |
| 140 | + |
| 141 | +main "$@" |
0 commit comments