This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
mac
executable file
·387 lines (326 loc) · 11.5 KB
/
mac
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
#!/bin/bash
# Welcome to the 18F laptop script!
# Be prepared to turn your laptop (or desktop)
# into an awesome development machine.
fancy_echo() {
# shellcheck disable=SC2039
local fmt="$1"; shift
# shellcheck disable=SC2059
printf "\n$fmt\n" "$@"
}
append_to_file() {
# shellcheck disable=SC2039
local file="$1"
# shellcheck disable=SC2039
local text="$2"
if [ "$file" = "$HOME/.zshrc" ]; then
if [ -w "$HOME/.zshrc.local" ]; then
file="$HOME/.zshrc.local"
else
file="$HOME/.zshrc"
fi
fi
if ! grep -qs "^$text$" "$file"; then
printf "\n%s\n" "$text" >> "$file"
fi
}
append_to_shell_file() {
append_to_file "$shell_file" "$1"
}
create_and_set_shell_file() {
shell_file="$1"
if [ ! -f "$shell_file" ]; then
touch "$shell_file"
fi
}
create_zshrc_and_set_it_as_shell_file() {
create_and_set_shell_file "$HOME/.zshrc"
}
create_fishconfig_and_set_it_as_shell_file() {
create_and_set_shell_file "$HOME/.config/fish/config.fish"
}
create_bash_profile_and_set_it_as_shell_file() {
create_and_set_shell_file "$HOME/.bash_profile"
}
# shellcheck disable=SC2154
trap 'ret=$?; test $ret -ne 0 && printf "failed\n\n" >&2; exit $ret' EXIT
set -e
if [ ! -d "$HOME/.bin/" ]; then
mkdir "$HOME/.bin"
fi
case "$SHELL" in
*/fish) :
create_fishconfig_and_set_it_as_shell_file
;;
*/zsh) :
create_zshrc_and_set_it_as_shell_file
;;
*)
create_bash_profile_and_set_it_as_shell_file
if [ -z "$CI" ]; then
bold=$(tput bold)
normal=$(tput sgr0)
echo "Want to switch your shell from the default ${bold}bash${normal} to ${bold}zsh${normal}?"
echo "Both work fine for development, and ${bold}zsh${normal} has some extra "
echo "features for customization and tab completion."
echo "If you aren't sure or don't care, we recommend ${bold}zsh${normal}."
echo "Note that you can always switch back to ${bold}bash${normal} if you change your mind."
echo "Please see the README for instructions."
echo -n "Press ${bold}y${normal} to switch to zsh, ${bold}n${normal} to keep bash: "
read -r -n 1 response
if [ "$response" = "y" ]; then
create_zshrc_and_set_it_as_shell_file
if grep "$(command -v zsh)" > /dev/null 2>&1 < /etc/shells; then
fancy_echo "=== Getting ready to change your shell to zsh. Please enter your password to continue. ==="
echo "=== Note that there won't be visual feedback when you type your password. Type it slowly and press return. ==="
echo "=== Press control-c to cancel ==="
chsh -s "$(command -v zsh)"
else
printf "\n\n"
echo "Can't switch shells automatically in this case. The path to zsh isn't in"
echo "the list of allowed shells. To manually switch to zsh, enter the following"
echo "two lines into your terminal (in another tab, or when this script is done):"
echo ""
echo "sudo echo \"\$(command -v zh)\" >> /etc/shells"
echo "chsh -s \"\$(command -v zs)\""
sleep 3
fi
else
fancy_echo "Shell will not be changed."
fi
else
fancy_echo "CI System detected, will not change shells"
fi
;;
esac
brew_is_installed() {
brew list -1 | grep -Fqx "$1"
}
tap_is_installed() {
brew tap | grep -Fqx "$1"
}
gem_install_or_update() {
if gem list "$1" | grep "^$1 ("; then
fancy_echo "Updating %s ..." "$1"
gem update "$@"
else
fancy_echo "Installing %s ..." "$1"
gem install "$@"
fi
}
latest_installed_ruby() {
find "$HOME/.rubies" -maxdepth 1 -name 'ruby-*' | tail -n1 | grep -E -o '\d+\.\d+\.\d+'
}
switch_to_latest_ruby() {
# shellcheck disable=SC1091
. /usr/local/share/chruby/chruby.sh
chruby "ruby-$(latest_installed_ruby)"
}
laptop_script_url="https://raw.githubusercontent.com/18F/laptop/master/laptop"
case "$SHELL" in
*/fish) :
append_to_shell_file "alias laptop='bash (curl -s $laptop_script_url|psub)'"
# shellcheck disable=SC2016
append_to_shell_file 'set -xg PATH $HOME/.bin $PATH'
;;
*) :
append_to_shell_file "alias laptop='bash <(curl -s $laptop_script_url)'"
# shellcheck disable=SC2016
append_to_shell_file 'export PATH="$HOME/.bin:$PATH"'
;;
esac
if ! command -v brew >/dev/null; then
fancy_echo "Installing Homebrew ..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
case "$SHELL" in
*/fish) :
# noop, fish ships with /usr/local/bin in a good spot in the path
;;
*) :
# shellcheck disable=SC2016
append_to_shell_file 'export PATH="/usr/local/bin:$PATH"'
;;
esac
else
fancy_echo "Homebrew already installed. Skipping ..."
fi
# Remove brew-cask since it is now installed as part of brew tap caskroom/cask.
# See https://github.com/caskroom/homebrew-cask/releases/tag/v0.60.0
if brew_is_installed 'brew-cask'; then
brew uninstall --force 'brew-cask'
fi
if tap_is_installed 'caskroom/versions'; then
brew untap caskroom/versions
fi
fancy_echo "Updating Homebrew..."
brew update
fancy_echo "Verifying the Homebrew installation..."
if brew doctor; then
fancy_echo "Your Homebrew installation is good to go."
else
fancy_echo "Your Homebrew installation reported some errors or warnings."
echo "If the warnings are related to Python, you can ignore them."
echo "Otherwise, review the Homebrew messages to see if any action is needed."
fi
if brew_is_installed 'cloudfoundry-cli'; then
brew uninstall --force cloudfoundry-cli
fi
fancy_echo "Installing formulas and casks from the Brewfile ..."
if brew bundle --file="$HOME/Brewfile"; then
fancy_echo "All formulas and casks were installed successfully."
else
fancy_echo "Some formulas or casks failed to install."
echo "This is usually due to one of the Mac apps being already installed,"
echo "in which case, you can ignore these errors."
fi
case "$SHELL" in
*/fish) :
mkdir -p "$HOME/.config/fish/functions"
hub alias fish > "$HOME/.config/fish/functions/git.fish"
;;
*) :
# shellcheck disable=SC2016
append_to_shell_file 'eval "$(hub alias -s)"'
;;
esac
fancy_echo 'Checking on Node.js installation...'
case "$SHELL" in
*/fish) :
fancy_echo "We recommend following the steps at https://github.com/FabioAntunes/fish-nvm to install nvm in fish"
;;
esac
if ! brew_is_installed "node"; then
if command -v n > /dev/null; then
fancy_echo "We recommend using \`nvm\` and not \`n\`."
fancy_echo "See https://pages.18f.gov/frontend/#install-npm"
elif ! command -v nvm > /dev/null; then
fancy_echo 'Installing nvm and lts Node.js and npm...'
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
# shellcheck source=/dev/null
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# nvm is a bash script itself, some commands of which may fail WITHOUT
# causing the whole operation to fail. To accomdate that, disable exit on
# any nonzero exit code while nvm runs.
set +e
nvm install --lts
# Turn it back on when nvm is done, since the rest of this script may have
# been written assuming this behavior.
set -e
else
fancy_echo 'version manager detected. Skipping...'
fi
else
brew bundle --file=- <<EOF
brew 'node'
EOF
fi
fancy_echo '...Finished Node.js installation checks.'
fancy_echo 'Checking on Python installation...'
if ! brew_is_installed "python3"; then
brew bundle --file=- <<EOF
brew 'zlib'
brew 'pyenv'
brew 'pyenv-virtualenv'
brew 'pyenv-virtualenvwrapper'
EOF
case "$SHELL" in
*/fish) :
append_to_shell_file "status --is-interactive; and source (pyenv init -|psub)"
append_to_shell_file "status --is-interactive; and source (pyenv virtualenv-init -|psub)"
;;
*) :
# shellcheck disable=SC2016
append_to_shell_file 'if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi'
# shellcheck disable=SC2016
append_to_shell_file 'if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi'
esac
# pyenv currently doesn't have a convenience version to use, e.g., "latest",
# so we check for the latest version against Homebrew instead.
latest_python_3="$(brew info python3 | grep -E -o "3\.\d+\.\d+" | head -1)"
if ! pyenv versions | ag "$latest_python_3" > /dev/null; then
# Starting with macOS 10.14 (Mojave), the header files for system libraries
# have been moved. Rather than hack the header paths based on OS version,
# just install zlib with brew and build against that directly for now.
brew install zlib
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
pyenv install "$latest_python_3"
pyenv global "$latest_python_3"
pyenv rehash
fi
else
brew bundle --file=- <<EOF
brew 'python3'
EOF
fi
if ! brew_is_installed "pyenv-virtualenvwrapper"; then
if ! pip3 list | ag "virtualenvwrapper" > /dev/null; then
case "$SHELL" in
*/fish) :
fancy_echo "virtualenvwrapper is not compatible with fish"
;;
*) :
fancy_echo 'Installing virtualenvwrapper...'
pip3 install virtualenvwrapper
append_to_shell_file 'export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3'
append_to_shell_file 'export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv'
append_to_shell_file 'source /usr/local/bin/virtualenvwrapper.sh'
;;
esac
fi
fi
fancy_echo '...Finished Python installation checks.'
fancy_echo 'Checking on Ruby installation...'
append_to_file "$HOME/.gemrc" 'gem: --no-document'
case "$SHELL" in
*/fish) :
fancy_echo "We recommend following the steps at https://github.com/JeanMertz/chruby-fish to install chruby in fish"
;;
# For all other shells, install chruby as long as rbenv is not installed
*) :
if command -v rbenv >/dev/null || command -v rvm >/dev/null; then
fancy_echo 'We recommend chruby and ruby-install over RVM or rbenv'
else
if ! brew_is_installed "chruby"; then
fancy_echo 'Installing chruby, ruby-install, and the latest Ruby...'
brew bundle --file=- <<EOF
brew 'chruby'
brew 'ruby-install'
EOF
append_to_shell_file 'source /usr/local/share/chruby/chruby.sh'
append_to_shell_file 'source /usr/local/share/chruby/auto.sh'
ruby-install ruby
append_to_shell_file "chruby ruby-$(latest_installed_ruby)"
switch_to_latest_ruby
else
brew bundle --file=- <<EOF
brew 'chruby'
brew 'ruby-install'
EOF
fancy_echo 'Checking if a newer version of Ruby is available...'
switch_to_latest_ruby
ruby-install --latest > /dev/null
latest_stable_ruby="$(cat < "$HOME/.cache/ruby-install/ruby/stable.txt" | tail -n1)"
if ! [ "$latest_stable_ruby" = "$(latest_installed_ruby)" ]; then
fancy_echo "Installing latest stable Ruby version: $latest_stable_ruby"
ruby-install ruby
else
fancy_echo 'You have the latest version of Ruby'
fi
fi
fi
esac
fancy_echo 'Updating Rubygems...'
gem update --system
gem_install_or_update 'bundler'
fancy_echo "Configuring Bundler ..."
number_of_cores=$(sysctl -n hw.ncpu)
bundle config --global jobs $((number_of_cores - 1))
fancy_echo '...Finished Ruby installation checks.'
curl -s https://raw.githubusercontent.com/18F/laptop/master/seekrets-install | sh -
if [ -f "$HOME/.laptop.local" ]; then
# shellcheck source=/dev/null
. "$HOME/.laptop.local"
fi
fancy_echo 'All done!'