-
Notifications
You must be signed in to change notification settings - Fork 1
/
good-morning.sh
executable file
·1533 lines (1431 loc) · 62.6 KB
/
good-morning.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
#!/usr/bin/env bash
# Do not exit immediately if a command exits with a non-zero status.
set +o errexit
# Print commands and their arguments as they are executed.
# Turn on for debugging
# set -o xtrace
bold=
normal=
if type tput &> /dev/null && test -t 1; then
ncolors=$(tput colors)
if test -n "$ncolors" && test "$ncolors" -ge 8; then
bold=$(tput bold)
normal=$(tput sgr0)
fi
fi
function errcho {
red='\033[0;31m'
nc='\033[0m'
echo -e "${bold}${red}ERROR: $*${nc}${normal}" >&2
}
function eccho {
local light_blue='\033[1;34m'
local nc='\033[0m'
echo -e "${bold}${light_blue}$*${nc}${normal}"
}
function randstring32 {
env LC_CTYPE=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 32 | head -n 1
}
if [[ -z "$GOOD_MORNING_PASSPHRASE" ]]; then
GOOD_MORNING_PASSPHRASE="$(randstring32)"
fi
function encryptToFile {
echo "$1" | openssl enc -aes-256-cbc -pbkdf2 -k "$GOOD_MORNING_PASSPHRASE" > "$2"
}
function decryptFromFile {
openssl enc -aes-256-cbc -pbkdf2 -d -k "$GOOD_MORNING_PASSPHRASE" < "$1"
}
function askto {
eccho "Do you want to $1? $3"
read -r -n 1 -p "(Y/n) " yn < /dev/tty;
echo # echo newline after input
# shellcheck disable=SC2091
case $yn in
y|Y ) $($2); return 0;;
n|N ) return 1;;
esac
}
function prompt {
if [[ -n "$2" ]]; then
read -r -p "$1" "$2" < /dev/tty
else
read -r -p "$1" < /dev/tty
fi
}
function promptsecret {
read -r -s -p "$1: " "$2" < /dev/tty
echo # echo newline after input
}
GOOD_MORNING_CONFIG_FILE="$HOME/.good_morning"
function getConfigValue {
local val
val="$( (grep -E "^$1=" -m 1 "$GOOD_MORNING_CONFIG_FILE" 2> /dev/null || echo "$1=$2") | head -n 1 | cut -d '=' -f 2-)"
printf -- "%s" "$val"
}
function setConfigValue {
# macOS uses Bash 3.x which does not support associative arrays yet
# faking it is not worth the added complexity
local keep_pass_for_session
keep_pass_for_session="$(getConfigValue "keep_pass_for_session" "not-asked")" # "not-asked", "no" or "yes"
local applied_cask_depends_on_fix
applied_cask_depends_on_fix="$(getConfigValue "applied_cask_depends_on_fix" "no")" # "no" or "yes"
local last_node_lts_installed
last_node_lts_installed="$(getConfigValue "last_node_lts_installed")"
local tempfile="$GOOD_MORNING_CONFIG_FILE""_temp"
# crude validation
if [[ "$1" == "keep_pass_for_session" ]] || \
[[ "$1" == "applied_cask_depends_on_fix" ]] || \
[[ "$1" == "last_node_lts_installed" ]]
then
# shellcheck disable=SC2086
export $1="$2"
else
errcho "Warning: Tried to set an unknown config key: $1"
fi
{
echo "# This file stores settings and flags for the good-morning script.";
echo "keep_pass_for_session=$keep_pass_for_session";
echo "applied_cask_depends_on_fix=$applied_cask_depends_on_fix";
echo "last_node_lts_installed=$last_node_lts_installed";
} > "$tempfile"
mv -f "$tempfile" "$GOOD_MORNING_CONFIG_FILE"
}
GOOD_MORNING_TEMP_FILE_PREFIX="$GOOD_MORNING_CONFIG_FILE""_temp_"
function sudoit {
local sudoOpt
# allow passing a flag or combination to sudo (example: -H)
if [[ "$(echo "$1" | cut -c1)" == "-" ]]; then
sudoOpt="$1"
shift
fi
if ! [[ -e "$GOOD_MORNING_ENCRYPTED_PASS_FILE" ]] || ! decryptFromFile "$GOOD_MORNING_ENCRYPTED_PASS_FILE" | sudo -S -p "" printf ""; then
GOOD_MORNING_ENCRYPTED_PASS_FILE="$GOOD_MORNING_TEMP_FILE_PREFIX$(randstring32)"
local p=
while [[ -z "$p" ]] || ! echo "$p" | sudo -S -p "" printf ""; do
promptsecret "Password" p
done
encryptToFile "$p" "$GOOD_MORNING_ENCRYPTED_PASS_FILE"
fi
# shellcheck disable=SC2086
decryptFromFile "$GOOD_MORNING_ENCRYPTED_PASS_FILE" | sudo $sudoOpt -S -p "" "$@"
}
function dmginstall {
local appPath="/Applications/$1.app"
local appPathUser="$HOME/Applications/$1.app"
local downloadPath="$HOME/Downloads/$1.dmg"
# only offer to install if not installed in either the user or "all users" locations
if ! [[ -d "$appPath" ]] && ! [[ -d "$appPathUser" ]] && askto "install $1"; then
curl -JL "$2" -o "$downloadPath"
yes | hdiutil attach "$downloadPath" > /dev/null
# install in the "all users" location
sudoit ditto "/Volumes/$3/$1.app" "$appPath"
diskutil unmount "$3" > /dev/null
rm -f "$downloadPath"
fi
}
function getOSVersion {
sw_vers | grep -E "ProductVersion" | sed -E "s/^.*(10\.[.0-9]+)/\1/"
}
function checkOSRequirement {
if getOSVersion | grep -qvE ' 1(0\.15|1\.)'; then
errcho "Good Morning must be run on either macOS 10.15 (Catalina) or 11.x (Big Sur)."
exit 1
fi
}
function checkPerms {
eccho "Checking directory permissions..."
# shellcheck disable=SC2207
local dirs=(
# Block of dirs that Homebrew needs the user to own for successful operation.
# The redundancy of nested dirs is left here intentionally even though we use -R.
/usr/local/bin
/usr/local/Caskroom
/usr/local/Cellar
/usr/local/etc
/usr/local/Frameworks
/usr/local/Homebrew
/usr/local/include
/usr/local/lib
/usr/local/lib/pkgconfig
/usr/local/lib/python2.7/site-packages
/usr/local/opt
/usr/local/sbin
/usr/local/share
/usr/local/share/locale
/usr/local/share/man/*
/usr/local/var
/usr/local/var/homebrew
# Needed for pip installs without requiring sudo
/Library/Python/2.7/site-packages
/Library/Ruby/Gems/*
/Library/Ruby/Site/*
/System/Library/Frameworks/Python.framework/Versions/2.7/share/doc
/System/Library/Frameworks/Python.framework/Versions/2.7/share/man
"$HOME/.pyenv"
# /Applications/*.app
)
local userPerm="$USER:wheel"
for dir in "${dirs[@]}"; do
if [[ -d "$dir" ]] && ! stat -f "%Su:%Sg" "$dir" 2> /dev/null | grep -qE "^$userPerm$"; then
eccho "Setting ownership of $dir to $USER..."
sudoit chown -R "$userPerm" "$dir"
fi
done
# "Allow apps downloaded from: Anywhere" for app backwards compatibility with macOS 10.15 Catalina
# Otherwise, some apps and quicklook extensions don't switch to the Allowed state properly from
# the Security & Privacy settings.
if spctl --status | grep -q "assessments enabled" && getOSVersion | grep -q "10.15"; then
sudoit spctl --master-disable
fi
}
checkPerms
function updateGems {
eccho "Checking Ruby system gem versions..."
gem update --system --force --no-document
eccho "Checking Ruby gem versions..."
local outdated
outdated="$(gem outdated | grep -Ev 'google-cloud-storage' | sed -E 's/[ ]*\([^)]*\)[ ]*/ /g')"
if [[ -n "$outdated" ]]; then
eccho "Updating these Ruby gems:"
eccho "$outdated"
# shellcheck disable=SC2086
gem update $outdated --force --no-document
fi
}
if ! type rvm &> /dev/null || rvm list | grep -q 'No rvm rubies'; then
eccho "Using macOS Ruby."
else
eccho "Using RVM's default Ruby..."
rvm use default
fi
eccho "Checking for existence of xcode-install..."
if ! gem list --local | grep -q "xcode-install"; then
eccho "Installing xcode-install for managing Xcode..."
# https://github.com/KrauseFx/xcode-install
# The alternative install instructions must be used since there is not a working
# compiler on the system at this point in the setup.
curl -sL https://github.com/neonichu/ruby-domain_name/releases/download/v0.5.99999999/domain_name-0.5.99999999.gem -o ~/Downloads/domain_name-0.5.99999999.gem
sudoit gem install ~/Downloads/domain_name-0.5.99999999.gem --no-document < /dev/tty
sudoit gem install --conservative xcode-install --no-document < /dev/tty
rm -f ~/Downloads/domain_name-0.5.99999999.gem
fi
function ensureXcodeInstallUserSet {
if [[ -z "$XCODE_INSTALL_USER" ]]; then
local xcode_install_user
eccho "Your Apple Developer ID is required to install Xcode and essential build tools."
eccho "The Apple ID you use must have accepted the Apple Developer Agreement."
eccho "You can do this by signing in or creating a new Apple ID at https://developer.apple.com/account/"
prompt "Enter your Apple Developer ID: " xcode_install_user
export XCODE_INSTALL_USER="$xcode_install_user"
if [[ -f ~/.bash_profile ]]; then
# append to .bash_profile since unlikely to change
echo "export XCODE_INSTALL_USER=\"$xcode_install_user\"" >> ~/.bash_profile
fi
fi
}
function installXcode {
local xcode_version="$1"
local xcode_build_version="$2"
local xcode_short_version
xcode_short_version="$(echo "$1" | sed -E 's/^([0-9|.]*).*/\1/')"
ensureXcodeInstallUserSet
eccho "Updating list of available Xcode versions..."
xcversion update < /dev/tty
eccho "Installing Xcode $xcode_version..."
xcversion install "$xcode_version" --force < /dev/tty # force makes upgrades from beta a simple process
xcversion select "$xcode_short_version" < /dev/tty
eccho "Installing Xcode command line tools..."
xcversion install-cli-tools < /dev/tty
# link to the header file locations
sudoit ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
# run cleanup if the install was successful
if [[ "$(getLocalXcodeBuildVersion)" = "$xcode_build_version" ]]; then
eccho "Cleaning up Xcode installers..."
xcversion cleanup
fi
}
function getLocalXcodeVersion {
/usr/bin/xcodebuild -version 2>&1 | grep "Xcode" | sed -E 's/Xcode ([0-9|.]*)/\1/'
}
function getLocalXcodeBuildVersion {
/usr/bin/xcodebuild -version 2>&1 | grep "Build" | sed -E 's/Build version ([0-9A-Za-z]+)/\1/'
}
function checkXcodeVersion {
local xcode_version="12.1" # do not append prerelease names such as "Beta" to this version number.
local xcode_prerelease_stage="" # leave blank when not a beta or leave a trailing space at the end if it is (e.g "Beta 1 ")
local xcode_build_version="11E503a"
eccho "Checking Xcode version..."
if ! /usr/bin/xcode-select --print-path &> /dev/null || \
! [[ -d "$(/usr/bin/xcode-select --print-path)" ]] || \
[[ "$(/usr/bin/xcode-select --print-path)" = "/Library/Developer/CommandLineTools" ]]; then
# One of these cases is true, so an install is necessary (no prompt).
# 1) Calling xcode-select failed for any reason
# 2) xcode-select is pointing to a Developer directory that does not exist
# 3) xcode-select is pointing to the default /Library location when no Xcode is installed
installXcode "$xcode_version" "$xcode_build_version"
else
# Xcode appears to be installed. Check to see if an upgrade option should be offered.
local local_version
local local_build_version
local_version="$(getLocalXcodeVersion)"
local_build_version="$(getLocalXcodeBuildVersion)"
if [[ "$local_build_version" < "$xcode_build_version" ]] \
&& askto "upgrade Xcode to $xcode_version $xcode_prerelease_stage(Build $xcode_build_version) from $local_version (Build $local_build_version)..."; then
installXcode "$xcode_version" "$xcode_build_version"
local new_local_version
new_local_version="$(getLocalXcodeVersion)"
# If there was a previous version installed, but it wasn't a beta which will have the same version number...
if [[ -n "$local_version" ]] && [[ "$local_version" != "$new_local_version" ]]; then
eccho "Uninstalling Xcode $local_version (Build $local_build_version)..."
xcversion uninstall "$local_version" < /dev/tty
fi
fi
fi
}
checkXcodeVersion
if /usr/bin/xcrun clang 2>&1 | grep -q "license"; then
eccho "Accepting the Xcode license..."
sudoit xcodebuild -license accept
eccho "Installing Xcode packages..."
sudoit installer -pkg /Applications/Xcode.app/Contents/Resources/Packages/MobileDevice.pkg -target /
sudoit installer -pkg /Applications/Xcode.app/Contents/Resources/Packages/MobileDeviceDevelopment.pkg -target /
sudoit installer -pkg /Applications/Xcode.app/Contents/Resources/Packages/XcodeSystemResources.pkg -target /
fi
GIT_EMAIL="$(git config --global --get user.email)"
if [[ -z "$GIT_EMAIL" ]]; then
prompt "Enter the email address you use for git commits: " GIT_EMAIL
git config --global user.email "$GIT_EMAIL"
fi
GIT_NAME="$(git config --global --get user.name)"
if [[ -z "$GIT_NAME" ]]; then
prompt "Enter the full name you use for git commits: " GIT_NAME
git config --global user.name "$GIT_NAME"
fi
# Generate a new SSH key for GitHub https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/
if ! [[ -f "$HOME/.ssh/id_rsa.pub" ]] && askto "create an SSH key for $GIT_EMAIL"; then
eccho "Generating SSH key to be stored at $HOME/.ssh/id_rsa ..."
ssh-keygen -t rsa -b 4096 -C "$GIT_EMAIL" < /dev/tty
eccho "Starting ssh-agent ..."
eval "$(ssh-agent -s)"
# automatically load the keys and store passphrases in your keychain
eccho "Initializing your ~/.ssh/config"
echo "Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile \"$HOME/.ssh/id_rsa\"" >> "$HOME/.ssh/config"
# add your ssh key to ssh-agent
ssh-add -K "$HOME/.ssh/id_rsa"
if askto "add your SSH key to GitHub or other source control provider"; then
# copy public ssh key to clipboard for pasting on GitHub
pbcopy < "$HOME/.ssh/id_rsa.pub"
eccho "The public key is now in your clipboard."
GITHUB_KEYS_URL="https://github.com/settings/keys"
if askto "open up GitHub's settings page for adding SSH keys"; then
eccho "GitHub will be opened next. Sign-in with $GIT_EMAIL if you have not already."
eccho "Click 'New SSH key' and paste in the copied key."
prompt "Hit Enter to open $GITHUB_KEYS_URL..."
open "$GITHUB_KEYS_URL"
prompt "Hit Enter to continue after the SSH key is saved on GitHub..."
else
eccho "With your new public key in the clipboard, take a moment to add it to all of the"
eccho "source control providers that you use, such as BitBucket or GitLab."
prompt "Hit Enter to continue running the script..."
fi
fi
fi
# This install is an artifact for first-run that is overridden by the brew install
# Some careful re-ordering will be able to eliminate this without breaking the first-run use case.
gpg_suite_new_install=
if ! [[ -d "/Applications/GPG Keychain.app" ]] \
&& askto "install GPG Suite"; then
eccho "Installing GPG Suite..."
dmg="$HOME/Downloads/GPGSuite.dmg"
curl -JL https://releases.gpgtools.org/GPG_Suite-2019.1_83.dmg -o "$dmg"
hdiutil attach "$dmg"
sudoit installer -pkg "/Volumes/GPG Suite/Install.pkg" -target /
diskutil unmount "GPG Suite"
rm -f "$dmg"
unset dmg
gpg_suite_new_install=1
fi
rvm_version=1.29.12
function installRVM {
eccho "Installing RVM..."
if type rvm &> /dev/null; then
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
fi
curl -sSL https://get.rvm.io | bash -s $rvm_version --ruby
rvm rubygems latest --force # gets updated immediately, but fixes issues that show up when running xcversion
# shellcheck source=/dev/null
source "$HOME/.profile" # load rvm
rvm cleanup all
# enable rvm auto-update
# echo rvm_autoupdate_flag=2 >> ~/.rvmrc
# enable rvm auto-reload on update
echo rvm_auto_reload_flag=2 >> ~/.rvmrc
# enable progress bar when downloading RVM / Rubies
echo progress-bar >> ~/.curlrc
# rvm loads in the profile file, not the same way with auto-dot files, so ignore next error
echo rvm_silence_path_mismatch_check_flag=1 >> ~/.rvmrc
}
function checkRubyVersion {
if rvm version | grep -qv "$rvm_version"; then
eccho "Upgrading RVM to $rvm_version..."
# hard-coded since auto upgrade check hits GitHub's rate limits too frequently
rvm get $rvm_version --auto-dotfiles
fi
eccho "Checking Ruby version..."
# rvm list known is only showing 3.0.0, which is outdated. Hard-coding until there is a better way found.
# latest_ruby_version="$(rvm list known 2> /dev/null | tr -d '[]' | grep -E "^ruby-[0-9.]+$" | tail -1)"
latest_ruby_version="ruby-3.2.2"
if rvm list | grep -q 'No rvm rubies'; then
rvm install "$latest_ruby_version"
rvm alias create default ruby "$latest_ruby_version"
rvm rubygems latest --force # gets updated immediately, but fixes issues that show up when running xcversion
rvm cleanup all
else
current_ruby_version="$(ruby --version | sed -E 's/ ([0-9.]+)(p[0-9]+)?([^ ]*).*/-\1-\3/' | sed -E 's/-$//')"
if [[ "$current_ruby_version" != "$latest_ruby_version" ]]; then
eccho "Upgrading Ruby from $current_ruby_version to $latest_ruby_version..."
eccho "The RVM upgrade feature is not used to provide you a more reliable experience."
rvm install "$latest_ruby_version"
rvm rubygems latest --force # gets updated immediately, but fixes issues that show up when running xcversion
rvm cleanup all
eccho "The previous version of Ruby is still available by running 'rvm use $current_ruby_version'."
rvm alias create default ruby "$latest_ruby_version"
fi
unset current_ruby_version
unset latest_ruby_version
fi
}
if ! [[ -s "$HOME/.rvm/scripts/rvm" ]] && ! type rvm &> /dev/null; then
installRVM
fi
checkRubyVersion
# ensure we are not using the system version
rvm use default > /dev/null
updateGems
function installGems {
local gem_list_temp_file="$GOOD_MORNING_TEMP_FILE_PREFIX""gem_list"
local gems=(
cocoapods
# fastlane # installed as dependency of xcode-install
sqlint
terraform_landscape
xcode-install # Will also replace the other xcode-install gem that was installed while bootstrapping...
)
gem list --local > "$gem_list_temp_file"
for gem in "${gems[@]}"; do
if ! grep -q "$gem" "$gem_list_temp_file"; then
eccho "Installing $gem..."
gem install "$gem" --no-document
fi
done
rm -f "$gem_list_temp_file"
# temp fix for fastlane having an internal version conflict with google-cloud-storage
# remove once fastlane fixes this
# eccho "Applying workaround to fix xcode-install..."
# eccho "See https://github.com/fastlane/fastlane/issues/14242 to learn more."
# gem uninstall google-cloud-storage --all --force &> /dev/null
# gem install google-cloud-storage -v 1.16.0 --no-document &> /dev/null
# end temp fix
gem cleanup
}
installGems
if (( gpg_suite_new_install == 1 )); then
unset gpg_suite_new_install
eccho "Creating a GPG key for you to use when signing commits is an excellent way to guarantee the"
eccho "integrity of your code changes for others."
eccho "Learn more about this here: https://git-scm.com/book/tr/v2/Git-Tools-Signing-Your-Work"
eccho "Learn about GitHub's use here: https://help.github.com/articles/generating-a-new-gpg-key/"
if askto "create a GPG signing key for signing your git commits"; then
eccho "Generating a GPG key for signing Git operations..."
promptsecret "Enter a passphrase for the GPG key" GPG_PASSPHRASE
gpg --batch --gen-key <<EOF
Key-Type: RSA
Key-Length: 4096
Subkey-Type: RSA
Subkey-Length: 4096
Name-Real: $GIT_NAME
Name-Comment: Git signing key
Name-Email: $GIT_EMAIL
Expire-Date: 2y
Passphrase: $GPG_PASSPHRASE
%commit
EOF
unset GPG_PASSPHRASE
eccho "Signing key created."
gpg_todays_date=$(date -u +"%Y-%m-%d")
gpg_expr="sec.*4096.*\/([[:xdigit:]]{16}) $gpg_todays_date.*"
gpg_key_id=$(gpg --list-secret-keys --keyid-format LONG | grep -E "$gpg_expr" | sed -E "s/$gpg_expr/\1/")
# copy the GPG public key for GitHub
gpg --armor --export "$gpg_key_id" | pbcopy
eccho "Your new GPG key is copied to the clipboard."
if askto "open up GitHub's settings page for adding GPG keys"; then
eccho "After GitHub opens, click 'New GPG key' and paste in the copied key."
prompt "Hit Enter to open up $GITHUB_KEYS_URL ..."
open "$GITHUB_KEYS_URL"
prompt "Hit Enter to continue after you have saved the GPG key on GitHub..."
fi
eccho "Enabling auto-signing of all commits and other git actions..."
git config --global commit.gpgsign true
git config --global user.signingkey "$gpg_key_id"
eccho "Finishing up GPG setup with a test that will complete the setup..."
eccho "Please accept the GPG related dialog box if it opens."
echo "test" | gpg --clearsign # will prompt with dialog for passphrase to store in keychain
fi
fi
# Pick a default repo root unless one is already set
if [[ -z "${REPO_ROOT+x}" ]]; then
REPO_ROOT="$HOME/repo"
fi
# Create local repository root
if ! [[ -d "$REPO_ROOT" ]]; then
eccho "Creating $REPO_ROOT"
mkdir -p "$REPO_ROOT"
fi
# Setup clone of good-morning repository
GOOD_MORNING_REPO_ROOT="$REPO_ROOT/good-morning"
if ! [[ -d "$GOOD_MORNING_REPO_ROOT/.git" ]]; then
eccho "Cloning good-morning repository..."
git clone https://github.com/dpwolfe/good-morning.git "$GOOD_MORNING_REPO_ROOT"
if [[ -s "$HOME\.bash_profile" ]]; then
eccho "Renaming previous ~/.bash_profile to ~/.old_bash_profile..."
mv "$HOME\.bash_profile" "$HOME\.old_bash_profile_$(date +%Y%m%d%H%M%S)"
fi
echo "export REPO_ROOT=\"\$HOME/repo\"
source \"\$REPO_ROOT/good-morning/dotfiles/.bash_profile\"
if ! contains \$(pwd) \"\$REPO_ROOT\"; then cd \"\$REPO_ROOT\"; fi
export NVM_DIR=\"\$HOME/.nvm\"
[ -s \"\$NVM_DIR/nvm.sh\" ] && \. \"\$NVM_DIR/nvm.sh\" # load nvm
[ -s \"\$NVM_DIR/bash_completion\" ] && \. \"\$NVM_DIR/bash_completion\" # load nvm bash_completion
[ -f /usr/local/etc/bash_completion ] && \. /usr/local/etc/bash_completion
if command -v pyenv 1> /dev/null 2>&1; then eval \"\$(pyenv init -)\"; fi
# RVM is sourced from the .profile file, make sure this happens last or RVM will complain
[ -s \"\$HOME/.profile\" ] && source \"\$HOME/.profile\"
" > "$HOME/.bash_profile"
ensureXcodeInstallUserSet
# copy some starter shell dot files
cp "$GOOD_MORNING_REPO_ROOT/dotfiles/.inputrc" "$HOME/.inputrc"
cp "$GOOD_MORNING_REPO_ROOT/dotfiles/.vimrc" "$HOME/.vimrc"
cp -rf "$GOOD_MORNING_REPO_ROOT/dotfiles/.vim" "$HOME/.vim"
# set flag to indicate this is the first run to turn on additional setup features
FIRST_RUN=1
fi
# Homebrew taps - add those needed and remove obosoleted that can create conflicts (example: java8)
function checkBrewTaps {
notaps=(
homebrew/cask-drivers
caskroom/caskroom
caskroom/versions
)
brew_tap_file="$GOOD_MORNING_TEMP_FILE_PREFIX""brew_tap"
brew tap > "$brew_tap_file"
for tap in "${notaps[@]}"; do
if grep -qE "^$tap$" "$brew_tap_file"; then
brew untap "$tap"
fi
done
taps=(
homebrew/cask-fonts
homebrew/cask-versions
homebrew/services
wata727/tflint # tflint - https://github.com/wata727/tflint#homebrew
)
for tap in "${taps[@]}"; do
if ! grep -qE "^$tap$" "$brew_tap_file"; then
brew tap "$tap"
fi
done
rm -f "$brew_tap_file"
}
# Install homebrew - https://brew.sh
if ! type brew &> /dev/null; then
eccho "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" < /dev/tty
else
eccho "Updating Homebrew..."
checkBrewTaps
brew update 2> /dev/null
eccho "Checking for outdated Homebrew formulas..."
if [[ -n "$(brew upgrade)" ]]; then
BREW_CLEANUP_NEEDED=1
# If there was any output from the cleanup task, assume a formula changed or was installed.
# Homebrew Doctor can take a long time to run, so now running only after formula changes...
eccho "Running Hombrew Doctor since Homebrew updates were installed..."
brew doctor
fi
eccho "Checking for outdated Homebrew Casks..."
for outdatedCask in $(brew outdated --cask | sed -E 's/^([^ ]*) .*$/\1/'); do
eccho "Upgrading $outdatedCask..."
brew reinstall "$outdatedCask"
BREW_CLEANUP_NEEDED=1
done
fi
# Homebrew cask depends_on fix from: https://github.com/Homebrew/homebrew-cask/issues/58046
# The wireshark 3.0.0 install was the first cask that started to fail to update, but now succeeds
# with the following fix applied.
if [[ "$(getConfigValue 'applied_cask_depends_on_fix')" != "yes" ]]; then
eccho "Applying the Homebrew depends_on metadata fix from https://github.com/Homebrew/homebrew-cask/issues/58046..."
/usr/bin/find "$(brew --prefix)/Caskroom/"*'/.metadata' -type f -name '*.rb' -print0 | /usr/bin/xargs -0 /usr/bin/perl -i -0pe 's/depends_on macos: \[.*?\]//gsm;s/depends_on macos: .*//g'
setConfigValue "applied_cask_depends_on_fix" "yes"
fi
# Homebrew casks
casks=(
# android-platform-tools # uncomment if you need Android dev tools
# android-studio # uncomment if you need Android dev tools
# beyond-compare
brave-browser
charles
# controlplane # mac automation based on hardware events
# cord # remote desktop into windows machines from macOS
# dash # https://kapeli.com/dash
# dbeaver-community
# discord
docker
# docker-edge
# dropbox dropbox.com returns 403 on install
# etcher # Flash OS images to SD cards & USB drives, safely and easily.
# firefox
font-fira-code
# google-backup-and-sync
# google-chrome
gpg-suite
handbrake
iterm2
keeweb
# keybase
keyboard-maestro # keyboard macros
# logitech-gaming-software # if you plug-in a logitech keyboard
# microsoft-office
# microsoft-teams
# omnifocus
# omnigraffle
# onedrive
# openconnect-gui # connect to a Cisco Connect VPN
# opera
# parallels
postman
provisionql # quick-look for iOS provisioning profiles
# qladdict # Need to check all these quick-look extensions for Big Sur compatibility.
# qlcolorcode
# qlmarkdown
# qlstephen
# quicklook-json
rocket # utf-8 emoji quick lookup and insert in any macOS app
# sketch
skitch
slack
# sourcetree
tableplus
the-unarchiver
transmission # open source BitTorrent client from https://github.com/transmission/transmission
tunnelblick # connect to your VPN
vanilla # hide menu icons on your mac
visual-studio-code
# visual-studio-code-insiders
wireshark
# xmind-zen
# zoom
)
cask_list_temp_file="$GOOD_MORNING_TEMP_FILE_PREFIX""cask_list"
cask_collision_file="$GOOD_MORNING_TEMP_FILE_PREFIX""cask_collision"
brew list --cask > "$cask_list_temp_file"
# Uninstall specific Homebrew casks that conflict with this script if installed.
problem_casks=(
insomniax # remove since this is now unmaintained
virtualbox # deprecated since Docker for Desktop already comes with hyperkit
wavtap # deprecated
zoomus # replaced with zoom
)
for cask in "${problem_casks[@]}"; do
if grep -qE "(^| )$cask($| )" "$cask_list_temp_file"; then
brew uninstall --cask --force "$cask"
fi
done
# Install Homebrew casks
for cask in "${casks[@]}"; do
if ! grep -qE "(^| )$cask($| )" "$cask_list_temp_file"; then
eccho "Installing $cask with Homebrew..."
brew install --cask "$cask" 2>&1 > /dev/null | grep "Error: It seems there is already an App at '.*'\." | sed -E "s/.*'(.*)'.*/\1/" > "$cask_collision_file"
if [[ -s "$cask_collision_file" ]]; then
# Remove non-brew installed version of app and retry.
sudoit rm -rf "$(cat "$cask_collision_file")"
rm -f "$cask_collision_file"
brew install --cask "$cask"
fi
NEW_BREW_CASK_INSTALLS=1
BREW_CLEANUP_NEEDED=1
fi
done
rm -f "$cask_collision_file" "$cask_list_temp_file"
unset cask_collision_file
unset brewCasks
# Install Homebrew formulas
formula_list_temp_file="$GOOD_MORNING_TEMP_FILE_PREFIX""formula_list"
function ensureFormulaListCache {
if ! [[ -s "$formula_list_temp_file" ]]; then
brew list --formula > "$formula_list_temp_file"
fi
}
function changeFormula {
local formula_name="$1"
local brew_command="$2"
local formula_ref="${3:-$formula_name}"
ensureFormulaListCache
if ! grep -qE "(^| )$formula_name($| )" "$formula_list_temp_file"; then
# shellcheck disable=SC2046
brew "$brew_command" "$formula_ref" \
$(if [[ "$brew_command" == "uninstall" ]]; then echo "--force --ignore-dependencies"; fi)
fi
}
function ensureFormulaInstalled {
local formula_name="$1"
local formula_ref="${2:-$formula_name}"
changeFormula "$formula_name" install "$formula_ref"
}
function ensureFormulaUninstalled {
local formula_name="$1"
changeFormula "$formula_name" uninstall
}
# Uninstall formulas that create conflicts and may or may not have been
# previously installed by earlier versions of this script.
problem_formulas=(
bash-completion
)
for formula in "${problem_formulas[@]}"; do
ensureFormulaUninstalled "$formula"
done
unset problem_formulas
# shellcheck disable=SC2034
formulas=(
# ansible
# automake
# azure-cli
bash
bash-completion@2
brew-cask-completion
caddy
# cassandra
certbot # For generating SSL certs with Let's Encrypt
coreutils
# dialog # https://invisible-island.net/dialog/
deno
direnv # https://direnv.net/
docker-squash # https://github.com/goldmann/docker-squash
fd # https://github.com/sharkdp/fd
# fish
fx # https://github.com/antonmedv/fx
fzf # https://github.com/junegunn/fzf
# gcc
# gem-completion
git
git-lfs
go
# highlight
httpie # https://github.com/jakubroztocil/httpie
# isl
jq
# kops
# kubernetes-cli
# kubernetes-helm
# launchctl-completion
# lnav
# maven
# maven-completion
# minikube
# neovim
nss # needed by caddy for certutil
openssl@1.1
openssl@3
p7zip # provides 7z command
# packer
# packer-completion
pandoc
# pgcli
# pgtune
# pgweb
pip-completion
# pyenv
python@3.12 # vim was failing load without this - 3/2/2018
readline # for pyenv installs of python
# redis
shellcheck # shell script linting
# swagger-codegen # requires brew install --cask homebrew/cask-versions/adoptopenjdk8
terraform
tflint
tmux
vegeta
vim
watchman
wget
xz # for pyenv installs of python
zlib
zsh
zsh-completions
)
for formula in "${formulas[@]}"; do
ensureFormulaInstalled "$formula"
done
unset formulas
rm -f "$formula_list_temp_file"
unset formula_list_temp_file
# sshpass is not for ssh novices. Leave this step disabled unless you understand the risks of it.
# ensureFormulaInstalled sshpass "https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb"
if [[ -n "$BREW_CLEANUP_NEEDED" ]]; then
unset BREW_CLEANUP_NEEDED;
eccho "Cleaning up Homebrew cache..."
# The -s option clears even the latest versions of uninstalled formulas and casks.
# This does not clear the cache of versions currently installed.
brew cleanup -s
fi
# Run this to set your shell to use fish (user, not root)
# chsh -s `which fish`
# function checkPythonInstall {
# local pythonVersion="$1"
# if ! pyenv versions | grep -q "$pythonVersion"; then
# SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
# CFLAGS="-I$(brew --prefix openssl)/include -O2" \
# LDFLAGS="-L$(brew --prefix openssl)/lib" \
# pyenv install "$pythonVersion"
# fi
# }
# function checkPythonVersions {
# local python2version="2.7.17"
# local python3version="3.8.1"
# checkPythonInstall "$python2version"
# checkPythonInstall "$python3version"
# local globalPythonVersion
# if [[ "$GOOD_MORNING_USE_LEGACY_PYTHON" == 1 ]]; then
# globalPythonVersion="$python2version"
# else
# globalPythonVersion="$python3version"
# fi
# pyenv global "$globalPythonVersion"
# }
# checkPythonVersions
# function checkOhMyFish {
# if ! type "omf" &> /dev/null; then
# local temp_omf_install_file="$HOME/.good_morning_omf_install.temp"
# curl -L https://get.oh-my.fish > "$temp_omf_install_file"
# fish "$temp_omf_install_file" < /dev/tty
# rm -f "$temp_omf_install_file"
# else
# omf update
# fi
# }
# checkOhMyFish - Need to find a way to avoid it immediately entering fish
# and stopping the rest of the script. Might try creating a process fork for this.
function pickbin {
local versions="$1"
for version in $versions; do
if type "$version" &> /dev/null; then
echo "$version"
return
fi
done
}
function findpip {
pickbin 'pip pip3 pip3.9'
}
eccho "Checking pip install..."
localpip="$(findpip)"
if [[ "$localpip" != "pip" ]] || ! pip &> /dev/null; then
eccho "Installing pip..."
wget https://bootstrap.pypa.io/get-pip.py --output-document ~/get-pip.py
python ~/get-pip.py --user
rm -f ~/get-pip.py
else
eccho "Checking for update to pip..."
pip install --upgrade pip --upgrade-strategy eager > /dev/null
fi
unset localpip
export PYCURL_SSL_LIBRARY=openssl
# Install pips in Python
piptempfile="$HOME/pipfreeze.temp"
$(findpip) freeze > "$piptempfile"
pips=(
aws-shell
awscli
boto
gitpython
glances
# gsutil # for programmatic access to Google Play Console reports
lxml
packaging
pip-review
pipdeptree
pipenv
pycurl
requests
virtualenv
)
for pip in "${pips[@]}"; do
if ! grep -qi "$pip==" "$piptempfile"; then
SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
CFLAGS="-I$(brew --prefix openssl)/include -O2" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \
"$(findpip)" install "$pip"
fi
done
unset pips
rm -f "$piptempfile"
unset piptempfile
if ! pip-review | grep -q "Everything up-to-date"; then
eccho "Upgrading pip installed packages..."
SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
CFLAGS="-I$(brew --prefix openssl)/include -O2" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \
pip-review --auto
# temporary workaround until we can ignore upgrading deps beyond what is supported (i.e. awscli and prompt-toolkit)
pip install "prompt-toolkit<1.1.0,>=1.0.0" > /dev/null # fix previous upgrades that went to 2.0
fi
function upgradeNPM {
eccho "Checking Node.js $(node -v) global npm package versions..."
# Upgrade all global packages other than npm to latest
for package in $(npm --global outdated --parseable --depth=0 | cut -d: -f4); do
eccho "Upgrading global package $package for Node.js $(node -v)..."
npm install "$package" --global
done
if ! type "ncu" &> /dev/null; then
eccho "Installing the npm-check-updates global package..."
npm install npm-check-updates --global
fi
if ! type "lerna" &> /dev/null; then
eccho "Installing the lerna global package..."
npm install lerna --global
fi
}
function upgradeNode {
local local_version="$1"
local new_version="$2"
local active_version # version user currently has active in then terminal
active_version="$(nvm current)"
if [[ "$(echo "$active_version" | cut -c1)" != "v" ]]; then
active_version="N/A"
fi
if [[ "$local_version" != "$new_version" ]]; then
local old_version="$local_version" # rename for readability
nvm install "$new_version"
eccho "Clearing Node Version Manager cache..."
nvm cache clear > /dev/null
if [[ "$active_version" == "$old_version" ]]; then
# In this case, the version that was active will be uninstalled.
# Track the new one as the active_version
active_version="$new_version"
fi
local reinstall_version
reinstall_version="$(if [[ \"$old_version\" == \"N/A\" ]]; then echo "$active_version"; else echo "$old_version"; fi)"
if [[ "$reinstall_version" != "N/A" ]] && [[ "$reinstall_version" != "$new_version" ]]; then
eccho "Installing global Node.js packages used by $reinstall_version into $new_version..."
nvm reinstall-packages "$reinstall_version"
fi
upgradeNPM
else
nvm use "$local_version" > /dev/null
upgradeNPM
fi
if [[ "$active_version" != "N/A" ]] && [[ "$active_version" != "$(nvm current)" ]]; then
# Switch to the node version in use before any install or 'nvm use' command executed
nvm use "$active_version" > /dev/null
fi