-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.sh
executable file
·597 lines (490 loc) · 16.1 KB
/
bootstrap.sh
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/usr/bin/env bash
# bootstrap installs things.
__ScriptVersion="20190803"
# import ~/.localrc to set environments correctly
[[ -a "$HOME/.localrc" ]] && source "$HOME/.localrc" top
if type perl &> /dev/null; then
DOTFILES_ROOT="$(dirname "$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$0")")"
elif [[ "$OSTYPE" == "linux-gnu" ]]; then
DOTFILES_ROOT="$(dirname "$(readlink -f "$0")")"
else
DOTFILES_ROOT="$(cd "$(dirname "$0")" && pwd -P)"
fi
export DOTFILES_ROOT
export DOTFILES_LOCAL="${XDG_CACHE_HOME:-$HOME/.cache}/dotfiles"
export DOTFILES_ENV=${DOTFILES_ENV:-inet}
source "${DOTFILES_ROOT}/logging.sh"
relpath() {
if type python3 &> /dev/null
then
python3 -c "import os.path; print(os.path.relpath('$1','${2:-$PWD}'))"
elif type python &> /dev/null
then
python -c "import os.path; print(os.path.relpath('$1','${2:-$PWD}'))"
elif type realpath &> /dev/null
then
realpath --relative-to="${2:-$PWD}" "$1"
fi
}
link_file () {
local src=$1 dst=$2
local overwrite=
local backup=
local skip=
local action=
if [ -f "$dst" ] || [ -d "$dst" ] || [ -L "$dst" ]
then
if [ "$overwrite_all" == "false" ] && [ "$backup_all" == "false" ] && [ "$skip_all" == "false" ]
then
local currentSrc
currentSrc="$(readlink "$dst")"
if [ "$currentSrc" == "$src" ]
then
skip=true;
else
user "File already exists: $dst ($(basename "$src")), what do you want to do?\n\
[s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all?"
read -r -n 1 action
case "$action" in
o )
overwrite=true;;
O )
overwrite_all=true;;
b )
backup=true;;
B )
backup_all=true;;
s )
skip=true;;
S )
skip_all=true;;
* )
;;
esac
fi
fi
overwrite=${overwrite:-$overwrite_all}
backup=${backup:-$backup_all}
skip=${skip:-$skip_all}
if [ "$overwrite" == "true" ]
then
if [ "${DRY_RUN}" = 'yes' ]
then
echo " => rm -rf \"$dst\""
else
rm -rf "$dst"
fi
success " removed $dst"
fi
if [ "$backup" == "true" ]
then
if [ "${DRY_RUN}" = 'yes' ]
then
echo " => mv \"$dst\" \"${dst}.backup\""
else
mv "$dst" "${dst}.backup"
fi
success "moved $dst to ${dst}.backup"
fi
if [ "$skip" == "true" ]
then
debug " ${WARN_FORMAT}skipped${RESET_FORMAT} ${INFO_FORMAT}$src${RESET_FORMAT}"
fi
fi
if [ "$skip" != "true" ] # "false" or empty
then
parent=$(dirname "$2")
if [ ! -d "$parent" ]
then
if [ "${DRY_RUN}" = 'yes' ]
then
echo " => mkdir -p \"$parent\""
else
mkdir -p "$parent"
fi
fi
if [ "${DRY_RUN}" = 'yes' ]
then
echo " => ln -s \"$1\" \"$2\""
else
ln -s "$1" "$2"
fi
success " linked $1 to $2"
fi
unset src dst overwrite backup skip action
}
get_symlinks() {
LINK_BASE="$1"
shift 1
for dir in "$@"
do
find -H "${LINK_BASE}/$dir" -name '*.symlink' -o -name "*.symlink-${DOTFILES_ENV}" -not -path '*.git' |
while read -r src
do
rel=$(relpath "$src" "$LINK_BASE")
echo "$rel"
done
done
unset dir src
}
create_symlinks () {
local dir
LINK_BASE="$1"
LINK_FILE="$2"
shift 2
info " ${LINK_BASE} => ${LINK_FILE} ..."
local -A links
for link in $(get_symlinks "$LINK_BASE" "$@")
do
links["$link"]="$link"
done
if [ -e "$DOTFILES_LOCAL/$LINK_FILE" ]
then
while read -r link
do
if [ -z "${links[$link]+isset}" ]
then
dst="$TARGET/${link#*/}"
dst="${dst%.symlink*}"
if [ -h "$dst" ]
then
if [ "$LINK_BASE/$link" == "$(readlink "$dst")" ]
then
if [ "${DRY_RUN}" = 'yes' ]
then
echo " => rm \"$dst\""
else
rm "$dst"
fi
success " remove outdated \"$dst\""
else
skip " $dst is not point to original position, don't need to be remove"
fi
else
skip " $dst is not exists, or not a symbol link, don't need to be remove"
fi
fi
done < "$DOTFILES_LOCAL/$LINK_FILE"
fi
mkdir -p "$DOTFILES_LOCAL"
cat /dev/null > "$DOTFILES_LOCAL/$LINK_FILE"
for link in "${links[@]}"
do
echo "$link" >> "$DOTFILES_LOCAL/$LINK_FILE"
dst="$TARGET/${link#*/}"
dst="${dst%.symlink*}"
link_file "$LINK_BASE/$link" "$dst"
done
}
generate_source_list () {
local f dir shell orig_nullglob
local path_env fpath_env
local -a path_sh env_sh completion_sh others_sh top_sh_sh
shell="$1"
info " Generating script files for ${HIGHLIGHT1_FORMAT}${shell}${RESET_FORMAT}..."
stage0_file="$DOTFILES_LOCAL/stage0.${shell}"
stage1_file="$DOTFILES_LOCAL/stage1.${shell}"
stage2_file="$DOTFILES_LOCAL/stage2.${shell}"
orig_nullglob=
shopt nullglob > /dev/null && orig_nullglob=1
shopt -s nullglob
# shellcheck disable=SC2016
path_env='export PATH=$PATH'
# shellcheck disable=SC2016
fpath_env='export fpath=($fpath'
while [ $# -gt 1 ]
do
shift
dir=$1
debug " Handling subdir ${SUCCESS_FORMAT}${dir}${RESET_FORMAT}..."
if [ -d "${DOTFILES_ROOT}/${dir}/bin" ]; then
path_env="${path_env}:${DOTFILES_ROOT}/${dir}/bin"
debug " Add ${INFO_FORMAT}${dir}/bin${RESET_FORMAT} to ${INFO_FORMAT}\$PATH${RESET_FORMAT}..."
fi
if [ -d "${DOTFILES_ROOT}/${dir}/zsh-completion" ]; then
fpath_env="${fpath_env} ${DOTFILES_ROOT}/${dir}/zsh-completion"
debug " Add ${INFO_FORMAT}${dir}/zsh-completion${RESET_FORMAT} to ${INFO_FORMAT}\$fpath${RESET_FORMAT}..."
fi
for f in "${DOTFILES_ROOT}/${dir}"/*.{sh,"${shell}"} "${DOTFILES_LOCAL}/${dir}"/*.{sh,"${shell}"}
do
if [ ! -f "$f" ]; then
continue
fi
if [[ "${f##*/}" =~ ^path\.[^.]*$ ]]; then
path_sh[${#path_sh[*]}]="$f"
debug " Add ${INFO_FORMAT}$f${RESET_FORMAT} to ${INFO_FORMAT}path.sh${RESET_FORMAT}..."
elif [[ "${f##*/}" =~ ^env\.[^.]*$ ]]; then
env_sh[${#env_sh[*]}]="$f"
debug " Add ${INFO_FORMAT}$f${RESET_FORMAT} to ${INFO_FORMAT}env.sh${RESET_FORMAT}..."
elif [[ "${f##*/}" =~ ^completion\.[^.]*$ ]]; then
completion_sh[${#completion_sh[*]}]="$f"
debug " Add ${INFO_FORMAT}$f${RESET_FORMAT} to ${INFO_FORMAT}completion.sh${RESET_FORMAT}..."
elif [[ "${f##*/}" =~ ^top_sh\.[^.]*$ ]]; then
top_sh_sh[${#top_sh_sh[*]}]="$f"
debug " Add ${INFO_FORMAT}$f${RESET_FORMAT} to ${INFO_FORMAT}top_sh.sh${RESET_FORMAT}..."
elif [[ "${f##*/}" =~ ^requirements\.[^.]*$ ]]; then
# debug " Ignoring $f..."
true
elif [[ "${f##*/}" =~ ^bootstrap\.[^.]*$ ]]; then
# debug " Ignoring $f..."
true
else
others_sh[${#others_sh[*]}]="$f"
debug " Add ${INFO_FORMAT}$f${RESET_FORMAT} to ${INFO_FORMAT}others.sh${RESET_FORMAT}..."
fi
done
done
fpath_env="${fpath_env})"
# reset nullglob if original value is false
[ -z "$orig_nullglob" ] && shopt -u nullglob
# stage0 - for top shell, SHLVL=1
info " ${stage0_file%/*}/${HIGHLIGHT2_FORMAT}${stage0_file##*/}${RESET_FORMAT}"
cat /dev/null > "${stage0_file}"
echo "### top_sh scripts ###" >> "${stage0_file}"
echo "" >> "${stage0_file}"
for f in "${top_sh_sh[@]}"
do
debug " ${f%/*}/${HIGHLIGHT2_FORMAT}${f##*/}${RESET_FORMAT}"
(
echo "# Script $f"
cat "$f"
echo ""
) >> "${stage0_file}"
done
echo "${path_env}" >> "${stage0_file}"
# stage1
info " ${stage1_file%/*}/${HIGHLIGHT2_FORMAT}${stage1_file##*/}${RESET_FORMAT}"
cat /dev/null > "${stage1_file}"
if [ "$shell" == "zsh" ]; then
echo "${fpath_env}" >> "${stage1_file}"
fi
echo "" >> "${stage1_file}"
echo "### set paths ###" >> "${stage1_file}"
for f in "${path_sh[@]}"
do
debug " ${f%/*}/${HIGHLIGHT2_FORMAT}${f##*/}${RESET_FORMAT}"
(
echo "# Script $f"
cat "$f"
echo ""
) >> "${stage1_file}"
done
echo "" >> "${stage1_file}"
echo "### set environments ###" >> "${stage1_file}"
for f in "${env_sh[@]}"
do
debug " ${f%/*}/${HIGHLIGHT2_FORMAT}${f##*/}${RESET_FORMAT}"
(
echo "# Script $f"
cat "$f"
echo ""
) >> "${stage1_file}"
done
echo "" >> "${stage1_file}"
# stage2
info " ${stage2_file%/*}/${HIGHLIGHT2_FORMAT}${stage2_file##*/}${RESET_FORMAT}"
cat /dev/null > "${stage2_file}"
echo "### others scripts ###" >> "${stage2_file}"
echo "" >> "${stage2_file}"
for f in "${others_sh[@]}"
do
debug " ${f%/*}/${HIGHLIGHT2_FORMAT}${f##*/}${RESET_FORMAT}"
(
echo "# Script $f"
cat "$f"
echo ""
) >> "${stage2_file}"
done
echo "" >> "${stage2_file}"
echo "### completion scripts ###" >> "${stage2_file}"
for f in "${completion_sh[@]}"
do
debug " ${f%/*}/${HIGHLIGHT2_FORMAT}${f##*/}${RESET_FORMAT}"
(
echo "# Script $f"
cat "$f"
echo ""
) >> "${stage2_file}"
done
}
function join_by {
local d=$1
shift
echo -n "$1"
shift
printf "%s" "${@/#/$d}"
}
get_enabled_dir() {
local dir
local old_path=$PATH
local -a dirs
IFS=$'\n' read -r -d '' -a dirs < <(find "$DOTFILES_ROOT" -maxdepth 1 -type d ! -name '.*' | sort)
info "Checking plugins status..."
mkdir -p "$DOTFILES_LOCAL"
: > "$DOTFILES_LOCAL/enabled.new.txt"
for dir in "${dirs[@]}"
do
[ -d "$dir/bin" ] && PATH=$PATH:"$dir/bin"
done
for dir in "${dirs[@]}"
do
if [ -e "${dir}/disabled" ] || [ -e "${dir}/disabled.global" ]
then
debug " ${WARN_FORMAT}Disable${RESET_FORMAT} ${INFO_FORMAT}${dir}${RESET_FORMAT} for global"
echo "# $(basename "${dir}")" >> "$DOTFILES_LOCAL/enabled.new.txt"
continue
fi
if [ -f "${dir}/requirements.sh" ] && ! "${dir}/requirements.sh" &> /dev/null
then
debug " ${WARN_FORMAT}Disable${RESET_FORMAT} ${INFO_FORMAT}${dir}${RESET_FORMAT} for requirements.sh"
echo "# $(basename "${dir}")" >> "$DOTFILES_LOCAL/enabled.new.txt"
continue
fi
basename "${dir}" >> "$DOTFILES_LOCAL/enabled.new.txt"
basename "${dir}"
done
PATH=$old_path
}
generate_files() {
local -A old_enabled
local dir
if [ -e "$DOTFILES_LOCAL/enabled.txt" ]
then
while read -r dir
do
old_enabled["$dir"]="0"
done < <( grep "^[^#]" "$DOTFILES_LOCAL/enabled.txt" )
fi
for dir in "$@"
do
if [ -z "${old_enabled[$dir]+isset}" ]
then
info " ${SUCCESS_FORMAT}Enable${RESET_FORMAT} ${INFO_FORMAT}${dir}${RESET_FORMAT}"
else
old_enabled["$dir"]="1"
debug " ${SUCCESS_FORMAT}Enable${RESET_FORMAT} ${INFO_FORMAT}${dir}${RESET_FORMAT}"
fi
# run bootstrap file
bootstrap_file=
if [ -f "${DOTFILES_ROOT}/${dir}/bootstrap.sh" ]
then
bootstrap_file="${DOTFILES_ROOT}/${dir}/bootstrap.sh"
elif [ -f "${DOTFILES_ROOT}/${dir}/bootstrap" ]
then
bootstrap_file="${DOTFILES_ROOT}/${dir}/bootstrap"
fi
if [ -n "${bootstrap_file}" ]
then
if [ -x "${bootstrap_file}" ]
then
debug " Executing ${bootstrap_file}"
"${bootstrap_file}" "${DRY_RUN}"
else
fail "'${bootstrap_file}' must be executable!"
fi
fi
done
for dir in "${!old_enabled[@]}"
do
if [ "${old_enabled[$dir]}" == "0" ]
then
info " ${WARN_FORMAT}Disable${RESET_FORMAT} ${INFO_FORMAT}${dir}${RESET_FORMAT}"
fi
done
info " ${SUCCESS_FORMAT}Done${RESET_FORMAT}"
generate_script_file "$@"
local overwrite_all=false backup_all=false skip_all=false
info ""
info "Creating symbol links..."
create_symlinks "${DOTFILES_ROOT}" "links.txt" "$@"
create_symlinks "${DOTFILES_LOCAL}" "links_local.txt" .
info " ${SUCCESS_FORMAT}Done${RESET_FORMAT}"
}
generate_script_file() {
local dir=
local shell=
info ""
info "Generating script files for shells..."
for shell in bash zsh
do
generate_source_list $shell "$@"
done
}
update_subtrees() {
cat "${DOTFILES_ROOT}/subtrees.txt" |
grep -v '^#' |
while read prefix url branch; do
git subtree pull --squash --prefix "${prefix}" "${url}" "${branch}"
done
}
EchoUsage()
{
echo "
Usage: $(basename "$0") [options] [--]
Options:
-h|help Display this message
-u|update Update subtrees
-V|version Display script version
-v|verbose Display more verbose log
-t|target <TARGET_DIR> Target directory, defaults to \$HOME
-d|dry-run Print modification instead of apply it
" >&2
}
DRY_RUN=
VERBOSE=
TARGET="$HOME"
while getopts ":huVvt:d" opt; do
case $opt in
h) # |help)
EchoUsage
exit 0
;;
u) # |update)
update_subtrees
exit 0
;;
V) # |version)
echo "$(basename "$0") -- Version $__ScriptVersion"
exit 0
;;
v) # |verbose)
VERBOSE="${VERBOSE}1"
;;
t) # |target)
TARGET="$OPTARG"
;;
d) # |dry-run)
DRY_RUN=yes
;;
* )
echo -e "\n Option does not exist : '$OPTARG' at position $OPTIND\n"
EchoUsage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ "${DRY_RUN}" = 'yes' ]
then
info "!!! ${WARN_FORMAT}DRY RUN${RESET_FORMAT} !!!"
info ""
else
info "!!! ${SUCCESS_FORMAT}Apply mode${RESET_FORMAT} !!!"
info ""
fi
info "Environments:"
info " ${HIGHLIGHT1_FORMAT}DOTFILES_ROOT${RESET_FORMAT} = ${HIGHLIGHT2_FORMAT}${DOTFILES_ROOT}${RESET_FORMAT}"
info " ${HIGHLIGHT1_FORMAT}DOTFILES_LOCAL${RESET_FORMAT} = ${HIGHLIGHT2_FORMAT}${DOTFILES_LOCAL}${RESET_FORMAT}"
info " ${HIGHLIGHT1_FORMAT}DOTFILES_ENV${RESET_FORMAT} = ${HIGHLIGHT2_FORMAT}${DOTFILES_ENV}${RESET_FORMAT}"
info ""
info "Install to $TARGET..."
info ""
typeset -a dirs
IFS=$'\n' read -r -d '' -a dirs < <(get_enabled_dir)
generate_files "${dirs[@]}"
[ -e "$DOTFILES_LOCAL/enabled.new.txt" ] && mv "$DOTFILES_LOCAL/enabled.new.txt" "$DOTFILES_LOCAL/enabled.txt"
# Delete zsh completion dump file, force regeneration
[ -e ~/.zcompdump ] && rm ~/.zcompdump
unset dirs
info ""
info "${SUCCESS_FORMAT}Done!${RESET_FORMAT}"