-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·373 lines (318 loc) · 14 KB
/
setup.sh
File metadata and controls
executable file
·373 lines (318 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
#
# SPDX-License-Identifier: MIT
set -uo pipefail
# ============================================================================
# DevBase Installation Setup Script
# ============================================================================
# Guard: Verify running in bash (must check before any bash-specific code)
# Note: BASH_VERSION check must use ${:-} since this runs before our imports
if [ -z "${BASH_VERSION:-}" ]; then
echo "ERROR: This script must be run with bash, not sourced in another shell."
echo "Usage: bash setup.sh"
echo " or: ./setup.sh"
exit 1
fi
handle_interrupt() {
printf "\n\nInstallation cancelled by user (Ctrl+C)\n" >&2
exit 130 # 128 + SIGINT(2)
}
handle_terminate() {
printf "\n\nInstallation terminated\n" >&2
exit 143 # 128 + SIGTERM(15)
}
trap handle_interrupt INT
trap handle_terminate TERM
# ============================================================================
# OPTIONAL ENVIRONMENT VARIABLES (fail-fast initialization)
# ============================================================================
# Import environment variables that may be set by user/environment before running setup.sh
# Importing here with empty defaults enables fail-fast (set -u) for the rest of the script
#
# For complete documentation: docs/environment.adoc
# User-provided overrides
DEVBASE_CUSTOM_DIR="${DEVBASE_CUSTOM_DIR:-}" # Custom config directory path
DEVBASE_DEBUG="${DEVBASE_DEBUG:-}" # Debug mode (set DEVBASE_DEBUG=1 for verbose output)
DEVBASE_THEME="${DEVBASE_THEME:-}" # Theme choice (default applied later)
DEVBASE_FONT="${DEVBASE_FONT:-}" # Font choice (default applied later)
export EDITOR="${EDITOR:-}" # Default editor (default applied later)
export VISUAL="${VISUAL:-$EDITOR}" # Visual editor (defaults to EDITOR)
GIT_EMAIL="${GIT_EMAIL:-@$(hostname)}" # Git email for non-interactive (default: user@hostname)
GIT_NAME="${GIT_NAME:-DevBase User}" # Git name for non-interactive (default: DevBase User)
MISE_GITHUB_TOKEN="${MISE_GITHUB_TOKEN:-}" # GitHub token for mise downloads
SSH_KEY_PASSPHRASE="${SSH_KEY_PASSPHRASE:-}" # SSH key passphrase
# XDG directories (may be pre-set by user, defaults follow XDG Base Directory spec)
XDG_BIN_HOME="${XDG_BIN_HOME:-${HOME}/.local/bin}" # Not in spec, but follows pattern
XDG_CACHE_HOME="${XDG_CACHE_HOME:-${HOME}/.cache}"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME}/.config}"
XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}"
# Network configuration (may be set by customization (org.env, see docs))
DEVBASE_PROXY_HOST="${DEVBASE_PROXY_HOST:-}"
DEVBASE_PROXY_PORT="${DEVBASE_PROXY_PORT:-}"
DEVBASE_NO_PROXY_DOMAINS="${DEVBASE_NO_PROXY_DOMAINS:-}"
DEVBASE_REGISTRY_HOST="${DEVBASE_REGISTRY_HOST:-}"
DEVBASE_REGISTRY_PORT="${DEVBASE_REGISTRY_PORT:-}"
DEVBASE_EMAIL_DOMAIN="${DEVBASE_EMAIL_DOMAIN:-}"
DEVBASE_LOCALE="${DEVBASE_LOCALE:-}"
DEVBASE_PYPI_REGISTRY="${DEVBASE_PYPI_REGISTRY:-}"
DEVBASE_NPM_REGISTRY="${DEVBASE_NPM_REGISTRY:-}"
DEVBASE_CYPRESS_REGISTRY="${DEVBASE_CYPRESS_REGISTRY:-}"
DEVBASE_TESTCONTAINERS_PREFIX="${DEVBASE_TESTCONTAINERS_PREFIX:-}"
# Custom configuration paths (set by find_custom_directory() if custom dir exists)
# Initialized here to avoid unbound variable errors with set -u
_DEVBASE_CUSTOM_CERTS=""
_DEVBASE_CUSTOM_HOOKS=""
_DEVBASE_CUSTOM_TEMPLATES=""
_DEVBASE_CUSTOM_SSH=""
_DEVBASE_CUSTOM_PACKAGES=""
# Installation feature flags (user can override via environment)
DEVBASE_INSTALL_DEVTOOLS="${DEVBASE_INSTALL_DEVTOOLS:-}" # Install development tools (default applied later)
DEVBASE_INSTALL_LAZYVIM="${DEVBASE_INSTALL_LAZYVIM:-}" # Install LazyVim configuration (default applied later)
DEVBASE_INSTALL_JMC="${DEVBASE_INSTALL_JMC:-}" # Install Java Mission Control (default applied later)
DEVBASE_INSTALL_INTELLIJ="${DEVBASE_INSTALL_INTELLIJ:-}" # Install IntelliJ IDEA (default applied later)
DEVBASE_ENABLE_GIT_HOOKS="${DEVBASE_ENABLE_GIT_HOOKS:-}" # Enable Git hooks (default applied later)
DEVBASE_ZELLIJ_AUTOSTART="${DEVBASE_ZELLIJ_AUTOSTART:-}" # Auto-start Zellij terminal multiplexer (default applied later)
# VSCode configuration flags (defaults set during user preferences collection)
DEVBASE_VSCODE_INSTALL="${DEVBASE_VSCODE_INSTALL:-}" # Install VSCode (set based on WSL detection)
DEVBASE_VSCODE_EXTENSIONS="${DEVBASE_VSCODE_EXTENSIONS:-}" # Install VSCode extensions
# SSH key management configuration
DEVBASE_SSH_KEY_TYPE="${DEVBASE_SSH_KEY_TYPE:-ed25519}" # SSH key type: ed25519, ecdsa, ed25519-sk, ecdsa-sk
DEVBASE_SSH_KEY_NAME="${DEVBASE_SSH_KEY_NAME:-}" # SSH key filename (default applied later)
DEVBASE_SSH_KEY_ACTION="${DEVBASE_SSH_KEY_ACTION:-}" # SSH key action: new, keep, or import
GENERATED_SSH_PASSPHRASE="${GENERATED_SSH_PASSPHRASE:-false}" # Flag: passphrase was auto-generated
# Advanced configuration (offline installs, registry overrides)
DEVBASE_DEB_CACHE="${DEVBASE_DEB_CACHE:-}" # Path to offline .deb package cache
DEVBASE_NO_PROXY_JAVA="${DEVBASE_NO_PROXY_JAVA:-}" # Java-specific no-proxy domains
# Non-interactive mode flag (initialized here, may be set by --non-interactive arg)
NON_INTERACTIVE="${NON_INTERACTIVE:-false}"
# Dry-run mode flag (skips installation, shows planned steps only)
DEVBASE_DRY_RUN="${DEVBASE_DRY_RUN:-false}"
# Strict checksum mode: off, warn, or fail
DEVBASE_STRICT_CHECKSUMS="${DEVBASE_STRICT_CHECKSUMS:-warn}"
# Allowlist of URL patterns allowed without checksums
DEVBASE_STRICT_CHECKSUMS_ALLOWLIST="${DEVBASE_STRICT_CHECKSUMS_ALLOWLIST:-}"
# Mise network timeout controls
export MISE_HTTP_TIMEOUT="${MISE_HTTP_TIMEOUT:-150s}"
export MISE_FETCH_REMOTE_VERSIONS_TIMEOUT="${MISE_FETCH_REMOTE_VERSIONS_TIMEOUT:-75s}"
# Internal flag: show version and exit
SHOW_VERSION="${SHOW_VERSION:-false}"
# Internal temp directory path (initialized by init_temp_directory)
_DEVBASE_TEMP="${_DEVBASE_TEMP:-}"
# TUI mode flag (default: gum, can be overridden with --tui=whiptail)
# Values: "gum", "whiptail" (or "none" for non-interactive mode)
DEVBASE_TUI_MODE="${DEVBASE_TUI_MODE:-gum}"
# ============================================================================
# EXPORTED ENVIRONMENT VARIABLES (see docs/environment.adoc for details)
# ============================================================================
#
# Core Paths (readonly):
# DEVBASE_ROOT, DEVBASE_LIBS, DEVBASE_DOT, DEVBASE_FILES, DEVBASE_ENVS, DEVBASE_DOCS
#
# Custom Configuration (optional):
# DEVBASE_CUSTOM_DIR, DEVBASE_CUSTOM_ENV, _DEVBASE_CUSTOM_CERTS,
# _DEVBASE_CUSTOM_HOOKS, _DEVBASE_CUSTOM_TEMPLATES, _DEVBASE_CUSTOM_SSH
#
# Environment & Network:
# DEVBASE_ENV, DEVBASE_ENV_FILE, DEVBASE_PROXY_HOST, DEVBASE_PROXY_PORT,
# DEVBASE_NO_PROXY_DOMAINS, DEVBASE_REGISTRY_HOST, DEVBASE_REGISTRY_PORT, DEVBASE_EMAIL_DOMAIN
#
# Installation Settings:
# DEVBASE_THEME, DEVBASE_CACHE_DIR, DEVBASE_CONFIG_DIR, DEVBASE_BACKUP_DIR,
# XDG_* directories
#
# User Preferences (collected during install):
# DEVBASE_GIT_*, DEVBASE_SSH_*, EDITOR, VISUAL, DEVBASE_INSTALL_*,
# DEVBASE_ENABLE_GIT_HOOKS, DEVBASE_ZELLIJ_AUTOSTART
#
# For complete documentation with descriptions and examples:
# → docs/environment.adoc
# ============================================================================
# Global variables - set by various functions below
# DevBase paths - set by initialize_devbase_paths()
# Custom paths - set by find_custom_directory()
# Environment vars - set by load_environment_configuration()
# Theme - set by user during installation
# ============================================================================
# ============================================================================
parse_arguments() {
for arg in "$@"; do
case $arg in
--non-interactive)
export NON_INTERACTIVE=true
export DEBIAN_FRONTEND=noninteractive
;;
--tui=*)
local tui_value="${arg#--tui=}"
case "$tui_value" in
gum | whiptail)
DEVBASE_TUI_MODE="$tui_value"
;;
*)
printf "Error: Invalid TUI mode '%s'. Valid options: gum, whiptail\n" "$tui_value" >&2
exit 1
;;
esac
;;
--dry-run)
export DEVBASE_DRY_RUN=true
;;
--version | -v)
SHOW_VERSION=true
;;
--help | -h)
printf "Usage: %s [OPTIONS]\n" "$0"
printf "\n"
printf "Options:\n"
printf " --non-interactive Run in non-interactive mode (for CI/automation)\n"
printf " --dry-run Print planned steps without installing\n"
printf " --tui=<mode> Set TUI mode: gum (default), whiptail\n"
printf " --version, -v Show version information\n"
printf " --help, -h Show this help message\n"
exit 0
;;
*)
printf "Error: Unknown option '%s'\n" "$arg" >&2
printf "Run '%s --help' for valid options.\n" "$0" >&2
exit 1
;;
esac
done
}
# Brief: Initialize core DevBase path variables
# Params: None
# Modifies: DEVBASE_ROOT, DEVBASE_LIBS, DEVBASE_DOT, DEVBASE_FILES, DEVBASE_ENVS,
# DEVBASE_DOCS (all exported as readonly)
# Returns: 0 always
# Side-effects: None
initialize_devbase_paths() {
# Get absolute path to devbase root (canonical bash idiom)
export DEVBASE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export DEVBASE_LIBS="${DEVBASE_ROOT}/libs"
export DEVBASE_DOT="${DEVBASE_ROOT}/dot"
export DEVBASE_FILES="${DEVBASE_ROOT}/devbase_files"
export DEVBASE_ENVS="${DEVBASE_ROOT}/environments"
export DEVBASE_DOCS="${DEVBASE_ROOT}/docs"
readonly DEVBASE_ROOT DEVBASE_LIBS DEVBASE_DOT DEVBASE_FILES DEVBASE_ENVS DEVBASE_DOCS
# Determine if running from git clone or release tarball (needed early for display_os_info)
if git -C "${DEVBASE_ROOT}" rev-parse --git-dir >/dev/null 2>&1; then
export _DEVBASE_FROM_GIT="true"
else
export _DEVBASE_FROM_GIT="false"
fi
}
load_devbase_libraries() {
local defaults_file="${DEVBASE_ROOT}/config/defaults.env"
if [[ -f "$defaults_file" ]]; then
# shellcheck disable=SC1090 # File path resolved at runtime
source "$defaults_file"
else
echo "ERROR: Defaults file not found: $defaults_file" >&2
return 1
fi
source "${DEVBASE_LIBS}/constants.sh"
source "${DEVBASE_LIBS}/define-colors.sh"
source "${DEVBASE_LIBS}/utils.sh"
source "${DEVBASE_LIBS}/defaults.sh"
source "${DEVBASE_LIBS}/ui/ui-messages.sh"
source "${DEVBASE_LIBS}/ui/ui-helpers.sh"
source "${DEVBASE_LIBS}/validation.sh"
source "${DEVBASE_LIBS}/distro.sh"
source "${DEVBASE_LIBS}/handle-network.sh"
source "${DEVBASE_LIBS}/process-templates.sh"
source "${DEVBASE_LIBS}/check-requirements.sh"
source "${DEVBASE_LIBS}/bootstrap/bootstrap-ui.sh"
source "${DEVBASE_LIBS}/bootstrap/bootstrap-context.sh"
source "${DEVBASE_LIBS}/bootstrap/bootstrap-config.sh"
source "${DEVBASE_LIBS}/bootstrap/bootstrap-apply.sh"
source "${DEVBASE_LIBS}/bootstrap/bootstrap.sh"
source "${DEVBASE_LIBS}/migrations.sh"
source "${DEVBASE_LIBS}/persist.sh"
source "${DEVBASE_LIBS}/install-certificates.sh"
source "${DEVBASE_LIBS}/configure-ssh-git.sh"
source "${DEVBASE_LIBS}/configure-git-hooks.sh"
source "${DEVBASE_LIBS}/configure-completions.sh"
source "${DEVBASE_LIBS}/configure-shell.sh"
source "${DEVBASE_LIBS}/configure-services.sh"
source "${DEVBASE_LIBS}/install-custom.sh"
source "${DEVBASE_LIBS}/setup-vscode.sh"
source "${DEVBASE_LIBS}/configure-theme.sh"
}
resolve_devbase_version() {
local git_tag=""
local git_sha="unknown"
if command -v git &>/dev/null && git -C "${DEVBASE_ROOT}" rev-parse --git-dir >/dev/null 2>&1; then
git_tag=$(git -C "${DEVBASE_ROOT}" describe --tags --abbrev=0 2>/dev/null || echo "")
git_sha=$(git -C "${DEVBASE_ROOT}" rev-parse --short HEAD 2>/dev/null || echo "unknown")
fi
local devbase_version="0.0.0-dev"
if [[ -n "$git_tag" ]]; then
devbase_version="$git_tag"
fi
printf "%s %s\n" "$devbase_version" "$git_sha"
}
print_version() {
local devbase_version
local git_sha
read -r devbase_version git_sha <<<"$(resolve_devbase_version)"
printf "devbase-core %s (%s)\n" "$devbase_version" "$git_sha"
}
run_installation() {
# Run the main installation script
local _INSTALL_SCRIPT="${DEVBASE_LIBS}/install.sh"
if [[ -f "$_INSTALL_SCRIPT" ]]; then
# Only export what child processes actually need
# Proxy variables already exported above if configured
# shellcheck disable=SC2153 # _DEVBASE_ENV/_DEVBASE_ENV_FILE set during bootstrap
export DEVBASE_ENV="$_DEVBASE_ENV" # Environment type (ubuntu/wsl-ubuntu)
# shellcheck disable=SC2153 # _DEVBASE_ENV_FILE set during bootstrap
export DEVBASE_ENV_FILE="$_DEVBASE_ENV_FILE" # Path to env file
# Source the main installation script (not bash) to keep arrays available
# shellcheck disable=SC1090 # Dynamic source path
source "$_INSTALL_SCRIPT"
else
die "Main installation script not found: ${_INSTALL_SCRIPT}"
fi
}
init_temp_directory() {
if [[ -n "${_DEVBASE_TEMP:-}" ]]; then
if [[ -d "${_DEVBASE_TEMP}" ]] && [[ -w "${_DEVBASE_TEMP}" ]]; then
export _DEVBASE_TEMP
readonly _DEVBASE_TEMP
return 0
fi
printf "ERROR: _DEVBASE_TEMP is set but not a writable directory: %s\n" "${_DEVBASE_TEMP}" >&2
return 1
fi
_DEVBASE_TEMP=$(mktemp -d --tmpdir devbase.XXXXXX) || {
printf "ERROR: Failed to create temp directory\n" >&2
return 1
}
export _DEVBASE_TEMP
readonly _DEVBASE_TEMP
return 0
}
main() {
parse_arguments "$@"
initialize_devbase_paths
load_devbase_libraries || return 1
apply_setup_defaults
if [[ "${SHOW_VERSION}" == "true" ]]; then
print_version
return 0
fi
init_temp_directory || return 1
run_bootstrap || return 1
show_global_warnings
if [[ "${DEVBASE_DRY_RUN}" == "true" ]]; then
show_dry_run_plan
show_progress info "$(ui_message dry_run_install_skip)"
return 0
fi
run_installation || return 1
# Run migrations after successful installation to clean up legacy files
if ! run_migrations; then
show_progress warning "Migrations failed - continuing"
fi
}
main "$@"