|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# devbase root directory (where devbase command is installed) |
| 5 | +# Resolve symlinks to find the actual location of the devbase script |
| 6 | +SCRIPT_PATH="${BASH_SOURCE[0]}" |
| 7 | +while [ -L "$SCRIPT_PATH" ]; do |
| 8 | + SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" && pwd)" |
| 9 | + SCRIPT_PATH="$(readlink "$SCRIPT_PATH")" |
| 10 | + [[ $SCRIPT_PATH != /* ]] && SCRIPT_PATH="$SCRIPT_DIR/$SCRIPT_PATH" |
| 11 | +done |
| 12 | +SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" && pwd)" |
| 13 | +DEVBASE_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 14 | + |
| 15 | +# Environment setup |
| 16 | +export DOCKER_GID=$( [ "$(uname)" = "Darwin" ] && echo "0" || grep docker /etc/group | cut -d: -f3 ) |
| 17 | +export COMPOSE_PROJECT_NAME=$(basename "$PWD") |
| 18 | +[ -f ".env" ] && set -a && source .env && set +a |
| 19 | +[ -f "env" ] && set -a && source ./env && set +a |
| 20 | + |
| 21 | +# Export for Python modules |
| 22 | +export DEVBASE_ROOT |
| 23 | + |
| 24 | +# =================================================================== |
| 25 | +# Function definitions |
| 26 | +# =================================================================== |
| 27 | + |
| 28 | +cmd_build() { |
| 29 | + echo "=== Building devbase images ===" |
| 30 | + |
| 31 | + # Helper: Check if Dockerfile uses devbase-* base images |
| 32 | + check_base_image_dependency() { |
| 33 | + local dockerfile_path="$1" |
| 34 | + if [ -f "$dockerfile_path" ]; then |
| 35 | + if grep -q "^FROM devbase-" "$dockerfile_path"; then |
| 36 | + # Extract base image name (e.g., "devbase-base", "devbase-general") |
| 37 | + grep "^FROM devbase-" "$dockerfile_path" | head -1 | awk '{print $2}' | cut -d: -f1 |
| 38 | + return 0 |
| 39 | + fi |
| 40 | + fi |
| 41 | + return 1 |
| 42 | + } |
| 43 | + |
| 44 | + # Helper: Build base image |
| 45 | + build_base_image() { |
| 46 | + local base_image="$1" |
| 47 | + shift |
| 48 | + local container_dir="${DEVBASE_ROOT}/containers/${base_image#devbase-}" |
| 49 | + |
| 50 | + if [ ! -d "$container_dir" ]; then |
| 51 | + echo "✗ Container directory not found: $container_dir" |
| 52 | + return 1 |
| 53 | + fi |
| 54 | + |
| 55 | + echo "Building ${base_image}:latest..." |
| 56 | + if docker buildx build --load -t "${base_image}:latest" "$container_dir" "$@"; then |
| 57 | + echo "✓ ${base_image} built successfully" |
| 58 | + return 0 |
| 59 | + else |
| 60 | + echo "✗ Failed to build ${base_image}" |
| 61 | + return 1 |
| 62 | + fi |
| 63 | + } |
| 64 | + |
| 65 | + # Detect Dockerfile path from compose.yml |
| 66 | + local dockerfile_path="Dockerfile" |
| 67 | + if [ -f "compose.yml" ]; then |
| 68 | + # Try to extract dockerfile path from compose.yml (simple grep approach) |
| 69 | + local dockerfile_line=$(grep -A 3 "build:" compose.yml | grep "dockerfile:" | head -1 | awk '{print $2}') |
| 70 | + if [ -n "$dockerfile_line" ]; then |
| 71 | + dockerfile_path="$dockerfile_line" |
| 72 | + fi |
| 73 | + |
| 74 | + # If dockerfile is relative, combine with context path |
| 75 | + local context_line=$(grep -A 3 "build:" compose.yml | grep "context:" | head -1 | awk '{print $2}') |
| 76 | + if [ -n "$context_line" ]; then |
| 77 | + # Expand environment variables in context path |
| 78 | + context_line=$(eval echo "$context_line") |
| 79 | + if [[ "$dockerfile_path" != /* ]]; then |
| 80 | + dockerfile_path="${context_line}/${dockerfile_path}" |
| 81 | + fi |
| 82 | + fi |
| 83 | + fi |
| 84 | + |
| 85 | + # --no-cache specified: always rebuild base first |
| 86 | + if [[ "$*" == *"--no-cache"* ]]; then |
| 87 | + echo "" |
| 88 | + echo "[1/3] Clearing buildx cache..." |
| 89 | + docker builder prune -af >/dev/null 2>&1 || true |
| 90 | + echo "✓ Buildx cache cleared" |
| 91 | + |
| 92 | + echo "" |
| 93 | + echo "[2/3] Building devbase-base..." |
| 94 | + if ! build_base_image "devbase-base" "$@"; then |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + |
| 98 | + echo "" |
| 99 | + echo "[3/3] Building project image..." |
| 100 | + if docker compose build dev "$@"; then |
| 101 | + echo "" |
| 102 | + echo "✓ All images built successfully" |
| 103 | + else |
| 104 | + echo "" |
| 105 | + echo "✗ Failed to build project image" |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | + return |
| 109 | + fi |
| 110 | + |
| 111 | + # Normal build: check if project uses devbase-* base image |
| 112 | + local base_image_name=$(check_base_image_dependency "$dockerfile_path") |
| 113 | + if [ -n "$base_image_name" ]; then |
| 114 | + echo "" |
| 115 | + echo "[1/2] Project uses ${base_image_name}, building base image..." |
| 116 | + if ! build_base_image "$base_image_name" "$@"; then |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + else |
| 120 | + # Fallback: check if devbase-base exists |
| 121 | + if ! docker image inspect devbase-base:latest >/dev/null 2>&1; then |
| 122 | + echo "" |
| 123 | + echo "[1/2] Building devbase-base..." |
| 124 | + if ! build_base_image "devbase-base" "$@"; then |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | + else |
| 128 | + echo "[1/2] devbase-base already exists (use --no-cache to rebuild)" |
| 129 | + fi |
| 130 | + fi |
| 131 | + |
| 132 | + echo "" |
| 133 | + echo "[2/2] Building project image..." |
| 134 | + if docker compose build dev "$@"; then |
| 135 | + echo "" |
| 136 | + echo "✓ All images built successfully" |
| 137 | + else |
| 138 | + echo "" |
| 139 | + echo "✗ Failed to build project image" |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | +} |
| 143 | + |
| 144 | +# =================================================================== |
| 145 | +# Command dispatch |
| 146 | +# =================================================================== |
| 147 | + |
| 148 | +# Ensure uv is available (auto-install if missing) |
| 149 | +ensure_uv() { |
| 150 | + if command -v uv >/dev/null 2>&1; then |
| 151 | + return 0 |
| 152 | + fi |
| 153 | + echo "Installing uv..." >&2 |
| 154 | + curl -LsSf https://astral.sh/uv/install.sh | sh >/dev/null 2>&1 |
| 155 | + export PATH="$HOME/.local/bin:$PATH" |
| 156 | + if ! command -v uv >/dev/null 2>&1; then |
| 157 | + echo "Error: Failed to install uv" >&2 |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +# Helper to run Python implementation via uv |
| 163 | +run_python() { |
| 164 | + ensure_uv |
| 165 | + PYTHONPATH="${DEVBASE_ROOT}/lib:$PYTHONPATH" exec uv run --project "$DEVBASE_ROOT" python -m devbase.cli "$@" |
| 166 | +} |
| 167 | + |
| 168 | +# Resolve abbreviated command to full command name via unique prefix matching |
| 169 | +resolve_command() { |
| 170 | + local input="$1" |
| 171 | + local commands="init status container ct env plugin pl snapshot ss up down login build ps help" |
| 172 | + local matches=() |
| 173 | + for cmd in $commands; do |
| 174 | + [[ "$cmd" == "$input"* ]] && matches+=("$cmd") |
| 175 | + done |
| 176 | + if [ ${#matches[@]} -eq 1 ]; then |
| 177 | + echo "${matches[0]}" |
| 178 | + else |
| 179 | + echo "$input" # no match or ambiguous -> return as-is |
| 180 | + fi |
| 181 | +} |
| 182 | + |
| 183 | +# Resolve the command (skip flags like --version, -V, -h, --help) |
| 184 | +_resolved_cmd="${1:-}" |
| 185 | +case "$_resolved_cmd" in |
| 186 | + --*|-*|"") ;; # flags and empty: don't resolve |
| 187 | + *) _resolved_cmd="$(resolve_command "$_resolved_cmd")" ;; |
| 188 | +esac |
| 189 | + |
| 190 | +case "$_resolved_cmd" in |
| 191 | + # Python-implemented commands |
| 192 | + --version|-V) |
| 193 | + run_python "$@" ;; |
| 194 | + init|status|container|ct|env|plugin|pl|snapshot|ss|up|down|login|ps) |
| 195 | + run_python "${_resolved_cmd}" "${@:2}" ;; |
| 196 | + # Shell-implemented commands |
| 197 | + build) shift; cmd_build "$@" ;; |
| 198 | + # Help and unknown |
| 199 | + -h|--help|help|"") run_python "--help" ;; |
| 200 | + *) echo "Error: unknown command '$1'" >&2; exit 1 ;; |
| 201 | +esac |
0 commit comments