-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·435 lines (354 loc) · 10.9 KB
/
deploy.sh
File metadata and controls
executable file
·435 lines (354 loc) · 10.9 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/bin/bash
#
# deploy.sh
#
# Smart dotfiles deployment with automatic path inference
#
# Author: Michael Gilchrist (michaelgilch@gmail.com)
set -e
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOME_DIR="$DOTFILES_DIR/home"
BIN_DIR="$DOTFILES_DIR/bin"
PACKAGES_MAP="$DOTFILES_DIR/packages.map"
BACKUP_DIR="$HOME/.dotfiles_backup_$(date +%Y%m%d_%H%M%S)"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Deployment mode
FORCE_MODE=false
NEW_ONLY_MODE=false
SKIP_PACKAGE_CHECK=false
DRY_RUN=false
TARGET=""
log_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
log_success() {
echo -e "${GREEN}✓${NC} $1"
}
log_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
log_error() {
echo -e "${RED}✗${NC} $1"
}
# Create backup directory if it doesn't exist
create_backup_dir() {
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
log_info "Created backup directory: $BACKUP_DIR"
fi
}
# Backup a file/directory/symlink
backup_path() {
local path="$1"
# Nothing to backup if it doesn't exist
[ ! -e "$path" ] && [ ! -L "$path" ] && return 0
# In dry-run mode, just show what would be backed up
if [ "$DRY_RUN" = true ]; then
log_info "[DRY_RUN] Would backup: $path"
return 0
fi
create_backup_dir
# Create relative path for backup
local rel_path="${path#$HOME/}"
local backup_path="$BACKUP_DIR/$rel_path"
# Create parent directory in backup
mkdir -p "$(dirname "$backup_path")"
# Copy (preserving symlinks and attributes)
if cp --no-dereference --preserve=all --recursive "$path" "$backup_path" 2>/dev/null; then
log_info "Backed up: $path"
return 0
else
log_warning "Failed to backup: $path"
return 1
fi
}
# Remove existing path (after backup if in force mode)
remove_existing() {
local path="$1"
# Nothing to remove if it doesn't exist
[ ! -e "$path" ] && [ ! -L "$path" ] && return 0
if [ "$FORCE_MODE" = true ]; then
backup_path "$path"
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would remove: $path"
else
rm -rf "$path"
log_info "Removed: $path"
fi
return 0
else
# Not in force mode, destination exists = skip
return 1
fi
}
# Check if package is installed
is_package_installed() {
local package="$1"
# Check with pacman (Arch)
if command -v pacman &> /dev/null; then
pacman -Q "$package" &> /dev/null && return 0
fi
# Check with dpkg (Debian/Ubuntu)
if command -v dpkg &> /dev/null; then
dpkg -l "$package" 2> /dev/null | grep -q "^ii" && return 0
fi
# Check with rpm (RedHat/Fedora)
if command -v rpm &> /dev/null; then
rpm -q "$package" &> /dev/null && return 0
fi
# Check if command exists (fallback)
command -v "$package" &> /dev/null && return 0
return 1
}
# Get required packages for a config
get_required_packages() {
local config_name="$1"
# If packages.map doesn't exist, assume no requirements
[ ! -f "$PACKAGES_MAP" ] && return 0
# Look for config in packages.map
local line=$(grep "^${config_name}:" "$PACKAGES_MAP" 2>/dev/null)
# If not found in map, assume no requirements
[ -z "$line" ] && return 0
# Extract packages (everything after the colon)
local packages="${line#*:}"
# If empty (config: with nothing after), no requirements
[ -z "$packages" ] && return 0
echo "$packages"
}
# Check if all required packages are installed
should_deploy_config() {
local config_name="$1"
# If skipping package checks, always deploy
[ "$SKIP_PACKAGE_CHECK" = true ] && return 0
local required_packages=$(get_required_packages "$config_name")
# No requirements, always deploy
[ -z "$required_packages" ] && return 0
# Check each package (comma-separated)
IFS=',' read -ra PACKAGES <<< "$required_packages"
for package in "${PACKAGES[@]}"; do
# Trim whitespace
package=$(echo "$package" | xargs)
if ! is_package_installed "$package"; then
return 1
fi
done
return 0
}
deploy_all() {
log_info "Starting deployment for host: $HOSTNAME"
if [ "$DRY_RUN" = true ]; then
log_info "DRY RUN MODE - No changes will be made"
fi
if [ "$FORCE_MODE" = true ]; then
log_warning "FORCE MODE - Existing files will be backed up and replaced"
fi
if [ -n "$TARGET" ]; then
log_info "Targeting: $TARGET"
fi
if [ "$SKIP_PACKAGE_CHECK" = false ] && [ -f "$PACKAGES_MAP" ]; then
log_info "Package checking enabled (use --skip-package-check to disable)"
else
log_warning "Package checking disabled"
fi
echo ""
find "$HOME_DIR" -mindepth 1 -maxdepth 1 | while read -r src; do
filename="$(basename "$src")"
if [ "$filename" = "config" ]; then
# Special handling for config/ - symlink each subdirectory
find "$src" -mindepth 1 -maxdepth 1 -type d | while read -r config_dir; do
app_name="$(basename "$config_dir")"
dest="$HOME/.config/$app_name"
# Skip if not the target
if [ -n "$TARGET" ] && [ "$app_name" != "$TARGET" ]; then
continue
fi
# Check if destination exists
if [ -e "$dest" ] || [ -L "$dest" ]; then
# Try to remove (will backup if force mode)
if ! remove_existing "$dest"; then
echo "Skipped (exists): $dest"
continue
fi
fi
# Check if required packages are installed
if ! should_deploy_config "$app_name"; then
required=$(get_required_packages "$app_name")
log_warning "Skipped (missing packages): $app_name (needs: $required)"
continue
fi
# DRY RUN: Don't actually create anything
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would link: $dest -> $config_dir"
continue
fi
# Create .config if it doesn't exist
mkdir -p "$HOME/.config"
# Create symlink
ln -sf "$config_dir" "$dest"
log_success "Linked: $dest"
done
else
# Regular files/directories in home/ -> ~/.filename
dest="$HOME/.$filename"
# Skip if not the target
if [ -n "$TARGET" ] && [ "$filename" != "$TARGET" ]; then
continue
fi
# Check if destination exists
if [ -e "$dest" ] || [ -L "$dest" ]; then
# Try to remove (will backup if force mode)
if ! remove_existing "$dest"; then
echo "Skipped (exists): $dest"
continue
fi
fi
# Check if required packages are installed
if ! should_deploy_config "$filename"; then
required=$(get_required_packages "$filename")
log_warning "Skipped (missing packages): $filename (needs: $required)"
continue
fi
# DRY RUN: Don't actually create anything
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would link: $dest -> $src"
continue
fi
# Only create parent directory if src is a FILE
if [ -f "$src" ]; then
mkdir -p "$(dirname "$dest")"
fi
# Create symlink
ln -sf "$src" "$dest"
log_success "Linked: $dest"
fi
done
# Deploy bin/ scripts (individual symlinks to ~/.local/bin/)
if [ -d "$BIN_DIR" ]; then
find "$BIN_DIR" -mindepth 1 -maxdepth 1 -type f | while read -r script; do
script_name="$(basename "$script")"
dest="$HOME/.local/bin/$script_name"
# Skip if not the target
if [ -n "$TARGET" ] && [ "$script_name" != "$TARGET" ]; then
continue
fi
# Check if destination exists
if [ -e "$dest" ] || [ -L "$dest" ]; then
if ! remove_existing "$dest"; then
echo "Skipped (exists): $dest"
continue
fi
fi
# Check if required packages are installed
if ! should_deploy_config "$script_name"; then
required=$(get_required_packages "$script_name")
log_warning "Skipped (missing packages): $script_name (needs: $required)"
continue
fi
# DRY RUN: Don't actually create anything
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would link: $dest -> $script"
continue
fi
# Create ~/.local/bin if it doesn't exist
mkdir -p "$HOME/.local/bin"
# Create symlink
ln -sf "$script" "$dest"
log_success "Linked: $dest"
done
fi
echo ""
if [ "$DRY_RUN" = true ]; then
log_info "Dry run complete - no changes were made"
elif [ "$FORCE_MODE" = true ] && [ -d "$BACKUP_DIR" ]; then
log_success "Deployment complete!"
log_info "Backups saved to: $BACKUP_DIR"
else
log_success "Deployment complete!"
fi
}
# Usage information
usage() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Deploy dotfiles with automatic path inference and hostname-specific overrides.
OPTIONS:
-h, --help Show this help message
-f, --force Backup and replace ALL existing files/symlinks
-n, --new-only Only deploy files that don't already exist
-d, --dry-run Show deployment plan without making changes
-t, --target NAME Deploy only the specified config/app/script
-s, --skip-package-check Skip package dependency checking
DIRECTORY STRUCTURE:
home/ -> ~/.*
home/config/ -> ~/.config/*
home/vim/ -> ~/.vim/*
bin/ -> ~/bin/* (helper scripts used by configs)
FORCE MODE:
With --force, existing files/directories/symlinks are backed up to
~/.dotfiles_backup_YYYYMMDD_HHMMSS/ and **then** replaced.
This handles:
- Regular files that conflict with dotfiles
- Old symlinks from previous deployments
- Orphaned configs from uninstalled packages
PACKAGE CHECKING:
By default, configs are only deployed if required packages are installed.
Edit packages.map to define package requirements for each config.
SPECIAL FILES:
filename@hostname Override for specific host
HOSTNAME-SPECIFIC:
Current hostname: $HOSTNAME
Override files ending with @$HOSTNAME will be used instead of base files.
EOF
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
-f|--force)
FORCE_MODE=true
shift
;;
-n|--new-only)
log_warning "$1 not yet implemented."
NEW_ONLY_MODE=true
shift
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-s|--skip-package-check)
SKIP_PACKAGE_CHECK=true
shift
;;
-t|--target)
if [ -z "${2:-}" ]; then
log_error "--target requires a config/app name"
exit 1
fi
TARGET="$2"
shift 2
;;
*)
log_error "Unknown option: $1"
echo ""
usage
exit 1
;;
esac
done
# Check if both force and new-only are set
if [ "$FORCE_MODE" = true ] && [ "$NEW_ONLY_MODE" = true ]; then
log_error "Cannot use --force and --new-only together"
exit 1
fi
# Run deployment
deploy_all