-
Notifications
You must be signed in to change notification settings - Fork 0
/
instally
executable file
·2598 lines (2482 loc) · 86.8 KB
/
instally
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/bash
################################################################################
# " m ""# ""# #######################
# mmm m mm mmm mm#mm mmm # # m m #######################
# # #" # # " # " # # # "m m" #######################
# # # # """m # m"""# # # #m# #######################
# mm#mm # # "mmm" "mm "mm"# "mm "mm "# #######################
# m" #######################
# "" #######################
################################################################################
# Global variables #############################################################
# Data used throughout the `instally` experience.
################################################################################
# Options
VERBOSE_MODE=false;
# OS
OS_NAME=$(grep '^NAME=' /etc/os-release | cut -d= -f2 | tr -d '"');
OS_PRETTY_NAME=$(grep '^PRETTY_NAME=' /etc/os-release | cut -d= -f2 | \
tr -d '"');
OS_IS_DEBIAN_BASED=false;
OS_IS_RHEL_BASED=false;
OS_IS_SUSE_BASED=false;
# package.json
PACKAGE_JSON="$HOME/.instally/package.json";
# instally.conf
INSTALLY_CONF="$HOME/.instally/instally.conf";
# Packages
PACKAGES_INSTALLED=();
PACKAGES_INSTALLED_COUNT=0;
PACKAGES_NOT_INSTALLED=();
PACKAGES_NOT_INSTALLED_COUNT=0;
PACKAGES_ALREADY_INSTALLED=();
PACKAGES_ALREADY_INSTALLED_COUNT=0;
PACKAGES_AMBIGUOUSLY_INSTALLED=();
PACKAGES_AMBIGUOUSLY_INSTALLED_COUNT=0;
PACKAGES_PENDING=0;
PACKAGES_DONE=0;
ACTIVE_GROUP="";
# apt
APT_IS_UPDATED=false;
# npm
NODE_VERSION_MANAGER_NEWLY_INSTALLED=false;
NEWLY_INSTALLED_NODE_VERSION_MANAGER="";
# UI
GUM_CHOOSE_CURSOR="▶";
GUM_CHOOSE_CURSOR_PREFIX="·";
GUM_CHOOSE_SELECTED_PREFIX="x";
GUM_CHOOSE_UNSELECTED_PREFIX="·";
# Colors
COLOR_ACTIVE=1;
COLOR_ACCENT=6;
COLOR_SELECTED=2;
GUM_CHOOSE_CURSOR_FOREGROUND="$COLOR_ACTIVE";
GUM_CHOOSE_SELECTED_FOREGROUND="$COLOR_SELECTED";
GUM_CONFIRM_SELECTED_BACKGROUND="$COLOR_ACTIVE";
# Delimiter
DELIMITER="|";
IFS="$DELIMITER";
# Gotta source `nvm` if it's installed.
if [ -e $HOME/.nvm ]; then
source $HOME/.nvm/nvm.sh;
fi
# OS Detection #################################################################
# Functions related to detecting the OS in order to determine the default
# package manager available.
################################################################################
# Detects whether the user's OS is Debian-based,
# Assigns `OS_IS_DEBIAN_BASED` to true if so.
os_is_debian_based () {
if \
[ "$OS_NAME" = "Pop!_OS" ] || \
[ "$OS_NAME" = "Ubuntu" ] || \
[ "$OS_NAME" = "Debian GNU/Linux" ] || \
[ "$OS_NAME" = "Linux Mint" ] || \
[ "$OS_NAME" = "elementary OS" ] || \
[ "$OS_NAME" = "Zorin OS" ] || \
[ "$OS_NAME" = "MX Linux" ] || \
[ "$OS_NAME" = "Raspberry Pi OS" ] || \
[ "$OS_NAME" = "Deepin" ] || \
[ "$OS_NAME" = "ArcoLinux" ] || \
[ "$OS_NAME" = "Peppermint Linux" ] || \
[ "$OS_NAME" = "Bodhi Linux" ]; then
OS_IS_DEBIAN_BASED=true;
fi
}
# Detects whether the user's OS is RHEL-based,
# Assigns `OS_IS_RHEL_BASED` to true if so.
os_is_rhel_based () {
if \
[ "$OS_NAME" = "Fedora Linux" ] || \
[ "$OS_NAME" = "Red Hat Enterprise Linux" ] || \
[ "$OS_NAME" = "CentOS Linux" ] || \
[ "$OS_NAME" = "Oracle Linux Server" ] || \
[ "$OS_NAME" = "Amazon Linux" ] || \
[ "$OS_NAME" = "Rocky Linux" ] || \
[ "$OS_NAME" = "AlmaLinux" ] || \
[ "$OS_NAME" = "OpenMandriva Lx" ] ||\
[ "$OS_NAME" = "Mageia" ] ; then
OS_IS_RHEL_BASED=true;
fi
}
# Detects whether the user's OS is SUSE-based,
# Assigns `OS_IS_SUSE_BASED` to true if so.
os_is_suse_based () {
if \
[ "$OS_NAME" = "openSUSE" ] || \
[ "$OS_NAME" = "openSUSE Tumbleweed" ] || \
[ "$OS_NAME" = "SUSE Enterprise Linux Server" ]; then
OS_IS_SUSE_BASED=true;
fi
}
# Detects the OS's lineage so as to determine the default package manager.
check_os () {
os_is_debian_based;
os_is_rhel_based;
os_is_suse_based;
}
# Dependencies #################################################################
# Functions related to detecting and installing `instally`'s dependencies.
################################################################################
# Checks for `instally`'s dependencies, and installs whatever dependencies are
# missing.
check_dependencies () {
# Detect missing dependencies,
if ! package_is_installed curl || \
! package_is_installed gum || \
! package_is_installed jq; then
printf "Welcome to instally! You're using $OS_PRETTY_NAME.\n";
printf "We need some dependencies to get started:\n";
# Install curl:
if ! package_is_installed curl; then
print_dependency_needed "curl";
if $OS_IS_DEBIAN_BASED; then
install_package_apt curl curl;
elif $OS_IS_RHEL_BASED; then
install_package_dnf curl curl;
elif $OS_IS_SUSE_BASED; then
install_package_zypper curl curl;
fi
fi
# Install gum:
if ! package_is_installed gum; then
print_dependency_needed "gum";
install_dependency_gum;
fi
# Install jq:
if ! package_is_installed jq; then
print_dependency_needed "jq";
if $OS_IS_DEBIAN_BASED; then
install_package_apt jq jq;
elif $OS_IS_RHEL_BASED; then
if [ "$OS_NAME" = "Fedora Linux" ]; then
install_package_dnf jq jq;
else
install_dependency_jq_using_binary;
fi
elif $OS_IS_SUSE_BASED; then
install_package_zypper jq jq;
else
install_dependency_jq_using_binary;
fi
fi
fi
if package_is_installed curl && \
package_is_installed gum && \
package_is_installed jq; then
return 0;
fi
}
# Installs gum for `instally`'s interactivity.
install_dependency_gum () {
# If the OS is Debian-based, use apt to install gum;
if $OS_IS_DEBIAN_BASED; then
sudo mkdir -p /etc/apt/keyrings;
curl -fsSL https://repo.charm.sh/apt/gpg.key | \
sudo gpg --dearmor -o /etc/apt/keyrings/charm.gpg
echo "deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *" | \
sudo tee /etc/apt/sources.list.d/charm.list;
install_package_apt gum gum;
# If OS is RHEL-based,
elif $OS_IS_RHEL_BASED; then
echo "[charm]
name=Charm
baseurl=https://repo.charm.sh/yum/
enabled=1
gpgcheck=1
gpgkey=https://repo.charm.sh/yum/gpg.key" | \
sudo tee /etc/yum.repos.d/charm.repo;
# Use dnf to install gum,
if package_is_installed dnf; then
install_package_dnf gum gum;
if [ $? == 0 ]; then
return 0;
fi
# Or use yum in absence of dnf to install gum.
else
install_package_yum gum gum;
if [ $? == 0 ]; then
return 0;
fi
fi
# Otherwise, install go to install gum.
else
if ! package_is_installed go; then
print_dependency_needed "gum" "Go";
install_package_manager_go;
if [ $? == 0 ]; then
install_dependency_gum_using_go;
fi
else
install_dependency_gum_using_go;
if ! package_is_installed gum; then
printf "\n";
print_error "Go is installed, and gum also might be installed, but Go \
is not finding gum.\n Ensure your Go binaries (/go/bin) are included in your \
PATH variable below:\n\n $PATH\n\n See \
https://github.com/jelizaga/instally/#gum-is-installed-but-wont-run for help.";
exit 1;
fi
fi
fi
}
# Installs `gum` for `instally`s interactivity using go.
install_dependency_gum_using_go () {
print_installing "gum" "gum" "go";
# Get the gum tarball off the GitHub repo,
wget -qP $HOME/Downloads \
https://github.com/charmbracelet/gum/releases/download/v0.10.0/gum-0.10.0.tar.gz \
> /dev/null;
# Extract the tarball in `~/Downloads/gum`,
mkdir $HOME/Downloads/gum;
tar -zxvf $HOME/Downloads/gum-0.10.0.tar.gz -C $HOME/Downloads/gum > \
/dev/null;
cd $HOME/Downloads/gum;
# Install gum using go,
go install > /dev/null;
# Clean up `/gum` and its tarball.
rm -rf $HOME/Downloads/gum;
rm -rf $HOME/Downloads/gum/gum-0.10.0.tar.gz;
}
# Installs `jq` for `instally`'s data parsing using a downloaded binary.
install_dependency_jq_using_binary () {
print_installing "jq" "jq" "downloaded binary \
(https://stedolan.github.io/jq/download/)";
wget -qP \
$HOME/Downloads https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 \
> /dev/null;
if [ $? == 0 ]; then
if ! [ -e $HOME/.local/bin ]; then
mkdir $HOME/.local/bin;
fi
mv $HOME/Downloads/jq-linux64 $HOME/.local/bin/jq;
chmod +x $HOME/.local/bin/jq;
print_installed "jq" "downloaded binary \
(https://stedolan.github.io/jq/download/)";
fi
}
# Config #######################################################################
# Functions related to reading `instally`'s config file and setting configured
# values.
################################################################################
# Checks and applies the config file's user-customized values.
check_instally_conf () {
if [ -e $INSTALLY_CONF ]; then
CONF_PACKAGE_JSON=$(awk -F '=' '/^PACKAGE_JSON=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_PACKAGE_JSON" ]; then
PACKAGE_JSON="$CONF_PACKAGE_JSON";
fi
CONF_COLOR_SELECTED=$(awk -F '=' '/^COLOR_SELECTED=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_COLOR_SELECTED" ]; then
COLOR_SELECTED="$CONF_COLOR_SELECTED";
GUM_CHOOSE_SELECTED_FOREGROUND="$COLOR_SELECTED";
fi
CONF_COLOR_ACTIVE=$(awk -F '=' '/^COLOR_ACTIVE=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_COLOR_ACTIVE" ]; then
COLOR_ACTIVE="$CONF_COLOR_ACTIVE";
GUM_CHOOSE_CURSOR_FOREGROUND="$COLOR_ACTIVE";
GUM_CONFIRM_SELECTED_BACKGROUND="$COLOR_ACTIVE";
fi
CONF_COLOR_ACCENT=$(awk -F '=' '/^COLOR_ACCENT=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_COLOR_ACCENT" ]; then
COLOR_ACCENT="$CONF_COLOR_ACCENT";
GUM_CHOOSE_SELECTED_FOREGROUND="$COLOR_ACCENT";
fi
CONF_CURSOR=$(awk -F '=' '/^CURSOR=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_CURSOR" ]; then
GUM_CHOOSE_CURSOR="$CONF_CURSOR";
fi
CONF_CHOOSE_DEFAULT=$(awk -F '=' '/^CHOOSE_DEFAULT=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_CHOOSE_DEFAULT" ]; then
GUM_CHOOSE_CURSOR_PREFIX="$CONF_CHOOSE_DEFAULT";
GUM_CHOOSE_UNSELECTED_PREFIX="$CONF_CHOOSE_DEFAULT";
fi
CONF_CHOOSE_SELECTED=$(awk -F '=' '/^CHOOSE_SELECTED=/ {print $2}' \
"$INSTALLY_CONF");
if ! [ -z "$CONF_CHOOSE_SELECTED" ]; then
GUM_CHOOSE_SELECTED_PREFIX="$CONF_CHOOSE_SELECTED";
fi
fi
}
# Updates a field in `instally.conf`.
# Args:
# `$1` - Field to be updated.
# `$2` - Value with which to update the field.
update_instally_conf () {
local FIELD=$1;
local VALUE=$2;
if [ -e $INSTALLY_CONF ]; then
if grep -q "$FIELD" $INSTALLY_CONF; then
sed -i "s|^${FIELD}=.*|${FIELD}=${VALUE}|" $INSTALLY_CONF;
else
echo "$FIELD=$VALUE" >> $INSTALLY_CONF;
fi
else
touch $INSTALLY_CONF;
echo "$FIELD=$VALUE" >> $INSTALLY_CONF;
fi
}
# File system ##################################################################
# Functions related to `instally`'s interactions with the file system.
################################################################################
# Checks for `~/.instally` & `~/.instally/package.json`.
# Creates either if they've yet to exist.
check_package_json () {
if ! [ -e $HOME/.instally ]; then
create_instally_dir;
create_package_json;
prompt_edit_package_json;
check_package_json;
elif ! [ -e $HOME/.instally/package.json ]; then
create_package_json;
prompt_edit_package_json;
check_package_json;
elif ! [ -s $PACKAGE_JSON ]; then
print_empty "package.json" "📒";
prompt_edit_package_json;
check_package_json;
else
return 0;
fi
}
# Creates `~/.instally/package.json` and reports this action.
create_package_json () {
touch $HOME/.instally/package.json;
print_created "~/.instally/package.json" "📒";
}
# Creates `~/.instally` and reports this action.
create_instally_dir () {
mkdir $HOME/.instally;
print_created "~/.instally" "📁";
}
# Menus ########################################################################
# `instally`'s system of interactive menus and prompts.
################################################################################
# Main menu presented on start-up and at the completion of certain tasks.
menu_main () {
print_title
print_os
printf "\n"
local SELECTED=$(gum choose \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
"Install Packages" \
"Settings" \
"Quit");
print_input_selection "$SELECTED";
case $SELECTED in
"Install Packages")
menu_select_groups;
;;
"Settings")
menu_settings;
;;
"Quit")
exit 0;
;;
*)
exit 1;
;;
esac
}
# Settings menu where `instally` can be configured.
menu_settings () {
print_section_heading "Settings";
local SELECTED=$(gum choose \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
"package.json" \
"Customize Appearance" \
"Back");
print_input_selection "$SELECTED";
case $SELECTED in
"package.json")
menu_package_json;
;;
"Customize Appearance")
menu_customize_appearance;
;;
"Back")
menu_main;
;;
*)
exit 1;
esac
}
# Menu wherein the user can configure package.json-related things.
menu_package_json () {
print_section_heading "Settings / package.json";
local SELECTED=$(gum choose \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
"Edit package.json" \
"Select package.json" \
"Back");
print_input_selection "$SELECTED";
case $SELECTED in
"Edit package.json")
print_section_heading "Edit package.json";
OLD_MODIFY=$(stat -L $PACKAGE_JSON | grep "Modify");
if [ -z $EDITOR ]; then
nano $PACKAGE_JSON;
elif [ -n $EDITOR ]; then
$EDITOR $PACKAGE_JSON;
fi
sleep 2;
NEW_MODIFY=$(stat -L $PACKAGE_JSON | grep "Modify");
if [ "$OLD_MODIFY" != "$NEW_MODIFY" ]; then
printf "💾 package.json edits saved.\n";
else
printf "No package.json edits made.\n";
fi
menu_package_json;
;;
"Select package.json")
print_section_heading "Select package.json";
printf "$(gum style --italic "Enter the path to the") \
$(gum style --bold --italic "package.json") \
$(gum style --italic "file you would like to use.")\n";
printf "$(gum style --bold "Current package.json:") $PACKAGE_JSON\n";
NEW_PACKAGE_JSON=$(gum input --placeholder "/path/to/package.json/file");
if ! [ -z "$NEW_PACKAGE_JSON" ]; then
PACKAGE_JSON=$NEW_PACKAGE_JSON;
update_instally_conf "PACKAGE_JSON" "$PACKAGE_JSON";
fi
printf "$(gum style --bold "New package.json:") $PACKAGE_JSON\n";
menu_package_json;
;;
"Back")
menu_settings;
;;
*)
exit 1;
esac
}
# Menu wherein the user can choose what parts of `instally`'s appearance to
# customize.
menu_customize_appearance () {
print_section_heading "Settings / Customize Appearance";
local SELECTED=$(gum choose \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
"Customize Colors" \
"Customize Interface" \
"Back");
print_input_selection "$SELECTED";
case $SELECTED in
"Customize Colors")
menu_customize_colors;
;;
"Customize Interface")
menu_customize_interface;
;;
"Back")
menu_settings;
esac
}
# Menu wherein the user can customize `instally`'s colors.
menu_customize_colors () {
print_section_heading "Settings / Customize Appearance / Colors";
local SELECTED=$(gum choose \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
"Accent Color" \
"Active Color" \
"Selected Color" \
"Back");
print_input_selection "$SELECTED";
case $SELECTED in
"Accent Color")
print_section_heading "Accent Color";
printf "$(gum style --italic "Enter a new accent color.")\n";
printf "$(gum style --bold "Current") \
$(gum style --bold "accent color:") \
$(gum style --foreground $COLOR_ACCENT "$COLOR_ACCENT")\n";
local NEW_COLOR_ACCENT=$(gum input --placeholder "010");
if ! [ -z "$NEW_COLOR_ACCENT" ]; then
COLOR_ACCENT=$NEW_COLOR_ACCENT;
GUM_CHOOSE_SELECTED_FOREGROUND="$COLOR_ACCENT";
update_instally_conf "COLOR_ACCENT" "$COLOR_ACCENT";
fi
printf "$(gum style --bold "New") \
$(gum style --bold "accent color:") \
$(gum style --foreground $COLOR_ACCENT "$COLOR_ACCENT")\n";
menu_customize_colors;
;;
"Active Color")
print_section_heading "Active Color";
printf "$(gum style --italic "Enter a new active color.")\n";
printf "$(gum style --bold "Current") \
$(gum style --bold "active color:") \
$(gum style --foreground $COLOR_ACTIVE "$COLOR_ACTIVE")\n";
local NEW_COLOR_ACTIVE=$(gum input --placeholder "010");
if ! [ -z "$NEW_COLOR_ACTIVE" ]; then
COLOR_ACTIVE=$NEW_COLOR_ACTIVE;
GUM_CHOOSE_CURSOR_FOREGROUND="$COLOR_ACTIVE";
GUM_CONFIRM_SELECTED_BACKGROUND="$COLOR_ACTIVE";
update_instally_conf "COLOR_ACTIVE" "$COLOR_ACTIVE";
fi
printf "$(gum style --bold "New") \
$(gum style --bold "active color:") \
$(gum style --foreground $COLOR_ACTIVE "$COLOR_ACTIVE")\n";
menu_customize_colors;
;;
"Selected Color")
print_selection_heading "Selected Color";
printf "$(gum style --italic "Enter a new selected color.")\n";
printf "$(gum style --bold "Current") \
$(gum style --bold "active color:") \
$(gum style --foreground $COLOR_SELECTED "$COLOR_SELECTED")\n";
local NEW_COLOR_SELECTED=$(gum input --placeholder "010");
if ! [ -z "$NEW_COLOR_SELECTED" ]; then
COLOR_SELECTED=$NEW_COLOR_SELECTED;
GUM_CHOOSE_SELECTED_FOREGROUND="$COLOR_SELECTED";
update_instally_conf "COLOR_SELECTED" "$COLOR_SELECTED";
fi
printf "$(gum style --bold "New") \
$(gum style --bold "active color:") \
$(gum style --foreground $COLOR_SELECTED "$COLOR_SELECTED")\n";
menu_customize_colors;
;;
"Back")
menu_customize_appearance;
;;
esac
}
# Menu wherein the user can customize `instally`'s interface elements.
menu_customize_interface () {
print_section_heading "Settings / Customize Appearance / Interface";
local SELECTED=$(gum choose \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
"Cursor" \
"Default Menu Item" \
"Selected Menu Item" \
"Back");
print_input_selection "$SELECTED";
case $SELECTED in
"Cursor")
print_section_heading "Cursor";
printf "$(gum style --italic "Enter a new cursor.")\n";
printf "$(gum style --bold "Old cursor:") $GUM_CHOOSE_CURSOR\n";
NEW_CURSOR=$(gum input --placeholder ">");
if ! [ -z "$NEW_CURSOR" ]; then
GUM_CHOOSE_CURSOR=$NEW_CURSOR;
update_instally_conf "CURSOR" "$GUM_CHOOSE_CURSOR";
fi
printf "$(gum style --bold "New cursor:") $GUM_CHOOSE_CURSOR\n";
menu_customize_interface;
;;
"Default Menu Item")
print_section_heading "Default Menu Item";
printf "$(gum style --italic "Enter a new default menu item prefix.")\n";
printf "$(gum style --bold "Old default menu item prefix:") \
$GUM_CHOOSE_CURSOR_PREFIX\n";
NEW_DEFAULT_MENU_ITEM_PREFIX=$(gum input --placeholder "n");
if ! [ -z "$NEW_DEFAULT_MENU_ITEM_PREFIX" ]; then
GUM_CHOOSE_CURSOR_PREFIX=$NEW_DEFAULT_MENU_ITEM_PREFIX;
GUM_CHOOSE_UNSELECTED_PREFIX=$NEW_DEFAULT_MENU_ITEM_PREFIX;
update_instally_conf "CHOOSE_DEFAULT" "$GUM_CHOOSE_CURSOR_PREFIX";
fi
printf "$(gum style --bold "New default menu item prefix:") \
$GUM_CHOOSE_CURSOR_PREFIX\n";
menu_customize_interface;
;;
"Selected Menu Item")
print_section_heading "Selected Menu Item";
printf "$(gum style --italic "Enter a new selected menu item prefix.")\n";
printf "$(gum style --bold "Old selected menu item prefix:") \
$GUM_CHOOSE_SELECTED_PREFIX\n";
NEW_SELECTED_MENU_ITEM_PREFIX=$(gum input --placeholder "y");
if ! [ -z "$NEW_SELECTED_MENU_ITEM_PREFIX" ]; then
GUM_CHOOSE_SELECTED_PREFIX=$NEW_SELECTED_MENU_ITEM_PREFIX;
update_instally_conf "CHOOSE_SELECTED" "$GUM_CHOOSE_SELECTED_PREFIX";
fi
printf "$(gum style --bold "New unselected menu item prefix:") \
$GUM_CHOOSE_SELECTED_PREFIX\n";
menu_customize_interface;
;;
"Back")
menu_customize_appearance;
esac
}
# Menu used to select groups of packages for installation.
# Invokes `menu_package_select` after user selects (or doesn't select)
# groups of packages.
menu_select_groups () {
PACKAGES_INSTALLED_COUNT=0;
check_package_json;
local HAS_GROUPS=$(jq 'has("groups")' $PACKAGE_JSON);
if [ "$HAS_GROUPS" = "true" ]; then
local GROUPS_ARRAY=$(jq '.groups' $PACKAGE_JSON);
local GROUPS_ARRAY_LENGTH=$(echo "$GROUPS_ARRAY" | jq 'length');
local MENU_ITEMS_ARRAY=();
for (( i=0; i<$GROUPS_ARRAY_LENGTH; i++ )); do
local GROUP=$(echo "$GROUPS_ARRAY" | \
jq --argjson INDEX $i '.[$INDEX]');
local GROUP_NAME=$(echo "$GROUP" | jq -r '.group');
local GROUP_HAS_DESCRIPTION=$(echo "$GROUP" | jq -r 'has("description")');
if [ "$GROUP_HAS_DESCRIPTION" = "true" ]; then
local GROUP_DESCRIPTION=$(echo "$GROUP" | jq -r '.description');
local MENU_ITEM="$(gum style --bold "$GROUP_NAME »") $GROUP_DESCRIPTION";
MENU_ITEMS_ARRAY+=("$MENU_ITEM");
else
local MENU_ITEM="$(gum style --bold "$GROUP_NAME")";
MENU_ITEMS_ARRAY+=("$MENU_ITEM");
fi
done
print_select_groups;
SELECTED_GROUPS=$(gum choose --no-limit \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor-prefix="$GUM_CHOOSE_CURSOR_PREFIX " \
--selected-prefix="$GUM_CHOOSE_SELECTED_PREFIX " \
--unselected-prefix="$GUM_CHOOSE_UNSELECTED_PREFIX " \
"${MENU_ITEMS_ARRAY[@]}");
SELECTED_GROUPS_ARRAY=();
readarray -t SELECTED_GROUPS_ARRAY <<< "$SELECTED_GROUPS"
if [ "${#SELECTED_GROUPS_ARRAY[@]}" -eq 1 ] \
&& [[ ${SELECTED_GROUPS_ARRAY[0]} == "" ]]; then
printf "No groups selected.\n";
menu_install_packages;
else
print_input_selection $(print_list "${SELECTED_GROUPS_ARRAY[@]}");
menu_install_packages "${SELECTED_GROUPS_ARRAY[@]}";
fi
else
menu_install_packages;
fi
}
# Menu used to select packages for installation.
# Args:
# `$@` - Array of selected package groups.
menu_install_packages () {
local SELECTED_GROUPS_ARRAY=("$@");
local GROUP_NAMES=();
for GROUP in "${SELECTED_GROUPS_ARRAY[@]}"; do
local GROUP_NAME=$(print_selected_menu_item "$GROUP");
GROUP_NAMES+=("$GROUP_NAME");
done
# Get the eligible package menu items for display, given the selected package
# groups.
local MENU_ITEMS=($(get_menu_items "${GROUP_NAMES[@]}"));
IFS=$'\n';
readarray -t MENU_ITEMS_ARRAY <<< "$MENU_ITEMS";
IFS="$DELIMITER";
print_install_packages;
# User selects packages to install:
local SELECTED_PACKAGES=$(gum choose --no-limit \
--cursor.foreground="$GUM_CHOOSE_CURSOR_FOREGROUND" \
--selected.foreground="$GUM_CHOOSE_SELECTED_FOREGROUND" \
--cursor="$GUM_CHOOSE_CURSOR " \
--cursor-prefix="$GUM_CHOOSE_CURSOR_PREFIX " \
--selected-prefix="$GUM_CHOOSE_SELECTED_PREFIX " \
--unselected-prefix="$GUM_CHOOSE_UNSELECTED_PREFIX " \
"${MENU_ITEMS_ARRAY[@]}");
# Roll selected packages into an array.
local SELECTED_PACKAGES_ARRAY=();
readarray -t SELECTED_PACKAGES_ARRAY <<< "$SELECTED_PACKAGES";
# If no packages are selected, return to main menu.
if [ "${#SELECTED_PACKAGES_ARRAY[@]}" -eq 1 ] && \
[[ ${SELECTED_PACKAGES_ARRAY[0]} == "" ]]; then
printf "No packages selected.\n"
menu_main;
# Otherwise, install the selected packages.
else
print_input_selection $(print_list "${SELECTED_PACKAGES_ARRAY[@]}");
install_packages "${SELECTED_PACKAGES_ARRAY[@]}";
fi
}
# Prompts the user whether or not they'd like to install more packages.
# Returns to package group select if so,
# Returns to main menu if not.
menu_install_more_packages () {
INSTALL_MORE=$(gum confirm "Install more packages?" \
--selected.background="$GUM_CONFIRM_SELECTED_BACKGROUND");
if [ $? == 0 ]; then
menu_select_groups;
else
menu_main;
fi
}
# Prompts user whether they'd like to edit `package.json`.
# Opens `package.json` in an editor if so,
# Returns to main menu if not.
prompt_edit_package_json () {
printf "$(gum style --italic \
'To define packages for instally to install, edit') ";
printf "$(gum style --bold \
'package.json').\n";
printf "$(gum style --bold --italic 'Instructions:') ";
printf "https://github.com/jelizaga/instally/#-packagejson\n";
EDIT_PACKAGE_JSON=$(gum confirm \
"📒 Edit $(gum style --bold 'package.json')?" \
--selected.background="$GUM_CONFIRM_SELECTED_BACKGROUND");
if [ $? == 0 ]; then
# If user hasn't set their `$EDITOR`, use `nano` to edit `package.json`,
if [ -z $EDITOR ]; then
nano $PACKAGE_JSON;
# Otherwise, use their `$EDITOR` to edit `package.json`.
elif [ -n $EDITOR ]; then
$EDITOR $PACKAGE_JSON;
fi
else
menu_main;
fi
}
################################################################################
# Printing #####################################################################
################################################################################
# Functions related to printing reusable messages and text.
# Prints declaration that a package has already been installed.
# Args:
# `$1` - Name of the installed package.
print_already_installed () {
local PACKAGE_NAME=$1;
if ! package_is_installed gum; then
printf "👍 $PACKAGE_NAME is already installed.\n";
else
printf "👍 $(gum style --bold "$PACKAGE_NAME") is already installed.\n";
fi
}
# Prints a message declaring that a given package could not be installed.
# Args:
# `$1` - Name of package that could not be installed.
# `$2` - Optional reason why the package could not be installed.
print_cannot_install () {
local PACKAGE_NAME=$1;
if [ -n "$2" ]; then
local REASON=$2;
if ! package_is_installed gum; then
printf "❗ $PACKAGE_NAME could not be installed: \
$REASON\n";
else
printf "❗ $(gum style --bold "$PACKAGE_NAME") could not be installed: \
$REASON\n";
fi
else
if ! package_is_installed gum; then
printf "❗ $PACKAGE_NAME could not be installed.\n";
else
printf "❗ $(gum style --bold "$PACKAGE_NAME") could not be installed.\n";
fi
fi
}
# Prints a message declaring that a given package cannot be installed until
# the new Node.js version manager is loaded by a reloaded terminal.
# Args:
# `$1` - Name of the package that could not be installed.
print_cannot_install_new_node_version_manager () {
local PACKAGE_NAME=$1;
print_cannot_install "$PACKAGE_NAME" "\n\
$(gum style --bold "$NEWLY_INSTALLED_NODE_VERSION_MANAGER") \
was installed as a Node.js version manager,\n but $(gum style --bold "npm") \
cannot install anything until you restart the terminal.";
}
# Prints declaration that something has been created.
# Args:
# `$1` - Name of item that has been created.
# `$2` - Icon prefixing this message—an emoji or some unicode glyph.
print_created () {
local ITEM=$1;
local ICON=$2;
printf "$ICON $(gum style --bold "$ITEM") created.\n";
}
# Prints declaration that a dependency is missing.
# Args:
# `$1` - Name of the dependency required.
print_dependency_needed () {
local DEPENDENCY=$1;
if [ -n $2 ]; then
local DEPENDENCY_DEPENDENCY=$2;
if ! package_is_installed gum; then
printf "🔩 We need $DEPENDENCY_DEPENDENCY to install $DEPENDENCY.\n";
else
printf "🔩 We need $(gum style --bold "$DEPENDENCY_DEPENDENCY") \
to install $(gum style --bold "$DEPENDENCY").\n";
fi
else
if ! package_is_installed gum; then
printf "🔩 We need $DEPENDENCY.\n";
else
printf "🔩 We need $(gum style --bold "$DEPENDENCY").\n";
fi
fi
}
# Prints warning that something is empty.
# Args:
# `$1` - Name of item that is empty.
# `$2` - Icon prefixing this message—an emoji or some unicode glyph.
print_empty () {
local ITEM=$1;
if [ -n "$2" ]; then
local ICON=$2;
printf "$ICON $(gum style --bold "$ITEM") is empty.\n";
else
printf "❌ $(gum style --bold "$ITEM") is empty.\n";
fi
}
# Prints error message.
# Args:
# `$1` - Message to print.
print_error () {
local MESSAGE=$1;
if ! package_is_installed gum; then
printf "🐛 Error: $MESSAGE\n";
else
printf "🐛 $(gum style --bold 'Error:') $MESSAGE\n";
fi
}
# Prints a message declaring that a given package could not be installed due to
# its installation method missing.
# Args:
# `$1` - Name of the package that could not be installed.
# `$2` - Name of the installation method for `$1` that is missing.
print_installation_method_missing () {
local PACKAGE_NAME=$1;
local INSTALLATION_METHOD=$2;
print_cannot_install "$PACKAGE_NAME" "$INSTALLATION_METHOD is \
$PACKAGE_NAME's installation method, but $INSTALLATION_METHOD is missing.\n\
Try updating $PACKAGE_JSON with an available installation method for \
$PACKAGE_NAME.";
}
# Prompts user to select packages for installation and provides instructions.
# Associated with `menu_install_packages`.
print_install_packages () {
print_section_heading "Install Packages";
printf "$(gum style --italic 'Press ')";
printf "$(gum style --bold --foreground "$COLOR_ACTIVE" 'x')";
printf "$(gum style --italic ' to select packages to install')\n";
printf "$(gum style --italic 'press ')"
printf "$(gum style --bold --foreground "$COLOR_ACTIVE" 'a')";
printf "$(gum style --italic ' to select all')\n";
printf "$(gum style --italic 'press ')";
printf "$(gum style --bold --foreground "$COLOR_ACTIVE" 'enter')";
printf "$(gum style --italic ' to confirm your selection:')\n";
}
# Prints input prefixed with a selection cursor, indicating a selection made
# by the user.
# Args:
# `$1` - Selection to print.
print_input_selection () {
local SELECTION=$1;
printf "$(gum style --foreground "$GUM_CHOOSE_SELECTED_FOREGROUND" \
"$GUM_CHOOSE_CURSOR") $SELECTION\n";
}
# Prints the ratio of packages 'done' to the packages to be installed.
print_installation_ratio () {
printf "$PACKAGES_DONE/$PACKAGES_PENDING";
}
# Prints declaration that a package has been successfully installed.
# Args:
# `$1` - Name of the installed package.
# `$2` - Optional name of the installation method used to install the package.
print_installed () {
local PACKAGE_NAME=$1;
if [ -n "$2" ]; then
local INSTALLATION_METHOD=$2;
if ! package_is_installed gum; then
printf "🎁 $PACKAGE_NAME \
installed using $INSTALLATION_METHOD.\n";
else
printf "🎁 $(gum style --bold "$PACKAGE_NAME") \
installed using $(gum style --bold "$INSTALLATION_METHOD").\n";
fi
else
if ! package_is_installed gum; then
printf "🎁 $PACKAGE_NAME installed.\n";
else
printf "🎁 $(gum style --bold "$PACKAGE_NAME") installed.\n";
fi
fi
}
# Prints declaration that a package is currently being installed.
# Args:
# `$1` - Name of the package being installed.
# `$2` - ID of the package being installed (`"id"` in `package.json`).
# `$3` - Installation method being used to install the package.
print_installing () {
local PACKAGE_NAME=$1;
local PACKAGE_ID=$2;
local INSTALLATION_METHOD=$3;
if ! package_is_installed gum; then
printf "🌎 Installing $PACKAGE_NAME ($PACKAGE_ID) \
using $INSTALLATION_METHOD...\n"
else
printf "Installing $(gum style --bold "$PACKAGE_NAME") \
($(gum style --italic "$PACKAGE_ID")) \
using $(gum style --bold "$INSTALLATION_METHOD")...";
fi
}
# Prints the given array of items as a comma-separated list.
# Args:
# `$@` - Array of items to print.
print_list () {
local ARRAY=("$@");
local LIST="";
if [ "${#ARRAY[@]}" -eq 1 ]; then
printf "$(print_selected_menu_item "${ARRAY[0]}").";
else
local INPUT="";
local SELECTED_GROUPS_ARRAY_LENGTH=${#ARRAY[@]};
for (( i=0; i<$((${#ARRAY[@]} - 1)); i++ )); do
local GROUP_NAME=$(print_selected_menu_item "${ARRAY[i]}");
LIST+="$GROUP_NAME, ";
done
LIST+="and $(print_selected_menu_item "${ARRAY[-1]}").";
printf "$LIST";
fi
}
# Prints declaration that a package cannot be installed due to a missing "id".