-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbashdot
More file actions
466 lines (401 loc) · 14.6 KB
/
Copy pathbashdot
File metadata and controls
466 lines (401 loc) · 14.6 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/bin/bash
VERSION=4.1.8
current_working_dir=$(pwd)
bashdot_config_file=$HOME/.bashdot
LOGGER_FMT=${LOGGER_FMT:="%Y-%m-%d"}
LOGGER_LVL=${LOGGER_LVL:="info"}
if [ -n "$BASHDOT_LOG_LEVEL" ]; then
LOGGER_LVL=$BASHDOT_LOG_LEVEL
fi
action=$1
usage() {
case "$1" in
commands)
echo "Usage: bashdot [dir|install|links|profiles|uninstall|version] OPTIONS"
;;
install)
echo "Usage: bashdot install PROFILE1 PROFILE2 ... PROFILEN"
;;
uninstall)
echo "Usage: bashdot uninstall PROFILE_DIRECTORY PROFILE"
;;
esac
}
if [ "$action" != "dir" ] && [ "$action" != "install" ] &&
[ "$action" != "links" ] && [ "$action" != "profiles" ] &&
[ "$action" != "uninstall" ] && [ "$action" != "version" ]; then
usage commands
exit 1
fi
exit_if_profile_directories_contain_invalid_characters() {
profile_dir=$1
if ls "$profile_dir" | grep -E '[[:space:]:,/\]'; then
log error "Files in '$profile_dir' contain invalid characters."
exit 1
fi
}
exit_if_invalid_directory_name() {
dir=$1
if ! echo "$dir" | grep "^[/.a-zA-Z0-9_-]*$" > /dev/null; then
log error "Current working directory '$dir' has an invalid character. The directory you are in when you install a profile must have alpha numeric characters, with only dashes, dots or underscores."
exit 1
fi
}
exit_if_invalid_profile_name() {
profile=$1
if ! echo "$profile" | grep "^[a-zA-Z0-9_-]*$" > /dev/null; then
log error "Invalid profile name '$profile'. Profiles must be alpha numeric with only dashes or underscores."
exit 1
fi
}
log() {
action=$1 && shift
case "$action" in
debug) [[ "$LOGGER_LVL" =~ debug ]] && echo "$( date "+${LOGGER_FMT}" ) - DEBUG - $@" 1>&2 ;;
info) [[ "$LOGGER_LVL" =~ debug|info ]] && echo "$( date "+${LOGGER_FMT}" ) - INFO - $@" 1>&2 ;;
warn) [[ "$LOGGER_LVL" =~ debug|info|warn ]] && echo "$( date "+${LOGGER_FMT}" ) - WARN - $@" 1>&2 ;;
error) [[ ! "$LOGGER_LVL" =~ none ]] && echo "$( date "+${LOGGER_FMT}" ) - ERROR - $@" 1>&2 ;;
esac
true
}
# Home-directory destination for a profile file. Templates install as
# ~/.<name> (suffix stripped), not ~/.<name>.template.
target_dotfile() {
file=$1
case "$file" in
*.template)
echo "$HOME/.${file%.template}"
;;
*)
echo "$HOME/.$file"
;;
esac
}
profile_source_file() {
profile_dir=$1
file=$2
case "$file" in
*.template)
echo "$profile_dir/${file%.template}.rendered"
;;
*)
echo "$profile_dir/$file"
;;
esac
}
# Substitute only $VAR and ${VAR} from the environment. Values are copied
# literally — command substitution, backticks, and arithmetic are not executed.
# Unset variables are a fatal error, matching the previous set -u check.
render_template() {
source_file=$1
dest_file=$2
content=$(cat "$source_file")
len=${#content}
result=""
i=0
while [ "$i" -lt "$len" ]; do
c="${content:$i:1}"
if [ "$c" != '$' ]; then
result="${result}${c}"
i=$((i + 1))
continue
fi
if [ $((i + 1)) -ge "$len" ]; then
result="${result}$"
i=$((i + 1))
continue
fi
next="${content:$((i + 1)):1}"
if [ "$next" = '{' ]; then
start=$((i + 2))
if [ "$start" -ge "$len" ]; then
result="${result}\${"
i=$((i + 2))
continue
fi
first="${content:$start:1}"
case "$first" in
[a-zA-Z_])
pos=$start
while [ "$pos" -lt "$len" ]; do
ch="${content:$pos:1}"
case "$ch" in
[a-zA-Z0-9_]) pos=$((pos + 1)) ;;
*) break ;;
esac
done
namelen=$((pos - start))
varname="${content:$start:$namelen}"
if [ "$pos" -lt "$len" ] && [ "${content:$pos:1}" = '}' ]; then
if [ -z "${!varname+x}" ]; then
log error "Variable '$varname' is unset in template '$source_file'."
exit 1
fi
result="${result}${!varname}"
i=$((pos + 1))
else
result="${result}$"
i=$((i + 1))
fi
;;
*)
result="${result}$"
i=$((i + 1))
;;
esac
else
case "$next" in
[a-zA-Z_])
start=$((i + 1))
pos=$start
while [ "$pos" -lt "$len" ]; do
ch="${content:$pos:1}"
case "$ch" in
[a-zA-Z0-9_]) pos=$((pos + 1)) ;;
*) break ;;
esac
done
namelen=$((pos - start))
varname="${content:$start:$namelen}"
if [ -z "${!varname+x}" ]; then
log error "Variable '$varname' is unset in template '$source_file'."
exit 1
fi
result="${result}${!varname}"
i=$pos
;;
*)
result="${result}$"
i=$((i + 1))
;;
esac
fi
done
if [ -n "$dest_file" ]; then
printf '%s\n' "$result" > "$dest_file"
fi
}
link_dotfile() {
dotfile=$1
source_file=$2
existing=$(readlink "$dotfile")
log debug "Evaluating if '$dotfile' which links to '$existing' matches desired target '$source_file'."
if [ "$existing" == "$source_file" ]; then
log info "File '$dotfile' already links to '$source_file', continuing."
return
fi
if [ -e "$dotfile" ] || [ -L "$dotfile" ]; then
log error "File '$dotfile' already exists, exiting."
exit 1
fi
log debug "'$dotfile' does not link to desired target '$source_file'."
log info "Linking '$source_file' to '$dotfile'."
if ! ln -s "$source_file" "$dotfile"; then
log error "Failed to link '$source_file' to '$dotfile'."
exit 1
fi
}
install() {
profile=$1
profile_dir="$current_working_dir/$profile"
exit_if_profile_directories_contain_invalid_characters "$profile_dir"
# Pipe separated regex (parsed by egrep, case insensitive)
# that will not be symlinked by bashdot
ignored_files='^changelog|^contributing|^dockerfile|^icon|^license|^makefile|^readme'
if [ ! -d "$profile_dir" ]; then
log error "Profile '$profile' directory does not exist."
exit 1
fi
log info "Adding dotfiles profile '$profile'."
log debug "Checking for exiting conflicting dotfiles."
cd "$profile_dir" || exit
for file in $(ls |grep -E -iv "$ignored_files"); do
dotfile=$(target_dotfile "$file")
source_file=$(profile_source_file "$profile_dir" "$file")
if [ -e "$dotfile" ] || [ -L "$dotfile" ]; then
existing=$(readlink "$dotfile")
# Skip files which already link to the same location
if [ "$existing" == "$source_file" ]; then
continue
fi
log error "File '$dotfile' already exists, exiting."
exit 1
fi
done
log debug "Found no conflicting dotfiles in home, proceeding to link dotfile."
if [ -f "$bashdot_config_file" ]; then
if ! grep -E "^$current_working_dir$" "$bashdot_config_file" > /dev/null; then
log info "Appending '$current_working_dir' to bashdot config file '$bashdot_config_file'"
echo "$current_working_dir" >> "$bashdot_config_file"
fi
else
log info "Creating bashdot config file '$bashdot_config_file' with '$current_working_dir'."
echo "$current_working_dir" > "$bashdot_config_file"
fi
# globskipdots (bash 5.2+) makes .* no longer match . or ..; if the
# profile has no hidden files, `ls -ad .*` then errors. Walk hidden
# names explicitly and skip the . and .. entries.
for skipped_file in .[!.]* ..?*; do
[ -e "$skipped_file" ] || [ -L "$skipped_file" ] || continue
log warn "Skipping file with dot prepended '$skipped_file'. Remove dot if file should be linked."
done
# Validate templates before writing any rendered files so a missing
# variable cannot leave a partial install behind.
for template in $(ls |grep -E '.*\.template$'); do
source_file="$profile_dir/$template"
log info "Ensuring all variables in template '$source_file' are set."
render_template "$source_file" ""
done
log info "All variables used in templates are set."
log info "Installing dotfiles from '$profile_dir'."
for file in $(ls |grep -E -iv "$ignored_files"); do
source_file="$profile_dir/$file"
if [[ "$source_file" == *.template ]]; then
rendered_file_path=$(profile_source_file "$profile_dir" "$file")
dotfile=$(target_dotfile "$file")
log info "'$source_file' is a template, rendering to '$rendered_file_path'."
render_template "$source_file" "$rendered_file_path"
link_dotfile "$dotfile" "$rendered_file_path"
else
dotfile=$(target_dotfile "$file")
link_dotfile "$dotfile" "$source_file"
fi
done
log info "Completed adding dotfiles profile '$profile'."
}
list_links() {
for file in $(ls -a ~); do
# Only evaluate symlinks
if [[ -h ~/"$file" ]]; then
# Only include if it points to the dotfiles directory
while IFS= read -r bashdot_dir; do
expected_target_file_name=$(basename "$file" | cut -c 2-)
if readlink ~/"$file" |grep -E "^$bashdot_dir/[a-zA-Z0-9_-]*/$expected_target_file_name(\.rendered)?$" > /dev/null; then
echo "$file"
fi
done < "$bashdot_config_file"
fi
done
}
list_profiles() {
if [ ! -f "$bashdot_config_file" ];then
log info "No dotfiles installed by bashdot."
else
while IFS= read -r dir; do
for link in $(list_links); do
expected_target_file_name=$(basename "$link" | cut -c 2-)
if readlink ~/"$link" | grep -E "^$dir/[.a-zA-Z0-9_-]*/$expected_target_file_name(\.rendered)?$" > /dev/null; then
profile=$(readlink ~/"$link" |sed -e "s/^.*\/[.a-zA-Z0-9_-]*\/\(.*\)\/.*$/\1/")
echo "$dir $profile"
fi
done
done < "$bashdot_config_file" |sort |uniq
fi
}
show_links() {
for link in $(list_links); do
dest=$(readlink ~/"$link")
chomped_link="${link%\\n}"
echo "~/$chomped_link -> $dest"
done
}
dir() {
if [ ! -f "$bashdot_config_file" ]; then
log info "No dotfiles installed by bashdot."
else
sort "$bashdot_config_file"
fi
}
uninstall() {
dir=$1
profile=$2
if [ ! -f "$bashdot_config_file" ]; then
log error "Config file '$bashdot_config_file' not found."
log error "No dotfiles installed by bashdot."
exit 1
fi
# Don't proceed with uninstall if profiles not available in given directory
if ! list_profiles |grep "^$dir $profile$" > /dev/null; then
log error "Profile '$profile' not installed from '$dir'."
exit 1
fi
# Loop through each file and only remove if they are a symlink
# and point to a file in this profile in the target dir
for link in $(list_links);do
log debug "Evaluating '$link' for removal."
target=$(readlink ~/"$link")
# Check if link target is part of this bashdot profile
expected_target_file_name=$(basename "$link" | cut -c 2-)
if echo "$target" |grep -E "^$dir/$profile/${expected_target_file_name}(\.rendered)?$" > /dev/null; then
# If a link target was rendered from a template, remove
# the rendered file on uninstall
if echo "$target" |grep -E '\.rendered$' > /dev/null; then
log info "Removing rendered file '$target'."
\rm "$target"
fi
log info "Removing '$link'."
\rm ~/"$link"
fi
done
log debug "All links for profile '$profile' removed."
# If no more profiles point to this directory, remove it
log debug "Updating bashdot config file '$bashdot_config_file'."
dir_empty=true
for link in $(list_links); do
log debug "Evaluating if '$link' is part of a bashdot profile in dir '$dir'."
expected_target_file_name=$(basename "$link" | cut -c 2-)
if readlink ~/"$link" |grep -E "^$dir/[a-zA-Z0-9_-]*/${expected_target_file_name}(\.rendered)?$" > /dev/null; then
log debug "'$link' is part of a bashdot profile in '$dir', not removing '$dir' from '$bashdot_config_file'."
dir_empty=false
break
fi
done
if [ "$dir_empty" = true ]; then
log info "Removing '$dir' from '$bashdot_config_file'."
mv "$bashdot_config_file" "${bashdot_config_file}".backup
grep -v "^$dir$" < "${bashdot_config_file}".backup > "$bashdot_config_file"
fi
# If there are no more bashdot profiles, remove .bashdot and backup
if [ ! -s "$bashdot_config_file" ]; then
log info "No more bashdot profiles installed, removing '$bashdot_config_file'."
\rm -f "$bashdot_config_file" "${bashdot_config_file}".backup
fi
}
case "$action" in
dir)
dir
;;
install)
if [ $# -lt 2 ]; then
usage install
exit 1
fi
exit_if_invalid_directory_name "$current_working_dir"
while true; do
shift
if [ -z "$1" ];then
break
fi
exit_if_invalid_profile_name "$1"
install "$1"
done
log info "Completed installation of all profiles successfully."
;;
links)
show_links
;;
profiles)
list_profiles
;;
uninstall)
if [ $# -ne 3 ]; then
usage uninstall
exit 1
fi
uninstall "$2" "$3"
log info "Completed uninstallation successfully."
;;
version)
echo "$VERSION"
;;
esac