-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsetup-util
executable file
·3848 lines (3500 loc) · 106 KB
/
setup-util
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
# @todo
# consider something like
# pkg=($(get-first-array "${PAMAC[@]}" -- "${AUR[@]}" -- "${ARCH[@]}"))
#
# check for leftover pkgs[@]
#
# APT='apt-transport-https ca-certificates' # UBUNTU
# and all others need to be changed
function setup_util() (
source "$DOROTHY/sources/bash.bash"
__require_array 'mapfile'
source "$(type -P eval-helper)"
# when running via fresh installer environment, ensure that environment gets configured
if test -z "${DOROTHY_ENVIRONMENT-}"; then
source "$DOROTHY/sources/environment.sh"
fi
# helpers
function maybe_echo_style {
if test "$option_quiet" != 'yes'; then
echo-style "$@"
fi
}
# prefer precompiled with autoupdate with extras
# extras = man pagages, autocompletes, etc
local installers=(
# +precompiled +compiling +autoupdate +extras
brew
cask
# +precompiled +compiling +autoupdate +extras
apt # debian package manager (includes DEB, successor to aptitude)
apk # alpine package manager (includes APK)
zypper # opensuse package manager (includes RPM, supports YaST2)
dnf # fedora package manager (includes RPM, successor to yum)
yum # fedora package manager (includes RPM, predecessor to dnf)
# +precompiled[containers] +autoupdate +extras
snap # dependencies are within the snap
flatpak # dependencies are their own flatpaks, but everything is precompiled
# +precompiled[first-class AUR support] +autoupdate +extras
pamac # manjaro package manager (includes AUR): GUI+CLI, vala, wraps libalpm, snap, flatpak: https://gitlab.manjaro.org/applications/pamac/
pacman # arch package manager (includes AUR): CLI, c, wraps libalpm: https://wiki.archlinux.org/title/pacman
# +precompiled[third-party AUR wrappers] +autoupdate +extras
# https://wiki.archlinux.org/title/AUR_helpers
# https://wiki.manjaro.org/index.php?title=Pamac
# https://wiki.manjaro.org/index.php/Arch_User_Repository
# https://itsfoss.com/paru-aur-helper/
# https://itsfoss.com/best-aur-helpers/
yay # GUI, go, wraps pacman: https://github.com/Jguer/yay
paru # CLI, rust, wraps pacman: https://github.com/Morganamilo/paru
pakku # CLI, nim, wraps pacman: https://github.com/kitsunyan/pakku
aurutils # CLI, shell, wraps pacman: https://github.com/AladW/aurutils
# +precompiled -autoupdate +extras
dpkg # prefer apt wrapper
rpm # prefer dnf wrapper, note this is also available on opensuse however zypper is preferred there
# +precompiled -autoupdate -extras
download # remote download from url
# +compiling +autoupdate
cargo # prefer over go
go
# +jit +autoupdate
npm # prefer over python and ruby
gem # prefer over python
pipx # prefer over pip
pip
# +precompiled +autoupdate
mas
# windows, unknown features
scoop # installs exes to consistent path location
winget # installs apps to windows, installs exes to windows, paths inherited but custom
choco # choco/chocolatey/cinst, interferes with WSL, so should be last preference on windows
# solaris/bsd, unknown features
pkgin
pkgadd
pkg
# unknown features
cabal
conda
emerge
eopkg
guix
nix
port
xbps
urpmi
# disable/removed as no documentation
# pkgutil
# pkgman
# huber
# eget
# stack
# kiss
# these just trigger one of the above with special wrapping
tea # +precompiled -autoupdate -extras +experimental
installer # local or remote installer
'eval' # run a function or another script
generic # generic uninstaller, for cleaning up leftovers
)
# =====================================
# Arguments
local invocation="$0 $*"
function help {
# get content of this file between <installer-options> and </installer-options>
if __command_exists -- sd; then
installer_options="$(sd --flags=s '.+# <installer-options>\s+|\s+# </installer-options>.+|\t|local ' '' <"${BASH_SOURCE[0]}")"
else
installer_options="$(echo-style --notice='[sd] is required to display the installer option help, install with [setup-util-sd]')"
fi
# output the help
cat <<-EOF >/dev/stderr
ABOUT:
Install a utility with support for several package managers.
USAGE:
setup-util [...options]
OPTIONS:
--order=[...installers]
Used to override the preferred order of installers, space separated.
--installer=<installer> -- [...packages]
Install all packages with this installer.
--cli=<cli>
Used to check if the utility is already installed, in this case, a CLI executable.
--app=<app>
Used to check if the utility is already installed, in this case, an application.
--font=<font>
Used to check if the utility is already installed, in this case, a font.
--name=<name>
The name to use for the utility in human readable output, such as log messages.
--no-xdg
If truthy, place the binary in /usr/local/bin and symlink the XDG binary to it.
--sudo
When testing the <cli>, use sudo.
--confirm
Confirm installation of the utility before installing it.
--optional
If truthy, do not return a failure code if the action was unable to be performed.
--no-fallback
If truthy, do not install a package system in order to install the package.
--force
If truthy, use the installer's force mode if supported.
--upgrade
If omitted, do not reinstall if already installed.
Not all installers support this.
--uninstall | --action=uninstall
If truthy, if the utility is already installed then uninstall it.
Not all installers support this.
--quiet
If unspecified, outputs only useful things.
If truthy, don't output anything that isn't an error message.
If falsey, output everything.
--check | --check=<command>
If used, then just check if an action is requried to be performed.
Returns [0] if quiet and no action is necessary.
Returns [1] if an action is necessary.
--installed | --uninstalled
If used, just check if an installation or uninstallation is necessary.
Returns [0] if quiet and no action is necessary.
Returns [1] if an action is necessary.
... uppercase arguments are options for our installers.
INSTALLERS:
The following installers are supported, in this default order of preference:
$(echo-lines --indent=' ' -- "${installers[@]}")
INSTALLER OPTIONS:
[=()] indicates the option can be specified multiple times, e.g. [APT=one APT=two].
[=''] indicates the option can be specified only once.
$(echo-lines --indent=' ' <<<"$installer_options")
EXAMPLE:
setup-util --name='bottom' --cli='btm' --order='brew cargo aur' \\
AUR='bottom-bin' \\
BREW='bottom' \\
CARGO='bottom'
EOF
if test "$#" -ne 0; then
echo-error "$@" $'\nFailed invocation: ' --code="$invocation"
fi
return 22 # EINVAL 22 Invalid argument
}
# <installer-options>
local APK=() APK_REPO=''
local APT=() APT_REPO='' APT_KEY='' APT_ID=''
local AUR=()
local AURUTILS=()
local BREW=() BREW_TAP=()
local BSD=()
local CABAL=()
local CARGO=() RUST=()
local CASK=() CASK_TAP=()
local CHOCO=() CHOCOLATEY=() CINST=()
local CONDA=() CONDA_CHANNEL=''
local DNF=() DNF_GROUP=() DNF_COPR=() DNF_REPO=()
local DOWNLOAD='' DOWNLOAD_FILENAME='' DOWNLOAD_ARCHIVE_FORMAT='' DOWNLOAD_ARCHIVE_GLOB='' DOWNLOAD_BUILD_INSTALL='' DOWNLOAD_BUILD_UNINSTALL='' DOWNLOAD_BUILD_GLOB='' DOWNLOAD_BEARER_TOKEN='' DOWNLOAD_TARGET_PATH=''
local DPKG=() DEB=()
local EMERGE=() EMERGE_REPO=()
local EOPKG=()
local EVAL=() EVAL_INSTALL=() EVAL_UNINSTALL=() EVAL_UPGRADE=()
local FLATPAK=() FLATPAK_REPO=()
local GEM=() RUBY=()
local GO=()
local GUIX=()
local INSTALLER='' INSTALLER_FILENAME='' INSTALLER_OPEN=''
local MAS=()
local NIX=() NIX_PROFILE=()
local NPM=() NODE=()
local PACMAN=()
local PAKKU=()
local PAMAC=()
local PARU=()
local PIPX=() PIP=() PYTHON=()
local PKG=()
local PKGADD=()
local PKGIN=()
local PORT=()
local RPM=()
local SCOOP=()
local SNAP=() SNAP_CHANNEL=''
local TEA=()
local URPMI=()
local WINGET=()
local XBPS=()
local YAY=()
local YUM=() YUM_REPO=()
local ZYPPER=() ZYPPER_TYPE='' ZYPPER_REPO='' ZYPPER_REPO_ALIAS=''
# </installer-options>
# process
local item option_quiet='' option_action='install' option_check='' option_installer='' option_app='' option_cli='' option_font='' option_name='' option_xdg='yes' option_sudo='no' option_confirm='no' option_optional='no' option_fallback='yes' option_upgrade='no' option_force='no' option_order=()
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--no-verbose'* | '--verbose'*)
option_quiet="$(get-flag-value --non-affirmative --fallback="$option_quiet" -- "$item")"
;;
'--no-quiet'* | '--quiet'*)
option_quiet="$(get-flag-value --affirmative --fallback="$option_quiet" -- "$item")"
;;
'--install') option_action='install' ;;
'--uninstall') option_action='uninstall' ;;
'--check='*) option_check="${item#*=}" ;;
'--check') option_check='yes' ;;
'--installed')
option_check='yes'
option_action='install'
;;
'--uninstalled')
option_check='yes'
option_action='uninstall'
;;
'--no-check') option_check='no' ;;
'--action='*) option_action="${item#*=}" ;;
'--order='*) mapfile -t option_order < <(echo-split ' ' -- "${item#*=}") ;;
'--installer='*) option_installer="${item#*=}" ;;
'--app='*) option_app="${item#*=}" ;;
'--cli='*) option_cli="${item#*=}" ;;
'--font='*) option_font="${item#*=}" ;;
'--name='*) option_name="${item#*=}" ;;
'--no-xdg'* | '--xdg'*)
option_xdg="$(get-flag-value --affirmative --fallback="$option_xdg" -- "$item")"
;;
'--no-sudo'* | '--sudo'*)
option_sudo="$(get-flag-value --affirmative --fallback="$option_sudo" -- "$item")"
;;
'--no-confirm'* | '--confirm'*)
option_confirm="$(get-flag-value --affirmative --fallback="$option_confirm" -- "$item")"
;;
'--no-optional'* | '--optional'*)
option_optional="$(get-flag-value --affirmative --fallback="$option_optional" -- "$item")"
;;
'--no-fallback'* | '--fallback'*)
option_fallback="$(get-flag-value --affirmative --fallback="$option_fallback" -- "$item")"
;;
'--no-force'* | '--force'*)
option_force="$(get-flag-value --affirmative --fallback="$option_force" -- "$item")"
;;
'--no-upgrade'* | '--upgrade'*)
option_upgrade="$(get-flag-value --affirmative --fallback="$option_upgrade" -- "$item")"
;;
'--')
if test -z "$option_installer"; then
help '--installer=<installer> is required when using: -- [...packages]'
fi
if test -z "$option_name"; then
option_name="${option_installer}:$*"
fi
if test "$#" -eq 0; then
return 0 # no packages to install
fi
# convert each argument into its X=Y equivalent
local new_args=()
for item in "$@"; do
new_args+=("${option_installer}=${item}")
done
set -- "${new_args[@]}"
continue
;;
# installer options
'APK_REPO='*) APK_REPO="${item#*=}" ;;
'APK='* | 'ALPINE='*) APK+=("${item#*=}") ;;
'APPIMAGE='*) APPIMAGE+=("${item#*=}") ;;
'APT_ID='*) APT_ID="${item#*=}" ;;
'APT_KEY='*) APT_KEY="${item#*=}" ;;
'APT_REPO='*) APT_REPO="${item#*=}" ;;
'APT='* | 'UBUNTU='*) APT+=("${item#*=}") ;;
'AUR='* | 'ARCH='*) AUR+=("${item#*=}") ;;
'AURUTILS='*) AURUTILS+=("${item#*=}") ;;
'BREW_TAP='*) BREW_TAP+=("${item#*=}") ;;
'BREW='*) BREW+=("${item#*=}") ;;
'BSD='*) BSD+=("${item#*=}") ;;
'CABAL='*) CABAL+=("${item#*=}") ;;
'CARGO='*) CARGO+=("${item#*=}") ;;
'CASK_TAP='*) CASK_TAP+=("${item#*=}") ;;
'CASK='*) CASK+=("${item#*=}") ;;
'CHOCO='*) CHOCO+=("${item#*=}") ;;
'CHOCOLATEY='*) CHOCOLATEY+=("${item#*=}") ;;
'CINST='*) CINST+=("${item#*=}") ;;
'CONDA_CHANNEL='*) CONDA_CHANNEL="${item#*=}" ;;
'CONDA='*) CONDA+=("${item#*=}") ;;
'DEB='* | 'DEBIAN='*) DEB+=("${item#*=}") ;;
'DNF_COPR='*) DNF_COPR+=("${item#*=}") ;;
'DNF_GROUP='*) DNF_GROUP+=("${item#*=}") ;;
'DNF_REPO='*) DNF_REPO+=("${item#*=}") ;;
'DNF='* | 'FEDORA='*) DNF+=("${item#*=}") ;;
'DOWNLOAD_ARCHIVE_FORMAT='* | 'DOWNLOAD_UNZIP_FORMAT='*) DOWNLOAD_ARCHIVE_FORMAT="${item#*=}" ;;
'DOWNLOAD_ARCHIVE_GLOB='* | 'DOWNLOAD_ARCHIVE_FILTER='* | 'DOWNLOAD_UNZIP_GLOB='* | 'DOWNLOAD_UNZIP_FILTER='*) DOWNLOAD_ARCHIVE_GLOB="${item#*=}" ;;
'DOWNLOAD_BEARER_TOKEN='*) DOWNLOAD_BEARER_TOKEN="${item#*=}" ;;
'DOWNLOAD_BUILD_INSTALL='* | 'DOWNLOAD_BUILD_EVAL='*) DOWNLOAD_BUILD_INSTALL="${item#*=}" ;;
'DOWNLOAD_BUILD_UNINSTALL='*) DOWNLOAD_BUILD_UNINSTALL="${item#*=}" ;;
'DOWNLOAD_BUILD_GLOB='* | 'DOWNLOAD_BUILD_FILTER='*) DOWNLOAD_BUILD_GLOB="${item#*=}" ;;
'DOWNLOAD_FILENAME='*) DOWNLOAD_FILENAME="${item#*=}" ;;
'DOWNLOAD_TARGET_PATH='*) DOWNLOAD_TARGET_PATH="${item#*=}" ;;
'DOWNLOAD='*) DOWNLOAD="${item#*=}" ;;
'DPKG='*) DPKG+=("${item#*=}") ;;
'EMERGE_REPO='*) EMERGE+=("${item#*=}") ;;
'EMERGE='* | 'GENTOO='*) EMERGE+=("${item#*=}") ;;
'EOPKG='* | 'SOLUS='*) EOPKG+=("${item#*=}") ;;
'EVAL_INSTALL='*) EVAL_INSTALL+=("${item#*=}") ;;
'EVAL_UNINSTALL='*) EVAL_UNINSTALL+=("${item#*=}") ;;
'EVAL_UPGRADE='*) EVAL_UPGRADE+=("${item#*=}") ;;
'EVAL='*) EVAL+=("${item#*=}") ;;
'FLATPAK_REPO='*) FLATPAK_REPO+=("${item#*=}") ;;
'FLATPAK='*) FLATPAK+=("${item#*=}") ;;
'GEM='*) GEM+=("${item#*=}") ;;
'GO='*) GO+=("${item#*=}") ;;
'GUIX='*) GUIX+=("${item#*=}") ;;
'INSTALLER_FILENAME='*) INSTALLER_FILENAME="${item#*=}" ;;
'INSTALLER_OPEN='*) INSTALLER_OPEN="${item#*=}" ;;
'INSTALLER='*) INSTALLER="${item#*=}" ;;
'MAS='*) MAS+=("${item#*=}") ;;
'NIX_PROFILE='*) NIX_PROFILE+=("${item#*=}") ;;
'NIX='*) NIX+=("${item#*=}") ;;
'NODE='*) NODE+=("${item#*=}") ;;
'NPM='*) NPM+=("${item#*=}") ;;
'PACMAN='*) PACMAN+=("${item#*=}") ;;
'PAKKU='*) PAKKU+=("${item#*=}") ;;
'PAMAC='*) PAMAC+=("${item#*=}") ;;
'PARU='*) PARU+=("${item#*=}") ;;
'PIP='*) PIP+=("${item#*=}") ;;
'PIPX='*) PIPX+=("${item#*=}") ;;
'PKG='*) PKG+=("${item#*=}") ;;
'PKGADD='*) PKGADD+=("${item#*=}") ;;
'PKGIN='*) PKGIN+=("${item#*=}") ;;
'PORT='* | 'MACPORTS='*) PORT+=("${item#*=}") ;;
'PYTHON='*) PYTHON+=("${item#*=}") ;;
'RPM='*) RPM+=("${item#*=}") ;;
'RUBY='*) RUBY+=("${item#*=}") ;;
'RUST='*) RUST+=("${item#*=}") ;;
'SCOOP='*) SCOOP+=("${item#*=}") ;;
'SNAP_CHANNEL='*) SNAP_CHANNEL="${item#*=}" ;;
'SNAP='*) SNAP+=("${item#*=}") ;;
'TEA='*) TEA+=("${item#*=}") ;;
'URPMI='* | 'MAGEIA='*) URPMI+=("${item#*=}") ;;
'WINGET='*) WINGET+=("${item#*=}") ;;
'XBPS='* | 'VOID='*) XBPS+=("${item#*=}") ;;
'YAY='*) YAY+=("${item#*=}") ;;
'YUM_REPO='*) YUM_REPO+=("${item#*=}") ;;
'YUM='*) YUM+=("${item#*=}") ;;
'ZYPPER_REPO_ALIAS='*) ZYPPER_REPO_ALIAS="${item#*=}" ;;
'ZYPPER_REPO='*) ZYPPER_REPO="${item#*=}" ;;
'ZYPPER_TYPE='*) ZYPPER_TYPE="${item#*=}" ;;
'ZYPPER='* | 'OPENSUSE='* | 'SUSE='*) ZYPPER+=("${item#*=}") ;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) help "An unrecognised flag was provided: $item" ;;
esac
done
# ensure action
if test -z "$option_action"; then
option_action='install'
fi
if ! [[ $option_action =~ ^(install|uninstall|upgrade)$ ]]; then
help 'Invalid <action>, must be [install], [uninstall], or [upgrade].'
fi
# if uninstall, disable upgrade
if test "$option_action" = 'uninstall'; then
option_upgrade='no'
fi
# ensure name fallback
if test -z "$option_name" -a -n "$option_app"; then
option_name="$option_app"
fi
if test -z "$option_name" -a -n "$option_cli"; then
option_name="$option_cli"
fi
if test -z "$option_name" -a -n "$option_font"; then
option_name="$option_font"
fi
if test -z "$option_name" -a -n "$option_installer"; then
option_name="$option_installer"
fi
# check if exists
local check_works='' app_exists='' cli_exists='' cli_working='' font_exists=''
if test -n "$option_check" -a "$option_check" != 'yes' -a "$option_check" != 'no'; then
if command "$option_check"; then
if test -z "$option_name"; then
option_name="$option_check"
fi
check_works='yes'
else
check_works='no'
fi
else
if test -n "$option_app"; then
if get-app --quiet -- "$option_app"; then
app_exists='yes'
else
app_exists='no'
fi
fi
if test -n "$option_cli"; then
if __command_exists -- "$option_cli"; then
cli_exists='yes'
if command-working --sudo="$option_sudo" -- "$option_cli"; then
cli_working='yes'
else
cli_working='no'
fi
else
cli_exists='no'
cli_working='no'
fi
fi
if test -n "$option_font"; then
if get-font --quiet -- "$option_font"; then
font_exists='yes'
else
font_exists='no'
fi
fi
fi
# check only
if test -n "$option_check" -a "$option_check" != 'no'; then
if test "$option_quiet" = 'yes' -a "$option_action" = 'install'; then
if test "$check_works" = 'yes' -o "$app_exists" = 'yes' -o "$cli_working" = 'yes' -o "$font_exists" = 'yes'; then
return 0
fi
fi
return 1
fi
# ensure name
if test -z "$option_name"; then
help 'Either <name>, <cli>, <app>, <font>, <installer>, <check> must be provided.'
fi
# prep terminal vars
local terminal_device_file use_alt_screen_buffer alternative_screen_buffer default_screen_buffer
terminal_device_file="$(get-terminal-device-file)"
if get-terminal-alternative-support --quiet && test "$option_quiet" != 'no'; then
use_alt_screen_buffer='yes'
alternative_screen_buffer="$(echo-style --no-trail --alternative-screen-buffer)"
default_screen_buffer="$(echo-style --no-trail --default-screen-buffer)"
else
use_alt_screen_buffer='no'
alternative_screen_buffer=''
default_screen_buffer=''
fi
# check for action (--check=yes/command would have already exited, as it would have above)
if test -n "$option_app" -o -n "$option_cli" -o -n "$option_font"; then
if test "$app_exists" != 'yes' -a "$cli_exists" != 'yes' -a "$font_exists" != 'yes'; then
# nothing installed
if test "$option_action" = 'uninstall'; then
# already uninstalled, so no need to uninstall
maybe_echo_style --good3="The [$option_name] utility was not found. Already uninstalled. ✅" >"$terminal_device_file"
# exit
return 0
fi
option_upgrade='no' # note that we aren't upgrading, which is used for logging
# perform install
maybe_echo_style --header3="The [$option_name] utility was not found. Installing automatically... ⏲" >"$terminal_device_file"
elif test "$option_action" = 'uninstall'; then
# exists, uninstall enabled
option_upgrade='no' # note that we aren't upgrading, which is used for logging
# perform uninstall
maybe_echo_style --header3="The [$option_name] utility is marked for uninstall. Uninstalling... ⏲" >"$terminal_device_file"
elif test "$option_upgrade" = 'yes'; then
# exists, upgrade enabled
# perform upgrade
maybe_echo_style --header3="The [$option_name] utility is marked for upgrade. Upgrading... ⏲" >"$terminal_device_file"
elif test "$cli_working" = 'fail'; then
# exists, not working
# perform reinstall
maybe_echo_style --header3="The [$option_name] utility via [$option_cli] is misbehaving. Reinstalling automatically... ⏲" >"$terminal_device_file"
elif test "$option_check" = 'no'; then
# exists, don't care, proceed anyway
# used by setup-util-nerd-fonts to ensure we check for the correct font
: # do nothing
else
# exists, no upgrade, is working
# already installed, so no need to install again
maybe_echo_style --good3="The [$option_name] utility is already installed. ✅" >"$terminal_device_file"
return 0
fi
fi
# ensure order
if test "${#option_order[@]}" -eq 0; then
option_order=("${installers[@]}")
else
# custom order, handle ...
# so [snap ...] means snap first, then everything else, whereas [... apt] means everyting then apt
# as such, the specified installers need to be trimmed from the ... substitution
# @todo this will currently hard fail if there is two ... occurences
local temp_order installer inject_installer
temp_order=()
for installer in "${option_order[@]}"; do
if test "$installer" = '...'; then
for inject_installer in "${installers[@]}"; do
if is-needle --needle="$inject_installer" -- "${option_order[@]}"; then
continue
else
temp_order+=("$inject_installer")
fi
done
else
temp_order+=("$installer")
fi
done
option_order=("${temp_order[@]}")
fi
# =====================================
# Process
# helpers
function echo_fallback {
local installer="$1"
echo-style --notice="[$option_name] via [$invocation] was unable to be installed by your system's existing package systems, we will now attempt to install it via by installing [$installer]"
}
function get_path_from_filename {
local filename="$1"
# only place in appimage_home if it keeps the appimage extension
if [[ $filename =~ \.(ttf|otf)$ ]]; then
local font_dir
font_dir="$(get-font --dirs | echo-first-line)"
__print_lines "$font_dir/$filename"
elif [[ $filename == *'.app' ]]; then
# macos /Applications/$filename.app
__print_lines "$HOME/Applications/$filename"
elif [[ $filename == *'.appimage' ]]; then
# macos $HOME/Applications/$filename.appimage
__mkdirp "$APPIMAGE_HOME"
__print_lines "$APPIMAGE_HOME/$filename"
elif test "$option_xdg" = 'no'; then
__sudo_mkdirp '/usr/local/bin'
__print_lines "/usr/local/bin/$filename"
else
# macos $HOME/.local/bin/$filename
__mkdirp "$XDG_BIN_HOME"
__print_lines "$XDG_BIN_HOME/$filename"
fi
}
function verify_saved_path {
local path="$1"
# verify
if is-missing -- "$path"; then
echo-style --error1='Failed to install: ' --code-error1="$path" >/dev/stderr
return 1
fi
# check font
if [[ $path =~ \.(ttf|otf)$ ]] && test -f "$path"; then
return 0
else
# adjust
fs-own --permissions='+x' -- "$path"
fs-dequarantine -- "$path"
# check app or cli
if [[ $path == *'.app' ]] && test -d "$path"; then
return 0
elif command-working --sudo="$option_sudo" -- "$path"; then
return 0
fi
fi
# failure
echo-style --error1='Failed to verify: ' --code-error1="$path" >/dev/stderr
# remove so it doesn't interfere with other install methods and other processes
rm_helper "$path"
# return failure
return 1
}
function rm_helper {
# prefer moving to trash
# because trash allows invocations to continue
# otherwise active invocations will crash (such as setup-util-bash uninstalling the bash it was invoked with)
if test "$#" -ne 0; then
fs-rm --quiet --no-confirm --trash --sudo="$option_sudo" -- "$@"
fi
}
function rm_helper_confirm {
local selection=()
mapfile -t selection < <(
choose --linger --confirm --multi --question='Which files to remove?' -- "$@"
)
if test "$#" -ne 0; then
rm_helper -- "${selection[@]}"
fi
}
# prepare fallbacks, for when the package could be installed, but requires the package system to sshe installed first
# which will be used, if none of the package systems were present, as such proceed in order of preference for installation of package systems
local fallbacks=()
# for upgrade only, remove existing bin if it exists, as we don't want conflicts if it is deprecated
# do not do this for uninstall, as we want the actual methods to handle that
if test "$option_upgrade" = 'yes'; then
if test -n "$option_cli"; then
rm_helper "$XDG_BIN_HOME/$option_cli" # "/usr/local/bin/$option_cli"
fi
fi
# confirm if necessary
if test "$option_confirm" = 'yes'; then
if ! confirm --linger --positive --ppid=$$ -- "Do you want to setup the [$option_name] utility?"; then
# we don't want to modify it
if test "$option_optional" = 'yes'; then
# it was optional, so success case
return 0
else
# it was not optional, so abort
return 125 # ECANCELED 125 Operation cancelled
fi
fi
fi
# =====================================
# Ecosystem Installers, groups sorted alphabetically, then installers sorted alphabetically
# -------------------------------------
# Alpine
# apk / Alpine Linux
# https://wiki.alpinelinux.org/wiki/Package_management
# https://pkgs.alpinelinux.org/packages
function __are_any_of_these_apk_packages_installed {
local package packages=("$@")
# returns 1 if any are missin
for package in "${packages[@]}"; do
if apk info "$package" &>/dev/null; then
return 0
fi
done
return 1
}
function do_apk {
local args=() packages=() repo="$APK_REPO"
if test "${#APK[@]}" -ne 0; then
packages+=("${APK[@]}")
fi
# checks
if test "${#packages[@]}" -eq 0; then
return 200 # ECUSTOM 200 Not applicable to this utility
fi
if __command_missing -- apk; then
return 200 # ECUSTOM 200 Not applicable for this package system
fi
# args
if test "$option_action" = 'uninstall'; then
if ! __are_any_of_these_apk_packages_installed "${packages[@]}"; then
return 200 # ECUSTOM 200 Not applicable to this uninstallation
fi
args+=(
'apk'
'del'
)
else
# install / upgrade
args+=(
'apk'
'add'
)
fi
args+=('--update-cache')
if test -n "$repo"; then
args+=(
'--repository'
"$repo"
)
fi
# packages
sudo-helper -- "${args[@]}" "${packages[@]}"
}
# -------------------------------------
# Anaconda
# conda / Anaconda
# https://docs.anaconda.com/anaconda/install/index.html
# https://docs.conda.io/projects/conda/en/latest/commands.html
# https://docs.conda.io/projects/conda/en/latest/commands/install.html
# https://docs.conda.io/projects/conda/en/latest/commands/remove.html
# https://docs.conda.io/projects/conda/en/latest/commands/update.html
function do_conda {
local args=() packages=() channel="$CONDA_CHANNEL"
if test "${#CONDA[@]}" -ne 0; then
packages+=("${CONDA[@]}")
fi
# checks
if test "${#packages[@]}" -eq 0; then
return 200 # ECUSTOM 200 Not applicable to this utility
fi
if __command_missing -- conda; then
return 200 # ECUSTOM 200 Not applicable for this package system
fi
# args
if test "$option_action" = 'uninstall'; then
# @todo figure out installed check
args+=(
'conda'
'remove'
)
elif test "$option_upgrade" = 'yes'; then
args+=(
'conda'
'update'
)
else
args+=(
'conda'
'install'
)
fi
if test -n "$channel"; then
args+=(
'--channel'
"$channel"
)
fi
# packages
sudo-helper -- "${args[@]}" "${packages[@]}"
}
# -------------------------------------
# AUR, Arch Linux
# To determine which aur handler to use
# Search https://archlinux.org/packages/
# If it appears, use AUR
# If it does not appear, use PAMAC as it is a manjaro repo
# aurutils / AUR / Arch Linux
# https://itsfoss.com/best-aur-helpers/
# https://github.com/AladW/aurutils
function do_aurutils {
local args=() packages=() package
if test "${#AURUTILS[@]}" -ne 0; then
packages+=("${AURUTILS[@]}")
elif test "${#AUR[@]}" -ne 0; then
packages+=("${AUR[@]}")
fi
# checks
if test "${#packages[@]}" -eq 0; then
return 200 # ECUSTOM 200 Not applicable to this utility
fi
if __command_missing -- aurutils; then
return 200 # ECUSTOM 200 Not applicable for this package system
fi
# args
if test "$option_action" = 'uninstall'; then
return 1 # unsupported
else
# install / upgrade
args+=(
'aurutils'
'-Sy'
)
fi
# packages
for package in "${packages[@]}"; do
sudo-helper -- "${args[@]}" "$package"
done
}
# pacman / Arch Linux / Manjaro
function __are_any_of_these_pacman_packages_installed {
local package packages=("$@")
# returns 1 if any are missing
for package in "${packages[@]}"; do
if pacman -Q "$package" &>/dev/null; then
return 0
fi
done
return 1
}
function do_pacman {
local opts=() args=() packages=()
if test "${#PACMAN[@]}" -ne 0; then
packages=("${PACMAN[@]}")
elif test "${#AUR[@]}" -ne 0; then
packages=("${AUR[@]}")
fi
# checks
if test "${#packages[@]}" -eq 0; then
return 200 # ECUSTOM 200 Not applicable to this utility
fi
if __command_missing -- pacman; then
return 200 # ECUSTOM 200 Not applicable for this package system
fi
# https://archlinux.org/pacman/pacman.8.html
# --noconfirm
# Bypass any and all “Are you sure?” messages. It’s not a good idea to do this unless you want to run pacman from a script.
# -q, --quiet
# Show less information for certain query operations.
# -y, --refresh
# Download fresh package databases from the server. Use twice to force a refresh even if databases are up to date.
# -S, --sync
# Synchronize packages. Packages are installed directly from the remote repositories, including all dependencies required to run the packages.
# -R, --remove
# Either a URL or file path can be specified. This is a “remove-then-add” process.
# --needed
# Do not reinstall the targets that are already up-to-date.
# -U, --upgrade
# Upgrade or add package(s) to the system and install the required dependencies from sync repositories.
# -u, --upgrades
# Restrict or filter output to packages that are out-of-date on the local system. Only package versions are used to find outdated packages; replacements are not checked here. This option works best if the sync database is refreshed using -Sy.
# init the local database
sudo-helper -- pacman-key --init
# args
if test "$option_action" = 'uninstall'; then
if ! __are_any_of_these_pacman_packages_installed "${packages[@]}"; then
return 200 # ECUSTOM 200 Not applicable to this uninstallation
fi
args+=(
'pacman'
'--noconfirm'
'--remove'
)
elif test "$option_upgrade" = 'yes'; then
args+=(
'pacman'
'--noconfirm'
'--refresh'
'--sync'
'--upgrade'
'--needed'
)
else
args+=(
'pacman'
'--noconfirm'
'--refresh'
'--sync'
'--needed'
)
fi
if test "$option_quiet" != 'no'; then
args+=('--quiet')
fi
# packages
sudo-helper -- "${args[@]}" "${packages[@]}"
}
# pakku / AUR / Arch Linux
# https://itsfoss.com/best-aur-helpers/
# https://github.com/kitsunyan/pakku
function do_pakku {
local args=() packages=() package
if test "${#PAKKU[@]}" -ne 0; then
packages+=("${PAKKU[@]}")
elif test "${#AUR[@]}" -ne 0; then
packages+=("${AUR[@]}")
fi
# checks
if test "${#packages[@]}" -eq 0; then
return 200 # ECUSTOM 200 Not applicable to this utility
fi
if __command_missing -- pakku; then
return 200 # ECUSTOM 200 Not applicable for this package system
fi
# args
if test "$option_action" = 'uninstall'; then
return 1 # unsupported
else
# install / upgrade
args+=(
'pakku'
'-Sy'
)
fi
# packages
for package in "${packages[@]}"; do
sudo-helper -- "${args[@]}" "$package"
done
}
# pamac / AUR / Arch Linux
# https://itsfoss.com/best-aur-helpers/
# https://linuxcommandlibrary.com/man/pamac
# sudo with pamac avoids gui sudo prompt
function do_pamac {
local args=() packages=()
if test "${#PAMAC[@]}" -ne 0; then
packages+=("${PAMAC[@]}")
elif test "${#AUR[@]}" -ne 0; then
packages+=("${AUR[@]}")
fi
# checks
if test "${#packages[@]}" -eq 0; then