-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinstall-termux.sh
More file actions
544 lines (456 loc) · 15.2 KB
/
install-termux.sh
File metadata and controls
544 lines (456 loc) · 15.2 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/usr/bin/env bash
##
# KolosalCode Termux Installer (Build from Source)
# Installs KolosalCode on Android via Termux without root
# Builds from the local repository source
#
# Usage:
# cd kolosal-cli
# bash install-termux.sh
#
# Requirements:
# - Termux with pkg
# - Internet connection (for npm dependencies)
##
# Exit on error
set -e
# Version
VERSION="${VERSION:-0.1.3}"
# GitHub repository (for reference)
GITHUB_REPO="KolosalAI/kolosal-cli"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'
# Print functions using printf for compatibility
print_info() {
printf "${BLUE}ℹ${NC} %s\n" "$1"
}
print_success() {
printf "${GREEN}✓${NC} %s\n" "$1"
}
print_warning() {
printf "${YELLOW}⚠${NC} %s\n" "$1"
}
print_error() {
printf "${RED}✗${NC} %s\n" "$1"
}
print_header() {
printf "\n${BOLD}%s${NC}\n" "$1"
printf "================================\n"
}
# Get the directory where this script is located (the repository root)
get_repo_dir() {
# Use $0 for script path, works in most shells
local script_path="$0"
# Handle relative paths
if [ -z "${script_path%%/*}" ]; then
# Absolute path
script_path="$script_path"
else
# Relative path
script_path="$(pwd)/$script_path"
fi
# Get directory and resolve to absolute path
cd "$(dirname "$script_path")" && pwd
}
REPO_DIR="$(get_repo_dir)"
# Check if running in Termux
check_termux() {
if [ -z "$PREFIX" ]; then
print_error "This script is designed for Termux on Android."
print_info "The PREFIX environment variable is not set."
print_info "Please run this inside Termux."
exit 1
fi
if [ ! -d "$PREFIX" ]; then
print_error "Termux PREFIX directory not found: $PREFIX"
exit 1
fi
}
# Detect Architecture
detect_arch() {
local arch
arch=$(uname -m)
case "$arch" in
aarch64|arm64)
printf "arm64"
;;
armv7l|armv8l)
printf "arm"
;;
x86_64)
printf "x64"
;;
i686|i386)
printf "x86"
;;
*)
print_error "Unsupported architecture: $arch"
print_info "KolosalCode on Termux supports: arm64, arm, x64"
exit 1
;;
esac
}
# Check if required files exist in the repository
check_repo_files() {
print_info "Checking repository files..."
print_info "Repository: $REPO_DIR"
if [ ! -f "$REPO_DIR/package.json" ]; then
print_error "Required file not found: package.json"
print_info "Please run this script from the kolosal-cli repository root."
exit 1
fi
if [ ! -f "$REPO_DIR/esbuild.config.js" ]; then
print_error "Required file not found: esbuild.config.js"
print_info "Please run this script from the kolosal-cli repository root."
exit 1
fi
print_success "Repository files verified"
}
# Install required dependencies via pkg
install_dependencies() {
print_header "Installing Dependencies"
print_info "Updating package lists..."
pkg update -y 2>/dev/null || true
print_info "Upgrading existing packages (this may take a while)..."
if pkg upgrade -y 2>&1; then
print_success "Packages upgraded"
else
print_warning "Some packages failed to upgrade, continuing..."
fi
print_info "Installing required packages..."
# Install packages one by one for better error handling
for package in nodejs-lts git python make clang; do
if pkg list-installed 2>/dev/null | grep -q "^$package/"; then
print_success "$package is already installed"
else
print_info "Installing $package..."
if pkg install -y "$package" 2>&1; then
print_success "$package installed"
else
print_warning "Failed to install $package, continuing..."
fi
fi
done
# Verify Node.js works
if command -v node >/dev/null 2>&1; then
if node --version >/dev/null 2>&1; then
print_success "Node.js is working: $(node --version)"
else
print_error "Node.js is installed but not working!"
print_info "Try running: pkg upgrade"
print_info "Then run this installer again."
exit 1
fi
else
print_error "Node.js is not installed!"
print_info "Try running manually: pkg install nodejs-lts"
exit 1
fi
print_success "Dependencies installed"
}
# Build the project from local source
build_project() {
print_header "Building KolosalCode"
cd "$REPO_DIR"
# Check Node.js version
if ! command -v node >/dev/null 2>&1; then
print_error "Node.js not found. Please install with: pkg install nodejs-lts"
exit 1
fi
local node_version
node_version=$(node --version)
print_info "Node.js version: $node_version"
local npm_version
npm_version=$(npm --version)
print_info "npm version: $npm_version"
# Clean previous build artifacts if they exist
if [ -d "$REPO_DIR/bundle" ]; then
print_info "Cleaning previous build..."
rm -rf "$REPO_DIR/bundle"
fi
# Clean node_modules to ensure fresh install without cached problematic scripts
if [ -d "$REPO_DIR/node_modules" ]; then
print_info "Cleaning node_modules for fresh install..."
rm -rf "$REPO_DIR/node_modules"
fi
# Install npm dependencies
# Use --ignore-scripts because some packages have postinstall scripts
# that don't work on Android (e.g., node-pty needs Android NDK, @lvce-editor/ripgrep
# doesn't recognize Android platform). We'll use Termux's system ripgrep instead.
print_info "Installing npm dependencies..."
print_info "This may take a while..."
print_info "Skipping native modules that don't support Android..."
# Force npm to ignore scripts via environment variable as well
export npm_config_ignore_scripts=true
# Use npm install with --ignore-scripts to skip postinstall scripts that fail on Android
# (e.g., node-pty, @lvce-editor/ripgrep). We do NOT use --omit=optional because
# esbuild needs its platform-specific binaries (@esbuild/android-arm64) which are optional.
if [ -f "package-lock.json" ]; then
if npm ci --ignore-scripts 2>&1; then
print_success "Dependencies installed with npm ci"
else
print_warning "npm ci failed, trying npm install..."
if npm install --ignore-scripts 2>&1; then
print_success "Dependencies installed with npm install"
else
print_error "Failed to install dependencies"
exit 1
fi
fi
else
if npm install --ignore-scripts 2>&1; then
print_success "Dependencies installed"
else
print_error "Failed to install dependencies"
exit 1
fi
fi
# Generate git commit info (if the script exists)
if [ -f "scripts/generate-git-commit-info.js" ]; then
print_info "Generating git commit info..."
node scripts/generate-git-commit-info.js 2>/dev/null || true
fi
# Bundle the application using esbuild
print_info "Bundling application with esbuild..."
if [ -f "esbuild.config.js" ]; then
if node esbuild.config.js; then
print_success "Bundle created"
else
print_error "Failed to bundle application"
exit 1
fi
else
print_error "esbuild.config.js not found"
exit 1
fi
# Copy bundle assets
if [ -f "scripts/copy_bundle_assets.js" ]; then
print_info "Copying bundle assets..."
node scripts/copy_bundle_assets.js 2>/dev/null || true
fi
# Verify bundle was created
if [ ! -d "$REPO_DIR/bundle" ]; then
print_error "Bundle directory was not created!"
exit 1
fi
if [ ! -f "$REPO_DIR/bundle/gemini.js" ]; then
print_error "Main bundle file (gemini.js) not found!"
exit 1
fi
print_success "Build complete"
}
# Install the built application
install_app() {
print_header "Installing KolosalCode"
local install_dir="$PREFIX/opt/kolosal-code"
local bin_dir="$PREFIX/bin"
print_info "Installing to $install_dir..."
# Remove existing installation if present
if [ -d "$install_dir" ]; then
print_info "Removing previous installation..."
rm -rf "$install_dir"
fi
# Create installation directory
mkdir -p "$install_dir"
mkdir -p "$install_dir/lib"
mkdir -p "$install_dir/bin"
mkdir -p "$bin_dir"
# Copy bundle
print_info "Copying bundle..."
cp -R "$REPO_DIR/bundle" "$install_dir/lib/"
# Copy required node_modules (external dependencies that aren't bundled)
if [ -d "$REPO_DIR/node_modules" ]; then
print_info "Copying required node_modules..."
mkdir -p "$install_dir/lib/node_modules"
# Copy only necessary external modules that can't be bundled
for dep in "tiktoken" "node-pty" "@lydell"; do
if [ -d "$REPO_DIR/node_modules/$dep" ]; then
dest="$install_dir/lib/node_modules/$dep"
mkdir -p "$(dirname "$dest")"
cp -R "$REPO_DIR/node_modules/$dep" "$dest" 2>/dev/null || true
fi
done
fi
# Create launcher script that uses Termux's Node.js
print_info "Creating launcher script..."
cat > "$install_dir/bin/kolosal" << 'LAUNCHER_EOF'
#!/usr/bin/env bash
# Resolve the actual script location (following symlinks)
# This is needed because the script is invoked via a symlink in $PREFIX/bin
SCRIPT_PATH="$0"
# Follow symlinks to get the real path
if command -v readlink > /dev/null 2>&1; then
# Try readlink -f first (GNU readlink)
REAL_PATH="$(readlink -f "$SCRIPT_PATH" 2>/dev/null)"
if [ -z "$REAL_PATH" ]; then
# Fallback for BSD readlink (no -f option)
REAL_PATH="$SCRIPT_PATH"
while [ -L "$REAL_PATH" ]; do
LINK_TARGET="$(readlink "$REAL_PATH")"
case "$LINK_TARGET" in
/*) REAL_PATH="$LINK_TARGET" ;;
*) REAL_PATH="$(dirname "$REAL_PATH")/$LINK_TARGET" ;;
esac
done
fi
else
REAL_PATH="$SCRIPT_PATH"
fi
# Get the directory where the actual script is located
SCRIPT_DIR="$(cd "$(dirname "$REAL_PATH")" && pwd)"
APP_DIR="$(dirname "$SCRIPT_DIR")"
# Use Termux's system Node.js (avoids glibc vs bionic issues)
NODE_BINARY="$(command -v node)"
if [ -z "$NODE_BINARY" ] || [ ! -x "$NODE_BINARY" ]; then
echo "Error: Node.js not found. Please install with: pkg install nodejs-lts"
exit 1
fi
# Set NODE_PATH to include our bundled node_modules
export NODE_PATH="$APP_DIR/lib/node_modules:$NODE_PATH"
# Suppress deprecation warnings
export NODE_OPTIONS="--no-deprecation"
# Execute the bundle with Termux's Node.js
exec "$NODE_BINARY" "$APP_DIR/lib/bundle/gemini.js" "$@"
LAUNCHER_EOF
chmod +x "$install_dir/bin/kolosal"
# Create symlink in Termux bin directory
print_info "Creating symlink in $bin_dir..."
rm -f "$bin_dir/kolosal"
ln -sf "$install_dir/bin/kolosal" "$bin_dir/kolosal"
print_success "Installation complete"
}
# Verify installation
verify_installation() {
print_header "Verifying Installation"
# Refresh command hash
hash -r 2>/dev/null || true
local kolosal_path="$PREFIX/bin/kolosal"
if [ -x "$kolosal_path" ]; then
print_success "KolosalCode installed successfully!"
printf "\n"
print_info "Location: $kolosal_path"
# Try to get version
print_info "Testing kolosal..."
if "$kolosal_path" --version 2>/dev/null; then
print_success "Version check passed"
else
print_warning "Version check returned non-zero, but command exists"
fi
else
print_error "Installation finished but kolosal not found at $kolosal_path"
return 1
fi
}
# Show usage instructions
show_usage() {
printf "\n"
print_header "Quick Start"
printf "\n"
printf "Run KolosalCode:\n"
printf " ${BOLD}kolosal${NC}\n"
printf "\n"
printf "Check version:\n"
printf " ${BOLD}kolosal --version${NC}\n"
printf "\n"
printf "Get help:\n"
printf " ${BOLD}kolosal --help${NC}\n"
printf "\n"
print_info "For more information, visit:"
print_info " https://github.com/${GITHUB_REPO}"
printf "\n"
}
# Show uninstall instructions
show_uninstall() {
printf "\n"
print_header "Uninstallation"
printf "\n"
printf "To uninstall KolosalCode from Termux:\n"
printf " rm -rf \$PREFIX/opt/kolosal-code\n"
printf " rm -f \$PREFIX/bin/kolosal\n"
printf "\n"
}
# Ask yes/no question (compatible with POSIX sh)
ask_yes_no() {
local prompt="$1"
local response
printf "%s (y/n) " "$prompt"
read response
case "$response" in
[Yy]|[Yy][Ee][Ss])
return 0
;;
*)
return 1
;;
esac
}
# Main installation flow
main() {
print_header "KolosalCode Termux Installer v${VERSION}"
printf "\n"
print_info "Building from local source (no root required)"
# Check if running in Termux
check_termux
print_success "Running in Termux environment"
print_info "PREFIX: $PREFIX"
# Detect Architecture
local arch
arch=$(detect_arch)
print_success "Detected architecture: $arch"
# Check repository files
check_repo_files
# Check if already installed
if [ -x "$PREFIX/bin/kolosal" ]; then
print_warning "KolosalCode is already installed"
print_info "Current location: $PREFIX/bin/kolosal"
printf "\n"
if ! ask_yes_no "Do you want to reinstall?"; then
print_info "Installation cancelled"
exit 0
fi
fi
# Install dependencies
install_dependencies
# Build the project from local source
build_project
# Install the application
install_app
# Verify installation
verify_installation
# Show usage
show_usage
print_success "Installation completed successfully!"
}
# Handle arguments
case "${1:-}" in
--uninstall)
show_uninstall
;;
--help|-h)
printf "KolosalCode Termux Installer (Build from Source)\n"
printf "\n"
printf "Usage:\n"
printf " cd kolosal-cli\n"
printf " bash install-termux.sh [OPTIONS]\n"
printf "\n"
printf "Options:\n"
printf " --help, -h Show this help message\n"
printf " --uninstall Show uninstall instructions\n"
printf "\n"
printf "This script builds KolosalCode from the local repository\n"
printf "and installs it to \$PREFIX/opt/kolosal-code\n"
printf "\n"
printf "Prerequisites:\n"
printf " 1. Clone the repository: git clone https://github.com/${GITHUB_REPO}.git\n"
printf " 2. Enter the directory: cd kolosal-cli\n"
printf " 3. Run installer: bash install-termux.sh\n"
;;
*)
main "$@"
;;
esac