forked from transmute-industries/launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils
More file actions
executable file
·772 lines (599 loc) · 18.4 KB
/
utils
File metadata and controls
executable file
·772 lines (599 loc) · 18.4 KB
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
#!/usr/bin/env bash
###############################################################################
# VARIABLES
###############################################################################
count=1
reset="\033[0m"
highlight="\033[41m\033[97m"
dot="\033[31m▸ $reset"
dim="\033[2m"
blue="\e[34m"
green="\e[32m"
yellow="\e[33m"
tag_green="\e[30;42m"
tag_blue="\e[30;46m"
bold=$(tput bold)
normal=$(tput sgr0)
underline="\e[37;4m"
indent=" "
# Get full directory name of this script
cwd="$(cd "$(dirname "$0")" && pwd)"
DOTFILES_ROOT=$HOME/.dotfiles
###############################################################################
# Utility Functions
###############################################################################
_print_in_color() {
printf "%b" \
"$(tput setaf "$2" 2>/dev/null)" \
"$1" \
"$(tput sgr0 2>/dev/null)"
}
_print_error_stream() {
while read -r line; do
print_in_red " ↳ ERROR: $line\n"
done
}
_show_spinner() {
local -r FRAMES='/-\|'
# shellcheck disable=SC2034
local -r NUMBER_OR_FRAMES=${#FRAMES}
local -r CMDS="$2"
local -r MSG="$3"
local -r PID="$1"
local i=0
local frameText=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Display spinner while the commands are being executed.
while kill -0 "$PID" &>/dev/null; do
frameText=" [${FRAMES:i++%NUMBER_OR_FRAMES:1}] $MSG"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Print frame text.
printf "%s" "$frameText"
sleep 0.2
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clear frame text.
printf "\r"
done
}
_kill_all_subprocesses() {
local i=""
for i in $(jobs -p); do
kill "$i"
wait "$i" &>/dev/null
done
}
_set_trap() {
trap -p "$1" | grep "$2" &>/dev/null ||
trap '$2' "$1"
}
_link_file() {
local src=$1 dst=$2
local overwrite= backup= skip=
local action=
if [ -f "$dst" ] || [ -d "$dst" ] || [ -L "$dst" ]; then
if [ "$overwrite_all" == "false" ] && [ "$backup_all" == "false" ] && [ "$skip_all" == "false" ]; then
local current_src="$(readlink $dst)"
if [ "$current_src" == "$src" ]; then
skip=true
else
printf "\r ${yellow}!${reset} File already exists: $dst ($(basename "$src")) \n
what do you want to do? \n
[s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all? \n"
read -n 1 action
case "$action" in
o)
overwrite=true
;;
O)
overwrite_all=true
;;
b)
backup=true
;;
B)
backup_all=true
;;
s)
skip=true
;;
S)
skip_all=true
;;
*) ;;
esac
fi
fi
overwrite=${overwrite:-$overwrite_all}
backup=${backup:-$backup_all}
skip=${skip:-$skip_all}
if [ "$overwrite" == "true" ]; then
rm -rf "$dst"
print_in_green "\n ✓ deleted $dst"
fi
if [ "$backup" == "true" ]; then
mv "$dst" "${dst}.backup"
print_in_green "\n ✓ moved $dst to ${dst}.backup"
fi
if [ "$skip" == "true" ]; then
printf "\n ${dim} ✓ $src already linked. Skipped.${reset}"
fi
fi
if [ "$skip" != "true" ]; then # "false" or empty
ln -s "$1" "$2"
print_in_green "\n ✓ linked $1 to $2"
fi
}
###############################################################################
# Print Functions
###############################################################################
print_in_red() {
_print_in_color "$1" 1
}
print_in_green() {
_print_in_color "$1" 2
}
print_in_yellow() {
_print_in_color "$1" 3
}
print_in_blue() {
_print_in_color "$1" 4
}
print_in_purple() {
_print_in_color "$1" 5
}
print_in_cyan() {
_print_in_color "$1" 6
}
print_in_white() {
_print_in_color "$1" 7
}
print_result() {
if [ "$1" -eq 0 ]; then
print_success "$2"
else
print_error "$2"
fi
return "$1"
}
print_question() {
print_in_yellow " [?] $1\n"
}
print_success() {
print_in_green " [✓] $1\n"
}
print_success_muted() {
printf " ${dim}[✓] $1${reset}\n" "$@"
}
print_muted() {
printf " ${dim}$1${reset}\n" "$@"
}
print_warning() {
print_in_yellow " [!] $1\n"
}
print_error() {
print_in_red " [𝘅] $1 $2\n"
}
###############################################################################
# Meta Checks
###############################################################################
check_bash_version() {
if ((BASH_VERSINFO[0] < 3)); then
print_error "Sorry, you need at least bash-3.0 to run this script."
exit 1
fi
}
get_os() {
local os=""
local kernelName=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
kernelName="$(uname -s)"
if [ "$kernelName" == "Darwin" ]; then
os="macOS"
elif [ "$kernelName" == "Linux" ] && [ -e "/etc/lsb-release" ]; then
os="ubuntu"
else
os="$kernelName"
fi
printf "%s" "$os"
}
get_os_version() {
local os=""
local version=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
os="$(get_os)"
if [ "$os" == "macOS" ]; then
version="$(sw_vers -productVersion)"
elif [ "$os" == "ubuntu" ]; then
version="$(lsb_release -d | cut -f2 | cut -d' ' -f2)"
fi
printf "%s" "$version"
}
check_internet_connection() {
if ping -q -w 1 -c 1 google.com &>/dev/null; then
print_error "Please check your internet connection";
exit 0
else
print_success "Internet connection"
fi
}
###############################################################################
# Execution
###############################################################################
execute() {
local -r CMDS="$1"
local -r MSG="${2:-$1}"
local -r TMP_FILE="$(mktemp /tmp/XXXXX)"
local exitCode=0
local cmdsPID=""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# If the current process is ended,
# also end all its subprocesses.
_set_trap "EXIT" "_kill_all_subprocesses"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Execute commands in background
eval "$CMDS" \
&>/dev/null \
2>"$TMP_FILE" &
cmdsPID=$!
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Show a spinner if the commands
# require more time to complete.
_show_spinner "$cmdsPID" "$CMDS" "$MSG"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Wait for the commands to no longer be executing
# in the background, and then get their exit code.
wait "$cmdsPID" &>/dev/null
exitCode=$?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Print output based on what happened.
print_result $exitCode "$MSG"
if [ $exitCode -ne 0 ]; then
_print_error_stream <"$TMP_FILE"
fi
rm -rf "$TMP_FILE"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return $exitCode
}
mkd() {
if [ -n "$1" ]; then
if [ -e "$1" ]; then
if [ ! -d "$1" ]; then
print_error "$1 - a file with the same name already exists!"
else
printf " ${dim}✓ $1 already exists. Skipped.${reset}\n"
fi
else
execute "mkdir -p $1" "$1"
fi
fi
}
symlink_dotfiles() {
local overwrite_all=false backup_all=false skip_all=false
for src in $(find -H "$DOTFILES_ROOT" -maxdepth 2 -name '*.symlink' -not -path '*.git*'); do
dst="$HOME/.$(basename "${src%.*}")"
_link_file "$src" "$dst"
done
}
###############################################################################
# Prompts
###############################################################################
ask_for_sudo() {
# Ask for the administrator password upfront.
sudo -v &>/dev/null
# Update existing `sudo` time stamp
# until this script has finished.
#
# https://gist.github.com/cowboy/3118588
# Keep-alive: update existing `sudo` time stamp until script has finished
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done 2>/dev/null &
print_success "Password cached"
}
ask() {
# https://djm.me/ask
local prompt default reply
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n " [?] $1 [$prompt] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read -r reply </dev/tty
# Default?
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y* | y*) return 0 ;;
N* | n*) return 1 ;;
esac
done
}
###############################################################################
###############################################################################
# return 1 if global command line program installed, else 0
cli_is_installed() {
# set to 1 initially
local return_status=1
# set to 0 if not found
type "$1" >/dev/null 2>&1 || { local return_status=0; }
# return value
echo "$return_status"
}
copy_key_github() {
inform "Public key copied, Paste into Github"
[[ -f "$pub" ]] && cat "$pub" | pbcopy
open 'https://github.com/account/ssh'
read -r -p " ✦ Press enter to continue"
print_success "SSH key"
return
}
github_key_check() {
if ask "SSH key found. Enter it in Github?" Y; then
copy_key_github
else
print_success "SSH key"
fi
}
create_ssh_key() {
if ask "No SSH key found. Create one?" Y; then
ssh-keygen -t rsa
github_key_check
else
return 0
fi
}
ssh_key_setup() {
local pub=$HOME/.ssh/id_rsa.pub
if ! [[ -f $pub ]]; then
create_ssh_key
else
github_key_check
fi
}
mas_setup() {
if mas account >/dev/null; then
return 0
else
return 1
fi
}
install_brews() {
if [[ ! $(brew list | grep $brew) ]]; then
echo_install "Installing $brew \n"
brew install $brew >/dev/null
print_in_green "${bold}✓ installed!${normal}\n"
else
print_success_muted "$brew already installed. Skipped."
fi
}
install_application_via_brew() {
if [[ $cask == *::* ]]; then
cask_name="${cask%%::*}"
application="${cask##*::}"
else
cask_name=$cask
application=$cask
fi
if [[ ! $(brew list --cask| grep $cask_name) && ! $(ls /Applications | grep -i "${application}") ]]; then
echo_install "Installing $cask_name"
brew install $cask --cask --appdir=/Applications >/dev/null
print_in_green "${bold}✓ installed!${normal}\n"
else
print_success_muted "$cask_name already installed. Skipped."
fi
}
install_application_via_app_store() {
if ! mas list | grep $1 &>/dev/null; then
echo_install "Installing $2"
mas install $1 >/dev/null
print_in_green "${bold}✓ installed!${normal}\n"
else
print_success_muted "$2 already installed. Skipped."
fi
}
install_gem_packages() {
if [[ ! $(gem list $1 -i) ]]; then
echo_install "Installing $1"
gem install $1 --silent
print_in_green "${bold}✓ installed!${normal}\n"
else
print_success_muted "$1 already installed. Skipped."
fi
}
install_ruby_version() {
PS3="Select option: "
options=("system" "2.5.5" "2.3.3" "Pick a custom Ruby Version")
select ruby_version in "${options[@]}"; do
case "$ruby_version" in
system)
ruby_version="system"
break
;;
2.5.5)
build='2.5.5'
break
;;
3.0.0)
build='3.0.0'
break
;;
Pick*)
read -p "Enter Ruby version: " number
ruby_version="$number"
break
;;
esac
done
if [[ $ruby_version == "system" ]]; then
print_success_muted "Using System ruby. Skipped."
elif [[ $ruby_version =~ ^[0-9]+\.[0-9]+ ]]; then
if [[ ! $(rbenv versions | grep "$ruby_version") ]]; then
echo_install "Installing $ruby_version"
RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl) --with-readline-dir=$(brew --prefix readline) --with-libyaml-dir=$(brew --prefix libyaml)" rbenv install $ruby_version
print_in_green "${bold}✓ installed!${normal}\n"
else
print_success_muted "Ruby $ruby_version already installed. Skipped."
fi
else
echo "Incorrect version: $ruby_version"
exit 1
fi
}
install_npm_packages() {
if [[ $(cli_is_installed $2) == 0 ]]; then
echo_install "Installing $1"
npm install $1 -g --silent
print_in_green "${bold}✓ installed!${normal}\n"
else
print_success_muted "$1 already installed. Skipped."
fi
}
checkRosetta() {
processor=$(/usr/sbin/sysctl -n machdep.cpu.brand_string | grep -o \"Intel\")
if [[ -n \"$processor\" ]]; then
echo 'intel'
else
if [[ \"`pkgutil --files com.apple.pkg.RosettaUpdateAuto`\" == \"\" ]]
then
echo 'not-present'
sudo /usr/sbin/softwareupdate --install-rosetta --agree-to-license
else
echo 'installed'
fi
fi
}
# The releases are returned in the format
# {"id":3622206,"tag_name":"hello-1.0.0.11"}
# we have to extract the tag_name.
get_github_version() {
echo $1 | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/'
}
set_hot_corners() {
# Hot corners
# Possible values:
# 0: no-op
# 2: Mission Control
# 3: Show application windows
# 4: Desktop
# 5: Start screen saver
# 6: Disable screen saver
# 7: Dashboard
# 10: Put display to sleep
# 11: Launchpad
# 12: Notification Center
# Bottom left screen corner ? Start screen saver
set_app_default com.apple.dock wvous-bl-corner -int 2
set_app_default com.apple.dock wvous-bl-modifier -int 0
# Bottom right screen corner ? Start screen saver
set_app_default com.apple.dock wvous-br-corner -int 4
set_app_default com.apple.dock wvous-br-modifier -int 0
}
set_dock_options() {
# Automatically hide and show the Dock
set_app_default com.apple.dock autohide -bool true
set_app_default com.apple.Dock showhidden -bool true
# Check of app is available
defaults write com.apple.menuextra.clock DateFormat -string 'EEE MMM d h:mm a'
set_app_default com.apple.menuextra.battery ShowPercent YES
}
set_finder_options() {
# Finder: show path bar
set_app_default com.apple.finder ShowPathbar -bool true
# Finder: allow text selection in Quick Look
set_app_default com.apple.finder QLEnableTextSelection -bool true
# When performing a search, search the current folder by default
set_app_default com.apple.finder FXDefaultSearchScope -string "SCcf"
# Avoid creating .DS_Store files on network volumes
set_app_default com.apple.desktopservices DSDontWriteNetworkStores -bool true
# Disable the warning before emptying the Trash
set_app_default com.apple.finder WarnOnEmptyTrash -bool false
# Set the icon size of Dock items to 32 pixels
set_app_default com.apple.dock tilesize -int 32
}
set_iterm_options() {
# Dont display the annoying prompt when quitting iTerm
set_app_default com.googlecode.iterm2 PromptOnQuit -bool false
echo ""
if ask "Load custom iTerm Profile: " Y; then
local src="$cwd/assets/com.googlecode.iterm2.plist"
local destination="$HOME/Library/Preferences"
cp -iv "$src" "$destination"
print_success "Using custom iTerm2 settings.";
else
print_success_muted "Using Default iTerm2 settings Skipped.";
fi
}
set_app_default() {
application="$1"
property_name="$2"
shift
local option="$@"
local read_command="defaults read $application $property_name"
local write_command="defaults write $application $option"
if eval "$read_command"; then
echo_install "Setting: [$write_command]"
eval "$write_command"
else
print_success_muted "$property_name is not available."
fi
}
###############################################################################
# Text Formatting
###############################################################################
title() {
local fmt="$1"; shift
printf "\n✦ ${bold}$fmt${normal}\n└─────────────────────────────────────────────────────○\n" "$@"
}
chapter() {
local fmt="$1"; shift
printf "\n 🛰 ${bold}$((count++)). $fmt${normal}\n└─────────────────────────────────────────────────────○\n" "$@"
}
echo_install() {
local fmt="$1"; shift
printf " [↓] $fmt " "$@"
}
todo() {
local fmt="$1"; shift
printf " [ ] $fmt\n" "$@"
}
inform() {
local fmt="$1"; shift
printf " ✦ $fmt\n" "$@"
}
announce() {
local fmt="$1"; shift
printf "○───✦ $fmt\n" "$@"
}
step() {
printf "\n ${dot}${underline}$*${reset}\n"
}
label_blue() {
printf "\e[30;46m $1 \033[0m\e[34m $2 \033[0m\n"
}
label_green() {
printf "\e[30;42m $1 \e[0m\e[32m $2 \033[0m\n"
}
we_have_lift_off() {
printf "
${bold}Congrats! You're ready to launch!${normal} 🚀
+- Lauchpad --------------------------------------------+
| Note: You might need to restart you machine or |
| the affected programs for the changes to take affect. |
| |
| https://github.com/Ashraf-Ali-aa/launchpad |
+-------------------------------------------------------+
"
}