-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·386 lines (326 loc) · 11.4 KB
/
install.sh
File metadata and controls
executable file
·386 lines (326 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/bin/bash
set -e
# ============================================================
# Sage Agent Installer
# https://github.com/majiayu000/sage
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/majiayu000/sage/main/install.sh | bash
#
# Options:
# SAGE_VERSION - Specific version to install (default: latest)
# SAGE_INSTALL_DIR - Installation directory (default: ~/.local/bin)
#
# ============================================================
VERSION="${SAGE_VERSION:-latest}"
INSTALL_DIR="${SAGE_INSTALL_DIR:-$HOME/.local/bin}"
REPO="majiayu000/sage"
BINARY_NAME="sage"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# ============================================================
# Helper Functions
# ============================================================
print_banner() {
echo ""
echo -e "${CYAN}${BOLD}"
cat << 'EOF'
____
/ ___| __ _ __ _ ___
\___ \ / _` |/ _` |/ _ \
___) | (_| | (_| | __/
|____/ \__,_|\__, |\___|
|___/
EOF
echo -e "${NC}"
echo -e "${BOLD}Blazing fast code agent in pure Rust${NC}"
echo ""
}
info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
success() {
echo -e "${GREEN}[OK]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
# ============================================================
# Platform Detection
# ============================================================
detect_platform() {
local os arch
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$os" in
linux)
case "$arch" in
x86_64|amd64)
echo "x86_64-unknown-linux-gnu"
;;
aarch64|arm64)
echo "aarch64-unknown-linux-gnu"
;;
armv7l)
echo "armv7-unknown-linux-gnueabihf"
;;
*)
echo "unsupported"
;;
esac
;;
darwin)
case "$arch" in
x86_64|amd64)
echo "x86_64-apple-darwin"
;;
arm64|aarch64)
echo "aarch64-apple-darwin"
;;
*)
echo "unsupported"
;;
esac
;;
mingw*|msys*|cygwin*)
case "$arch" in
x86_64|amd64)
echo "x86_64-pc-windows-msvc"
;;
*)
echo "unsupported"
;;
esac
;;
*)
echo "unsupported"
;;
esac
}
# ============================================================
# Version Management
# ============================================================
get_latest_version() {
local url="https://api.github.com/repos/${REPO}/releases/latest"
local version
if command -v curl &> /dev/null; then
version=$(curl -sL "$url" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
elif command -v wget &> /dev/null; then
version=$(wget -qO- "$url" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
else
error "curl or wget is required"
fi
if [ -z "$version" ]; then
error "Failed to fetch latest version. Please check your internet connection."
fi
echo "$version"
}
# ============================================================
# Download and Install
# ============================================================
download_and_install() {
local platform=$1
local version=$2
if [ "$version" = "latest" ]; then
info "Fetching latest version..."
version=$(get_latest_version)
fi
# Remove 'v' prefix if present for filename
local version_num="${version#v}"
local filename="${BINARY_NAME}-${version}-${platform}.tar.gz"
local url="https://github.com/${REPO}/releases/download/${version}/${filename}"
info "Downloading Sage ${version} for ${platform}..."
# Create temp directory
local tmp_dir
tmp_dir=$(mktemp -d)
trap "rm -rf '$tmp_dir'" EXIT
# Download
local download_path="$tmp_dir/sage.tar.gz"
if command -v curl &> /dev/null; then
if ! curl -fsSL "$url" -o "$download_path" 2>/dev/null; then
# Try alternative naming convention
filename="${BINARY_NAME}-v${version_num}-${platform}.tar.gz"
url="https://github.com/${REPO}/releases/download/${version}/${filename}"
curl -fsSL "$url" -o "$download_path" || error "Download failed. URL: $url"
fi
elif command -v wget &> /dev/null; then
wget -q "$url" -O "$download_path" || error "Download failed. URL: $url"
else
error "curl or wget is required"
fi
success "Downloaded successfully"
# Extract
info "Extracting..."
tar -xzf "$download_path" -C "$tmp_dir" || error "Extraction failed"
# Find binary (might be in subdirectory)
local binary_path
binary_path=$(find "$tmp_dir" -name "$BINARY_NAME" -type f -perm -u+x 2>/dev/null | head -1)
if [ -z "$binary_path" ]; then
binary_path=$(find "$tmp_dir" -name "$BINARY_NAME" -type f 2>/dev/null | head -1)
fi
if [ -z "$binary_path" ]; then
error "Binary not found in archive"
fi
# Install
info "Installing to ${INSTALL_DIR}..."
mkdir -p "$INSTALL_DIR"
mv "$binary_path" "$INSTALL_DIR/$BINARY_NAME"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
success "Installed successfully!"
}
# ============================================================
# PATH Setup
# ============================================================
setup_path() {
local shell_rc=""
local shell_name=""
# Detect shell config file
if [ -n "$ZSH_VERSION" ] || [ "$SHELL" = "$(which zsh 2>/dev/null)" ]; then
shell_rc="$HOME/.zshrc"
shell_name="zsh"
elif [ -n "$BASH_VERSION" ] || [ "$SHELL" = "$(which bash 2>/dev/null)" ]; then
if [ -f "$HOME/.bashrc" ]; then
shell_rc="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
shell_rc="$HOME/.bash_profile"
fi
shell_name="bash"
elif [ -f "$HOME/.profile" ]; then
shell_rc="$HOME/.profile"
shell_name="sh"
fi
# Check if already in PATH
if echo "$PATH" | grep -q "$INSTALL_DIR"; then
return 0
fi
# Add to shell config if found
if [ -n "$shell_rc" ]; then
if ! grep -q "$INSTALL_DIR" "$shell_rc" 2>/dev/null; then
echo "" >> "$shell_rc"
echo "# Sage Agent" >> "$shell_rc"
echo "export PATH=\"\$PATH:$INSTALL_DIR\"" >> "$shell_rc"
warn "Added ${INSTALL_DIR} to PATH in ${shell_rc}"
echo ""
echo -e " ${YELLOW}Run this to use sage immediately:${NC}"
echo -e " ${CYAN}source ${shell_rc}${NC}"
echo ""
echo -e " ${YELLOW}Or restart your terminal.${NC}"
fi
else
warn "Could not detect shell config file."
echo ""
echo -e " ${YELLOW}Add this to your shell config:${NC}"
echo -e " ${CYAN}export PATH=\"\$PATH:$INSTALL_DIR\"${NC}"
fi
}
# ============================================================
# Verify Installation
# ============================================================
verify_installation() {
local sage_path="$INSTALL_DIR/$BINARY_NAME"
if [ -x "$sage_path" ]; then
echo ""
echo -e "${GREEN}${BOLD}Installation complete!${NC}"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo -e " ${BOLD}Get started:${NC}"
echo ""
echo -e " ${CYAN}sage --help${NC} Show help"
echo -e " ${CYAN}sage interactive${NC} Start interactive mode"
echo -e " ${CYAN}sage \"Your task\"${NC} Run a one-shot task"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo -e " ${BOLD}Documentation:${NC} ${BLUE}https://github.com/${REPO}${NC}"
echo ""
# Show version if possible
if command -v "$sage_path" &> /dev/null || [ -x "$sage_path" ]; then
echo -e " ${BOLD}Installed version:${NC} $($sage_path --version 2>/dev/null || echo 'unknown')"
echo ""
fi
else
error "Installation verification failed. Binary not found at $sage_path"
fi
}
# ============================================================
# Build from Source (Fallback)
# ============================================================
build_from_source() {
info "Building from source..."
if ! command -v cargo &> /dev/null; then
error "Rust is not installed. Please install Rust first: https://rustup.rs"
fi
if ! command -v git &> /dev/null; then
error "Git is not installed"
fi
local tmp_dir
tmp_dir=$(mktemp -d)
trap "rm -rf '$tmp_dir'" EXIT
info "Cloning repository..."
git clone --depth 1 "https://github.com/${REPO}.git" "$tmp_dir/sage"
info "Building (this may take a few minutes)..."
cd "$tmp_dir/sage"
cargo build --release
info "Installing..."
mkdir -p "$INSTALL_DIR"
cp "target/release/$BINARY_NAME" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
success "Built and installed successfully!"
}
# ============================================================
# Main
# ============================================================
main() {
print_banner
# Check for help flag
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo "Usage: install.sh [OPTIONS]"
echo ""
echo "Options:"
echo " --help, -h Show this help message"
echo " --source Build from source instead of downloading binary"
echo ""
echo "Environment variables:"
echo " SAGE_VERSION Version to install (default: latest)"
echo " SAGE_INSTALL_DIR Installation directory (default: ~/.local/bin)"
echo ""
exit 0
fi
# Check for source build flag
if [ "$1" = "--source" ]; then
build_from_source
setup_path
verify_installation
exit 0
fi
# Detect platform
local platform
platform=$(detect_platform)
if [ "$platform" = "unsupported" ]; then
warn "Pre-built binary not available for $(uname -s)/$(uname -m)"
echo ""
echo -e " ${YELLOW}Attempting to build from source...${NC}"
echo ""
build_from_source
setup_path
verify_installation
exit 0
fi
info "Detected platform: ${platform}"
# Download and install
download_and_install "$platform" "$VERSION"
setup_path
verify_installation
}
main "$@"