install.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # OpenCode Agents Installer
  4. # Interactive installer for OpenCode agents, commands, tools, and plugins
  5. #############################################################################
  6. set -e
  7. # Colors for output
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. YELLOW='\033[1;33m'
  11. BLUE='\033[0;34m'
  12. MAGENTA='\033[0;35m'
  13. CYAN='\033[0;36m'
  14. BOLD='\033[1m'
  15. NC='\033[0m' # No Color
  16. # Configuration
  17. REPO_URL="https://github.com/darrenhinde/opencode-agents"
  18. RAW_URL="https://raw.githubusercontent.com/darrenhinde/opencode-agents/main"
  19. REGISTRY_URL="${RAW_URL}/registry.json"
  20. INSTALL_DIR=".opencode"
  21. TEMP_DIR="/tmp/opencode-installer-$$"
  22. # Global variables
  23. SELECTED_COMPONENTS=()
  24. INSTALL_MODE=""
  25. PROFILE=""
  26. #############################################################################
  27. # Utility Functions
  28. #############################################################################
  29. print_header() {
  30. echo -e "${CYAN}${BOLD}"
  31. echo "╔════════════════════════════════════════════════════════════════╗"
  32. echo "║ ║"
  33. echo "║ OpenCode Agents Installer v1.0.0 ║"
  34. echo "║ ║"
  35. echo "╚════════════════════════════════════════════════════════════════╝"
  36. echo -e "${NC}"
  37. }
  38. print_success() {
  39. echo -e "${GREEN}✓${NC} $1"
  40. }
  41. print_error() {
  42. echo -e "${RED}✗${NC} $1"
  43. }
  44. print_info() {
  45. echo -e "${BLUE}ℹ${NC} $1"
  46. }
  47. print_warning() {
  48. echo -e "${YELLOW}⚠${NC} $1"
  49. }
  50. print_step() {
  51. echo -e "\n${MAGENTA}${BOLD}▶${NC} $1\n"
  52. }
  53. #############################################################################
  54. # Dependency Checks
  55. #############################################################################
  56. check_dependencies() {
  57. print_step "Checking dependencies..."
  58. local missing_deps=()
  59. if ! command -v curl &> /dev/null; then
  60. missing_deps+=("curl")
  61. fi
  62. if ! command -v jq &> /dev/null; then
  63. missing_deps+=("jq")
  64. fi
  65. if [ ${#missing_deps[@]} -ne 0 ]; then
  66. print_error "Missing required dependencies: ${missing_deps[*]}"
  67. echo ""
  68. echo "Please install them:"
  69. echo " macOS: brew install ${missing_deps[*]}"
  70. echo " Ubuntu: sudo apt-get install ${missing_deps[*]}"
  71. echo " Fedora: sudo dnf install ${missing_deps[*]}"
  72. exit 1
  73. fi
  74. print_success "All dependencies found"
  75. }
  76. #############################################################################
  77. # Registry Functions
  78. #############################################################################
  79. fetch_registry() {
  80. print_step "Fetching component registry..."
  81. mkdir -p "$TEMP_DIR"
  82. if ! curl -fsSL "$REGISTRY_URL" -o "$TEMP_DIR/registry.json"; then
  83. print_error "Failed to fetch registry from $REGISTRY_URL"
  84. exit 1
  85. fi
  86. print_success "Registry fetched successfully"
  87. }
  88. get_profile_components() {
  89. local profile=$1
  90. jq -r ".profiles.${profile}.components[]" "$TEMP_DIR/registry.json"
  91. }
  92. get_component_info() {
  93. local component_id=$1
  94. local component_type=$2
  95. jq -r ".components.${component_type}[] | select(.id == \"${component_id}\")" "$TEMP_DIR/registry.json"
  96. }
  97. resolve_dependencies() {
  98. local component=$1
  99. local type="${component%%:*}"
  100. local id="${component##*:}"
  101. # Get dependencies for this component
  102. local deps=$(jq -r ".components.${type}s[] | select(.id == \"${id}\") | .dependencies[]?" "$TEMP_DIR/registry.json" 2>/dev/null || echo "")
  103. if [ -n "$deps" ]; then
  104. for dep in $deps; do
  105. # Add dependency if not already in list
  106. if [[ ! " ${SELECTED_COMPONENTS[@]} " =~ " ${dep} " ]]; then
  107. SELECTED_COMPONENTS+=("$dep")
  108. # Recursively resolve dependencies
  109. resolve_dependencies "$dep"
  110. fi
  111. done
  112. fi
  113. }
  114. #############################################################################
  115. # Installation Mode Selection
  116. #############################################################################
  117. show_main_menu() {
  118. clear
  119. print_header
  120. echo -e "${BOLD}Choose installation mode:${NC}\n"
  121. echo " 1) Quick Install (Choose a profile)"
  122. echo " 2) Custom Install (Pick individual components)"
  123. echo " 3) List Available Components"
  124. echo " 4) Exit"
  125. echo ""
  126. read -p "Enter your choice [1-4]: " choice
  127. case $choice in
  128. 1) INSTALL_MODE="profile" ;;
  129. 2) INSTALL_MODE="custom" ;;
  130. 3) list_components; show_main_menu ;;
  131. 4) cleanup_and_exit 0 ;;
  132. *) print_error "Invalid choice"; sleep 2; show_main_menu ;;
  133. esac
  134. }
  135. #############################################################################
  136. # Profile Installation
  137. #############################################################################
  138. show_profile_menu() {
  139. clear
  140. print_header
  141. echo -e "${BOLD}Available Installation Profiles:${NC}\n"
  142. # Core profile
  143. local core_desc=$(jq -r '.profiles.core.description' "$TEMP_DIR/registry.json")
  144. local core_count=$(jq -r '.profiles.core.components | length' "$TEMP_DIR/registry.json")
  145. echo -e " ${GREEN}1) Core${NC}"
  146. echo -e " ${core_desc}"
  147. echo -e " Components: ${core_count}\n"
  148. # Developer profile
  149. local dev_desc=$(jq -r '.profiles.developer.description' "$TEMP_DIR/registry.json")
  150. local dev_count=$(jq -r '.profiles.developer.components | length' "$TEMP_DIR/registry.json")
  151. echo -e " ${BLUE}2) Developer${NC}"
  152. echo -e " ${dev_desc}"
  153. echo -e " Components: ${dev_count}\n"
  154. # Full profile
  155. local full_desc=$(jq -r '.profiles.full.description' "$TEMP_DIR/registry.json")
  156. local full_count=$(jq -r '.profiles.full.components | length' "$TEMP_DIR/registry.json")
  157. echo -e " ${MAGENTA}3) Full${NC}"
  158. echo -e " ${full_desc}"
  159. echo -e " Components: ${full_count}\n"
  160. # Advanced profile
  161. local adv_desc=$(jq -r '.profiles.advanced.description' "$TEMP_DIR/registry.json")
  162. local adv_count=$(jq -r '.profiles.advanced.components | length' "$TEMP_DIR/registry.json")
  163. echo -e " ${YELLOW}4) Advanced${NC}"
  164. echo -e " ${adv_desc}"
  165. echo -e " Components: ${adv_count}\n"
  166. echo " 5) Back to main menu"
  167. echo ""
  168. read -p "Enter your choice [1-5]: " choice
  169. case $choice in
  170. 1) PROFILE="core" ;;
  171. 2) PROFILE="developer" ;;
  172. 3) PROFILE="full" ;;
  173. 4) PROFILE="advanced" ;;
  174. 5) show_main_menu; return ;;
  175. *) print_error "Invalid choice"; sleep 2; show_profile_menu; return ;;
  176. esac
  177. # Load profile components
  178. mapfile -t SELECTED_COMPONENTS < <(get_profile_components "$PROFILE")
  179. show_installation_preview
  180. }
  181. #############################################################################
  182. # Custom Component Selection
  183. #############################################################################
  184. show_custom_menu() {
  185. clear
  186. print_header
  187. echo -e "${BOLD}Select component categories to install:${NC}\n"
  188. echo "Use space to toggle, Enter to continue"
  189. echo ""
  190. local categories=("agents" "subagents" "commands" "tools" "plugins" "contexts" "config")
  191. local selected_categories=()
  192. # Simple selection (for now, we'll make it interactive later)
  193. echo "Available categories:"
  194. for i in "${!categories[@]}"; do
  195. local cat="${categories[$i]}"
  196. local count=$(jq -r ".components.${cat} | length" "$TEMP_DIR/registry.json")
  197. echo " $((i+1))) ${cat^} (${count} available)"
  198. done
  199. echo " $((${#categories[@]}+1))) Select All"
  200. echo " $((${#categories[@]}+2))) Continue to component selection"
  201. echo " $((${#categories[@]}+3))) Back to main menu"
  202. echo ""
  203. read -p "Enter category numbers (space-separated) or option: " -a selections
  204. for sel in "${selections[@]}"; do
  205. if [ "$sel" -eq $((${#categories[@]}+1)) ]; then
  206. selected_categories=("${categories[@]}")
  207. break
  208. elif [ "$sel" -eq $((${#categories[@]}+2)) ]; then
  209. break
  210. elif [ "$sel" -eq $((${#categories[@]}+3)) ]; then
  211. show_main_menu
  212. return
  213. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#categories[@]} ]; then
  214. selected_categories+=("${categories[$((sel-1))]}")
  215. fi
  216. done
  217. if [ ${#selected_categories[@]} -eq 0 ]; then
  218. print_warning "No categories selected"
  219. sleep 2
  220. show_custom_menu
  221. return
  222. fi
  223. show_component_selection "${selected_categories[@]}"
  224. }
  225. show_component_selection() {
  226. local categories=("$@")
  227. clear
  228. print_header
  229. echo -e "${BOLD}Select components to install:${NC}\n"
  230. local all_components=()
  231. local component_details=()
  232. for category in "${categories[@]}"; do
  233. echo -e "${CYAN}${BOLD}${category^}:${NC}"
  234. local components=$(jq -r ".components.${category}[] | .id" "$TEMP_DIR/registry.json")
  235. local idx=1
  236. while IFS= read -r comp_id; do
  237. local comp_name=$(jq -r ".components.${category}[] | select(.id == \"${comp_id}\") | .name" "$TEMP_DIR/registry.json")
  238. local comp_desc=$(jq -r ".components.${category}[] | select(.id == \"${comp_id}\") | .description" "$TEMP_DIR/registry.json")
  239. echo " ${idx}) ${comp_name}"
  240. echo " ${comp_desc}"
  241. all_components+=("${category}:${comp_id}")
  242. component_details+=("${comp_name}|${comp_desc}")
  243. idx=$((idx+1))
  244. done <<< "$components"
  245. echo ""
  246. done
  247. echo "Enter component numbers (space-separated), 'all' for all, or 'done' to continue:"
  248. read -a selections
  249. for sel in "${selections[@]}"; do
  250. if [ "$sel" = "all" ]; then
  251. SELECTED_COMPONENTS=("${all_components[@]}")
  252. break
  253. elif [ "$sel" = "done" ]; then
  254. break
  255. elif [ "$sel" -ge 1 ] && [ "$sel" -le ${#all_components[@]} ]; then
  256. SELECTED_COMPONENTS+=("${all_components[$((sel-1))]}")
  257. fi
  258. done
  259. if [ ${#SELECTED_COMPONENTS[@]} -eq 0 ]; then
  260. print_warning "No components selected"
  261. sleep 2
  262. show_custom_menu
  263. return
  264. fi
  265. # Resolve dependencies
  266. print_step "Resolving dependencies..."
  267. local original_count=${#SELECTED_COMPONENTS[@]}
  268. for comp in "${SELECTED_COMPONENTS[@]}"; do
  269. resolve_dependencies "$comp"
  270. done
  271. if [ ${#SELECTED_COMPONENTS[@]} -gt $original_count ]; then
  272. print_info "Added $((${#SELECTED_COMPONENTS[@]} - original_count)) dependencies"
  273. fi
  274. show_installation_preview
  275. }
  276. #############################################################################
  277. # Installation Preview & Confirmation
  278. #############################################################################
  279. show_installation_preview() {
  280. clear
  281. print_header
  282. echo -e "${BOLD}Installation Preview${NC}\n"
  283. if [ -n "$PROFILE" ]; then
  284. echo -e "Profile: ${GREEN}${PROFILE}${NC}"
  285. else
  286. echo -e "Mode: ${GREEN}Custom${NC}"
  287. fi
  288. echo -e "\nComponents to install (${#SELECTED_COMPONENTS[@]} total):\n"
  289. # Group by type
  290. local agents=()
  291. local subagents=()
  292. local commands=()
  293. local tools=()
  294. local plugins=()
  295. local contexts=()
  296. local configs=()
  297. for comp in "${SELECTED_COMPONENTS[@]}"; do
  298. local type="${comp%%:*}"
  299. case $type in
  300. agent) agents+=("$comp") ;;
  301. subagent) subagents+=("$comp") ;;
  302. command) commands+=("$comp") ;;
  303. tool) tools+=("$comp") ;;
  304. plugin) plugins+=("$comp") ;;
  305. context) contexts+=("$comp") ;;
  306. config) configs+=("$comp") ;;
  307. esac
  308. done
  309. [ ${#agents[@]} -gt 0 ] && echo -e "${CYAN}Agents (${#agents[@]}):${NC} ${agents[*]##*:}"
  310. [ ${#subagents[@]} -gt 0 ] && echo -e "${CYAN}Subagents (${#subagents[@]}):${NC} ${subagents[*]##*:}"
  311. [ ${#commands[@]} -gt 0 ] && echo -e "${CYAN}Commands (${#commands[@]}):${NC} ${commands[*]##*:}"
  312. [ ${#tools[@]} -gt 0 ] && echo -e "${CYAN}Tools (${#tools[@]}):${NC} ${tools[*]##*:}"
  313. [ ${#plugins[@]} -gt 0 ] && echo -e "${CYAN}Plugins (${#plugins[@]}):${NC} ${plugins[*]##*:}"
  314. [ ${#contexts[@]} -gt 0 ] && echo -e "${CYAN}Contexts (${#contexts[@]}):${NC} ${contexts[*]##*:}"
  315. [ ${#configs[@]} -gt 0 ] && echo -e "${CYAN}Config (${#configs[@]}):${NC} ${configs[*]##*:}"
  316. echo ""
  317. read -p "Proceed with installation? [Y/n]: " confirm
  318. if [[ $confirm =~ ^[Nn] ]]; then
  319. print_info "Installation cancelled"
  320. cleanup_and_exit 0
  321. fi
  322. perform_installation
  323. }
  324. #############################################################################
  325. # Installation
  326. #############################################################################
  327. perform_installation() {
  328. print_step "Installing components..."
  329. # Check if .opencode already exists
  330. if [ -d "$INSTALL_DIR" ]; then
  331. print_warning "$INSTALL_DIR directory already exists"
  332. read -p "Backup and continue? [Y/n]: " backup_confirm
  333. if [[ ! $backup_confirm =~ ^[Nn] ]]; then
  334. local backup_dir="${INSTALL_DIR}.backup.$(date +%Y%m%d-%H%M%S)"
  335. mv "$INSTALL_DIR" "$backup_dir"
  336. print_success "Backed up to $backup_dir"
  337. else
  338. print_error "Installation cancelled"
  339. cleanup_and_exit 1
  340. fi
  341. fi
  342. # Create directory structure
  343. mkdir -p "$INSTALL_DIR"/{agent/subagents,command,tool,plugin,context/{core,project}}
  344. local installed=0
  345. local failed=0
  346. for comp in "${SELECTED_COMPONENTS[@]}"; do
  347. local type="${comp%%:*}"
  348. local id="${comp##*:}"
  349. # Get component path
  350. local path=$(jq -r ".components.${type}s[] | select(.id == \"${id}\") | .path" "$TEMP_DIR/registry.json")
  351. if [ -z "$path" ] || [ "$path" = "null" ]; then
  352. print_warning "Could not find path for ${comp}"
  353. ((failed++))
  354. continue
  355. fi
  356. # Download component
  357. local url="${RAW_URL}/${path}"
  358. local dest="${path}"
  359. # Create parent directory if needed
  360. mkdir -p "$(dirname "$dest")"
  361. if curl -fsSL "$url" -o "$dest"; then
  362. print_success "Installed ${type}: ${id}"
  363. ((installed++))
  364. else
  365. print_error "Failed to install ${type}: ${id}"
  366. ((failed++))
  367. fi
  368. done
  369. # Handle additional paths for advanced profile
  370. if [ "$PROFILE" = "advanced" ]; then
  371. local additional_paths=$(jq -r '.profiles.advanced.additionalPaths[]?' "$TEMP_DIR/registry.json")
  372. if [ -n "$additional_paths" ]; then
  373. print_step "Installing additional paths..."
  374. while IFS= read -r path; do
  375. # For directories, we'd need to recursively download
  376. # For now, just note them
  377. print_info "Additional path: $path (manual download required)"
  378. done <<< "$additional_paths"
  379. fi
  380. fi
  381. echo ""
  382. print_success "Installation complete!"
  383. echo -e " Installed: ${GREEN}${installed}${NC}"
  384. [ $failed -gt 0 ] && echo -e " Failed: ${RED}${failed}${NC}"
  385. show_post_install
  386. }
  387. #############################################################################
  388. # Post-Installation
  389. #############################################################################
  390. show_post_install() {
  391. echo ""
  392. print_step "Next Steps"
  393. echo "1. Review the installed components in .opencode/"
  394. echo "2. Copy env.example to .env and configure:"
  395. echo " ${CYAN}cp env.example .env${NC}"
  396. echo "3. Start using OpenCode agents:"
  397. echo " ${CYAN}opencode${NC}"
  398. echo ""
  399. print_info "Documentation: ${REPO_URL}"
  400. echo ""
  401. cleanup_and_exit 0
  402. }
  403. #############################################################################
  404. # Component Listing
  405. #############################################################################
  406. list_components() {
  407. clear
  408. print_header
  409. echo -e "${BOLD}Available Components${NC}\n"
  410. local categories=("agents" "subagents" "commands" "tools" "plugins" "contexts")
  411. for category in "${categories[@]}"; do
  412. echo -e "${CYAN}${BOLD}${category^}:${NC}"
  413. local components=$(jq -r ".components.${category}[] | \"\(.id)|\(.name)|\(.description)\"" "$TEMP_DIR/registry.json")
  414. while IFS='|' read -r id name desc; do
  415. echo -e " ${GREEN}${name}${NC} (${id})"
  416. echo -e " ${desc}"
  417. done <<< "$components"
  418. echo ""
  419. done
  420. read -p "Press Enter to continue..."
  421. }
  422. #############################################################################
  423. # Cleanup
  424. #############################################################################
  425. cleanup_and_exit() {
  426. rm -rf "$TEMP_DIR"
  427. exit "$1"
  428. }
  429. trap 'cleanup_and_exit 1' INT TERM
  430. #############################################################################
  431. # Main
  432. #############################################################################
  433. main() {
  434. # Parse command line arguments
  435. case "${1:-}" in
  436. --core)
  437. INSTALL_MODE="profile"
  438. PROFILE="core"
  439. ;;
  440. --developer)
  441. INSTALL_MODE="profile"
  442. PROFILE="developer"
  443. ;;
  444. --full)
  445. INSTALL_MODE="profile"
  446. PROFILE="full"
  447. ;;
  448. --advanced)
  449. INSTALL_MODE="profile"
  450. PROFILE="advanced"
  451. ;;
  452. --list)
  453. check_dependencies
  454. fetch_registry
  455. list_components
  456. cleanup_and_exit 0
  457. ;;
  458. --help|-h)
  459. print_header
  460. echo "Usage: $0 [OPTIONS]"
  461. echo ""
  462. echo "Options:"
  463. echo " --core Install core profile"
  464. echo " --developer Install developer profile"
  465. echo " --full Install full profile"
  466. echo " --advanced Install advanced profile"
  467. echo " --list List all available components"
  468. echo " --help Show this help message"
  469. echo ""
  470. echo "Without options, runs in interactive mode"
  471. exit 0
  472. ;;
  473. esac
  474. check_dependencies
  475. fetch_registry
  476. if [ -n "$PROFILE" ]; then
  477. # Non-interactive mode
  478. mapfile -t SELECTED_COMPONENTS < <(get_profile_components "$PROFILE")
  479. show_installation_preview
  480. else
  481. # Interactive mode
  482. show_main_menu
  483. if [ "$INSTALL_MODE" = "profile" ]; then
  484. show_profile_menu
  485. elif [ "$INSTALL_MODE" = "custom" ]; then
  486. show_custom_menu
  487. fi
  488. fi
  489. }
  490. main "$@"