-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pont.sh
executable file
·1551 lines (1381 loc) · 44.3 KB
/
pont.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
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
#!/bin/sh
# /$$
# | $$
# /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$
# /$$__ $$ /$$__ $$| $$__ $$|_ $$_/
# | $$ \ $$| $$ \ $$| $$ \ $$ | $$
# | $$ | $$| $$ | $$| $$ | $$ | $$ /$$
# | $$$$$$$/| $$$$$$/| $$ | $$ | $$$$/
# | $$____/ \______/ |__/ |__/ \___/
# | $$
# | $$
# |__/
#
# - The dotmodule manager
#
# Copyright (c) 2020-2024 Győri Sándor (AlexAegis) <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
C_RESET='\033[0m'
C_RED='\033[0;31m'
C_GREEN='\033[0;32m'
C_YELLOW='\033[0;33m'
C_BLUE='\033[0;34m'
C_CYAN='\033[0;36m'
# Make all the variables (except IFS) in this script available to the subshells
set -a
is_installed() {
# explicit check of wsl and systemd pairings
if [ "$1" = "systemctl" ] && [ "$wsl" ]; then
return 1
fi
command -v "$1" 2>/dev/null 1>/dev/null
}
get_home() {
# This solution returns the home folder of the original invoker of sudo
if is_installed getent; then
getent passwd "${SUDO_USER:-$USER}" | cut -d: -f6
else
# On MINGW getent is not available, but elevated privileges don't
# change the home folder either, so this should be enough
echo "$HOME"
fi
}
# Environment
user_home=$(get_home)
uname_result="$(uname -s)"
case "${uname_result}" in
Linux*) os_type="linux";;
Darwin*) os_type="mac";;
CYGWIN*|MINGW32*|MSYS*|MINGW*) os_type="windows";;
FreeBSD*) os_type="freebsd";;
*) os_type="unknown:${uname_result}"
esac
# Normalize XDG variables according to the spec (Set it only if absent)
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-"$user_home/.config"}
XDG_CACHE_HOME=${XDG_CACHE_HOME:-"$user_home/.cache"}
XDG_DATA_HOME=${XDG_DATA_HOME:-"$user_home/.local/share"}
XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-"$user_home/.cache/run"}
XDG_BIN_HOME=${XDG_BIN_HOME:-"$user_home/.local/bin"}
# Environmental config
script_location=$(
cd "${0%\/*}" || exit
pwd
)
# if it has been installed
if [ -L "$script_location/pont" ]; then
script_link_location=$(readlink "$script_location/pont")
if ! [ "${script_link_location#\/}" = "$script_link_location" ]; then
# absulte link
script_location="$script_link_location"
else
# relative link
script_location="$script_location/$script_link_location"
fi
script_location=${script_location%\/*} # cut filename
fi
## This where the packages will be stowed to. Can also be set with -t
PONT_TARGET=${PONT_TARGET:-"$user_home"}
## By default, assume this script is two levels in the dotfiles folder
DOTFILES_HOME=${DOTFILES_HOME:-"$script_location/../.."}
# TODO: Support multiple folders $IFS separated, quote them
PONT_MODULES_HOME=${PONT_MODULES_HOME:-"$DOTFILES_HOME/modules"}
PONT_PRESETS_HOME=${PONT_PRESETS_HOME:-"$DOTFILES_HOME/presets"}
PONT_TEMPLATE_HOME=${PONT_TEMPLATE_HOME:-"$DOTFILES_HOME/template"}
# Config
PONT_LOG_LEVEL=${PONT_LOG_LEVEL:-1}
PONT_CONFIG_FLAG=${PONT_CONFIG_FLAG:-0}
PONT_DRY_FLAG=${PONT_DRY_FLAG:-0}
PONT_FORCE_FLAG=${PONT_FORCE_FLAG:-0}
PONT_NO_BASE_FLAG=${PONT_NO_BASE_FLAG:-0}
PONT_ROOT_FLAG=${PONT_ROOT_FLAG:-1}
PONT_SCRIPTS_ENABLED=${PONT_SCRIPTS_ENABLED:-1}
PONT_MAKE_ENABLED=${PONT_MAKE_ENABLED:-1}
PONT_PRESET_EXTENSION=${PONT_PRESET_EXTENSION:-".preset"}
PONT_HASHFILE_NAME=${PONT_HASHFILE_NAME:-".tarhash"}
PONT_DEPRECATIONFILE_NAME=${PONT_DEPRECATIONFILE_NAME:-".deprecated"}
PONT_DEPENDENCIESFILE_NAME=${PONT_DEPENDENCIESFILE_NAME:-".dependencies"}
PONT_CONDITIONFILE_NAME=${PONT_CONDITIONFILE_NAME:-".condition"}
PONT_CLASHFILE_NAME=${PONT_CLASHFILE_NAME:-".clash"}
PONT_TAGSFILE_NAME=${PONT_TAGSFILE_NAME:-".tags"}
PONT_DEFAULT_EXPANSION_ACTION=${PONT_DEFAULT_EXPANSION_ACTION:-"action_expand_none"}
PONT_CLEAN_SYMLINKS=${PONT_CLEAN_SYMLINKS:-0}
PONT_FIX_PERMISSIONS=${PONT_FIX_PERMISSIONS:-0}
HASH_COMMAND=
if is_installed sha1sum; then
HASH_COMMAND="sha1sum"
elif is_installed shasum; then
HASH_COMMAND="shasum"
fi
if [ -z "$HASH_COMMAND" ]; then
echo 'No hasher available: sha1sum or shasum' >&2
exit 1
fi
## Precalculated environmental variables for modules
# OS
linux=$(if [ "$os_type" = "linux" ]; then echo 1; fi)
export linux
mac=$(if [ "$os_type" = "mac" ]; then echo 1; fi)
export mac
windows=$(if [ "$os_type" = "windows" ]; then echo 1; fi)
export windows
bsd=$(if [ "$os_type" = "freebsd" ]; then echo 1; fi)
export bsd
wsl=$(if grep -qEi "(Microsoft|WSL)" /proc/version \
2>/dev/null 1>/dev/null; then echo 1; fi)
export wsl
# wsl is always headless, others should be configured in pontrc
headless=$wsl
export headless
# Package manager
pacman=$(if is_installed pacman; then echo 1; fi)
export pacman
apt=$(if is_installed apt; then echo 1; fi)
export apt
xbps=$(if is_installed xbps; then echo 1; fi)
export xbps
emerge=$(if is_installed emerge; then echo 1; fi)
export emerge
# Init system
sysctl=$(if is_installed sysctl; then echo 1; fi)
export sysctl
systemctl=$(if is_installed systemctl; then echo 1; fi)
export systemctl
systemd=$systemctl # alias
export systemd
openrc=$(if is_installed rc-service; then echo 1; fi)
export openrc
# other features
if is_installed ldd; then
pam=$(if ldd /bin/su | grep -q pam; then echo 1; fi)
fi
export pam
# Distribution
# ! May not be available on some systems
distribution=$(grep "^NAME" /etc/os-release 2>/dev/null | grep -oh "=.*" | \
tr -d '="')
export distribution
# It uses if and not && because when using && a new line would
# return on false evaluation. `If` captures the output of test
alarm=$(if [ "$distribution" = 'Arch Linux ARM' ]; then echo 1; fi)
export alarm
archx86=$(if [ "$distribution" = 'Arch Linux' ]; then echo 1; fi)
export archx86
arch=$(if [ "$alarm" ] || [ "$archx86" ]; then echo 1; fi)
export arch
gentoo=$(if [ "$distribution" = 'Gentoo' ]; then echo 1; fi)
export gentoo
void=$(if [ "$distribution" = 'Void Linux' ]; then echo 1; fi)
export void
debian=$(if [ "$distribution" = 'Debian GNU/Linux' ]; then echo 1; fi)
export debian
ubuntu=$(if [ "$distribution" = 'Ubuntu' ]; then echo 1; fi)
export ubuntu
fedora=$(if [ "$distribution" = 'Fedora' ]; then echo 1; fi)
export fedora
# Config file sourcing
# shellcheck disable=SC1090
[ -e "$XDG_CONFIG_HOME/pont/pontrc" ] && . "$XDG_CONFIG_HOME/pont/pontrc"
# shellcheck disable=SC1090
[ -e "$XDG_CONFIG_HOME/pontrc" ] && . "$XDG_CONFIG_HOME/pontrc"
# shellcheck disable=SC1090
[ -e "$user_home/.pontrc" ] && . "$user_home/.pontrc"
# shellcheck disable=SC1091
[ -e "./.pontrc" ] && . "./.pontrc"
set +a
# Inner variables that are not allowed to be changed using pontrc
all_modules=
all_presets=
all_installed_modules=
all_outdated_modules=
all_deprecated_modules=
all_tags=
yank_target=
resolved=
expanded_presets=
entries_selected=
final_module_list=
expand_abstract_only=
# Newline separated list of actions. Used to preserve order of flags
execution_queue=
## Internal functions
is_deprecated() {
# TODO: Implement conditional deprecation
[ -e "$PONT_MODULES_HOME/$1/$PONT_DEPRECATIONFILE_NAME" ]
}
module_exists() {
[ ! "$all_modules" ] && get_all_modules
echo "$all_modules" | grep -x "$1"
}
get_all_modules() {
all_modules=$(find "$PONT_MODULES_HOME/" -maxdepth 1 -mindepth 1 \
-type d | sed 's|.*/||' | sort)
}
get_all_presets() {
all_presets=$(find "$PONT_PRESETS_HOME/" -mindepth 1 \
-type f -name '*.preset' | sed -e 's|.*/||' -e 's/.preset//' | sort)
}
get_all_installed_modules() {
#shellcheck disable=SC2016
all_installed_modules=$(grep -l -- "" \
"$PONT_MODULES_HOME"/**/"$PONT_HASHFILE_NAME" | \
sed -r 's_^.*/([^/]*)/[^/]*$_\1_g' | sort)
}
echo_all_outdated_modules() {
[ ! "$all_modules" ] && get_all_modules
for mod in $all_modules; do
if [ -e "$PONT_MODULES_HOME/$mod/$PONT_HASHFILE_NAME" ]; then
fresh_hash="$(do_hash "$mod")"
old_hash="$(cat "$PONT_MODULES_HOME/$mod/$PONT_HASHFILE_NAME")"
[ "$fresh_hash" != "$old_hash" ] && echo "$mod"
fi
done
}
get_all_outdated_modules() {
all_outdated_modules=$(echo_all_outdated_modules)
}
get_all_deprecated_modules() {
#shellcheck disable=SC2016
all_deprecated_modules=$(find "$PONT_MODULES_HOME"/*/ -maxdepth 1 \
-mindepth 1 -type f -name "$PONT_DEPRECATIONFILE_NAME" | \
sed -r 's_^.*/([^/]*)/[^/]*$_\1_g' | sort)
}
get_all_tags() {
all_tags=$(find "$PONT_MODULES_HOME"/*/ -maxdepth 1 -mindepth 1 \
-type f -name '.tags' -exec cat {} + | \
sed -e 's/ *#.*$//' -e '/^$/d' | sort | uniq)
}
rename_module() {
# $1 from
# $2 to
log_info "Rename module $1 to $2"
# TODO: Check if from exists, fail if not
# TODO: Check if to exists, fail if is
# TODO: Rename module folder,
# TODO: Rename all packages ending inside it
# TODO: rename all references in dependency files, presets
# TODO: expand it to tags and presets
# TODO: reinstall if it was installed
echo "Not implemented!"
}
# Unused, left here for reference
# dequeue() {
# # remove last or remove the supplied items
# if [ ! "$1" ]; then
# execution_queue=$(echo "$execution_queue" | sed '$ d')
# return
# fi
# while [ "$1" ]; do
# execution_queue=$(echo "$execution_queue" | grep -v "$1")
# shift
# done
# }
enqueue() {
log_trace "Enqueuing $*"
while [ "$1" ]; do
if [ "$execution_queue" ]; then
execution_queue="${execution_queue}${IFS:-\0}${1}"
else
execution_queue="${1}"
fi
shift
done
}
# Unused, left here for reference
# enqueue_front() {
# log_trace "Enqueuing to the front $*"
# while [ "$1" ]; do
# if [ "$execution_queue" ]; then
# execution_queue="${1}${IFS:-\0}${execution_queue}"
# else
# execution_queue="${1}"
# fi
# shift
# done
# }
# Logging, the default log level is 1 meaning only trace logs are omitted
log_trace() {
# Visible at and under log level 0
[ "${PONT_LOG_LEVEL:-1}" -le 0 ] && \
echo "${C_CYAN}[ Trace ]: $*${C_RESET}" >&2
}
log_info() {
# Visible at and under log level 1
[ "${PONT_LOG_LEVEL:-1}" -le 1 ] && \
echo "${C_BLUE}[ Info ]: $*${C_RESET}" >&2
}
log_warning() {
# Visible at and under log level 2
[ "${PONT_LOG_LEVEL:-1}" -le 2 ] && \
echo "${C_YELLOW}[ Warning ]: $*${C_RESET}" >&2
}
log_success() {
# Visible at and under log level 2, same as warning but green
[ "${PONT_LOG_LEVEL:-1}" -le 2 ] && \
echo "${C_GREEN}[ Success ]: $*${C_RESET}" >&2
}
log_error() {
# Visible at and under log level 3
[ "${PONT_LOG_LEVEL:-1}" -le 3 ] && \
echo "${C_RED}[ Error ]: $*${C_RESET}" >&2
}
show_help() {
echo "$(show_version)
-h, --help -- Print information on usage and flags then
exit
-V, --version -- Print script version then exit
-l <LOGLEVEL>,
--log <LOGLEVEL>,
--log-level <LOGLEVEL>, -- set log level, possible values are:
0, trace, TRACE
1, info, INFO
2, warning, WARNING, success, SUCCESS
3, error, ERROR
4, none, NONE
each option in a line mean the same thing
-v, --verbose -- log level 0 (trace)
-q, --quiet -- log level 3 (error)
-I, --list-installed -- List all installed modules then exit
-A, --list-modules -- List all modules then exit
-D, --list-deprecated -- List all deprecated modules then exit
-P, --list-presets -- List all presets then exit
-T, --list-tags -- List all tags then exit
-E, --list-environment -- List the config environment then exit
-L, --list-install -- List the resolved final module list then exit
-Q, --list-queue -- List the execution queue then exit
-O, --list-outdated -- List all outdated (installed but has hash
mismatch) modules then exit
-C, --toggle-clean-symlinks -- Removes broken symlinks in the target
directory. (By default it turns on the
feautre, but if it was turned on by
the environment it turns it off.)
-X, --toggle-fix-permissions -- Adds user execute permissions to all module
scripts before running them.
-p, --pull-dotfiles -- Perform git pull on the dotfiles home folder
-u, --update -- Run all scrips starting with u in the
selected modules.
-x, --execute, --install -- Run init scripts, stow configs, then run
scripts starting with a number and the
Makefile in all the selected modules.
-r, --remove -- Unstows every stow package in the selected
modules. If this flag is added twice it will
also run all scrips starting with r in the
selected modules.
-n, --expand-none -- Expands only the abstract entries in the
selection. No dependencies are resolved.
-e, --expand-seleted -- Expands the original selection (the
argument list) down to its dependencies
recursively. Use this for regular
installations.
-a, --expand-all -- Expands every module regardless of the
current selection.
-i, --expand-installed -- Expands every installed module regardless
of the current selection. Useful for batch
running update and backup scripts.
-o, --expand-outdated -- Expands every installed module regardless
of the current selection thats saved hash
is no longer matching a freshly calculated
one. Useful for batch refreshing modules
after modifying them.
-d, --dry -- Disables modifications. No stowing, no script
execution. Useful for testing flag
combinations.
-w, --wet -- Enabled modifications. Stowing, Script
execution. On by default.
-b, --skip-base -- Skip the base modules when expanding selection
(Only useful before the -e flag).
-f, --force -- Ignores hashfiles. To avoid accidentally
installing large dependency trees, this
automatically turns on --expand-none.
Expansion can be changed after.
-c --config -- Instead of the selection in the argument
list, select entries in a TUI with whiptail.
--root -- Enables root privileged script execution.
On by default.
-R, --skip-root -- Disables root privileged script execution.
-s, --scripts -- Enables script execution. On by default.
-S, --skip-scripts -- Disables script execution. Its like dry
execution but with stowing enabled.
-m, --make -- Enables Makefile execution. On by default.
-M, --skip-make -- Disables Makefile execution.
-t <DIR>, --target <DIR> -- Its value will specify PONT_TARGET for this
execution.
--scaffold, --cpt -- Instead of executing modules, the selection
now will used to scaffold modules based on a
template folder at PONT_TEMPLATE_HOME
-y <DIR>, --yank <DIR> -- will yank the selection (with none expansion
by default) to the target folder. Useful for
copying modules along with their
dependencies. (Used presets are also copied!)
-Y <DIR>, --yank-expanded <DIR> -- will yank the expanded selection
(same as -ey) to the target folder.
Some scenarios:
- Update all installed modules
pont -iu
# Expand installed modules, update them
- Reinstall all modified modules after a editing a lot of them
pont -ox
# Expand outdated modules, execute them
- Safe force. Install dependencies of the selection on demand
and then force install the selection
pont -exfx zsh
# expand, execute, force (select none), execute
"
exit 0
}
show_version() {
echo "pont version: 0.9.0" && exit 0
}
clean_symlinks() {
# recusively removes every broken symlink in a directory
# used to clean before installation and after uninstallation
# ! TODO: skip protected files, on mac it tries to delete and fail many
# ! extended file permission bits are used
find "${1-$PWD}" -type l -exec \
sh -c 'for x; do [ -e "$x" ] || rm "$x"; done' _ {} + 2>/dev/null
}
scaffold() {
# TODO scaffold
# cpt template and pont --scaffold command to create from template
# Use the remaining inputs as module folders to scaffold using cpt
# If cpt is not available then try install it with cargo first
# If no cargo is available then prompt the user to install it
log_info "Scaffolding module $1 using cpt and $PONT_TEMPLATE_HOME \
as the template"
}
# Listings
action_list_execution_queue() {
log_trace "Listing execution queue:"
echo "$execution_queue"
}
action_list_installed_modules() {
log_trace "All installed modules:"
[ ! "$all_installed_modules" ] && get_all_installed_modules
echo "$all_installed_modules"
}
action_list_deprecated() {
log_trace "All deprecated modules:"
[ ! "$all_deprecated_modules" ] && get_all_deprecated_modules
echo "$all_deprecated_modules"
}
action_list_modules() {
log_trace "All available modules:"
[ ! "$all_modules" ] && get_all_modules
echo "$all_modules"
}
action_list_presets() {
log_trace "All available presets:"
[ ! "$all_presets" ] && get_all_presets
echo "$all_presets"
}
action_list_tags() {
log_trace "All available tags:"
[ ! "$all_tags" ] && get_all_tags
echo "$all_tags"
}
action_list_outdated() {
log_trace "Listing outdated modules:"
[ ! "$all_outdated_modules" ] && get_all_outdated_modules
echo "$all_outdated_modules"
}
action_list_environment() {
log_info "All configurable variables:"
echo "PONT_DRY_FLAG=${PONT_DRY_FLAG:-0}" \
"PONT_FORCE_FLAG=${PONT_FORCE_FLAG:-0}" \
"PONT_ROOT_FLAG=${PONT_ROOT_FLAG:-1}" \
"PONT_CONFIG_FLAG=${PONT_CONFIG_FLAG:-0}" \
"PONT_PRESET_EXTENSION=$PONT_PRESET_EXTENSION" \
"PONT_HASHFILE_NAME=$PONT_HASHFILE_NAME" \
"PONT_DEPENDENCIESFILE_NAME=$PONT_DEPENDENCIESFILE_NAME" \
"PONT_CLASHFILE_NAME=$PONT_CLASHFILE_NAME" \
"PONT_TAGSFILE_NAME=$PONT_TAGSFILE_NAME" \
"PONT_DEFAULT_EXPANSION_ACTION=$PONT_DEFAULT_EXPANSION_ACTION" \
"wsl=$wsl" \
"headless=$headless" \
"pacman=$pacman" \
"emerge=$emerge" \
"apt=$apt" \
"xbps=$xbps" \
"sysctl=$sysctl" \
"systemctl=$systemctl" \
"systemd=$systemd" \
"openrc=$openrc" \
"distribution=$distribution" \
"archx86=$archx86" \
"alarm=$alarm" \
"arch=$arch" \
"gentoo=$gentoo" \
"void=$void" \
"debian=$debian" \
"ubuntu=$ubuntu" \
"fedora=$fedora" && exit 0
}
action_list_modules_to_execute() {
# Print the to-be installed modules
log_info "List modules to execute:"
echo "$final_module_list"
}
## Argument handling
expand_single_args() {
var="${1#-}" # cut off first, and only dash
while [ "$var" ]; do
next="${var#?}"
first_char="${var%"$next"}"
echo "-$first_char"
var="$next" # next
done
}
# POSIX compliant argument parser.
# This function will parse and return a separated argument list. It handles
# long arguments and checks for missing or extra values.
# it handles both whitespace and '=' separated values
# it also treats quoted parameters as one
# It does NOT check for unknown variables as there is no list of allowed args
# but those are easy to handle later
parse_args() {
# first parameter is a single string, an IFS separated list of arguments
# that should have a single value
with_parameters="$1"
shift
while [ "$1" ]; do
single_cut_with_equalparam="${1##-}"
single_cut="${single_cut_with_equalparam%%=*}" # = value cut pff
double_cut_with_equalparam="${1##--}"
double_cut="${double_cut_with_equalparam%%=*}" # = value cut pff
equalparam=${1##*=}
if [ "$equalparam" = "$1" ]; then
equalparam=''
fi
# starts with one dash but not two
if ! [ "$single_cut_with_equalparam" = "$1" ] && [ "$double_cut_with_equalparam" = "$1" ]; then
split_args=$(expand_single_args "$single_cut")
shift
if [ -n "$equalparam" ]; then
set -- "$equalparam" "$@"
fi
# shellcheck disable=SC2086
set -- $split_args "$@"
# two dash
elif ! [ "$double_cut_with_equalparam" = "$1" ]; then
shift
if [ -n "$equalparam" ]; then
set -- "$equalparam" "$@"
fi
set -- "--$double_cut" "$@"
fi
has_parameter=''
for a in $with_parameters; do
if [ "$a" = "$1" ]; then
has_parameter='1'
break
fi
done
if [ -n "$has_parameter" ]; then
if [ -z "$2" ] || ! [ "${2##-}" = "$2" ]; then
echo "$1 is missing its parameter!" >&2
exit 1
fi
fi
printf "%s\n" "$1"
shift
done
}
_args_with_params='-l
--log-level
-t
--target
-y
--yank
-Y
--yank-expanded
--rename
'
# TODO: --rename second argument is not handled
interpret_args() {
while [ "${1-\0}" != '\0' ]; do
case $1 in
-h | -\? | --help) show_help ;;
-V | --version) show_version ;;
-l | --log | --log-level)
case $2 in
'trace' | 'TRACE' | '0') PONT_LOG_LEVEL='0' ;;
'info' | 'INFO' | '1') PONT_LOG_LEVEL='1' ;;
'warning' | 'WARNING' | '2') PONT_LOG_LEVEL='2' ;;
'success' | 'SUCCESS') PONT_LOG_LEVEL='2' ;;
'error' | 'ERROR' | '3') PONT_LOG_LEVEL='3' ;;
'none' | 'NONE' | '4') PONT_LOG_LEVEL='4' ;;
*) log_error "Invalid loglevel: $2"; exit 1 ;;
esac
shift
;;
-v | --verbose) PONT_LOG_LEVEL=0 ;; # Log level trace
-q | --quiet) PONT_LOG_LEVEL=3 ;; # Log level error
-I | --list-installed) action_list_installed_modules; exit 0 ;;
-A | --list-modules) action_list_modules; exit 0 ;;
-D | --list-deprecated) action_list_deprecated; exit 0 ;;
-P | --list-presets) action_list_presets; exit 0 ;;
-T | --list-tags) action_list_tags; exit 0 ;;
-E | --list-environment) action_list_environment; exit 0 ;;
-L | --list-install) enqueue "action_list_modules_to_install" ;;
-Q | --list-queue) action_list_execution_queue; exit 0 ;;
-O | --list-outdated) action_list_outdated; exit 0 ;;
-C | --toggle-clean-symlinks)
PONT_CLEAN_SYMLINKS=$((1-PONT_CLEAN_SYMLINKS)) ;;
-X | --toggle-fix-permissions)
PONT_FIX_PERMISSIONS=$((1-PONT_FIX_PERMISSIONS)) ;;
-p | --pull-dotfiles)
enqueue "pull_dotfiles" ;;
-u | --update) enqueue "action_expand_default_if_not_yet" \
"action_update_modules" ;;
-x | --execute | --install) enqueue \
"action_expand_default_if_not_yet" "action_execute_modules" ;;
-r | --remove) # behaves differently when called multiple times
remove_count=$((${remove_count:-0} + 1))
[ ${remove_count:-0} = 1 ] && \
enqueue "action_expand_default_if_not_yet" \
"action_remove_modules" ;;
-n | --expand-none) enqueue "action_expand_none" ;;
-e | --expand-selected) enqueue "action_expand_selected" ;;
-a | --expand-all) enqueue "action_expand_all" ;;
-i | --expand-installed) enqueue "action_expand_installed" ;;
-o | --expand-outdated) enqueue "action_expand_outdated" ;;
-d | --dry) PONT_DRY_FLAG=1 ;;
-w | --wet) PONT_DRY_FLAG=0 ;;
-b | --skip-base) PONT_NO_BASE_FLAG=1 ;;
-f | --force) PONT_FORCE_FLAG=1; enqueue "action_expand_none" ;;
-c | --config) PONT_CONFIG_FLAG=1 ;;
--root) PONT_ROOT_FLAG=1 ;;
-R | --skip-root) PONT_ROOT_FLAG=0 ;;
-s | --scripts) PONT_SCRIPTS_ENABLED=1 ;;
-S | --skip-scripts) PONT_SCRIPTS_ENABLED=0 ;;
-m | --make) PONT_MAKE_ENABLED=1 ;;
-M | --skip-make) PONT_MAKE_ENABLED=0 ;;
-t | --target) # package installation target
if [ -d "$2" ]; then
PONT_TARGET="$2"
else
log_error "Invalid target: $2"; exit 1
fi
shift
;;
--scaffold | --cpt) # Ask for everything
shift
scaffold "$@"
exit 0
;;
-y | --yank)
enqueue "action_expand_default_if_not_yet" "action_yank"
if [ -d "$2" ]; then
yank_target="$2"
else
log_error "Invalid target: $2"; exit 1
fi
echo "yank_target $yank_target"
shift
;;
-Y | --yank-expanded)
enqueue "action_expand_selected" "action_yank"
if [ -d "$2" ]; then
yank_target="$2"
else
log_error "Invalid target: $2"; exit 1
fi
shift
;;
--rename)
shift
rename_module "$2" "$3"
exit 0
;;
--) ;;
-?*) log_error "Unknown option (ignored): $1";;
*) # The rest are selected modules
# TODO: Pre validate them
if [ "$1" ]; then
if [ "$entries_selected" ]; then
entries_selected="$entries_selected${IFS:-\0}$1"
else
entries_selected="$1"
fi
log_trace "Initially selected:
$entries_selected"
else
break
fi
;;
esac
shift
done
}
trim_around() {
# removes the first and last characters from every line
last_removed=${$1::-1}
echo "${last_removed:1}"
}
has_tag() {
# Returns every dotmodule that contains any of the tags
# shellcheck disable=SC2016
grep -lRxEm 1 -- "$1 ?#?.*" \
"$PONT_MODULES_HOME"/*/"$PONT_TAGSFILE_NAME" |
sed -r 's_^.*/([^/]*)/[^/]*$_\1_g'
}
in_preset() {
# returns every entry in a preset
find "$PONT_PRESETS_HOME" -mindepth 1 -name "$1$PONT_PRESET_EXTENSION" \
-type f -print0 | xargs -0 sed -e 's/ *#.*$//' -e '/^$/d'
}
get_clashes() {
if [ -f "$PONT_MODULES_HOME/$1/$PONT_CLASHFILE_NAME" ]; then
sed -e 's/ *#.*$//' -e '/^$/d' \
"$PONT_MODULES_HOME/$1/$PONT_CLASHFILE_NAME"
fi
}
get_dependencies() {
if [ -f "$PONT_MODULES_HOME/$1/$PONT_DEPENDENCIESFILE_NAME" ]; then
sed -e 's/ *#.*$//' -e '/^$/d' \
"$PONT_MODULES_HOME/$1/$PONT_DEPENDENCIESFILE_NAME"
fi
}
get_entry() {
echo "$1" | cut -d '?' -f 1 | cut -d '#' -f 1 | sed 's/ $//'
}
get_condition() {
echo "$1" | cut -d '?' -s -f 2- | cut -d '#' -f 1 | sed 's/^ //'
}
pull_dotfiles() {
log_trace "Performing git pull on DOTFILES_HOME ($DOTFILES_HOME)"
if [ -d "$DOTFILES_HOME/.git" ]; then
(
cd "$DOTFILES_HOME" || exit 1
git pull
git submodule update --init
)
else
log_error "DOTFILES_HOME ($DOTFILES_HOME) is not a git folder"
fi
}
execute_scripts_for_module() {
# 1: module name
# 2: scripts to run
# 3: sourcing setting, if set, user privileged scripts will be sourced
cd "$PONT_MODULES_HOME/$1" || exit 1
group_result=0
successful_scripts=0
for script in $2; do
result=0
if [ "${PONT_DRY_FLAG:-0}" = 0 ] && \
[ "${PONT_SCRIPTS_ENABLED:-1}" = 1 ]; then
log_trace "Running $script..."
privilege='user'
[ "$(echo "$script" | grep -o '\.' | wc -w)" -gt 1 ] && \
privilege=$(echo "$script" | cut -d '.' -f 2 | sed 's/-.*//')
if [ "$privilege" = "root" ] ||
[ "$privilege" = "sudo" ]; then
if [ "${PONT_ROOT_FLAG:-1}" = 1 ]; then
(
sudo --preserve-env="PATH" -E \
"$PONT_MODULES_HOME/$1/$script"
)
else
log_info "Skipping $script because root execution" \
"is disabled"
fi
else
if [ "$SUDO_USER" ]; then
(
sudo --preserve-env="PATH" -E \
-u "$SUDO_USER" "$PONT_MODULES_HOME/$1/$script"
)
else
if [ "$3" ]; then
set -a
# shellcheck disable=SC1090
. "$PONT_MODULES_HOME/$1/$script"
set +a
else
(
"$PONT_MODULES_HOME/$1/$script"
)
fi
fi
fi
result=$?
group_result=$((group_result + result))
if [ $result = 0 ]; then
successful_scripts=$((successful_scripts + 1))
fi
else
log_trace "Skipping $script..."
fi
done
}
do_expand_entries() {
while [ "$1" ]; do
# Extracting condition, if there is
condition="$(get_condition "$1")"
# TODO: .condition files and $HEADLESS variable !!!
log_trace "Trying to expand $(get_entry "$1")..."
[ "$condition" ] && log_trace "...with condition $condition..."
if ! eval "$condition"; then
log_info "Condition ($condition) for $1 did not met, skipping"
shift
continue
fi
log_trace "Already resolved entries are: $resolved"
if [ "$(echo "$resolved" | grep -x "$1")" = "" ]; then
if [ -z "$resolved" ]; then
resolved="$1"
else
resolved="$resolved${IFS:-\0}$1"
fi
case "$1" in
+*) # presets
# collect expanded presets in case a yank action needs it
if [ -z "$expanded_presets" ]; then
expanded_presets="$1"
else
expanded_presets="$expanded_presets${IFS:-\0}$1"
fi
# shellcheck disable=SC2046
do_expand_entries \
$(in_preset "$(get_entry "$1" | cut -c2-)")
;;
:*) # tags
# shellcheck disable=SC2046
do_expand_entries \
$(has_tag "$(get_entry "$1" | cut -c2-)")
;;
*) # modules
# shellcheck disable=SC2046
if [ "${expand_abstract_only:-0}" = 0 ]; then
do_expand_entries \
$(get_dependencies "$(get_entry "$1")")
fi
get_entry "$1"
;;
esac
log_trace "...done resolving $1"
else
log_trace "...already resolved $1"
fi
shift
done
}
expand_entries() {
final_module_list="$(do_expand_entries "$@")"
}
expand_abstract_entries() {
expand_abstract_only=1
final_module_list="$(do_expand_entries "$@")"
expand_abstract_only=
}
init_modules() {
log_info "Initializing modules $*"
while [ "$1" ]; do
init_sripts_in_module=$(find "$PONT_MODULES_HOME/$1/" \
-mindepth 1 -maxdepth 1 -type f | sed 's|.*/||' \
| grep '^i.*\..*\..*$' | sort)
execute_scripts_for_module "$1" "$init_sripts_in_module" "1"
shift
done
}
source_modules_envs() {
log_info "Sourcing modules envs $*"
while [ "$1" ]; do
env_sripts_in_module=$(find "$PONT_MODULES_HOME/$1/" \
-mindepth 1 -maxdepth 1 -type f | sed 's|.*/||' \
| grep '^e.*\..*\..*$' | sort)
log_trace "Environmental scripts in $1 are $env_sripts_in_module"
execute_scripts_for_module "$1" "$env_sripts_in_module" "1"
shift
done
}