-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
404 lines (294 loc) · 9.56 KB
/
setup
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
#!/bin/bash
# Only proceed if Xcode Command Line Tools is installed.
if ! xcode-select -p &>/dev/null; then
printf "Please install Xcode Command Line Tools first:\nxcode-select --install\n" && exit 1
fi
# Define variables.
mac_setup=$HOME/.mac-setup # Installation directory.
github_username=caiwilliamson
# Clone mac-setup if it doesn't already exist.
if [ ! -d "$mac_setup" ]; then
mkdir "$mac_setup"
git clone --recurse-submodules https://github.com/$github_username/mac-setup.git "$mac_setup"
fi
cd "$mac_setup" || exit 1
# Include utilities.
. utilities
print_multiline "$(start_notice)"
read -erp "Enter \"yes\" to continue: " choice
[ "$choice" = "yes" ] || exit 1
############
# Dotfiles #
############
print_heading "Dotfiles"
# Symlink dotfile directories and files.
if ! find ~/.mac-setup/dotfiles -mindepth 1 -maxdepth 1 | while read -r target; do
link_name=$HOME/$(basename "$target")
create_symlink "$target" "$link_name"
done; then
exit 1 # Exit if any symlinks fail.
fi
############
# Homebrew #
############
print_heading "Homebrew"
# Install Homebrew.
if [ ! "$(command -v /usr/local/bin/brew)" ] &&
[ ! "$(command -v /opt/homebrew/bin/brew)" ]; then
if ! /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"; then
# Exit if installation fails.
print_error "Homebrew" && exit 1
fi
fi
print_success "Homebrew"
# Locate Homebrew installation.
[ -d /opt/homebrew ] &&
homebrew_prefix=/opt/homebrew || # Apple Silicon.
homebrew_prefix=/usr/local # Intel.
# Add brew to PATH and set some environment variables.
eval "$($homebrew_prefix/bin/brew shellenv)"
# Verify installation.
[ "$(which brew)" = $homebrew_prefix/bin/brew ]
print_result $? "Homebrew: Using $homebrew_prefix/bin/brew"
########
# Fish #
########
print_heading "Fish"
brew_install "Fish" "fish"
fish_path=$homebrew_prefix/bin/fish
# Add Fish path to /etc/shells.
if ! grep -q $fish_path /etc/shells; then
sudo sh -c "printf '$fish_path\n' >> /etc/shells"
fi
print_result $? "Fish: Add $fish_path to /etc/shells"
# Change login shell to Fish.
if [ "$SHELL" != $fish_path ]; then
sudo chsh -s $fish_path "$USER"
fi
print_result $? "Fish: Change login shell to $fish_path"
# Verify installation.
[ "$(which fish)" = $fish_path ]
print_result $? "Fish: Using $fish_path"
#######
# Zsh #
#######
print_heading "Zsh"
brew_install "Zsh" "zsh"
zsh_path=$homebrew_prefix/bin/zsh
# Add Zsh path to /etc/shells.
if ! grep -q $zsh_path /etc/shells; then
sudo sh -c "printf '$zsh_path\n' >> /etc/shells"
fi
print_result $? "Zsh: Add $zsh_path to /etc/shells"
# Add a directory for zsh_history otherwise it wont save.
mkdir -p "$HOME"/.local/share/zsh
print_result $? "Zsh: Create $HOME/.local/share/zsh/"
# Verify installation.
[ "$(which zsh)" = $zsh_path ]
print_result $? "Zsh: Using $zsh_path"
########
# asdf #
########
print_heading "asdf"
brew_install "asdf" "asdf"
# Ensure asdf installs config and data to the right location.
export ASDF_CONFIG_FILE="$HOME/.config/asdf/.asdfrc"
export ASDF_DATA_DIR="$HOME/.local/share/asdf/.asdf"
# https://github.com/asdf-vm/asdf-ruby#install
# https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
brew_install "asdf: Ruby dependencies" "openssl@3 readline libyaml gmp autoconf"
RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
export RUBY_CONFIGURE_OPTS
# Add asdf to PATH.
. "$(brew --prefix asdf)"/libexec/asdf.sh
########
# Ruby #
########
print_heading "Ruby"
asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git
case $? in
0|2) print_success "Ruby: Install asdf-ruby plugin";;
*) print_error "Ruby: Install asdf-ruby plugin" && exit 1;;
esac
asdf install ruby latest
print_result $? "Ruby: Install Ruby with asdf"
asdf global ruby latest
print_result $? "Ruby: Set global Ruby with asdf"
###########
# Node.js #
###########
print_heading "Node.js"
asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git
case $? in
0|2) print_success "Node.js: Install asdf-nodejs plugin";;
*) print_error "Node.js: Install asdf-nodejs plugin" && exit 1;;
esac
asdf install nodejs latest
print_result $? "Node.js: Install Node.js with asdf"
asdf global nodejs latest
print_result $? "Node.js: Set global Node.js with asdf"
#######
# Git #
#######
print_heading "Git"
brew_install "Git" "git"
git_path=$homebrew_prefix/bin/git
# Verify installation.
[ "$(which git)" = $git_path ]
print_result $? "Git: Using $git_path"
###################
# GitHub SSH Keys #
###################
print_heading "GitHub SSH Keys"
ssh_key=$HOME/.ssh/github
# Set up new keys if either key (public or private) is missing.
if [ ! -e "$ssh_key" ] || [ ! -e "$ssh_key".pub ]; then
# Generate a new key pair.
key_name="$(hostname)_$(date '+%Y-%m-%d_%H:%M:%S')"
ssh-keygen -t rsa -b 4096 -C "$key_name" -f "$ssh_key"
print_result $? "GitHub SSH Keys: Generate a new key pair"
# Create an SSH config file.
if [ ! -e "$HOME"/.ssh/config ]; then
touch "$HOME"/.ssh/config
fi
cat <<EOF > "$HOME"/.ssh/config
Host *
AddKeysToAgent yes
UseKeychain yes
Host github.com
User git
HostName github.com
IdentityFile $ssh_key
Host gist.github.com
User gist
HostName github.com
IdentityFile $ssh_key
EOF
print_result $? "GitHub SSH Keys: Create an SSH config file: $HOME/.ssh/config"
# Purge all identities from the ssh-agent. Identities will be added
# automatically by the SHH config when needed.
ssh-add -D &>/dev/null
# Add the public key to GitHub. 3 attempts so we don't spam the API
# and get locked out.
public_key=$(cat "$ssh_key".pub)
for (( i=0; i<3; ++i ))
do
key_uploaded="$(curl -f -s \
-u "$github_username" \
--data "{\"title\":\"$key_name\",\"key\":\"$public_key\"}" \
https://api.github.com/user/keys \
)"
if [ -n "$key_uploaded" ]; then
print_success "GitHub SSH Keys: Add the public key to GitHub"
break # Success. Exit loop.
fi
done
if [ -z "$key_uploaded" ]; then
# Failed all 3 attempts to add the public key to GitHub.
print_error "GitHub SSH Keys: Add the public key to GitHub"
# Remove keys.
rm "$ssh_key" "$ssh_key".pub
# Purge all identities from the ssh-agent.
ssh-add -D &>/dev/null
print_notification "Cleaning up..."
print_notification "Removed $ssh_key and $ssh_key.pub"
print_notification "Removed all identities from ssh-agent."
# Exit.
exit 1
fi
else
print_success "GitHub SSH Keys: Existing key pair found"
fi
# Authenticate with GitHub.
ssh -T [email protected] &>/dev/null
# Exit if authentication fails.
[ $? -ne 1 ] && print_error "GitHub SSH Keys: Authenticate with GitHub" && exit 1
print_success "GitHub SSH Keys: Authenticate with GitHub"
# Make mac-setup use the SSH remote url now that SSH is set up.
git remote set-url origin [email protected]:$github_username/mac-setup.git
#########
# Rails #
#########
print_heading "Rails"
# Install Rails.
if [ "$(gem list '^rails$' -i)" = "false" ]; then
if ! gem install rails; then
# Exit if installation fails.
print_error "Rails" && exit 1
fi
fi
print_success "Rails"
##############
# PostgreSQL #
##############
print_heading "PostgreSQL"
brew_install "PostgreSQL" "postgresql"
if [ "$(command -v psql)" ]; then
# Start PostgreSQL.
brew services restart postgresql
print_result $? "PostgreSQL: Started"
# Wait a bit to ensure PostgreSQL has started.
sleep 1
# Create a database for the current user.
if ! psql -lqt | cut -d \| -f 1 | grep -qw "$USER"; then
createdb
fi
print_result $? "PostgreSQL: Create database \"$USER\""
fi
#########
# Redis #
#########
print_heading "Redis"
brew_install "Redis" "redis"
if [ "$(command -v redis-cli)" ]; then
# Start Redis.
brew services restart redis
print_result $? "Redis: Started"
fi
#######
# Vim #
#######
print_heading "Vim"
brew_install "Vim" "vim"
#########################
# Sublime Text Settings #
#########################
print_heading "Sublime Text Settings"
# Symlink Sublime Text preferences.
for sublime_text_user_dir in "$HOME"/Library/Application\ Support/Sublime\ Text*/Packages/User; do
create_symlink "$mac_setup"/app_settings/Sublime\ Text/Preferences.sublime-settings \
"$sublime_text_user_dir"/Preferences.sublime-settings
done
###############################
# Visual Studio Code Settings #
###############################
print_heading "Visual Studio Code Settings"
# Symlink Visual Studio Code settings.
create_symlink "$mac_setup"/app_settings/Visual\ Studio\ Code/settings.json \
"$HOME"/Library/Application\ Support/Code/User/settings.json
##################
# MacOS Settings #
##################
print_heading "MacOS Settings"
# Disable the annoying line marks in Terminal.
defaults write com.apple.Terminal ShowLineMarks -int 0
print_result $? "MacOS Settings: Disable the annoying line marks in Terminal"
# Stop requests to use new disks for Time Machine backups.
defaults write com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool true
print_result $? "MacOS Settings: Stop requests to use new disks for Time Machine backups"
####################
# Terminal Profile #
####################
print_heading "Terminal Profile"
brew_install "Terminal Profile: Install Hack font" "font-hack-nerd-font" "" "cask"
./app_settings/Terminal/set_terminal_profile.applescript
print_result $? "Terminal Profile: Set profile"
#################
# Finishing up! #
#################
print_heading "Finishing up!"
brew update
print_result $? "brew update"
brew upgrade
print_result $? "brew upgrade"
print_multiline "$(end_notice)"