install.sh 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # OpenCode Agents Installer
  4. # Interactive installer for OpenCode agents, commands, tools, and plugins
  5. #
  6. # Compatible with:
  7. # - macOS (bash 3.2+)
  8. # - Linux (bash 3.2+)
  9. # - Windows (Git Bash, WSL)
  10. #############################################################################
  11. set -e
  12. # Detect platform
  13. PLATFORM="$(uname -s)"
  14. case "$PLATFORM" in
  15. Linux*) PLATFORM="Linux";;
  16. Darwin*) PLATFORM="macOS";;
  17. CYGWIN*|MINGW*|MSYS*) PLATFORM="Windows";;
  18. *) PLATFORM="Unknown";;
  19. esac
  20. # Colors for output (disable on Windows if not supported)
  21. if [ "$PLATFORM" = "Windows" ] && [ -z "$WT_SESSION" ] && [ -z "$ConEmuPID" ]; then
  22. # Basic Windows terminal without color support
  23. RED=''
  24. GREEN=''
  25. YELLOW=''
  26. BLUE=''
  27. MAGENTA=''
  28. CYAN=''
  29. BOLD=''
  30. NC=''
  31. else
  32. RED='\033[0;31m'
  33. GREEN='\033[0;32m'
  34. YELLOW='\033[1;33m'
  35. BLUE='\033[0;34m'
  36. MAGENTA='\033[0;35m'
  37. CYAN='\033[0;36m'
  38. BOLD='\033[1m'
  39. NC='\033[0m' # No Color
  40. fi
  41. # Configuration
  42. REPO_URL="https://github.com/darrenhinde/opencode-agents"
  43. BRANCH="${OPENCODE_BRANCH:-main}" # Allow override via environment variable
  44. RAW_URL="https://raw.githubusercontent.com/darrenhinde/opencode-agents/${BRANCH}"
  45. REGISTRY_URL="${RAW_URL}/registry.json"
  46. INSTALL_DIR=".opencode"
  47. TEMP_DIR="/tmp/opencode-installer-$$"
  48. # Global variables
  49. SELECTED_COMPONENTS=()
  50. INSTALL_MODE=""
  51. PROFILE=""
  52. NON_INTERACTIVE=false
  53. #############################################################################
  54. # Utility Functions
  55. #############################################################################
  56. print_header() {
  57. echo -e "${CYAN}${BOLD}"
  58. echo "╔════════════════════════════════════════════════════════════════╗"
  59. echo "║ ║"
  60. echo "║ OpenCode Agents Installer v1.0.0 ║"
  61. echo "║ ║"
  62. echo "╚════════════════════════════════════════════════════════════════╝"
  63. echo -e "${NC}"
  64. }
  65. print_success() {
  66. echo -e "${GREEN}✓${NC} $1"
  67. }
  68. print_error() {
  69. echo -e "${RED}✗${NC} $1"
  70. }
  71. print_info() {
  72. echo -e "${BLUE}ℹ${NC} $1"
  73. }
  74. print_warning() {
  75. echo -e "${YELLOW}⚠${NC} $1"
  76. }
  77. print_step() {
  78. echo -e "\n${MAGENTA}${BOLD}▶${NC} $1\n"
  79. }
  80. #############################################################################
  81. # Dependency Checks
  82. #############################################################################
  83. check_bash_version() {
  84. # Check bash version (need 3.2+)
  85. local bash_version="${BASH_VERSION%%.*}"
  86. if [ "$bash_version" -lt 3 ]; then
  87. echo "Error: This script requires Bash 3.2 or higher"
  88. echo "Current version: $BASH_VERSION"
  89. echo ""
  90. echo "Please upgrade bash or use a different shell:"
  91. echo " macOS: brew install bash"
  92. echo " Linux: Use your package manager to update bash"
  93. echo " Windows: Use Git Bash or WSL"
  94. exit 1
  95. fi
  96. }
  97. check_dependencies() {
  98. print_step "Checking dependencies..."
  99. local missing_deps=()
  100. if ! command -v curl &> /dev/null; then
  101. missing_deps+=("curl")
  102. fi
  103. if ! command -v jq &> /dev/null; then
  104. missing_deps+=("jq")
  105. fi
  106. if [ ${#missing_deps[@]} -ne 0 ]; then
  107. print_error "Missing required dependencies: ${missing_deps[*]}"
  108. echo ""
  109. echo "Please install them:"
  110. case "$PLATFORM" in
  111. macOS)
  112. echo " brew install ${missing_deps[*]}"
  113. ;;
  114. Linux)
  115. echo " Ubuntu/Debian: sudo apt-get install ${missing_deps[*]}"
  116. echo " Fedora/RHEL: sudo dnf install ${missing_deps[*]}"
  117. echo " Arch: sudo pacman -S ${missing_deps[*]}"
  118. ;;
  119. Windows)
  120. echo " Git Bash: Install via https://git-scm.com/"
  121. echo " WSL: sudo apt-get install ${missing_deps[*]}"
  122. echo " Scoop: scoop install ${missing_deps[*]}"
  123. ;;
  124. *)
  125. echo " Use your package manager to install: ${missing_deps[*]}"
  126. ;;
  127. esac
  128. exit 1
  129. fi
  130. print_success "All dependencies found"
  131. }
  132. #############################################################################
  133. # Registry Functions
  134. #############################################################################
  135. fetch_registry() {
  136. print_step "Fetching component registry..."
  137. mkdir -p "$TEMP_DIR"
  138. if ! curl -fsSL "$REGISTRY_URL" -o "$TEMP_DIR/registry.json"; then
  139. print_error "Failed to fetch registry from $REGISTRY_URL"
  140. exit 1
  141. fi
  142. print_success "Registry fetched successfully"
  143. }
  144. get_profile_components() {
  145. local profile=$1
  146. jq -r ".profiles.${profile}.components[]" "$TEMP_DIR/registry.json"
  147. }
  148. get_component_info() {
  149. local component_id=$1
  150. local component_type=$2
  151. jq -r ".components.${component_type}[] | select(.id == \"${component_id}\")" "$TEMP_DIR/registry.json"
  152. }
  153. # Helper function to get the correct registry key for a component type
  154. get_registry_key() {
  155. local type=$1
  156. # Most types are pluralized, but 'config' stays singular
  157. case "$type" in
  158. config) echo "config" ;;
  159. *) echo "${type}s" ;;
  160. esac
  161. }
  162. resolve_dependencies() {
  163. local component=$1
  164. local type="${component%%:*}"
  165. local id="${component##*:}"
  166. # Get the correct registry key (handles singular/plural)
  167. local registry_key=$(get_registry_key "$type")
  168. # Get dependencies for this component
  169. local deps=$(jq -r ".components.${registry_key}[] | select(.id == \"${id}\") | .dependencies[]?" "$TEMP_DIR/registry.json" 2>/dev/null || echo "")
  170. if [ -n "$deps" ]; then
  171. for dep in $deps; do
  172. # Add dependency if not already in list
  173. if [[ ! " ${SELECTED_COMPONENTS[@]} " =~ " ${dep} " ]]; then
  174. SELECTED_COMPONENTS+=("$dep")
  175. # Recursively resolve dependencies
  176. resolve_dependencies "$dep"
  177. fi
  178. done
  179. fi
  180. }
  181. #############################################################################
  182. # Installation Mode Selection
  183. #############################################################################
  184. check_interactive_mode() {
  185. # Check if stdin is a terminal (not piped from curl)
  186. if [ ! -t 0 ]; then
  187. print_header
  188. print_error "Interactive mode requires a terminal"
  189. echo ""
  190. echo "You're running this script in a pipe (e.g., curl | bash)"
  191. echo "For interactive mode, download the script first:"
  192. echo ""
  193. echo -e "${CYAN}# Download the script${NC}"
  194. echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/opencode-agents/main/install.sh -o install.sh"
  195. echo ""
  196. echo -e "${CYAN}# Run interactively${NC}"
  197. echo "bash install.sh"
  198. echo ""
  199. echo "Or use a profile directly:"
  200. echo ""
  201. echo -e "${CYAN}# Quick install with profile${NC}"
  202. echo "curl -fsSL https://raw.githubusercontent.com/darrenhinde/opencode-agents/main/install.sh | bash -s core"
  203. echo ""
  204. echo "Available profiles: core, developer, full, advanced"
  205. echo ""
  206. cleanup_and_exit 1
  207. fi
  208. }
  209. show_main_menu() {
  210. check_interactive_mode
  211. clear
  212. print_header
  213. echo -e "${BOLD}Choose installation mode:${NC}\n"
  214. echo " 1) Quick Install (Choose a profile)"
  215. echo " 2) Custom Install (Pick individual components)"
  216. echo " 3) List Available Components"
  217. echo " 4) Exit"
  218. echo ""
  219. read -p "Enter your choice [1-4]: " choice
  220. case $choice in
  221. 1) INSTALL_MODE="profile" ;;
  222. 2) INSTALL_MODE="custom" ;;
  223. 3) list_components; show_main_menu ;;
  224. 4) cleanup_and_exit 0 ;;
  225. *) print_error "Invalid choice"; sleep 2; show_main_menu ;;
  226. esac
  227. }
  228. #############################################################################
  229. # Profile Installation
  230. #############################################################################
  231. show_profile_menu() {
  232. clear
  233. print_header
  234. echo -e "${BOLD}Available Installation Profiles:${NC}\n"
  235. # Core profile
  236. local core_desc=$(jq -r '.profiles.core.description' "$TEMP_DIR/registry.json")
  237. local core_count=$(jq -r '.profiles.core.components | length' "$TEMP_DIR/registry.json")
  238. echo -e " ${GREEN}1) Core${NC}"
  239. echo -e " ${core_desc}"
  240. echo -e " Components: ${core_count}\n"
  241. # Developer profile
  242. local dev_desc=$(jq -r '.profiles.developer.description' "$TEMP_DIR/registry.json")
  243. local dev_count=$(jq -r '.profiles.developer.components | length' "$TEMP_DIR/registry.json")
  244. echo -e " ${BLUE}2) Developer${NC}"
  245. echo -e " ${dev_desc}"
  246. echo -e " Components: ${dev_count}\n"
  247. # Full profile
  248. local full_desc=$(jq -r '.profiles.full.description' "$TEMP_DIR/registry.json")
  249. local full_count=$(jq -r '.profiles.full.components | length' "$TEMP_DIR/registry.json")
  250. echo -e " ${MAGENTA}3) Full${NC}"
  251. echo -e " ${full_desc}"
  252. echo -e " Components: ${full_count}\n"
  253. # Advanced profile
  254. local adv_desc=$(jq -r '.profiles.advanced.description' "$TEMP_DIR/registry.json")
  255. local adv_count=$(jq -r '.profiles.advanced.components | length' "$TEMP_DIR/registry.json")
  256. echo -e " ${YELLOW}4) Advanced${NC}"
  257. echo -e " ${adv_desc}"
  258. echo -e " Components: ${adv_count}\n"
  259. echo " 5) Back to main menu"
  260. echo ""
  261. read -p "Enter your choice [1-5]: " choice
  262. case $choice in
  263. 1) PROFILE="core" ;;
  264. 2) PROFILE="developer" ;;
  265. 3) PROFILE="full" ;;
  266. 4) PROFILE="advanced" ;;
  267. 5) show_main_menu; return ;;
  268. *) print_error "Invalid choice"; sleep 2; show_profile_menu; return ;;
  269. esac
  270. # Load profile components (compatible with bash 3.2+)
  271. SELECTED_COMPONENTS=()
  272. local temp_file="$TEMP_DIR/components.tmp"
  273. get_profile_components "$PROFILE" > "$temp_file"
  274. while IFS= read -r component; do
  275. [ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
  276. done < "$temp_file"
  277. show_installation_preview
  278. }
  279. #############################################################################
  280. # Custom Component Selection
  281. #############################################################################
  282. show_custom_menu() {
  283. clear
  284. print_header
  285. echo -e "${BOLD}Select component categories to install:${NC}\n"
  286. echo "Use space to toggle, Enter to continue"
  287. echo ""
  288. local categories=("agents" "subagents" "commands" "tools" "plugins" "contexts" "config")
  289. local selected_categories=()
  290. # Simple selection (for now, we'll make it interactive later)
  291. echo "Available categories:"
  292. for i in "${!categories[@]}"; do
  293. local cat="${categories[$i]}"
  294. local count=$(jq -r ".components.${cat} | length" "$TEMP_DIR/registry.json")
  295. local cat_display=$(echo "$cat" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  296. echo " $((i+1))) ${cat_display} (${count} available)"
  297. done
  298. echo " $((${#categories[@]}+1))) Select All"
  299. echo " $((${#categories[@]}+2))) Continue to component selection"
  300. echo " $((${#categories[@]}+3))) Back to main menu"
  301. echo ""
  302. read -p "Enter category numbers (space-separated) or option: " -a selections
  303. for sel in "${selections[@]}"; do
  304. if [ "$sel" -eq $((${#categories[@]}+1)) ]; then
  305. selected_categories=("${categories[@]}")
  306. break
  307. elif [ "$sel" -eq $((${#categories[@]}+2)) ]; then
  308. break
  309. elif [ "$sel" -eq $((${#categories[@]}+3)) ]; then
  310. show_main_menu
  311. return
  312. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#categories[@]} ]; then
  313. selected_categories+=("${categories[$((sel-1))]}")
  314. fi
  315. done
  316. if [ ${#selected_categories[@]} -eq 0 ]; then
  317. print_warning "No categories selected"
  318. sleep 2
  319. show_custom_menu
  320. return
  321. fi
  322. show_component_selection "${selected_categories[@]}"
  323. }
  324. show_component_selection() {
  325. local categories=("$@")
  326. clear
  327. print_header
  328. echo -e "${BOLD}Select components to install:${NC}\n"
  329. local all_components=()
  330. local component_details=()
  331. for category in "${categories[@]}"; do
  332. local cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  333. echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
  334. local components=$(jq -r ".components.${category}[] | .id" "$TEMP_DIR/registry.json")
  335. local idx=1
  336. while IFS= read -r comp_id; do
  337. local comp_name=$(jq -r ".components.${category}[] | select(.id == \"${comp_id}\") | .name" "$TEMP_DIR/registry.json")
  338. local comp_desc=$(jq -r ".components.${category}[] | select(.id == \"${comp_id}\") | .description" "$TEMP_DIR/registry.json")
  339. echo " ${idx}) ${comp_name}"
  340. echo " ${comp_desc}"
  341. all_components+=("${category}:${comp_id}")
  342. component_details+=("${comp_name}|${comp_desc}")
  343. idx=$((idx+1))
  344. done <<< "$components"
  345. echo ""
  346. done
  347. echo "Enter component numbers (space-separated), 'all' for all, or 'done' to continue:"
  348. read -a selections
  349. for sel in "${selections[@]}"; do
  350. if [ "$sel" = "all" ]; then
  351. SELECTED_COMPONENTS=("${all_components[@]}")
  352. break
  353. elif [ "$sel" = "done" ]; then
  354. break
  355. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#all_components[@]} ]; then
  356. SELECTED_COMPONENTS+=("${all_components[$((sel-1))]}")
  357. fi
  358. done
  359. if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
  360. print_warning "No components selected"
  361. sleep 2
  362. show_custom_menu
  363. return
  364. fi
  365. # Resolve dependencies
  366. print_step "Resolving dependencies..."
  367. local original_count=${#SELECTED_COMPONENTS[@]}
  368. for comp in "${SELECTED_COMPONENTS[@]}"; do
  369. resolve_dependencies "$comp"
  370. done
  371. if [ ${#SELECTED_COMPONENTS[@]} -gt $original_count ]; then
  372. print_info "Added $((${#SELECTED_COMPONENTS[@]} - original_count)) dependencies"
  373. fi
  374. show_installation_preview
  375. }
  376. #############################################################################
  377. # Installation Preview & Confirmation
  378. #############################################################################
  379. show_installation_preview() {
  380. # Only clear screen in interactive mode
  381. if [ "$NON_INTERACTIVE" != true ]; then
  382. clear
  383. fi
  384. print_header
  385. echo -e "${BOLD}Installation Preview${NC}\n"
  386. if [ -n "$PROFILE" ]; then
  387. echo -e "Profile: ${GREEN}${PROFILE}${NC}"
  388. else
  389. echo -e "Mode: ${GREEN}Custom${NC}"
  390. fi
  391. echo -e "\nComponents to install (${#SELECTED_COMPONENTS[@]} total):\n"
  392. # Group by type
  393. local agents=()
  394. local subagents=()
  395. local commands=()
  396. local tools=()
  397. local plugins=()
  398. local contexts=()
  399. local configs=()
  400. for comp in "${SELECTED_COMPONENTS[@]}"; do
  401. local type="${comp%%:*}"
  402. case $type in
  403. agent) agents+=("$comp") ;;
  404. subagent) subagents+=("$comp") ;;
  405. command) commands+=("$comp") ;;
  406. tool) tools+=("$comp") ;;
  407. plugin) plugins+=("$comp") ;;
  408. context) contexts+=("$comp") ;;
  409. config) configs+=("$comp") ;;
  410. esac
  411. done
  412. [ ${#agents[@]} -gt 0 ] && echo -e "${CYAN}Agents (${#agents[@]}):${NC} ${agents[*]##*:}"
  413. [ ${#subagents[@]} -gt 0 ] && echo -e "${CYAN}Subagents (${#subagents[@]}):${NC} ${subagents[*]##*:}"
  414. [ ${#commands[@]} -gt 0 ] && echo -e "${CYAN}Commands (${#commands[@]}):${NC} ${commands[*]##*:}"
  415. [ ${#tools[@]} -gt 0 ] && echo -e "${CYAN}Tools (${#tools[@]}):${NC} ${tools[*]##*:}"
  416. [ ${#plugins[@]} -gt 0 ] && echo -e "${CYAN}Plugins (${#plugins[@]}):${NC} ${plugins[*]##*:}"
  417. [ ${#contexts[@]} -gt 0 ] && echo -e "${CYAN}Contexts (${#contexts[@]}):${NC} ${contexts[*]##*:}"
  418. [ ${#configs[@]} -gt 0 ] && echo -e "${CYAN}Config (${#configs[@]}):${NC} ${configs[*]##*:}"
  419. echo ""
  420. # Skip confirmation if profile was provided via command line
  421. if [ "$NON_INTERACTIVE" = true ]; then
  422. print_info "Installing automatically (profile specified)..."
  423. perform_installation
  424. else
  425. read -p "Proceed with installation? [Y/n]: " confirm
  426. if [[ $confirm =~ ^[Nn] ]]; then
  427. print_info "Installation cancelled"
  428. cleanup_and_exit 0
  429. fi
  430. perform_installation
  431. fi
  432. }
  433. #############################################################################
  434. # Collision Detection
  435. #############################################################################
  436. show_collision_report() {
  437. local collision_count=$1
  438. shift
  439. local collisions=("$@")
  440. echo ""
  441. print_warning "Found ${collision_count} file collision(s):"
  442. echo ""
  443. # Group by type
  444. local agents=()
  445. local subagents=()
  446. local commands=()
  447. local tools=()
  448. local plugins=()
  449. local contexts=()
  450. local configs=()
  451. for file in "${collisions[@]}"; do
  452. # Skip empty entries
  453. [ -z "$file" ] && continue
  454. if [[ $file == *"/agent/subagents/"* ]]; then
  455. subagents+=("$file")
  456. elif [[ $file == *"/agent/"* ]]; then
  457. agents+=("$file")
  458. elif [[ $file == *"/command/"* ]]; then
  459. commands+=("$file")
  460. elif [[ $file == *"/tool/"* ]]; then
  461. tools+=("$file")
  462. elif [[ $file == *"/plugin/"* ]]; then
  463. plugins+=("$file")
  464. elif [[ $file == *"/context/"* ]]; then
  465. contexts+=("$file")
  466. else
  467. configs+=("$file")
  468. fi
  469. done
  470. # Display grouped collisions
  471. [ ${#agents[@]} -gt 0 ] && echo -e "${YELLOW} Agents (${#agents[@]}):${NC}" && printf ' %s\n' "${agents[@]}"
  472. [ ${#subagents[@]} -gt 0 ] && echo -e "${YELLOW} Subagents (${#subagents[@]}):${NC}" && printf ' %s\n' "${subagents[@]}"
  473. [ ${#commands[@]} -gt 0 ] && echo -e "${YELLOW} Commands (${#commands[@]}):${NC}" && printf ' %s\n' "${commands[@]}"
  474. [ ${#tools[@]} -gt 0 ] && echo -e "${YELLOW} Tools (${#tools[@]}):${NC}" && printf ' %s\n' "${tools[@]}"
  475. [ ${#plugins[@]} -gt 0 ] && echo -e "${YELLOW} Plugins (${#plugins[@]}):${NC}" && printf ' %s\n' "${plugins[@]}"
  476. [ ${#contexts[@]} -gt 0 ] && echo -e "${YELLOW} Context (${#contexts[@]}):${NC}" && printf ' %s\n' "${contexts[@]}"
  477. [ ${#configs[@]} -gt 0 ] && echo -e "${YELLOW} Config (${#configs[@]}):${NC}" && printf ' %s\n' "${configs[@]}"
  478. echo ""
  479. }
  480. get_install_strategy() {
  481. echo -e "${BOLD}How would you like to proceed?${NC}\n"
  482. echo " 1) ${GREEN}Skip existing${NC} - Only install new files, keep all existing files unchanged"
  483. echo " 2) ${YELLOW}Overwrite all${NC} - Replace existing files with new versions (your changes will be lost)"
  484. echo " 3) ${CYAN}Backup & overwrite${NC} - Backup existing files, then install new versions"
  485. echo " 4) ${RED}Cancel${NC} - Exit without making changes"
  486. echo ""
  487. read -p "Enter your choice [1-4]: " strategy_choice
  488. case $strategy_choice in
  489. 1) echo "skip" ;;
  490. 2)
  491. echo ""
  492. print_warning "This will overwrite existing files. Your changes will be lost!"
  493. read -p "Are you sure? Type 'yes' to confirm: " confirm
  494. if [ "$confirm" = "yes" ]; then
  495. echo "overwrite"
  496. else
  497. echo "cancel"
  498. fi
  499. ;;
  500. 3) echo "backup" ;;
  501. 4) echo "cancel" ;;
  502. *) echo "cancel" ;;
  503. esac
  504. }
  505. #############################################################################
  506. # Installation
  507. #############################################################################
  508. perform_installation() {
  509. print_step "Preparing installation..."
  510. # Create directory structure if it doesn't exist
  511. mkdir -p "$INSTALL_DIR"/{agent/subagents,command,tool,plugin,context/{core,project}}
  512. # Check for collisions
  513. local collisions=()
  514. for comp in "${SELECTED_COMPONENTS[@]}"; do
  515. local type="${comp%%:*}"
  516. local id="${comp##*:}"
  517. local registry_key=$(get_registry_key "$type")
  518. local path=$(jq -r ".components.${registry_key}[] | select(.id == \"${id}\") | .path" "$TEMP_DIR/registry.json")
  519. if [ -n "$path" ] && [ "$path" != "null" ] && [ -f "$path" ]; then
  520. collisions+=("$path")
  521. fi
  522. done
  523. # Determine installation strategy
  524. local install_strategy="fresh"
  525. if [ ${#collisions[@]} -gt 0 ]; then
  526. show_collision_report ${#collisions[@]} "${collisions[@]}"
  527. install_strategy=$(get_install_strategy)
  528. if [ "$install_strategy" = "cancel" ]; then
  529. print_info "Installation cancelled by user"
  530. cleanup_and_exit 0
  531. fi
  532. # Handle backup strategy
  533. if [ "$install_strategy" = "backup" ]; then
  534. local backup_dir="${INSTALL_DIR}.backup.$(date +%Y%m%d-%H%M%S)"
  535. print_step "Creating backup..."
  536. # Only backup files that will be overwritten
  537. local backup_count=0
  538. for file in "${collisions[@]}"; do
  539. if [ -f "$file" ]; then
  540. local backup_file="${backup_dir}/${file}"
  541. mkdir -p "$(dirname "$backup_file")"
  542. if cp "$file" "$backup_file" 2>/dev/null; then
  543. ((backup_count++))
  544. else
  545. print_warning "Failed to backup: $file"
  546. fi
  547. fi
  548. done
  549. if [ $backup_count -gt 0 ]; then
  550. print_success "Backed up ${backup_count} file(s) to $backup_dir"
  551. install_strategy="overwrite" # Now we can overwrite
  552. else
  553. print_error "Backup failed. Installation cancelled."
  554. cleanup_and_exit 1
  555. fi
  556. fi
  557. fi
  558. # Perform installation
  559. print_step "Installing components..."
  560. local installed=0
  561. local skipped=0
  562. local failed=0
  563. for comp in "${SELECTED_COMPONENTS[@]}"; do
  564. local type="${comp%%:*}"
  565. local id="${comp##*:}"
  566. # Get the correct registry key (handles singular/plural)
  567. local registry_key=$(get_registry_key "$type")
  568. # Get component path
  569. local path=$(jq -r ".components.${registry_key}[] | select(.id == \"${id}\") | .path" "$TEMP_DIR/registry.json")
  570. if [ -z "$path" ] || [ "$path" = "null" ]; then
  571. print_warning "Could not find path for ${comp}"
  572. ((failed++))
  573. continue
  574. fi
  575. # Check if file exists before we install (for proper messaging)
  576. local file_existed=false
  577. if [ -f "$path" ]; then
  578. file_existed=true
  579. fi
  580. # Check if file exists and we're in skip mode
  581. if [ "$file_existed" = true ] && [ "$install_strategy" = "skip" ]; then
  582. print_info "Skipped existing: ${type}:${id}"
  583. ((skipped++))
  584. continue
  585. fi
  586. # Download component
  587. local url="${RAW_URL}/${path}"
  588. local dest="${path}"
  589. # Create parent directory if needed
  590. mkdir -p "$(dirname "$dest")"
  591. if curl -fsSL "$url" -o "$dest"; then
  592. # Show appropriate message based on whether file existed before
  593. if [ "$file_existed" = true ]; then
  594. print_success "Updated ${type}: ${id}"
  595. else
  596. print_success "Installed ${type}: ${id}"
  597. fi
  598. ((installed++))
  599. else
  600. print_error "Failed to install ${type}: ${id}"
  601. ((failed++))
  602. fi
  603. done
  604. # Handle additional paths for advanced profile
  605. if [ "$PROFILE" = "advanced" ]; then
  606. local additional_paths=$(jq -r '.profiles.advanced.additionalPaths[]?' "$TEMP_DIR/registry.json")
  607. if [ -n "$additional_paths" ]; then
  608. print_step "Installing additional paths..."
  609. while IFS= read -r path; do
  610. # For directories, we'd need to recursively download
  611. # For now, just note them
  612. print_info "Additional path: $path (manual download required)"
  613. done <<< "$additional_paths"
  614. fi
  615. fi
  616. echo ""
  617. print_success "Installation complete!"
  618. echo -e " Installed: ${GREEN}${installed}${NC}"
  619. [ $skipped -gt 0 ] && echo -e " Skipped: ${CYAN}${skipped}${NC}"
  620. [ $failed -gt 0 ] && echo -e " Failed: ${RED}${failed}${NC}"
  621. show_post_install
  622. }
  623. #############################################################################
  624. # Post-Installation
  625. #############################################################################
  626. show_post_install() {
  627. echo ""
  628. print_step "Next Steps"
  629. echo "1. Review the installed components in .opencode/"
  630. echo "2. Copy env.example to .env and configure:"
  631. echo " ${CYAN}cp env.example .env${NC}"
  632. echo "3. Start using OpenCode agents:"
  633. echo " ${CYAN}opencode${NC}"
  634. echo ""
  635. if [ -d "${INSTALL_DIR}.backup."* ] 2>/dev/null; then
  636. print_info "Backup created - you can restore files from .opencode.backup.* if needed"
  637. fi
  638. print_info "Documentation: ${REPO_URL}"
  639. echo ""
  640. cleanup_and_exit 0
  641. }
  642. #############################################################################
  643. # Component Listing
  644. #############################################################################
  645. list_components() {
  646. clear
  647. print_header
  648. echo -e "${BOLD}Available Components${NC}\n"
  649. local categories=("agents" "subagents" "commands" "tools" "plugins" "contexts")
  650. for category in "${categories[@]}"; do
  651. local cat_display=$(echo "$category" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')
  652. echo -e "${CYAN}${BOLD}${cat_display}:${NC}"
  653. local components=$(jq -r ".components.${category}[] | \"\(.id)|\(.name)|\(.description)\"" "$TEMP_DIR/registry.json")
  654. while IFS='|' read -r id name desc; do
  655. echo -e " ${GREEN}${name}${NC} (${id})"
  656. echo -e " ${desc}"
  657. done <<< "$components"
  658. echo ""
  659. done
  660. read -p "Press Enter to continue..."
  661. }
  662. #############################################################################
  663. # Cleanup
  664. #############################################################################
  665. cleanup_and_exit() {
  666. rm -rf "$TEMP_DIR"
  667. exit "$1"
  668. }
  669. trap 'cleanup_and_exit 1' INT TERM
  670. #############################################################################
  671. # Main
  672. #############################################################################
  673. main() {
  674. # Parse command line arguments
  675. case "${1:-}" in
  676. core|--core)
  677. INSTALL_MODE="profile"
  678. PROFILE="core"
  679. NON_INTERACTIVE=true
  680. ;;
  681. developer|--developer)
  682. INSTALL_MODE="profile"
  683. PROFILE="developer"
  684. NON_INTERACTIVE=true
  685. ;;
  686. full|--full)
  687. INSTALL_MODE="profile"
  688. PROFILE="full"
  689. NON_INTERACTIVE=true
  690. ;;
  691. advanced|--advanced)
  692. INSTALL_MODE="profile"
  693. PROFILE="advanced"
  694. NON_INTERACTIVE=true
  695. ;;
  696. list|--list)
  697. check_dependencies
  698. fetch_registry
  699. list_components
  700. cleanup_and_exit 0
  701. ;;
  702. --help|-h|help)
  703. print_header
  704. echo "Usage: $0 [OPTIONS]"
  705. echo ""
  706. echo "Options:"
  707. echo " core, --core Install core profile"
  708. echo " developer, --developer Install developer profile"
  709. echo " full, --full Install full profile"
  710. echo " advanced, --advanced Install advanced profile"
  711. echo " list, --list List all available components"
  712. echo " help, --help, -h Show this help message"
  713. echo ""
  714. echo "Examples:"
  715. echo " $0 core"
  716. echo " $0 --developer"
  717. echo " curl -fsSL https://raw.githubusercontent.com/darrenhinde/opencode-agents/main/install.sh | bash -s core"
  718. echo ""
  719. echo "Without options, runs in interactive mode"
  720. exit 0
  721. ;;
  722. esac
  723. check_bash_version
  724. check_dependencies
  725. fetch_registry
  726. if [ -n "$PROFILE" ]; then
  727. # Non-interactive mode (compatible with bash 3.2+)
  728. SELECTED_COMPONENTS=()
  729. local temp_file="$TEMP_DIR/components.tmp"
  730. get_profile_components "$PROFILE" > "$temp_file"
  731. while IFS= read -r component; do
  732. [ -n "$component" ] && SELECTED_COMPONENTS+=("$component")
  733. done < "$temp_file"
  734. show_installation_preview
  735. else
  736. # Interactive mode
  737. show_main_menu
  738. if [ "$INSTALL_MODE" = "profile" ]; then
  739. show_profile_menu
  740. elif [ "$INSTALL_MODE" = "custom" ]; then
  741. show_custom_menu
  742. fi
  743. fi
  744. }
  745. main "$@"