forked from divad12/khan-dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 18
/
shared-functions.sh
273 lines (243 loc) · 8.85 KB
/
shared-functions.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
bad_usage_get_yn_input=100
# for printing standard echoish messages
notice() {
printf " $1\n"
}
# for printing logging messages that *may* be replaced by
# a success/warn/error message
info() {
printf " [ \033[00;34m..\033[0m ] $1"
}
# for printing prompts that expect user input and will be
# replaced by a success/warn/error message
user() {
printf "\r [ \033[0;33m??\033[0m ] $1 "
}
# for replacing previous input prompts with success messages
success() {
printf "\r\033[2K [ \033[00;32mOK\033[0m ] $1\n"
}
# for replacing previous input prompts with warnings
warn() {
printf "\r\033[2K [\033[0;33mWARN\033[0m] $1\n"
}
# for replacing previous prompts with errors
error() {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n"
}
# Replacement for clone_repo() function using ka-clone tool for local config
# If run on an existing repository, will *update* and do --repair
# Arguments:
# $1: url of the repository to clone
# $2: directory to put repo
# $3 onwards: any arguments to pass along to kaclone
kaclone_repo() {
local src="$1"
shift
local dst="$1"
shift
(
mkdir -p "$dst"
cd "$dst"
dirname=$(basename "$src")
if [ ! -d "$dirname" ]; then
"$KACLONE_BIN" "$src" "$dirname" "$@"
cd "$dirname"
git submodule update --init --recursive
else
cd "$dirname"
# This 'ka-clone --repair' installs any new settings
"$KACLONE_BIN" --repair --quiet "$@"
fi
)
}
# Print update in blue.
# $1: update message
update() {
printf "\e[0;34m$1\e[0m\n"
}
# Print error in red and exit.
# $1: error message
# TODO(hannah): Factor out message-printing functions from mac-setup.sh.
err_and_exit() {
printf "\e[0;31m$1\e[0m\n"
exit 1
}
# Get yes or no input from the user. Return default value if the user does no
# enter a valid value (y, yes, n, or no with any captialization).
# $1: prompt
# $2: default value
get_yn_input() {
if [ "$2" = "y" ]; then
prompt="${1} [Y/n]: "
elif [ "$2" = "n" ]; then
prompt="${1} [y/N]: "
else
echo "Error: bad default value given to get_yn_input()" >&2
exit $bad_usage_get_yn_input
fi
read -r -p "$prompt" input
case "$input" in
[yY][eE][sS] | [yY])
echo "y"
;;
[nN][oO] | [nN])
echo "n"
;;
*)
echo $2
;;
esac
}
# Exit with an error if the script is not being run on a Mac. (iOS development
# can only be done on Macs.)
ensure_mac_os() {
if [ "`uname -s`" != "Darwin" ]; then
err_and_exit "This script can only be run on Mac OS."
fi
}
# $1: the package to install
brew_install() {
if ! command -v brew >/dev/null 2>&1; then
echo "Homebrew is not installed."
exit 1
fi
if ! brew ls "$@" >/dev/null 2>&1; then
echo "$@ is not installed, installing $@"
brew install "$@" || echo "Failed to install $@, perhaps it is already installed."
else
echo "$@ already installed"
fi
}
# $1: the text to add
add_to_dotfile() {
grep -F -q "$1" "$HOME/.profile-generated.khan" || echo "$1" >> "$HOME/.profile-generated.khan" || exit 1
}
# Mac-specific function to install Java JDK
install_mac_java() {
# It's surprisingly difficult to tell what java versions are
# already installed -- there are different java providers -- so we
# just always try to install the one we want. For more info, see
# https://github.com/Khan/khan-dotfiles/pull/61/files#r964917242
echo "Installing openjdk 11..."
brew_install openjdk@11
# Symlink openjdk for the system Java wrappers. This supports
# both M1 and x86_64 macs.
if [ -d /opt/homebrew/opt/openjdk@11 ]; then
brew_loc=/opt/homebrew/opt/openjdk@11
elif [ -d /usr/local/Cellar/openjdk@11 ]; then
# Different versions are installed here, we'll take the latest.
brew_loc=$(ls -td /usr/local/Cellar/openjdk@11/11.* | head -n1)
else
error "Could not find the location of java 11, not installing it"
fi
sudo ln -sfn "$brew_loc"/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
# Ensure JAVA_HOME is set in ~/.profile.khan
# TODO (jwiesebron): Update other parts of dotfiles to use this convention
add_to_dotfile 'export JAVA_HOME="$(/usr/libexec/java_home)"'
add_to_dotfile 'export PATH="'"$brew_loc"/bin':$PATH"'
}
install_protoc_common() {
# Platform independent installation of protoc.
# usage: install_protoc_common <zip_url>
# The URL of the protoc zip file is passed as the first argument to this
# function. This file is platform dependent.
zip_url=$1
# We use protocol buffers in webapp's event log stream infrastructure. This
# installs the protocol buffer compiler (which generates go & java code
# from the protocol buffer definitions), as well as a go-based compiler
# plugin that allows us to generate bigquery schemas as well.
if ! which protoc >/dev/null || ! protoc --version | grep -q 3.4.0; then
echo "Installing protoc"
mkdir -p /tmp/protoc
wget -O /tmp/protoc/protoc-3.4.0.zip "$zip_url"
# Change directories within a subshell so that we don't have to worry
# about changing back to the current directory when done.
(
cd /tmp/protoc
# This puts the compiler itself into ./bin/protoc and several
# definitions into ./include/google/protobuf we move them both
# into /usr/local.
unzip -q protoc-3.4.0.zip
# Move the protoc binary to the final location and set the
# permissions as needed.
sudo install -m755 ./bin/protoc /usr/local/bin
# Remove old versions of the includes, if they exist
sudo rm -rf /usr/local/include/google/protobuf
sudo mkdir -p /usr/local/include/google
# Move the protoc include files to the final location and set the
# permissions as needed.
sudo mv ./include/google/protobuf /usr/local/include/google/
sudo chmod -R a+rX /usr/local/include/google/protobuf
)
rm -rf /tmp/protoc
else
echo "protoc already installed"
fi
}
DESIRED_GO_MAJOR_VERISON=1
DESIRED_GO_MINOR_VERISON=22
DESIRED_GO_VERSION="$DESIRED_GO_MAJOR_VERISON.$DESIRED_GO_MINOR_VERISON"
# Evaluates to truthy if go is installed and
# >=$DESIRED_GO_VERSION. Evaluates to falsey else.
has_recent_go() {
which go >/dev/null || return 1
go_version=`go version`
go_major_version=`expr "$go_version" : '.*go\([0-9]*\)'`
go_minor_version=`expr "$go_version" : '.*go[0-9]*\.\([0-9]*\)'`
[ "$go_major_version" -gt "$DESIRED_GO_MAJOR_VERISON" \
-o "$go_minor_version" -ge "$DESIRED_GO_MINOR_VERISON" ]
}
# pip-install something globally. Recent pythons don't let you do
# that (https://peps.python.org/pep-0668/), but we do it anyway.
# $@: the arguments to `pip install`.
pip3_install() {
# If brew is installed, use its pip3 instead of the system pip3.
if which brew >/dev/null 2>&1 && [ -e "$(brew --prefix)/bin/pip3" ]; then
PIP3=$(brew --prefix)/bin/pip3
else
PIP3=pip3
fi
"$PIP3" install --break-system-packages "$@" >/dev/null 2>&1 \
|| "$PIP3" install "$@"
}
maybe_generate_ssh_keys() {
# Create a public key if need be.
info "Checking for ssh keys"
mkdir -p ~/.ssh
if [ -s ~/.ssh/id_rsa ] || [ -s ~/.ssh/id_ecdsa ]; then
# TODO(ebrown): Verify these key(s) have passphrases on them
success "Found existing ssh keys"
else
echo
echo "Creating your ssh key pair for this machine"
echo "Please DO NOT use an empty passphrase"
APPLE_SSH_ADD_BEHAVIOR=macos ssh-keygen -t ecdsa -f ~/.ssh/id_ecdsa
# Old: ssh-keygen -q -N "" -t rsa -f ~/.ssh/id_rsa
success "Generated an rsa ssh key at ~/.ssh/id_ecdsa"
echo "Your ssh public key is:"
cat ~/.ssh/id_ecdsa.pub
echo "Please manually copy this public key to https://github.com/settings/keys."
read -p "Press enter when you've done this..."
fi
# Add the keys to the keychain if needed
if [ -z "`ssh-add -l 2>/dev/null`" ]; then
ssh-add >/dev/null || {
# ssh-agent isn't running, let's fix that
eval $(ssh-agent -s)
ssh-add
}
success "Added key to your keychain"
fi
return 0
}
# If we exit unexpectedly, log this warning.
# Scripts should call "trap exit_warning EXIT" near the top to enable,
# then "trap - EXIT" just before exiting on success.
exit_warning() {
echo "*** FATAL ERROR: khan-dotfiles crashed! ***"
echo "*** Please check the dev setup docs for common ***"
echo "*** errors, or send the output above to @dev-support. ***"
echo "*** Once you've resolved the problem, re-run 'make'. ***"
echo "*** Khan dev tools WILL NOT WORK until you do! ***"
}