-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdotty
executable file
·1434 lines (1253 loc) · 36.6 KB
/
dotty
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
# Dotfile management script in the vein of [[https://github.com/anishathalye/dotbot][dotbot]] and [[https://github.com/bashdot/bashdot][bashdot]].
#
# This script is designed to replace [[https://github.com/mohkale/dotty][dotty]] and acts a single file that's
# to be sourced and by some management script that can subsequently install
# your dotfiles. I migrated from my initial lispy dotfile manager to this
# script because I don't always have the freedom to download an external
# program but any unix environment should have a bash like shell available.
#
# Dependencies: bash>=4, readlink
# Environment variables:
# + root: Path to the root of your dotfiles repository.
# Notes:
# + Installation scripts should avoid prompting the user or writing to
# stdout unless explicitly required. Some executions of this script
# may not have an interactive stdin or terminal stdout.
# shellcheck disable=SC2317
set -a # Export everything
set -o pipefail # Fail a command when anything in its pipeline fails
shopt -s nullglob # Make globs with no results evaluate to nothing
root="${DOTFILES:-${XDG_CONFIG_HOME:-$HOME/.config}/dotfiles}"
exit_code=0
fail_early=0
BOTS_FILE="$root/setup/cache/dotty.bots"
_PYTHON_VENV_DIR="$XDG_LIB_HOME/python-venvs/default"
# Utils {{{
# _ _ _ _ _
# | | | | |_(_) |___
# | | | | __| | / __|
# | |_| | |_| | \__ \
# \___/ \__|_|_|___/
_abspath() {
local path="$1"
if [[ "$path" =~ ^/.* ]]; then
echo "$*"
elif [[ "$path" =~ ^~/.* ]]; then
# WARN: Doesn't account for ~username/ paths.
echo "${*/#\~/$HOME}"
elif hash realpath 2>/dev/null; then
realpath -m -- "$path"
elif hash readlink 2>/dev/null; then
readlink -m -- "$path"
else
echo "$(pwd)/$path"
fi
}
_basename() {
echo "${1##*/}"
}
_dirname() {
dirname -- "$1"
}
_contains() {
# TODO: Flexibly handle hashes.
local e match="$1"
shift
for e in "$@"; do
if [[ "$e" == "$match" ]]; then
return 0
fi
done
return 1
}
_csv() {
local IFS=,
echo "$*"
}
_skip_directive() { #(directive)
trace 'Checking whether skipping directive %s' "$1"
if [ "${#only_directives[@]}" -gt 0 ]; then
! _contains "$1" "${only_directives[@]}"
elif [ "${#except_directives[@]}" -gt 0 ]; then
_contains "$1" "${except_directives[@]}"
else
false
fi
local run="$?"
if [ "$run" -ne 0 ]; then
debug 'Skipping directive %s because of configuration' "$1"
fi
return "$run"
}
# Whenever you need to run a command in the following directives for dotfile
# installation, you should use run-cmd or run-cmd-xargs instead of running
# it directly. This lets me preview the instructions my dotfiles will run to
# install and allow verification or partial execution through vipe.
dry_run=0
_run_cmd() { #(cmd, args)
if [ "$dry_run" -eq 0 ]; then
"$@"
else
printf '%q ' "$@"
echo
fi
}
run-cmd() { if ! _skip_directive shell; then _run_cmd "$@"; fi; }
# Should be fully quoted if required.
_run_cmds() {
if [ "$dry_run" -eq 0 ]; then
eval "$(cat)"
else
cat
fi
}
run-cmds() { if ! _skip_directive shell; then _run_cmds; fi; }
# Variant of run-cmd to direct in a specific directory
_run_cmd_at() { #(dir, cmd)
local dir="$1"; shift
local dir_quoted; dir_quoted="$(printf %q "$(_abspath "$dir")")"
if [ "$dry_run" -eq 0 ]; then
if pushd "$dir" >/dev/null; then
"$@"
popd >/dev/null || exit 1
fi
else
cat <<-EOF
if pushd $dir_quoted >/dev/null 2>&1; then
$(printf '%q ' "$@")
popd >/dev/null || exit 1
fi
EOF
fi
}
run-cmd-at() { if ! _skip_directive shell; then _run_cmd_at "$@"; fi; }
_run_cmd_xargs() {
local default_flags=( -r -d '\n' )
if [ "$dry_run" -eq 0 ]; then
xargs "${default_flags[@]}" "$@"
else
# WARN: Won't work if you pass flags which require separate arguments.
local it=
while [ "$#" -gt 0 ]; do
it="$1"
if [[ "$it" =~ ^- ]]; then
default_flags+=( "$it" )
else
break
fi
shift
done
# -t makes xargs print out the command, quoted, to /dev/null but there's
# no way to prevent it from running the command. To get around this we're
# making it run an echo of all the arguments which we just throw away.
xargs "${default_flags[@]}" -t echo "$@" 2>&1 >/dev/null |
awk '/^echo / { sub(/^echo /, "", $0); print $0 }'
fi
}
run-cmd-xargs() { if ! _skip_directive shell; then _run_cmd_xargs "$@"; fi; }
# Utils }}}
# Color {{{
# ____ _
# / ___|___ | | ___ _ __
# | | / _ \| |/ _ \| '__|
# | |__| (_) | | (_) | |
# \____\___/|_|\___/|_|
COLOR=1
[ -t 1 ] || COLOR=0
color() { #(color[,colors], args)
local color='' it_color=''
if [ "$COLOR" -eq 1 ]; then
it_color=''
while read -d, -r it; do
case "$it" in
reset) it_color=0 ;;
bold) it_color=1 ;;
dim) it_color=2 ;;
italic) it_color=3 ;;
underline) it_color=4 ;;
black) it_color=30 ;;
red) it_color=31 ;;
green) it_color=32 ;;
yellow|orange) it_color=33 ;;
blue) it_color=34 ;;
purple|magenta) it_color=35 ;;
cyan) it_color=36 ;;
white) it_color=37 ;;
bg-black) it_color=40 ;;
bg-red) it_color=41 ;;
bg-green) it_color=42 ;;
bg-yellow|bg-orange) it_color=43 ;;
bg-blue) it_color=44 ;;
bg-purple|bg-magenta) it_color=45 ;;
bg-cyan) it_color=46 ;;
bg-white) it_color=47 ;;
intense-black) it_color=90 ;;
intense-red) it_color=91 ;;
intense-green) it_color=92 ;;
intense-yellow|intense-orange) it_color=93 ;;
intense-blue) it_color=94 ;;
intense-purple|intense-magenta) it_color=95 ;;
intense-cyan) it_color=96 ;;
intense-white) it_color=97 ;;
*) echo "color: error: unknown color format $it" >&2 ;;
esac
if [ -n "$it_color" ]; then
if [ -n "$color" ]; then
color="$color;"
fi
color="$color$it_color"
fi
done <<< "$1,"
fi
if [ -n "$color" ]; then
printf '\e[%sm' "$color"
fi
shift
for it in "$@"; do
printf '%s' "$it"
done
if [ -n "$color" ]; then
printf '\e[0m'
fi
printf '\n'
}
# Color }}}
# Logging {{{
# _ _
# | | ___ __ _ __ _(_)_ __ __ _
# | | / _ \ / _` |/ _` | | '_ \ / _` |
# | |__| (_) | (_| | (_| | | | | | (_| |
# |_____\___/ \__, |\__, |_|_| |_|\__, |
# |___/ |___/ |___/
LOG_LEVEL=2
LOG_LEVELS=( )
_log_time_format="+$(color intense-black '%Y-%m-%d %R')"
_log() { #(lvl, ind, args)
local level="$2" format="$3"
if [ "$1" -ge "$LOG_LEVEL" ]; then
shift; shift; shift
printf "%s %s $format\n" "$(date "$_log_time_format")" "$level" "$@" >&2
fi
}
_trace_ind="$(color magenta TRC)"
LOG_LEVELS+=(trace)
trace() { _log 0 "$_trace_ind" "$@"; }
_debug_ind="$(color blue DBG)"
LOG_LEVELS+=(debug)
debug() { _log 1 "$_debug_ind" "$@"; }
_info_ind="$(color green INF)"
LOG_LEVELS+=(info)
info() { _log 2 "$_info_ind" "$@"; }
_warn_ind="$(color yellow WRN)"
LOG_LEVELS+=(warn)
warn() { _log 3 "$_warn_ind" "$@"; }
_todo_ind="$(color red TODO)"
LOG_LEVELS+=(todo)
todo() { _log 4 "$_todo_ind" "$@"; }
# I'm too lazy to implement proper error handling so lets
# just treat any error as one justifying a bad exit code.
_error_ind="$(color red ERR)"
LOG_LEVELS+=(error)
error() {
# shellcheck disable=SC2034
exit_code=1
if [ "$fail_early" -eq 1 ]; then
exit 1
fi
_log 4 "$_error_ind" "$@"
}
# Logging }}}
# Symlink {{{
# ____ _ _ _
# / ___| _ _ _ __ ___ | (_)_ __ | | __
# \___ \| | | | '_ ` _ \| | | '_ \| |/ /
# ___) | |_| | | | | | | | | | | | <
# |____/ \__, |_| |_| |_|_|_|_| |_|_|\_\
# |___/
_link_usage='Usage: link {DEST|SRC:DEST} [{DEST|SRC:DEST}...]'
read -d '' -r _link_help <<EOF
$_link_usage
Optional arguments:
-h Show this help message and exit
-H Create a hard link instead of a symlink
-r Relink DEST if its a symlink already
-f Overwrite DEST if it already exists
-i Don't check whether SRC exists, link anyways
-p NAME Prefix the base name of DEST with NAME
EOF
link() {
local link_args=() symlink=1 relink=0 force=0 ignore_missing=0 dest_basename_prefix=
local OPTIND
while getopts hHrfip: OPTION; do
case "$OPTION" in
h) echo "$_link_help"
return 0 ;;
\?) echo "$_link_usage" >&2
return 1 ;;
H) symlink=0 ;;
r) relink=1 ;;
f) force=1 ;;
i) ignore_missing=1 ;;
p) dest_basename_prefix="$OPTARG" ;;
esac
done
shift "$((OPTIND-1))"
[ "$symlink" -eq 1 ] && link_args+=(-s)
_skip_directive link && return
local src='' dest=''
while IFS=: read -r src dest; do
local l_link_args=( "${link_args[@]}" )
if [ -z "$dest" ]; then
if [ -z "$src" ]; then
continue
fi
# Argument is destination, src is same as argument but without an optional
# leading ".".
dest="$src"
src="$(_basename "$src")"
src="${src#.}"
fi
if [ -z "$src" ]; then
warn 'No src path for linking to dest %s found' "$dest"
fi
if [ -n "$dest_basename_prefix" ]; then
dest="$(_dirname "$dest")/$dest_basename_prefix$(_basename "$dest")"
fi
src="$(_abspath "$src")"
dest="$(_abspath "$dest")"
if [ "$src" = "$dest" ]; then
continue
fi
if [ "$ignore_missing" -eq 0 ]; then
trace 'Checking whether symlink src exists: %s' "$src"
if ! [ -e "$src" ]; then
error 'Symlink src not found: %s' "$src"
continue
fi
fi
trace 'Checking whether symlink destination already exists: %s' "$dest"
if ! [ -L "$dest" ] && [ -d "$dest" ]; then
warn 'Skipping symlinking %s into %s because dest already exists and is a directory' "$dest" "$src"
continue
fi
local existing_src existing_dest
existing_src="$(readlink -m "$src")"
existing_dest=
if [ -L "$dest" ] || [ -e "$dest" ]; then
existing_dest="$(readlink -m "$dest")"
fi
if [ -n "$existing_dest" ]; then
if [ -n "$existing_src" ] && [ "$existing_dest" = "$existing_src" ]; then
debug 'Skipping symlink because %s already points to %s' "$dest" "$src"
continue
elif [ "$force" -eq 1 ]; then
warn 'Overwriting destination %s with link to %s' "$dest" "$src"
l_link_args+=( -f )
elif [ "$relink" -eq 1 ] && [ -L "$dest" ]; then
warn 'Overwriting existing symlink %s with link to %s' "$dest" "$src"
l_link_args+=( -f )
else
warn 'Skipping symlinking %s into %s because destination already exists' "$dest" "$src"
continue
fi
fi
info 'Linking %s to %s' "$src" "$dest"
trace 'Checking whether base directory of destination exists'
local dir; dir="$(_dirname "$dest")"
if ! [ -e "$dir" ]; then
# KLUDGE: In dry run mode this'll be repeated multiple times.
_run_cmd mkdir -p "$dir"
fi
_run_cmd ln "${l_link_args[@]}" -- "$src" "$dest"
done <<< "$(printf "%s\n" "$@")"
}
# A work-around for link which can be used with shell globs
link-to() { #(dest, file, ...files)
if [ "$#" -le 1 ]; then
warn 'Link to called with one argument, must supply at least two'
return
fi
_skip_directive link && return
local dest="$1"; shift
local args=( )
while [ "$#" -ne 0 ]; do
args+=( "$1:$dest/$(_basename "$1")" )
shift
done
link "${args[@]}"
}
# Symlink }}}
# mkdir {{{
# _ _ _
# _ __ ___ | | ____| (_)_ __
# | '_ ` _ \| |/ / _` | | '__|
# | | | | | | < (_| | | |
# |_| |_| |_|_|\_\__,_|_|_|
_makedir_usage='Usage: makedir [-h] [-m MODE] PATH [PATH...]'
read -d '' -r _makedir_help <<EOF
$_makedir_usage
Positional arguments:
PATH path to a directory to create
Optional arguments:
-h Show this help message and exit
-m Set the file modes of any directories
EOF
makedir() {
local mkdir_args=( )
local OPTIND
while getopts 'hm:' OPTION; do
case "$OPTION" in
h) echo "$_makedir_help"
return 0 ;;
\?) echo "$_makedir_usage" >&2
return 1 ;;
m) mkdir_args+=( -m "$OPTARG" ) ;;
esac
done
shift "$((OPTIND-1))"
_skip_directive mkdir makedir && return
local it
while [ "$#" -gt 0 ]; do
it="$1"; shift
if [ -L "$it" ] || [ -d "$it" ]; then
trace 'Skipping creating directory because it already exists: %s' "$it"
elif [ -e "$it" ]; then
warn 'Cannot create directory because %s already exists' "$it"
else
it="$(_abspath "$it")"
info 'Creating directory %s' "$it"
# shellcheck disable=SC2086
_run_cmd mkdir -p "${mkdir_args[@]}" -- "$it"
fi
done
}
# Mkdir }}}
# Clean {{{
# _
# ___| | ___ __ _ _ __
# / __| |/ _ \/ _` | '_ \
# | (__| | __/ (_| | | | |
# \___|_|\___|\__,_|_| |_|
_clean_usage="Usage: clean [-r] [-f] PATH [PATH...]"
read -d '' -r _clean_help <<EOF
$_clean_usage
Finds and remove any broken links that point to your
dotfiles.
Positional arguments:
PATH path to a directory/file that should be cleaned
Optional arguments:
-h Show this help message and exit
-r Recursively traverse PATH if directory for files to clean
-f Remove broken links even those not pointing to dotfiles
EOF
clean() {
local recursive=0 force=0
local OPTIND
while getopts 'hrf' OPTION; do
case "$OPTION" in
h) echo "$_clean_help"
return 0 ;;
\?) echo "$_clean_usage" >&2
return 1 ;;
r) recursive=1 ;;
f) force=1 ;;
esac
done
shift "$((OPTIND-1))"
_skip_directive clean && return
local it
while [ "$#" -gt 0 ]; do
it="$1"; shift
trace 'Checking whether %s is a valid symlink' "$it"
if [ -L "$it" ]; then
if ! [ -e "$it" ]; then
info 'Cleaning %s' "$it"
echo "$it:$(readlink -m "$it")"
fi
elif [ -d "$it" ]; then
info 'Cleaning %s' "$it"
debug 'Cleaning all files in %s because its a directory' "$it"
find_args=( )
if [ "${recursive}" -eq 0 ]; then
find_args+=( -maxdepth 1 )
fi
find "$it" -mindepth 1 "${find_args[@]}" -xtype l -not -exec [ -e {} ] ';' -printf '%p:' -exec readlink -m {} ';'
fi |
if [ "$force" -eq 1 ]; then
cut -d: -f1
else
# only those within a subdirectory of $DOTFILES.
while IFS=: read -r src dest; do
case "$dest" in
"$DOTFILES"/*) echo "$src" ;;
esac
done
fi |
_run_cmd_xargs rm
done
}
# Clean }}}
# Platform {{{
# ____ _ _ __
# | _ \| | __ _| |_ / _| ___ _ __ _ __ ___
# | |_) | |/ _` | __| |_ / _ \| '__| '_ ` _ \
# | __/| | (_| | |_| _| (_) | | | | | | | |
# |_| |_|\__,_|\__|_| \___/|_| |_| |_| |_|
_platform="$("$DOTFILES/bin/ls-platform")"
is-linux() { [ "$_platform" = "linux" ]; }
is-freebsd() { [ "$_platform" = "freebsd" ]; }
is-darwin() { [ "$_platform" = "darwin" ]; }
is-windows() { [ "$_platform" = "windows" ]; }
is-unix() { ! is-windows; }
# Platform }}}
# Packages {{{
# ____ _
# | _ \ __ _ ___| | ____ _ __ _ ___ ___
# | |_) / _` |/ __| |/ / _` |/ _` |/ _ \/ __|
# | __/ (_| | (__| < (_| | (_| | __/\__ \
# |_| \__,_|\___|_|\_\__,_|\__, |\___||___/
# |___/
# This directive is a little more involved than the others because
# it requires some extra flexibility. For each call to it the first
# argument is the package manager to use and the rest are arguments
# to that package manager. If the package manager doesn't exist the
# command fails and you can branch to another package manager. If
# called with no packages this directive just checks whether that
# package manager is available. You can use this to create a branching
# installer directive such as:
# package yay foo bar ||
# package pacman foo bar ||
# error 'No package manager found, skipping'
#
# This first tries installing with yay, then with pacman and if both
# those don't exist then it outputs and error message and continues.
PACMANS=( )
# Handler for basic package managers that just check and substitue args.
_package_simple() { #(name, prog, pre-args, prog-args, update-cmd, pkgs)
local name="$1"; shift
local prog="$1"; shift
local pre_args="$1"; shift
local prog_args="$1"; shift
local update_args="$1"; shift
local usage="Usage: package $name [-h] PKG [PKGS...]"
local OPTIND
while getopts 'h' OPTION; do
case "$OPTION" in
h) cat <<EOF
$usage
Install $name packages.
Positional arguments:
PKGS List of packages to install
Optional arguments:
-h Show this help message and exit
EOF
return 0 ;;
\?) echo "$usage" >&2
return 1 ;;
esac
done
shift "$((OPTIND-1))"
if ! hash "$prog" 2>/dev/null; then
return 1
fi
_skip_directive package && return
if [ "$#" -gt 0 ]; then
debug 'Installing packages with %s: %s' "$name" "$(_csv "$@")"
local prog_args_arr pre_args_arr
IFS=, read -r -a prog_args_arr <<< "$prog_args"
IFS=, read -r -a pre_args_arr <<< "$pre_args"
if [ "${#pre_args_arr[@]}" -gt 0 ] \
&& [ "${pre_args_arr[0]}" = sudo ] \
&& [ "$EUID" -eq 0 ]; then
pre_args_arr=( "${pre_args_arr[@]:1}" )
fi
local update_var="_pacman_${name}_update"
if [ -n "$update_args" ] &&
[ -z "${!update_var}" ]; then
local update_args_arr
IFS=, read -r -a update_args_arr <<< "$update_args"
info 'Syncing package archive for package manager %s' "$name"
_run_cmd "${pre_args_arr[@]}" "$prog" "${update_args_arr[@]}"
eval "$update_var"=1
fi
# shellcheck disable=SC2086
printf '%s\n' "$@" |
_run_cmd_xargs "${pre_args_arr[@]}" "$prog" "${prog_args_arr[@]}"
fi
}
# Package manager for the python pip module system.
# See also https://github.com/sobolevn/dotbot-pip
PACMANS+=(pip)
_package_pip_usage="Usage: package pip [-h] PKG [PKGS...]"
read -d '' -r _package_pip_help <<EOF
$_package_pip_usage
Install python pip packages.
Positional arguments:
PKGS List of packages to install
Optional arguments:
-h Show this help message and exit
EOF
_package_pip() {
local OPTIND
while getopts 'h' OPTION; do
case "$OPTION" in
h) echo "$_package_pip_help"
return 0 ;;
\?) echo "$_package_pip_usage" >&2
return 1 ;;
esac
done
shift "$((OPTIND-1))"
if ! hash python3 pip 2>/dev/null; then
return 1
fi
_skip_directive package && return
if ! [ -e "$_PYTHON_VENV_DIR" ]; then
debug 'Setting up venv for local pip installs'
_run_cmd python3 -m venv "$_PYTHON_VENV_DIR"
fi
if [ "$#" -gt 0 ]; then
debug 'Installing packages with pip: %s' "$(_csv "$@")"
printf '%s\n' "$@" |
_run_cmd_xargs "$_PYTHON_VENV_DIR/bin/python3" -m pip install
fi
}
# Package manager for pipx.
# See also https://github.com/pypa/pipx
PACMANS+=(pipx)
_package_pipx_usage="Usage: package pipx [-h] PKG [PKGS...]"
read -d '' -r _package_pipx_help <<EOF
$_package_pipx_usage
Install python commands using pipx.
Positional arguments:
PKGS List of python packages providing commands to install
Optional arguments:
-h Show this help message and exit
EOF
_package_pipx() {
local OPTIND
while getopts 'h' OPTION; do
case "$OPTION" in
h) echo "$_package_pipx_help"
return 0 ;;
\?) echo "$_package_pipx_usage" >&2
return 1 ;;
esac
done
shift "$((OPTIND-1))"
if ! hash pipx 2>/dev/null; then
return 1
fi
_skip_directive package && return
if [ "$#" -gt 0 ]; then
debug 'Installing packages with pipx: %s' "$(_csv "$@")"
for spec in "$@"; do
read -a speca <<< "$spec"
_run_cmd pipx install "${speca[@]}"
done
fi
}
# Package manager the for golang module system.
# See also https://github.com/delicb/dotbot-golang
PACMANS+=(go)
_package_go() { _package_simple go go '' install '' "$@"; }
# Package manager for the node-package-manager.
# See also https://github.com/npm/cli
PACMANS+=(npm)
_package_npm() { _package_simple npm npm '' install,-g '' "$@"; }
# Package manager for yarn.
# See also https://github.com/yarnpkg/yarn
PACMANS+=(yarn)
_package_yarn() { _package_simple yarn yarn '' global,add '' "$@"; }
# Package manager the for cygwins cyg-get package manager.
PACMANS+=(cygwin)
_package_cygwin() { _package_simple cygwin cyg-get.bat '' '' '' "$@"; }
# Package manager for rubygems.
PACMANS+=(gem)
_package_gem_usage="Usage: package gem [-h] [-g] PKG [PKGS...]"
read -d '' -r _package_gem_help <<EOF
$_package_gem_usage
Install ruby gem packages.
Positional arguments:
PKGS List of packages to install
Optional arguments:
-h Show this help message and exit
-g Install PKGS globally instead of locally
EOF
_package_gem() {
local global=0
local OPTIND
while getopts 'hg' OPTION; do
case "$OPTION" in
h) echo "$_package_gem_help"
return 0 ;;
\?) echo "$_package_gem_usage" >&2
return 1 ;;
g) global=1 ;;
esac
done
shift "$((OPTIND-1))"
local flags=()
[ "$global" -eq 0 ] &&
flags+=( '--user-install' )
if ! hash gem 2>/dev/null; then
return 1
fi
_skip_directive package && return
if [ "$#" -gt 0 ]; then
debug 'Installing packages with gem: %s' "$(_csv "$@")"
printf '%s\n' "$@" |
_run_cmd_xargs gem install "${flags[@]}"
fi
}
# Package manager for the chocolatey (windows) package manager.
# See also https://chocolatey.org/
PACMANS+=(choco)
_package_choco() { _package_simple choco choco '' install,--yes '' "$@"; }
# The pacmans ヽ(^‥^=ゞ)
PACMANS+=(pacman)
_package_pacman() { _package_simple pacman pacman 'sudo' -S,--needed,--noconfirm -Sy "$@"; }
PACMANS+=(yay)
_package_yay() { _package_simple yay yay '' -S,--needed,--noconfirm -Sy "$@"; }
PACMANS+=(msys)
_package_msys() { _package_simple pacman pacman.exe '' -S,--needed,--noconfirm -Sy "$@"; }
PACMANS+=(apt)
_package_apt() { _package_simple apt apt sudo install,--yes update "$@"; }
package() {
local pacman="$1"; shift
if _contains "$pacman" "${PACMANS[@]}"; then
_package_"$pacman" "$@"
else
warn 'Unknown package manager %s' "$pacman"
return 1
fi
}
_packages_usage="Usage: packages [-h] INSTALLER:PKG[,PKG...]"
read -d '' -r _packages_help <<EOF
$_packages_usage
A wrapper around package with a more convenient interface
Positional arguments:
INSTALLER The package manager to use to install PKGS
PKGS List of packages to install
EOF
packages() {
local OPTIND
while getopts 'h' OPTION; do
case "$OPTION" in
h) echo "$_packages_help"
return 0 ;;
\?) echo "$_packages_usage" >&2
return 1 ;;
esac
done
shift "$((OPTIND-1))"
local pacman pkgs pkgs_str
while IFS=: read -r pacman pkgs_str _; do
trace 'Checking whether package manager %s is available' "$pacman"
if package "$pacman"; then
IFS=, read -r -a pkgs <<< "$pkgs_str"
package "$pacman" "${pkgs[@]}"
return "$?"
fi
done <<< "$(printf '%s\n' "$@")"
error 'No suitable package manager found, tried %s' "$(printf '%s\n' "$@" | cut -d : -f1 | tr '\n' ',')"
# Disregard error code when we weren't meant to install anything anyways.
_skip_directive package && return
return 1
}
PACKAGEX_FILE="$DOTFILES/setup/packages.csv"
_packagex_installed_p() { # (package, predicate)
local package="$1" pred="$2"
trace 'Checking if package=%s is already installed with pred=%s' "$package" "$pred"
if [ -z "$2" ]; then
trace 'No package installed predicate exists for package=%s' "$package"
return 1
fi
local result=0
local pred_type=
local pred_arg=
IFS=: read -r pred_type pred_arg <<< "$pred"
case "$pred_type" in
which*)
hash "$pred_arg" 2>/dev/null
;;
stat*)
[ -e "$pred_arg" ]
;;
python-module*)
[ -e "$_PYTHON_VENV_DIR" ] &&
"$_PYTHON_VENV_DIR/bin/python3" -c '
import sys
import importlib.util
sys.exit(0 if importlib.util.find_spec(sys.argv[2]) is not None else 1)' -- "$pred_arg" >/dev/null 2>&1
;;
*)
error 'Unsupported package installed predicate type=%s for package=%s' "$pred_type" "$package"
false
;;
esac || result=1
if [ "$result" -eq 0 ]; then
debug 'Package installed predicate is true for package=%s' "$1"
else
debug 'Package installed predicate is false for package=%s' "$1"
fi
return "$result"
}
_packagex_usage="Usage: packagex [-h] PKG[,PKG...]"
read -d '' -r _packagex_help <<EOF
$_packagex_usage
A wrapper around packages sourcing definitions from $PACKAGEX_FILE.
Positional arguments:
PKGS List of packages that have declarations in packages.csv
to install.
EOF
packagex() {
local OPTIND
while getopts 'h' OPTION; do
case "$OPTION" in
h) echo "$_packagex_help"
return 0 ;;
\?) echo "$_packagex_usage" >&2
return 1 ;;
esac
done
shift "$((OPTIND-1))"
result=0
local package
for package in "$@"; do
trace 'Searching for package spec for package=%s' "$package"
local config
if ! config=$(grep '^'"$package"',' "$PACKAGEX_FILE"); then
error 'Unable to find package spec for package=%s' "$package"
result=1
continue
fi
local package_installed_p package_install_specs
IFS=, read -r _ package_installed_p package_install_specs <<< "$config"
if _packagex_installed_p "$package" "$package_installed_p"; then
debug "Skipping installing package=%s because it is already installed" "$package"
continue
fi
local package_install_specs_array=( )
IFS=, read -r -a package_install_specs_array <<< "$package_install_specs"
if ! packages "${package_install_specs_array[@]}"; then
result=1
fi
done
return "$result"
}
# Packages }}}
# Import {{{
# ___ _
# |_ _|_ __ ___ _ __ ___ _ __| |_
# | || '_ ` _ \| '_ \ / _ \| '__| __|
# | || | | | | | |_) | (_) | | | |_
# |___|_| |_| |_| .__/ \___/|_| \__|
# |_|
BOTS=()
bots_all=0
_bots_usage="Usage: bots [-h] [-a] BOT [BOTS...]"
read -d '' -r _bots_help <<EOF
$_bots_usage
Assert whether installing any of the bots in BOTS.
Positional arguments:
BOT
Name of bot that can be installed.
Optional arguments:
-a
Assert whether all of the bots in BOTS is being installed, not
any.
EOF
bots() {
local OPTIND all=0