|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Install prerequisites for just recipes |
| 3 | +# |
| 4 | +# Usage: ./.just/install-prerequisites.sh |
| 5 | +# |
| 6 | +# This script checks for required tools (just, gh, shellcheck, markdownlint-cli2, jq) |
| 7 | +# and helps install them: |
| 8 | +# |
| 9 | +# - macOS: Automatically installs missing tools using Homebrew |
| 10 | +# - Linux: Displays installation commands for manual execution |
| 11 | +# - Other: Shows links to installation documentation |
| 12 | +# |
| 13 | +# Run multiple times to verify installations completed successfully. |
| 14 | + |
| 15 | +set -euo pipefail # strict mode |
| 16 | + |
| 17 | +# ANSI color codes |
| 18 | +RED='\033[0;31m' |
| 19 | +GREEN='\033[0;32m' |
| 20 | +YELLOW='\033[1;33m' |
| 21 | +BLUE='\033[0;34m' |
| 22 | +CYAN='\033[0;36m' |
| 23 | +MAGENTA='\033[0;35m' |
| 24 | +NC='\033[0m' # No Color |
| 25 | + |
| 26 | +echo -e "${MAGENTA}Checking prerequisites for just recipes...${NC}" |
| 27 | +echo "" |
| 28 | + |
| 29 | +MISSING=() |
| 30 | +INSTALLED=() |
| 31 | + |
| 32 | +# check each prerequisite |
| 33 | +if command -v just &> /dev/null; then |
| 34 | + INSTALLED+=("just") |
| 35 | +else |
| 36 | + MISSING+=("just") |
| 37 | +fi |
| 38 | + |
| 39 | +if command -v gh &> /dev/null; then |
| 40 | + INSTALLED+=("gh") |
| 41 | +else |
| 42 | + MISSING+=("gh") |
| 43 | +fi |
| 44 | + |
| 45 | +if command -v shellcheck &> /dev/null; then |
| 46 | + INSTALLED+=("shellcheck") |
| 47 | +else |
| 48 | + MISSING+=("shellcheck") |
| 49 | +fi |
| 50 | + |
| 51 | +if command -v markdownlint-cli2 &> /dev/null; then |
| 52 | + INSTALLED+=("markdownlint-cli2") |
| 53 | +else |
| 54 | + MISSING+=("markdownlint-cli2") |
| 55 | +fi |
| 56 | + |
| 57 | +if command -v jq &> /dev/null; then |
| 58 | + INSTALLED+=("jq") |
| 59 | +else |
| 60 | + MISSING+=("jq") |
| 61 | +fi |
| 62 | + |
| 63 | +# report what's already installed |
| 64 | +if [[ ${#INSTALLED[@]} -gt 0 ]]; then |
| 65 | + echo -e "${GREEN}Already installed:${NC}" |
| 66 | + for tool in "${INSTALLED[@]}"; do |
| 67 | + echo -e " ${GREEN}✓${NC} $tool" |
| 68 | + done |
| 69 | + echo "" |
| 70 | +fi |
| 71 | + |
| 72 | +# if everything is installed, we're done |
| 73 | +if [[ ${#MISSING[@]} -eq 0 ]]; then |
| 74 | + echo -e "${GREEN}All prerequisites are installed!${NC}" |
| 75 | + exit 0 |
| 76 | +fi |
| 77 | + |
| 78 | +# report what's missing |
| 79 | +echo -e "${YELLOW}Missing prerequisites:${NC}" |
| 80 | +for tool in "${MISSING[@]}"; do |
| 81 | + echo -e " ${YELLOW}✗${NC} $tool" |
| 82 | +done |
| 83 | +echo "" |
| 84 | + |
| 85 | +# detect OS and provide installation instructions |
| 86 | +if [[ "$OSTYPE" == "darwin"* ]]; then |
| 87 | + echo -e "${BLUE}Detected macOS. Installing with Homebrew...${NC}" |
| 88 | + echo "" |
| 89 | + |
| 90 | + if ! command -v brew &> /dev/null; then |
| 91 | + echo -e "${RED}Homebrew is not installed!${NC}" |
| 92 | + echo "Install Homebrew first: https://brew.sh" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + |
| 96 | + INSTALL_SUCCESS=() |
| 97 | + INSTALL_FAILED=() |
| 98 | + |
| 99 | + for tool in "${MISSING[@]}"; do |
| 100 | + case "$tool" in |
| 101 | + just) |
| 102 | + echo -e "${CYAN}Installing just...${NC}" |
| 103 | + if brew install just; then |
| 104 | + INSTALL_SUCCESS+=("just") |
| 105 | + else |
| 106 | + INSTALL_FAILED+=("just") |
| 107 | + echo -e "${RED}Failed to install just${NC}" |
| 108 | + fi |
| 109 | + ;; |
| 110 | + gh) |
| 111 | + echo -e "${CYAN}Installing GitHub CLI...${NC}" |
| 112 | + if brew install gh; then |
| 113 | + INSTALL_SUCCESS+=("gh") |
| 114 | + echo -e "${YELLOW}Don't forget to authenticate: gh auth login${NC}" |
| 115 | + else |
| 116 | + INSTALL_FAILED+=("gh") |
| 117 | + echo -e "${RED}Failed to install gh${NC}" |
| 118 | + fi |
| 119 | + ;; |
| 120 | + shellcheck) |
| 121 | + echo -e "${CYAN}Installing shellcheck...${NC}" |
| 122 | + if brew install shellcheck; then |
| 123 | + INSTALL_SUCCESS+=("shellcheck") |
| 124 | + else |
| 125 | + INSTALL_FAILED+=("shellcheck") |
| 126 | + echo -e "${RED}Failed to install shellcheck${NC}" |
| 127 | + fi |
| 128 | + ;; |
| 129 | + markdownlint-cli2) |
| 130 | + echo -e "${CYAN}Installing markdownlint-cli2...${NC}" |
| 131 | + if ! command -v npm &> /dev/null; then |
| 132 | + echo -e "${RED}npm is not installed! Install Node.js first.${NC}" |
| 133 | + echo "Install Node.js: brew install node" |
| 134 | + INSTALL_FAILED+=("markdownlint-cli2") |
| 135 | + elif npm install -g markdownlint-cli2; then |
| 136 | + INSTALL_SUCCESS+=("markdownlint-cli2") |
| 137 | + else |
| 138 | + INSTALL_FAILED+=("markdownlint-cli2") |
| 139 | + echo -e "${RED}Failed to install markdownlint-cli2${NC}" |
| 140 | + fi |
| 141 | + ;; |
| 142 | + jq) |
| 143 | + echo -e "${CYAN}Installing jq...${NC}" |
| 144 | + if brew install jq; then |
| 145 | + INSTALL_SUCCESS+=("jq") |
| 146 | + else |
| 147 | + INSTALL_FAILED+=("jq") |
| 148 | + echo -e "${RED}Failed to install jq${NC}" |
| 149 | + fi |
| 150 | + ;; |
| 151 | + esac |
| 152 | + done |
| 153 | + |
| 154 | + echo "" |
| 155 | + if [[ ${#INSTALL_SUCCESS[@]} -gt 0 ]]; then |
| 156 | + echo -e "${GREEN}Successfully installed:${NC}" |
| 157 | + for tool in "${INSTALL_SUCCESS[@]}"; do |
| 158 | + echo -e " ${GREEN}✓${NC} $tool" |
| 159 | + done |
| 160 | + fi |
| 161 | + |
| 162 | + if [[ ${#INSTALL_FAILED[@]} -gt 0 ]]; then |
| 163 | + echo "" |
| 164 | + echo -e "${RED}Failed to install:${NC}" |
| 165 | + for tool in "${INSTALL_FAILED[@]}"; do |
| 166 | + echo -e " ${RED}✗${NC} $tool" |
| 167 | + done |
| 168 | + echo "" |
| 169 | + echo -e "${YELLOW}Run this script again to retry or install manually.${NC}" |
| 170 | + exit 1 |
| 171 | + fi |
| 172 | + |
| 173 | +elif [[ "$OSTYPE" == "linux-gnu"* ]]; then |
| 174 | + echo -e "${BLUE}Detected Linux. Showing installation commands...${NC}" |
| 175 | + echo "" |
| 176 | + |
| 177 | + # detect package manager |
| 178 | + if command -v apt-get &> /dev/null; then |
| 179 | + PKG_MGR="apt-get" |
| 180 | + echo -e "${BLUE}Using apt-get package manager${NC}" |
| 181 | + elif command -v dnf &> /dev/null; then |
| 182 | + PKG_MGR="dnf" |
| 183 | + echo -e "${BLUE}Using dnf package manager${NC}" |
| 184 | + elif command -v pacman &> /dev/null; then |
| 185 | + PKG_MGR="pacman" |
| 186 | + echo -e "${BLUE}Using pacman package manager${NC}" |
| 187 | + else |
| 188 | + echo -e "${YELLOW}Could not detect package manager. Manual installation required.${NC}" |
| 189 | + PKG_MGR="manual" |
| 190 | + fi |
| 191 | + |
| 192 | + for tool in "${MISSING[@]}"; do |
| 193 | + case "$tool" in |
| 194 | + just) |
| 195 | + if [[ "$PKG_MGR" == "apt-get" ]]; then |
| 196 | + echo -e "${CYAN}Install just:${NC}" |
| 197 | + echo " wget -qO - 'https://proget.makedeb.org/debian-feeds/prebuilt-mpr.pub' | gpg --dearmor | sudo tee /usr/share/keyrings/prebuilt-mpr-archive-keyring.gpg 1> /dev/null" |
| 198 | + echo " echo \"deb [arch=all,\$(dpkg --print-architecture) signed-by=/usr/share/keyrings/prebuilt-mpr-archive-keyring.gpg] https://proget.makedeb.org prebuilt-mpr \$(lsb_release -cs)\" | sudo tee /etc/apt/sources.list.d/prebuilt-mpr.list" |
| 199 | + echo " sudo apt update && sudo apt install just" |
| 200 | + elif [[ "$PKG_MGR" == "dnf" ]]; then |
| 201 | + echo -e "${CYAN}Install just:${NC} sudo dnf install just" |
| 202 | + elif [[ "$PKG_MGR" == "pacman" ]]; then |
| 203 | + echo -e "${CYAN}Install just:${NC} sudo pacman -S just" |
| 204 | + else |
| 205 | + echo -e "${CYAN}Install just:${NC} See https://github.com/casey/just#installation" |
| 206 | + fi |
| 207 | + ;; |
| 208 | + gh) |
| 209 | + echo -e "${CYAN}Install GitHub CLI:${NC} See https://github.com/cli/cli/blob/trunk/docs/install_linux.md" |
| 210 | + echo -e "${YELLOW}Don't forget to authenticate: gh auth login${NC}" |
| 211 | + ;; |
| 212 | + shellcheck) |
| 213 | + if [[ "$PKG_MGR" == "apt-get" ]]; then |
| 214 | + echo -e "${CYAN}Install shellcheck:${NC} sudo apt-get install shellcheck" |
| 215 | + elif [[ "$PKG_MGR" == "dnf" ]]; then |
| 216 | + echo -e "${CYAN}Install shellcheck:${NC} sudo dnf install shellcheck" |
| 217 | + elif [[ "$PKG_MGR" == "pacman" ]]; then |
| 218 | + echo -e "${CYAN}Install shellcheck:${NC} sudo pacman -S shellcheck" |
| 219 | + fi |
| 220 | + ;; |
| 221 | + markdownlint-cli2) |
| 222 | + echo -e "${CYAN}Install markdownlint-cli2:${NC} npm install -g markdownlint-cli2" |
| 223 | + echo -e "${YELLOW}(Requires Node.js/npm)${NC}" |
| 224 | + ;; |
| 225 | + jq) |
| 226 | + if [[ "$PKG_MGR" == "apt-get" ]]; then |
| 227 | + echo -e "${CYAN}Install jq:${NC} sudo apt-get install jq" |
| 228 | + elif [[ "$PKG_MGR" == "dnf" ]]; then |
| 229 | + echo -e "${CYAN}Install jq:${NC} sudo dnf install jq" |
| 230 | + elif [[ "$PKG_MGR" == "pacman" ]]; then |
| 231 | + echo -e "${CYAN}Install jq:${NC} sudo pacman -S jq" |
| 232 | + fi |
| 233 | + ;; |
| 234 | + esac |
| 235 | + done |
| 236 | + |
| 237 | + echo "" |
| 238 | + echo -e "${YELLOW}Note: Commands shown above for manual execution.${NC}" |
| 239 | + echo -e "${YELLOW}Run this script again after installing to verify.${NC}" |
| 240 | + |
| 241 | +else |
| 242 | + echo -e "${YELLOW}Unsupported OS: $OSTYPE${NC}" |
| 243 | + echo -e "${YELLOW}Please install these tools manually:${NC}" |
| 244 | + for tool in "${MISSING[@]}"; do |
| 245 | + echo " - $tool" |
| 246 | + done |
| 247 | + echo "" |
| 248 | + echo -e "${CYAN}Installation resources:${NC}" |
| 249 | + echo " just: https://github.com/casey/just#installation" |
| 250 | + echo " gh: https://cli.github.com/manual/installation" |
| 251 | + echo " shellcheck: https://github.com/koalaman/shellcheck#installing" |
| 252 | + echo " markdownlint-cli2: npm install -g markdownlint-cli2" |
| 253 | + echo " jq: https://stedolan.github.io/jq/download/" |
| 254 | + exit 1 |
| 255 | +fi |
| 256 | + |
| 257 | +echo "" |
| 258 | +echo -e "${GREEN}Installation complete! Run this script again to verify.${NC}" |
0 commit comments