|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Brand Colors (True Color / 24-bit) |
| 6 | +YELLOW='\033[38;2;224;255;110m' # #E0FF6E |
| 7 | +GREEN='\033[38;2;121;252;150m' # #79FC96 |
| 8 | +GREEN_LIGHT='\033[38;2;197;220;217m' # #C5DCD9 |
| 9 | +GREEN_DARK='\033[38;2;34;57;54m' # #223936 |
| 10 | +BLUE='\033[38;2;10;31;212m' # #0A1FD4 |
| 11 | +BLACK='\033[38;2;26;26;26m' # #1A1A1A |
| 12 | +WHITE='\033[38;2;255;255;255m' # #FFFFFF |
| 13 | +RED='\033[38;2;239;68;68m' # #EF4444 |
| 14 | +GRAY='\033[38;2;128;128;128m' # #808080 |
| 15 | +GRAY_LIGHT='\033[38;2;211;211;211m' # #D3D3D3 |
| 16 | +BOLD='\033[1m' |
| 17 | +NC='\033[0m' # No Color / Reset |
| 18 | + |
| 19 | +# Required Python version |
| 20 | +REQUIRED_MAJOR=3 |
| 21 | +REQUIRED_MINOR=11 |
| 22 | + |
| 23 | +# Detect OS |
| 24 | +detect_os() { |
| 25 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 26 | + echo "macos" |
| 27 | + elif [[ -f /etc/debian_version ]]; then |
| 28 | + echo "debian" |
| 29 | + elif [[ -f /etc/redhat-release ]]; then |
| 30 | + echo "redhat" |
| 31 | + else |
| 32 | + echo "unknown" |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +echo -e "started ${YELLOW}${BOLD}*codeplain CLI${NC} installation..." |
| 37 | + |
| 38 | +# Install Python based on OS |
| 39 | +install_python() { |
| 40 | + local os=$(detect_os) |
| 41 | + |
| 42 | + case $os in |
| 43 | + macos) |
| 44 | + if command -v brew &> /dev/null; then |
| 45 | + echo -e "installing Python ${REQUIRED_MAJOR}.${REQUIRED_MINOR} via Homebrew..." |
| 46 | + brew install python@${REQUIRED_MAJOR}.${REQUIRED_MINOR} |
| 47 | + else |
| 48 | + echo -e "${RED}Error: Homebrew is not installed.${NC}" |
| 49 | + echo "please install Homebrew first: https://brew.sh" |
| 50 | + echo "or install Python manually from: https://www.python.org/downloads/" |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + ;; |
| 54 | + debian) |
| 55 | + echo -e "installing Python ${REQUIRED_MAJOR}.${REQUIRED_MINOR} via apt..." |
| 56 | + sudo apt update |
| 57 | + sudo apt install -y python${REQUIRED_MAJOR}.${REQUIRED_MINOR} python${REQUIRED_MAJOR}.${REQUIRED_MINOR}-venv python3-pip |
| 58 | + ;; |
| 59 | + redhat) |
| 60 | + echo -e "installing Python ${REQUIRED_MAJOR}.${REQUIRED_MINOR} via dnf..." |
| 61 | + sudo dnf install -y python${REQUIRED_MAJOR}.${REQUIRED_MINOR} |
| 62 | + ;; |
| 63 | + *) |
| 64 | + echo -e "${RED}Error: Automatic installation not supported for your OS.${NC}" |
| 65 | + echo "please install Python ${REQUIRED_MAJOR}.${REQUIRED_MINOR} manually from:" |
| 66 | + echo " https://www.python.org/downloads/" |
| 67 | + exit 1 |
| 68 | + ;; |
| 69 | + esac |
| 70 | +} |
| 71 | + |
| 72 | +# Prompt user to install Python |
| 73 | +prompt_install_python() { |
| 74 | + echo "" |
| 75 | + read -p "$(echo -e ${YELLOW}would you like to install Python ${REQUIRED_MAJOR}.${REQUIRED_MINOR}? \(Y/n\): ${NC})" response |
| 76 | + case "$response" in |
| 77 | + [yY][eE][sS]|[yY]|"") |
| 78 | + install_python |
| 79 | + echo "" |
| 80 | + echo -e "${GREEN}✓ python installed.${NC} please restart your terminal and run this script again." |
| 81 | + exit 0 |
| 82 | + ;; |
| 83 | + *) |
| 84 | + echo -e "${YELLOW}installation cancelled.${NC}" |
| 85 | + exit 1 |
| 86 | + ;; |
| 87 | + esac |
| 88 | +} |
| 89 | + |
| 90 | +# Check if python3 or python is installed |
| 91 | +if command -v python3.11 &> /dev/null; then |
| 92 | + PYTHON_CMD="python3.11" |
| 93 | +elif command -v python3 &> /dev/null; then |
| 94 | + PYTHON_CMD="python3" |
| 95 | +elif command -v python &> /dev/null; then |
| 96 | + PYTHON_CMD="python" |
| 97 | +else |
| 98 | + echo -e "${RED}error: Python 3 is not installed.${NC}" |
| 99 | + prompt_install_python |
| 100 | +fi |
| 101 | + |
| 102 | +# Get Python version |
| 103 | +PYTHON_VERSION=$($PYTHON_CMD -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")') |
| 104 | +PYTHON_MAJOR=$(echo "$PYTHON_VERSION" | cut -d. -f1) |
| 105 | +PYTHON_MINOR=$(echo "$PYTHON_VERSION" | cut -d. -f2) |
| 106 | + |
| 107 | +# Check version |
| 108 | +if [ "$PYTHON_MAJOR" -lt "$REQUIRED_MAJOR" ] || \ |
| 109 | + ([ "$PYTHON_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$PYTHON_MINOR" -lt "$REQUIRED_MINOR" ]); then |
| 110 | + echo -e "${RED}error: Python ${REQUIRED_MAJOR}.${REQUIRED_MINOR} or greater is required.${NC}" |
| 111 | + echo -e "found: python ${YELLOW}${PYTHON_VERSION}${NC}" |
| 112 | + prompt_install_python |
| 113 | +fi |
| 114 | + |
| 115 | +echo -e "" |
| 116 | +echo -e "${GREEN}✓${NC} python ${BOLD}${PYTHON_VERSION}${NC} detected" |
| 117 | +echo -e "" |
| 118 | +# Use python -m pip for reliability |
| 119 | +PIP_CMD="$PYTHON_CMD -m pip" |
| 120 | + |
| 121 | +# Install or upgrade codeplain |
| 122 | +if $PIP_CMD show codeplain &> /dev/null; then |
| 123 | + CURRENT_VERSION=$($PIP_CMD show codeplain | grep "^Version:" | cut -d' ' -f2) |
| 124 | + echo -e "${GRAY}codeplain ${CURRENT_VERSION} is already installed.${NC}" |
| 125 | + echo -e "upgrading to latest version..." |
| 126 | + echo -e "" |
| 127 | + $PIP_CMD install --upgrade codeplain &> /dev/null |
| 128 | + NEW_VERSION=$($PIP_CMD show codeplain | grep "^Version:" | cut -d' ' -f2) |
| 129 | + if [ "$CURRENT_VERSION" = "$NEW_VERSION" ]; then |
| 130 | + echo -e "${GREEN}✓${NC} codeplain is already up to date (${NEW_VERSION})" |
| 131 | + else |
| 132 | + echo -e "${GREEN}✓${NC} codeplain upgraded from ${CURRENT_VERSION} to ${NEW_VERSION}!" |
| 133 | + fi |
| 134 | +else |
| 135 | + echo -e "installing codeplain...${NC}" |
| 136 | + echo -e "" |
| 137 | + $PIP_CMD install codeplain &> /dev/null |
| 138 | + echo -e "${GREEN}✓ codeplain installed successfully!${NC}" |
| 139 | +fi |
| 140 | + |
| 141 | +echo -e "${GREEN}✓${NC} the latest version of *codeplain CLI is now installed." |
| 142 | +echo "" |
| 143 | +echo -e "go to ${YELLOW}https://platform.codeplain.ai${NC} and sign up to get your API key." |
| 144 | +echo "" |
| 145 | +read -p "paste your API key here: " API_KEY |
| 146 | +echo "" |
| 147 | + |
| 148 | +if [ -z "$API_KEY" ]; then |
| 149 | + echo -e "${GRAY}no API key provided. you can set it later with:${NC}" |
| 150 | + echo -e " export CODEPLAIN_API_KEY=\"your_api_key\"" |
| 151 | +else |
| 152 | + # Export for current session |
| 153 | + export CODEPLAIN_API_KEY="$API_KEY" |
| 154 | + |
| 155 | + # Detect user's default shell from $SHELL (works even when script runs in different shell) |
| 156 | + case "$SHELL" in |
| 157 | + */zsh) |
| 158 | + SHELL_RC="$HOME/.zprofile" |
| 159 | + ;; |
| 160 | + */bash) |
| 161 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 162 | + # macOS uses .bash_profile for login shells |
| 163 | + SHELL_RC="$HOME/.bash_profile" |
| 164 | + else |
| 165 | + SHELL_RC="$HOME/.bashrc" |
| 166 | + fi |
| 167 | + ;; |
| 168 | + *) |
| 169 | + SHELL_RC="$HOME/.profile" |
| 170 | + ;; |
| 171 | + esac |
| 172 | + |
| 173 | + # Create the file if it doesn't exist |
| 174 | + touch "$SHELL_RC" |
| 175 | + |
| 176 | + # Add to shell config if not already present |
| 177 | + if ! grep -q "CODEPLAIN_API_KEY" "$SHELL_RC" 2>/dev/null; then |
| 178 | + echo "" >> "$SHELL_RC" |
| 179 | + echo "# codeplain API Key" >> "$SHELL_RC" |
| 180 | + echo "export CODEPLAIN_API_KEY=\"$API_KEY\"" >> "$SHELL_RC" |
| 181 | + echo -e "${GREEN}✓ API key saved to ${SHELL_RC}${NC}" |
| 182 | + else |
| 183 | + # Update existing key (different sed syntax for macOS vs Linux) |
| 184 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 185 | + sed -i '' "s|export CODEPLAIN_API_KEY=.*|export CODEPLAIN_API_KEY=\"$API_KEY\"|" "$SHELL_RC" |
| 186 | + else |
| 187 | + sed -i "s|export CODEPLAIN_API_KEY=.*|export CODEPLAIN_API_KEY=\"$API_KEY\"|" "$SHELL_RC" |
| 188 | + fi |
| 189 | + echo -e "${GREEN}✓${NC} API key added to ${SHELL_RC}" |
| 190 | + fi |
| 191 | + |
| 192 | +fi |
| 193 | + |
| 194 | +# ASCII Art Welcome |
| 195 | +echo "" |
| 196 | +echo -e "${NC}" |
| 197 | +echo -e "${GRAY}────────────────────────────────────────────${NC}" |
| 198 | +echo -e "" |
| 199 | +cat << 'EOF' |
| 200 | + _ _ _ |
| 201 | + ___ ___ __| | ___ _ __ | | __ _(_)_ __ |
| 202 | + / __/ _ \ / _` |/ _ \ '_ \| |/ _` | | '_ \ |
| 203 | + | (_| (_) | (_| | __/ |_) | | (_| | | | | | |
| 204 | + \___\___/ \__,_|\___| .__/|_|\__,_|_|_| |_| |
| 205 | + |_| |
| 206 | +EOF |
| 207 | +echo "" |
| 208 | +echo -e " ${YELLOW}welcome to *codeplain!${NC}" |
| 209 | +echo "" |
| 210 | +echo -e " spec-driven, production-ready code generation" |
| 211 | +echo "" |
| 212 | +echo "" |
| 213 | +echo -e "${GRAY}────────────────────────────────────────────${NC}" |
| 214 | +echo "" |
| 215 | +echo -e " thank you for using *codeplain!" |
| 216 | +echo "" |
| 217 | +echo -e " run '${YELLOW}${BOLD}codeplain <path_to_plain_file>${NC}' to get started." |
0 commit comments