-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·147 lines (115 loc) · 2.68 KB
/
install.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
#!/bin/bash
# https://docs.brew.sh/Installation
# https://sharats.me/posts/shell-script-best-practices/
set -u
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
project_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -t 1 ]]
then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_yellow="$(tty_mkbold 33)"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
# chomp() {
# printf "%s" "${1/"$'\n'"/}"
# }
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"
do
printf " "
printf "%s" "${arg// /\ }"
done
}
execute() {
if ! "$@"
then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
warn() {
printf "${tty_yellow}Warning${tty_reset}: %s\n" "$(chomp "$1")"
}
abort() {
printf "${tty_red}fail${tty_reset}: %s\n" "$(chomp "$1")"
exit 0
}
abort_println() {
printf "${tty_red}performing${tty_reset}: %s\n" "$(chomp "$1")"
# exit 0
}
perform_task() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
UNAME_MACHINE="$(/usr/bin/uname -m)"
if [[ "${UNAME_MACHINE}" != "arm64" ]] && [[ "${UNAME_MACHINE}" != "x86_64" ]]
then
if [[ "${UNAME_MACHINE}" == "aarch64" ]]
then
warn "Raspberry Pi 4 is not fully supported."
else
abort "only supported on Intel and ARM processors!."
fi
fi
# TODO provide a way to override path option for testing purposes
OS="$(uname)"
if [[ "${OS}" == "Linux" ]]
then
local_executable="/usr/local"
elif [[ "${OS}" == "Darwin" ]]
then
local_executable="/usr/local"
else
abort "This script is only supported on macOS and Linux."
fi
link_project(){
perform_task "creating symbolic link and doing chmod +x dev..."
chmod +x dev
execute "ln" "-sfv" "$project_dir/dev" "${local_executable}/bin/dev"
}
unlink_project(){
perform_task "removing symbolic link ..."
execute "rm" "-fv" "${local_executable}/bin/dev"
}
# this is for python to use.
if [[ "${1-}" =~ ^-*l(link)?$ ]]; then
link_project
exit 0
elif [[ "${1-}" =~ ^-*u(nlink)?$ ]]; then
unlink_project
exit 0
fi
newline=$'\n'
warn "Note: this script uses symbolic link method in$newline/usr/local/bin on both macOS and Linux."
if [[ -f "${local_executable}/bin/dev" ]]
then
warn "dev is already installed."
# ask to remove
read -p "Do you want to remove it? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
unlink_project
exit 0
fi
else
link_project
exit 0
fi