|
1 | | -# Shell scripts |
| 1 | +# Sparkdock Development Guide |
2 | 2 |
|
3 | | -When asked to write or suggest shell scripts, use all best practices for shell scripting, including: |
| 3 | +Sparkdock is an automated macOS development environment provisioner built with Ansible, featuring containerized workflows, an HTTP proxy system, and a Swift menu bar app for update notifications. |
4 | 4 |
|
5 | | -- Use `#!/usr/bin/env bash` as the shebang line. |
6 | | -- Use `set -euo pipefail` to ensure the script exits on errors, undefined variables, or failed commands in a pipeline. |
7 | | -- Use curly braces for variable expansion, e.g., `${variable}`. Completely avoid using `$variable` without braces. |
8 | | -- Use `local` for variables inside functions to avoid polluting the global namespace. |
9 | | -- Always run `shellcheck` on the script to catch common errors. |
| 5 | +## Core Architecture |
| 6 | + |
| 7 | +**Three-Layer System:** |
| 8 | + |
| 9 | +- **Ansible Provisioning**: Core system configuration (`ansible/macos.yml` → `ansible/macos/macos/base.yml`) |
| 10 | +- **SparkJust Task Runner**: Development workflow automation (`sjust/` with modular recipes) |
| 11 | +- **Menu Bar Manager**: Swift app for update notifications (`src/menubar-app/`) |
| 12 | + |
| 13 | +**Key Integration Points:** |
| 14 | + |
| 15 | +- Package definitions in `config/packages/all-packages.yml` drive Ansible provisioning |
| 16 | +- SparkJust wrapper (`sjust/sjust.sh`) delegates to Just task runner with custom recipes |
| 17 | +- HTTP proxy system cloned separately to `/opt/sparkdock/http-proxy` during installation |
| 18 | + |
| 19 | +## Essential Workflows |
| 20 | + |
| 21 | +**Installation Chain**: `bin/install.macos` → `bin/sparkdock.macos` → Ansible provisioning → HTTP proxy setup → Menu bar app |
| 22 | +**Update Workflow**: Auto-update via git fetch/reset → rollback capability → background notifications |
| 23 | +**Task Execution**: `sjust` → Just recipes in `sjust/recipes/` → Ansible tags or shell commands |
| 24 | + |
| 25 | +## Project Conventions |
| 26 | + |
| 27 | +**Package Management Pattern:** |
| 28 | + |
| 29 | +```yaml |
| 30 | +# config/packages/all-packages.yml structure |
| 31 | +taps: [koekeishiya/formulae] |
| 32 | +cask_packages: [docker-desktop, visual-studio-code] |
| 33 | +homebrew_packages: [awscli, kubernetes-cli] |
| 34 | +removed_cask_packages: [] # Track for clean uninstalls |
| 35 | +``` |
| 36 | +
|
| 37 | +**Ansible Tag Strategy:** |
| 38 | +
|
| 39 | +- Tags like `docker`, `http-proxy`, `menubar` enable selective provisioning |
| 40 | +- Use `sjust install-tags "docker,keyboard"` for targeted installs |
| 41 | +- All tasks must be idempotent with proper assertion checks |
| 42 | + |
| 43 | +**SparkJust Recipe Organization:** |
| 44 | + |
| 45 | +- `00-default.just`: Core system tasks (cleanup, updates, device info) |
| 46 | +- `01-lima.just`: Lima container environment tasks |
| 47 | +- `~/.config/sjust/100-custom.just`: User customizations (optional import) |
| 48 | + |
| 49 | +## Swift Menu Bar App Patterns |
| 50 | + |
| 51 | +**Modern Concurrency**: Uses structured concurrency with `withTaskCancellationHandler` for process timeout |
| 52 | +**Event-Driven Updates**: Triggers on network changes and system wake (no polling) |
| 53 | +**Resource Fallbacks**: Custom logo → SF Symbols, JSON config → minimal menu |
| 54 | +**Integration**: Installed via Ansible `menubar` tag, auto-starts via LaunchAgent |
| 55 | + |
| 56 | +Example structured concurrency pattern: |
| 57 | + |
| 58 | +```swift |
| 59 | +let result = await withTaskCancellationHandler( |
| 60 | + operation: { try await withTimeout(seconds: 30) { /* async work */ } }, |
| 61 | + onCancel: { process.terminate() } |
| 62 | +) |
| 63 | +``` |
| 64 | + |
| 65 | +## Shell Script Standards |
| 66 | + |
| 67 | +All shell scripts must follow these patterns: |
| 68 | + |
| 69 | +- Use `#!/usr/bin/env bash` shebang line |
| 70 | +- Include `set -euo pipefail` for strict error handling |
| 71 | +- Use `${variable}` syntax with braces (never `$variable`) |
| 72 | +- Use `local` for function variables to avoid namespace pollution |
| 73 | +- Pass `shellcheck` validation before committing |
| 74 | +- When implementing commands, when things exist, do not use `else` branches unless necessary, for example: |
| 75 | + |
| 76 | +Prefer to do this: |
| 77 | + |
| 78 | +```bash |
| 79 | +#!/usr/bin/env bash |
| 80 | +if ! tart list | grep -q "sparkdock-test"; then |
| 81 | + tart clone {{IMAGE}} sparkdock-test |
| 82 | + echo "✅ VM 'sparkdock-test' created successfully" |
| 83 | +fi |
| 84 | +``` |
| 85 | +Instead of this: |
| 86 | + |
| 87 | +Do not do this: |
| 88 | + |
| 89 | +```bash |
| 90 | +#!/usr/bin/env bash |
| 91 | + if ! tart list | grep -q "sparkdock-test"; then |
| 92 | + tart clone {{IMAGE}} sparkdock-test |
| 93 | + echo "✅ VM 'sparkdock-test' created successfully" |
| 94 | + else |
| 95 | + echo "VM 'sparkdock-test' already exists" |
| 96 | + fi |
| 97 | +```` |
| 98 | +
|
| 99 | +Instead of this: |
| 100 | +
|
| 101 | +```bash |
| 102 | + if ! command -v cirrus >/dev/null 2>&1; then |
| 103 | + echo "Installing Cirrus CLI via Homebrew..." |
| 104 | + brew install cirruslabs/cli/cirrus |
| 105 | + else |
| 106 | + echo "Cirrus CLI is already installed" |
| 107 | + fi |
| 108 | +``` |
| 109 | + |
| 110 | +Do this: |
| 111 | + |
| 112 | +```bash |
| 113 | + if ! command -v cirrus >/dev/null 2>&1; then |
| 114 | + echo "Installing Cirrus CLI via Homebrew..." |
| 115 | + brew install cirruslabs/cli/cirrus |
| 116 | + fi |
| 117 | +``` |
| 118 | + |
| 119 | +**Critical: No Trailing Whitespace** |
| 120 | + |
| 121 | +- Git warns about trailing whitespace in commits |
| 122 | +- Clean up before staging changes across ALL file types |
| 123 | +- Use editor whitespace visualization to identify issues |
| 124 | + |
| 125 | +## HTTP Proxy Integration |
| 126 | + |
| 127 | +**Architecture**: Separate repository cloned to `/opt/sparkdock/http-proxy` |
| 128 | +**DNS Resolution**: `.loc` domains automatically resolved via DNS resolver configuration |
| 129 | +**SSL Certificates**: mkcert integration for local HTTPS development |
| 130 | +**Management**: `spark-http-proxy start/stop/status` commands |
| 131 | + |
| 132 | +## Build and Test Patterns |
| 133 | + |
| 134 | +**Ansible**: `make run-ansible-macos TAGS="docker,http-proxy"` for targeted provisioning |
| 135 | +**Swift App**: `cd src/menubar-app && make build/test/install` for menu bar development |
| 136 | +**System Updates**: `sjust upgrade-system` for Homebrew maintenance |
| 137 | +**Debugging**: Use `sjust device-info` for system diagnostics |
| 138 | + |
| 139 | +## Git Workflow Specifics |
| 140 | + |
| 141 | +**Default Branch**: `master` (project-specific, not `main`) |
| 142 | +**Update Safety**: Automatic stashing of local changes during updates |
| 143 | +**Lock File**: `/tmp/sparkdock.lock` prevents concurrent updates |
| 144 | +**Rollback**: Built-in rollback on failed updates using stored commit hashes |
0 commit comments