-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdm.template
executable file
·1290 lines (1160 loc) · 39.9 KB
/
bdm.template
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# BDM - A dotfile manager written in bash
# Copyright (C) 2021 Hu Sixu <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
################################################################################
# shellcheck disable=SC1090
# shellcheck disable=SC2181
# Startup Checking #############################################################
################################################################################
# This script requires bash >= 4.3, since it uses `declare -n`
if [[ ${BASH_VERSINFO[0]} -lt 4 ]] ||
[[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 3 ]]; then
echo "This script requires Bash version >= 4.3"
exit 1
fi
[[ $EUID -eq 0 ]] && {
echo "This script cannot be run as root," \
"as it might cause unexpected damage." >&2
exit 1
}
BDM_PREFIX=$(
cd "$(dirname "$(realpath "${BASH_SOURCE[0]}")")" || exit
pwd
)
export BDM_PREFIX
# replaced by Makefile
BDM_LIBDIR="@BDM_LIBDIR@"
BDM_CONFDIR="@BDM_CONFDIR@"
export BDM_LIBDIR
# Init & Includes ##############################################################
################################################################################
source "$BDM_LIBDIR/utils.sh"
source "$BDM_LIBDIR/distro.sh"
source "$BDM_LIBDIR/read_ini.sh"
source "$BDM_LIBDIR/install.sh"
# Read Configs #################################################################
################################################################################
readonly configs=(
"$HOME/.bdm/bdm.conf"
"$LOCAL_CONFIG_DIR/bdm.conf"
"$LOCAL_CONFIG_DIR/bdm/bdm.conf"
"$BDM_CONFDIR/bdm.conf"
)
config=""
for f in "${configs[@]}"; do
[[ -f "$f" ]] && {
config="$f"
read_ini --prefix CONF --booleans 0 "$config"
break
}
done
[[ -n $config ]] || {
log:error "Cannot find config file"
exit 1
}
# for lib/install.sh to store dotfile installation information
STORAGE_DIR="$LOCAL_CONFIG_DIR/bdm/.db"
mkdir -p "$STORAGE_DIR"
export STORAGE_DIR
# global variables
declare -a dotfile_dirs parsed_dotfile_dirs
read -ra dotfile_dirs <<<"${CONF__bdm__dotfile_dirs:-~/.config/bdm/dotfiles}"
for dotfile_dir in "${dotfile_dirs[@]}"; do
parsed_dotfile_dirs+=("${dotfile_dir/#\~/$HOME}")
done
dotfile_dirs=("${parsed_dotfile_dirs[@]}")
unset parsed_dotfile_dirs
declare cache_dir="${CONF__bdm__cache_dir:-$HOME/.cache/bdm}"
cache_dir="${cache_dir/#\~/$HOME}"
mkdir -p "$cache_dir" || return 1
# Termux Support ###############################################################
################################################################################
# Fake a `sudo' command, since termux does not have a sudo command
[[ $DISTRO == "termux" ]] && {
sudo() {
while [[ $1 =~ ^- ]]; do shift; done
"$@"
}
export -f sudo
}
# FUNCTIONS ####################################################################
################################################################################
readonly _OPTION_PATTERN='^--?[[:alnum:]][[:alnum:]-]*$'
main() {
[[ $# -eq 0 ]] && {
echo "$_HELP_MESSAGE"
return 0
}
# Parse command line #######################################################
############################################################################
# get command
local cmd=$1
shift
# avoid updating cache when printing help/version
case $cmd in
-h | --help)
echo "$_HELP_MESSAGE"
return 0
;;
-v | --version)
echo "bdm @BDM_VERSION@"
return 0
;;
esac
update_cache || return $?
case $cmd in
i*)
# Install a package
local -A install_options=(
[depends]="${CONF__bdm__depends:-check}"
[usermode]="${CONF__bdm__usermode:-false}"
[holdsudo]=true
)
while [[ $1 =~ $_OPTION_PATTERN ]]; do
case $1 in
"-d" | "--depends")
shift
install_options[depends]="$1"
;;
"-s" | "--skip-depends")
install_options[depends]="skip"
;;
"-c" | "--check-depends")
install_options[depends]="check"
;;
"-i" | "--install-depends")
install_options[depends]="install"
;;
"-u" | "--user-mode")
install_options[usermode]=true
;;
"-r" | "--root-mode")
install_options[usermode]=false
;;
# BDM refresh sudo periodically in the background,
# this option turns this behavior off. It's currently only used
# for testing (to workaround SimonKagstrom/kcov/issues/325).
"-dhs" | "--dont-hold-sudo")
install_options[holdsudo]=false
;;
*)
log:error "Option $1 unrecognized"
return 1
;;
esac
shift
done
[[ ' skip check install ' == *" ${install_options[depends]} "* ]] || {
log:error "-d | --depends must be followed by one of skip|check|install"
return 1
}
[[ ' false true ' == *" ${install_options[usermode]} "* ]] || {
log:error "-u | --usermode must be followed by either 'true' or 'false'"
return 1
}
export ISROOT=true
${install_options[usermode]} && export ISROOT=false
bdm:install "$@"
;;
u*)
# Uninstall a package
while [[ $1 =~ $_OPTION_PATTERN ]]; do
case $1 in
*)
log:error "Option $1 unrecognized"
return 1
;;
esac
shift
done
bdm:uninstall "$@"
;;
s*)
# search packages
local -A search_options=(
[depends]=false
[tags]=false
[usermode]="$CONF__bdm__usermode"
)
while [[ $1 =~ $_OPTION_PATTERN ]]; do
case $1 in
'-t' | '--tags')
search_options['tags']=true
;;
'-d' | '--depends')
search_options['depends']=true
;;
*)
log:error "Option $1 unrecognized"
exit 1
;;
esac
shift
done
bdm:search "$@"
;;
l*)
# search packages
local -A list_options=(
[files]=false
)
while [[ $1 =~ $_OPTION_PATTERN ]]; do
case $1 in
'-f' | '--files')
list_options['files']=true
;;
*)
log:error "Option $1 unrecognized"
exit 1
;;
esac
shift
done
# list only installed packages
bdm:list "$@"
;;
n*)
# search packages
local -A new_options=(
[usermode]=''
[target_dir]=''
[distros]=''
)
while [[ $1 =~ $_OPTION_PATTERN ]]; do
case $1 in
'-u' | '--usermode')
new_options['usermode']=true
;;
'-d' | '--distros')
shift
new_options[distros]+=":$1:"
;;
'-t' | '--target-dir')
shift
new_options[target_dir]="$1"
;;
*)
log:error "Option $1 unrecognized"
exit 1
;;
esac
shift
done
# create a new package
bdm:new "$1"
;;
*)
log:error "Command '$cmd' not recognized. Run '${BASH_SOURCE[0]}' for help"
exit 1
;;
esac
}
# 'bdm:' namesapce avoids function name overwrites
bdm:install() {
# Preparation install ######################################################
# require root if not in usermode
if ${install_options[holdsudo]}; then
${install_options[usermode]} || require_and_hold_root_access || return 1
fi
# dotfiles to install
local -a dotfiles=()
# filter a list of valid dotfiles
mapfile -t dotfiles < <(filter_valid_dotfiles "$@")
if [[ "${#dotfiles[@]}" -eq 0 ]]; then
log:warning "No dotfile to install..."
return 0
fi
# Parse dependency graph
if [[ ${install_options[depends]} != 'skip' ]]; then
# Check for dependency loop, including makedepends
dependency_loop_detection "${dotfiles[@]}" || {
log:error "Dependency checking failed"
return 1
}
if [[ ${install_options[depends]} == 'install' ]]; then
# add all dependency to 'dotfiles' and correct their order
mapfile -t dotfiles < <(list_and_sort_dependencies "${dotfiles[@]}")
fi
fi
echo "dotfiles to install: ${dotfiles[*]}"
echo -n "Proceed? [Y/n]: "
read -r ans
[[ $ans == n ]] && return 0
# Dotfile installation #####################################################
for dotfile in "${dotfiles[@]}"; do
log:info "Processing $dotfile ..."
# 1. check for misisng dependencies
if [[ ${install_options[depends]} != 'skip' ]]; then
# 1. check if all dependencies in the 'check' array,
# if dependency is not met, check if there's a entry in the
# 'packages' array to install it
local -a missing_deps=()
local missing_deps_list
missing_deps_list="$(check_depends "$dotfile")" || {
log:error "Unable to meet dependency(s), aborting... "
return 1
}
[[ -n $missing_deps_list ]] &&
mapfile -t missing_deps <<<"$missing_deps_list"
fi
# 2. install misisng dependencies
if [[ ${#missing_deps[@]} -gt 0 ]]; then
if [[ ${install_options[depends]} == 'check' ]]; then
log:error "dependency missing."
return 1
elif [[ ${install_options[depends]} == 'install' ]]; then
# Don't use `install_depends ... || return $?' here, otherwise
# all errexit flags in the install_depends function will be shadowed
install_depends "$dotfile" "${missing_deps[@]}"
[[ $? == 0 ]] || return 1
fi
fi
# 3. install dotfiles
(
set -eo pipefail
bootstrap_path="$(find_bootstrap_sh "$dotfile")" || return $?
source "$bootstrap_path" >/dev/null
if [[ $(type -t "bootstrap:__install") == "function" ]]; then
bootstrap:__install
else
log:warning "'bootstrap:__install()' not defined in '$bootstrap_path', skipping."
fi
)
[[ $? == 0 ]] || {
log:warning "Failed installing $dotfile"
return 1
}
# 4. evaluate post-install function
local evaluate_func
evaluate_func="$(
set -eo pipefail
export LOG_INDENT=4
bootstrap_path="$(find_bootstrap_sh "$dotfile")" || return 1
source "$bootstrap_path" >/dev/null
if [[ $(type -t "bootstrap:evaluate") == "function" ]]; then
declare -pf bootstrap:evaluate
fi
)"
if [[ -n ${evaluate_func} ]]; then
log:info "Executing bootstrap:evaluate function of $dotfile"
eval "${evaluate_func}"
bootstrap:evaluate || {
log:error "$dotfile: Failed executing the bootstrap:evaluate function. " \
"Aborting to avoid subsequent failures..."
return 1
}
unset bootstrap:evaluate
fi
done
for dotfile in "${dotfiles[@]}"; do
(
set -eo pipefail
bootstrap_path="$(find_bootstrap_sh "$dotfile")" || return $?
source "$bootstrap_path" >/dev/null
if [[ $(type -t "bootstrap:post_install") == "function" ]]; then
bootstrap:post_install
else
exit 0
fi
)
[[ $? == 0 ]] || {
log:warning "Failed to execute bootstrap:post_install of $dotfile"
return 1
}
done
}
bdm:uninstall() {
# dotfiles to uninstall
local -a dotfiles=()
# filter a list of valid dotfiles
mapfile -t dotfiles < <(filter_valid_dotfiles "$@")
if [[ "${#dotfiles[@]}" -eq 0 ]]; then
log:warning "No dotfile to uninstall..."
return 0
fi
for dotfile in "${dotfiles[@]}"; do
log:info "Processing $dotfile ..."
# uninstall dotfiles
(
set -eo pipefail
bootstrap_path="$(find_bootstrap_sh "$dotfile")" || return $?
source "$bootstrap_path" >/dev/null
if [[ $(type -t "bootstrap:__uninstall") == "function" ]]; then
bootstrap:__uninstall >/dev/null
else
log:warning "'bootstrap:__uninstall()' not defined in '$dotfile/bootstrap.sh', skipping."
fi
)
[[ $? == 0 ]] || {
log:warning "Failed uninstalling $dotfile"
return 1
}
done
}
bdm:search() {
# dotfiles to search
local -a dotfiles
mapfile -t dotfiles < <(filter_valid_dotfiles "$@")
if [[ "${#dotfiles[@]}" -eq 0 ]]; then
log:warning "No result ..."
return 0
fi
if ${search_options[depends]}; then
# Check for dependency loop, including makedepends
dependency_loop_detection "${dotfiles[@]}" || {
log:error "Dependency checking failed"
return 1
}
fi
for dotfile in "${dotfiles[@]}"; do
echo -e "\033[1m$dotfile\033[0m"
if "${search_options[tags]}"; then
local -a tags
mapfile -t tags < <(dotfile_tags "$dotfile")
echo " Tags: ${tags[*]}"
fi
if "${search_options[depends]}"; then
local -a deps
mapfile -t deps < <(dotfile_deps "$dotfile")
echo " Deps: ${deps[*]}"
fi
echo
done
}
bdm:list() {
# dotfiles to list
local -a dotfiles
local -A dotfiles_set
mapfile -t dotfiles < <(filter_valid_dotfiles "$@")
for dotfile in "${dotfiles[@]}"; do
dotfiles_set["$dotfile"]=1
done
# installed dotfiles
local dotfile_name
local -a installed_dotfiles
for db_file in "$STORAGE_DIR"/*.db; do
if [[ -f "$db_file" ]]; then
dotfile_name="$(basename "${db_file%.db}")"
local -gA tmp_db=()
db:load "$db_file" tmp_db
if [[ "${#tmp_db[@]}" -gt 0 && -n "${dotfiles_set["$dotfile_name"]}" ]]; then
installed_dotfiles+=("$dotfile_name")
fi
fi
done
# list all entries
for dotfile in "${installed_dotfiles[@]}"; do
echo -e "\033[1m$dotfile\033[0m"
if "${list_options[files]}"; then
local -gA tmp_db=()
db:load "$STORAGE_DIR/$dotfile.db" tmp_db
for rec in "${tmp_db[@]}"; do
local -A record=()
eval "$rec"
if [[ "${record[hash]}" == "directory" ]]; then
echo " Dir: ${record[target]}"
elif [[ "${record[hash]}" == "symlink" ]]; then
echo " Link: ${record[source]} --> ${record[target]}"
else
echo " File: ${record[source]} --> ${record[target]}"
fi
done
echo
fi
done
}
# $@: list of dotfiles or tags. 'all' or 't:all' for every valid dotfiles
# print: list of valid dotfiles, one per line
filter_valid_dotfiles() {
local -a items=("$@")
[[ $# -eq 1 && ($1 == "all" || $1 == "t:all") ]] && {
items=()
for dir in "${dotfile_dirs[@]}"; do
if [[ -d "$dir" ]]; then
mapfile -t list < <(ls "$dir")
items+=("${list[@]}")
else
log:warning "dotfiles dir '$dir' does not exist"
fi
done
}
# filter out invalid dotfiles and tags
local -a valid_dotfiles=()
local -a tags=()
for item in "${items[@]}"; do
if [[ "$item" =~ ^t: ]]; then
tags+=("${item#t:}")
else
if find_dotfile_dir "$item" >/dev/null; then
valid_dotfiles+=("$item")
else
tags+=("$item")
fi
fi
done
local -a extra_dotfiles=()
mapfile -t extra_dotfiles <<<"$(tags_to_dotfiles "${tags[@]}")"
[[ -n "${extra_dotfiles[*]}" ]] && valid_dotfiles+=("${extra_dotfiles[@]}")
# return
for dotfile in "${valid_dotfiles[@]}"; do
echo "$dotfile"
done | sort -u
}
require_and_hold_root_access() {
# At least `sudo` is needed
type sudo >/dev/null 2>&1 || {
log:error "the 'sudo' program is needed for running this script"
exit 1
}
# Require root privilege
if sudo -v; then
export ISROOT=true
else
log:warning "Require root failed."
echo "All installation process will be done in user mode."
echo -n "Those dotfiles that requires root are doomed to fail. Proceed? [y/N]: "
read -r ans
if [[ $ans != y ]]; then
return 1
fi
export ISROOT=false
fi
# keep sudo credential cache up-to-date
while true; do
sleep 5
sudo -n true
kill -0 "$$" 2>/dev/null || exit
done &
pid=$!
trap_add "kill -- $pid" SIGINT SIGTERM EXIT
}
# $1 dependency string
# print: <type> <name> <install-type> <install-name>, separated with space,
# can be read into variable with `read -r type name i_type i_name`
parse_dep_info() {
local dep_check="${1%%::*}"
if [[ "$1" == *::* ]]; then
local dep_install="${1##*::}"
else
local dep_install=""
fi
local -A check_prefix_map=(
["v"]="virtual" ["fi"]="file" ["fu"]="function"
["d"]="dotfile" ["e"]="executable"
)
local dep_type=""
local dep_name=""
local number_virtual=0
if [[ -z $dep_check ]]; then
dep_type="virtual"
dep_name="virtual-$number_virtual"
((++number_virtual))
else
local match_prefix=false
for prefix in "${!check_prefix_map[@]}"; do
if [[ $dep_check =~ ^${prefix}[[:alnum:]]*:[[:print:]]+$ ]]; then
dep_type="${check_prefix_map[$prefix]}"
dep_name="${dep_check#${prefix}*:}"
match_prefix=true
break
fi
done
if ! $match_prefix; then
if [[ $dep_check =~ ^[[:alnum:]]*:[[:print:]]+$ ]]; then
dep_type="unknown"
dep_name="unknown"
else
# If command not found, and bootstrap script for the dotfile
# of the same name is found, treat as dotfile.
# In all other cases still treat as executable.
if ! command -v "$dep_check" >/dev/null 2>&1 &&
find_dotfile_dir "$dep_check" >/dev/null 2>&1; then
dep_type="dotfile"
dep_name="$dep_check"
else
dep_type="executable"
dep_name="$dep_check"
fi
fi
fi
fi
local -A install_prefix_map=(["s"]="system" ["u"]="user" ["f"]="function" ["a"]="aur")
local install_type=""
local install_name=""
if [[ $dep_type == "dotfile" ]]; then
# installation type and name is invalid for dotfiles
install_type="_"
install_name="_"
elif [[ $dep_type == "executable" && -z "$dep_install" ]]; then
# if item after `::` is not specified, and dep_type is executable,
# treat as a system package with the of the same name
install_type="system"
install_name="${dep_check#*:}"
elif [[ -z "$dep_install" ]]; then
# if item after `::` is not specified, report error
install_type="unspecified"
install_name="unspecified"
else
local match_prefix=false
for prefix in "${!install_prefix_map[@]}"; do
if [[ $dep_install =~ ^${prefix}[[:alnum:]]*:[[:print:]]+$ ]]; then
install_type="${install_prefix_map[$prefix]}"
install_name="${dep_install#${prefix}*:}"
match_prefix=true
break
fi
done
if ! $match_prefix; then
if [[ $dep_install =~ ^[[:alnum:]]*:[[:print:]]+$ ]]; then
install_type="unknown"
install_name="unknown"
else
# if no prefix is detected, default to system package if $ISROOT is
# set, otherwise default to usermode package
if $ISROOT; then
install_type="system"
else
install_type="user"
fi
install_name="${dep_install}"
fi
fi
fi
echo "$dep_type $dep_name $install_type $install_name"
}
# $@: dotfiles to install
dependency_loop_detection() {
local -a exam_queue
local -a exam_queue_level
for dotfile in "$@"; do
exam_queue+=("$dotfile")
exam_queue_level+=("1")
done
# non-recursive DFS to find all dependency loops
local -A deps_set
local -a deps_stack # deps_stack[-1] is stack top
while [[ ${#exam_queue[@]} -ne 0 ]]; do
local cur_dotfile="${exam_queue[0]}"
local cur_dotfile_level="${exam_queue_level[0]}"
exam_queue=("${exam_queue[@]:1}")
exam_queue_level=("${exam_queue_level[@]:1}")
# set depends stack to correct level
while [[ ${#deps_stack[@]} -ne $((cur_dotfile_level - 1)) ]]; do
unset deps_set["${deps_stack[-1]}"]
deps_stack=("${deps_stack[@]::${#deps_stack[@]}-1}")
done
# check for dependency loop
if [[ -n ${deps_set[$cur_dotfile]} ]]; then
local loop
for depend in "${deps_stack[@]}"; do
if [[ "$depend" == "$cur_dotfile" ]]; then
loop+="[1m[31m${depend}[0m -> "
else
loop+="$depend -> "
fi
done
loop+="[1m[31m$cur_dotfile[0m"
log:error "Dependency loop detected: $loop"
unset loop
return 1
fi
# read all dependency of current dotfile
if ! find_dotfile_dir "$cur_dotfile" >/dev/null; then
local chain
chain=$(printf "%s -> " "${deps_stack[@]}")
chain+="$cur_dotfile"
log:error "Dependency chain: $chain," \
"but 'bootstrap.sh' script for '$cur_dotfile' does not exist."
return 1
fi
local -a depends
mapfile -t depends < <(extract_dotfile_depends "$cur_dotfile")
if [[ ${#depends[@]} -ne 0 ]]; then
exam_queue=("${depends[@]}" "${exam_queue[@]}")
for ((i = 0; i < ${#depends[@]}; ++i)); do
exam_queue_level=("$((cur_dotfile_level + 1))" "${exam_queue_level[@]}")
done
deps_stack+=("$cur_dotfile")
deps_set["$cur_dotfile"]=1
fi
done
return 0
}
# $@: all dotfiles to install
list_and_sort_dependencies() {
local -a exam_queue
for dotfile in "$@"; do
exam_queue+=("$dotfile")
done
# Use BFS to get the topological order.
# Yeah I know this function can be merged with dependency_loop_detection,
# but for the sake of simplicity and readability I'll just use BFS here.
for ((i = 0; i < ${#exam_queue[@]}; ++i)); do
local cur_dotfile="${exam_queue[$i]}"
local -a depends
mapfile -t depends < <(extract_dotfile_depends "$cur_dotfile")
if [[ ${#depends[@]} -ne 0 ]]; then
exam_queue+=("${depends[@]}")
fi
done
local -A unique_set
for ((i = 1; i <= ${#exam_queue[@]}; ++i)); do
if [[ -z ${unique_set[${exam_queue[-$i]}]} ]]; then
unique_set[${exam_queue[-$i]}]=1
echo "${exam_queue[-$i]}"
fi
done
}
# Extract `d*:` types dependencies from dotfiles' bootstrap file
# $1: name of the dotfile
extract_dotfile_depends() {
# avoid variable contamination of parent shell
local -a deps=()
mapfile -t deps < <(dotfile_deps "$1")
if [[ "${#deps[@]}" -eq 0 ]]; then return 0; fi
for dep in "${deps[@]}"; do
read -r dep_type dep_name _ _ <<<"$(parse_dep_info "$dep")"
if [[ $dep_type == "dotfile" ]]; then
echo "$dep_name"
fi
done
}
# $1 dotfile dir name
# ${install_options[depends]} affects the behavior of this function
# print: a list of missing depends, one per line
# return: 0 for success, 1 for fail
check_depends() {
(
# run in subshell. exit when any error happens
set -eo pipefail
unset tags
local -a deps=()
mapfile -t deps <<<"$(dotfile_deps "$1")"
if [[ -z ${deps[*]} ]]; then return 0; fi
# these variables are defined in subshell and
# does not interfere with the variables outside
declare -a missing_deps=()
# Check dependency files before installing
for dep in "${deps[@]}"; do
read -r dep_type dep_name _ _ <<<"$(parse_dep_info "$dep")"
case "$dep_type" in
'file')
# test if file exists
if [[ ! -e $dep_name ]]; then
missing_deps+=("$dep")
fi
;;
'virtual')
# item is virtual, skip checking, but list as install
if [[ ${install_options[depends]} == 'install' ]]; then
missing_deps+=("$dep")
fi
;;
'function')
# if dep_type is function, update corresponding functions
bootstrap_path="$(find_bootstrap_sh "$1")" || return 1
source "$bootstrap_path" >/dev/null
# Use a function to decide if item exists
set +e
(
set -e
${dep_name#fu*:}
)
if [[ $? != 0 ]]; then
missing_deps+=("$dep")
fi
set -e
;;
'executable')
# test if executable exists in $PATH
if ! command -v "$dep_name" >/dev/null 2>&1; then
missing_deps+=("$dep")
fi
;;
'dotfile')
: # Skip. dotfile dependency is already handled before checking.
;;
'unknown')
log:error "$dotfile: unrecognized dependency prefix in '$dep'"
return 1
;;
esac
done
if [[ ${#missing_deps[@]} -gt 0 ]]; then
log:warning "$dotfile: Dependency missing: ${missing_deps[*]}" >&2
fi
local able_to_meet_dependency=true
for dep in "${missing_deps[@]}"; do
# pass to outer shell
echo "$dep"
# check if this dependency can be met
read -r _ _ install_type install_name <<<"$(parse_dep_info "$dep")"
if [[ $install_type == 'unknown' || \
$install_type == 'unspecified' ]]; then
log:error "$dotfile: Dependency '$dep': installation method $install_type."
able_to_meet_dependency=false
fi
done
$able_to_meet_dependency
)
}
# $1: dotfile dir name
# ${@:1}: a list of missing deps in the `deps` array
# return: 0 for success, 1 for fail
install_depends() {
local dotfile=$1
shift
local -a missing_deps=("$@")
# 2. install all the missing dependencies
(
set -eo pipefail
unset deps
for dep in "${missing_deps[@]}"; do
read -r _ _ install_type install_name <<<"$(parse_dep_info "$dep")"
case $install_type in
'function')
install_pkg_command="${install_name}"
# update functions if install_type is functions
bootstrap_path="$(find_bootstrap_sh "$dotfile")" || return 1
source "$bootstrap_path" >/dev/null 2>&1
;;
'system')
# package should be installed through package manager.
install_pkg_command="install_system_package_$DISTRO ${install_name}"
;;
'user')
install_pkg_command="install_userland_package_pkgsrc ${install_name}"
;;
'aur')
install_pkg_command="install_aur_package ${install_name}"
;;
esac
# package should be installed through function, in a subshell,
# so that set -e works correctly (both "exceptions" and returned
# error code are captured)
# We have to use this crooked way to simulate try-catch in an errexit environment ...
# see https://stackoverflow.com/questions/29532904/bash-subshell-errexit-semantics
set +e
(
set -e
$install_pkg_command
)
[[ $? == 0 ]] || {
log:error "$dotfile: Failed to process dependency $dep. Aborting ..."
return 1
}
set -e
done
)
}
bdm:new() {
[[ -z $1 ]] && {
log:error "Please specify a name."
return 1
}
local old_dir
if old_dir="$(find_dotfile_dir "$1")"; then
log:error "$1 already exists ($old_dir)"
return 1
fi
log:info "Creating package '$1'"
install_dir="${dotfile_dirs[0]}"
if [[ ${#dotfile_dirs[@]} -eq 0 ]]; then
log:error "'dotfile_dirs' not specified in config file."
elif [[ ${#dotfile_dirs[@]} -ge 1 ]]; then
if [[ -n "${new_options[target_dir]}" ]]; then
# test if the specified dir is one of the configured dirs ...
for dir in "${dotfile_dirs[@]}"; do
local dir_found=false
if [[ "$dir" -ef "${new_options[target_dir]}" ]]; then
dir_found=true
install_dir="$dir"
fi
done
# ... if not, report erro and exit
if ! $dir_found; then
log:error "'${new_options[target_dir]}' invalid. " \
"Please specify one of the following directory:"
for dir in "${dotfile_dirs[@]}"; do echo " $dir"; done
return 1
fi
else
# if target dir not specified, ask interactively
log:info "Select one of the following directories to put the new dotfile package:"
for i in $(seq "${#dotfile_dirs[@]}"); do
echo "$i: ${dotfile_dirs[$((i - 1))]}"
done
while true; do
echo -n "Please specify a number [1-${#dotfile_dirs[@]}], (default 1, 'q' to quit): "
read -r ans