forked from mk12/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupmacos.sh
More file actions
executable file
·345 lines (296 loc) · 8.79 KB
/
setupmacos.sh
File metadata and controls
executable file
·345 lines (296 loc) · 8.79 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
#!/bin/bash
set -ufo pipefail
# Options
mode=setup
primary=false
br=$(brew --prefix)/bin
pj=~/Projects
secrets=~/.profile.local
backups=~/Dropbox/Backups
homebrew_formulas=(
"bat"
"clang-format"
"deno"
"dust"
"emacs"
"entr"
"exa"
"fd"
"fish"
"gawk"
"git"
"git-delta:delta"
"hexyl"
"htop"
"jq"
"ledger"
"neovim:nvim"
"oath-toolkit:oathtool"
"python@3.9:python3"
"reattach-to-user-namespace"
"ripgrep:rg"
"sd"
"shellcheck"
"tldr"
"tmux"
"topgrade"
"trash"
"vim"
)
homebrew_casks=(
"ballast"
"bitwarden"
"dropbox:Dropbox"
"hammerspoon:Hammerspoon"
"kitty"
"visual-studio-code:Visual Studio Code"
)
usage() {
cat <<EOS
Usage: $(basename "$0") [options]
This script configures a new Mac with all the settings and software I want. It
can be run multiple times safely.
Options:
-h Print this help message
-b Print comparision of currently installed homebrew formulas to the ones
that this script would install.
-p Setup as the primary Mac. This includes some additional configuration
that should only be present on one machine at a time.
EOS
}
step() {
echo -e "\x1b[31m[SETUP] $*\x1b[0m"
}
clone_git_repo() {
[[ -d "$2" ]] || "$br/git" clone "git@github.com:$1" "$2"
}
clone_git_repo_from_bundle() {
if ! [[ -d "$pj/$1" ]]; then
bundle="$backups/$1.gitbundle"
[[ -f "$bundle" ]] || die "$bundle: not found"
git clone -b master "$bundle" "$pj/$1"
fi
}
setup_xcode_cli() {
step "Installing Xcode command line tools"
xcode-select --print-path > /dev/null || xcode-select --install
}
setup_ssh_github() {
step "Setting up SSH"
if ! [[ -f ~/.ssh/id_rsa ]]; then
step "Generating an SSH key"
echo -n "Enter email address to use with Git: "
read -r email_address
ssh-keygen -t rsa -b 4096 -C "$email_address"
step "Adding SSH key to keychain"
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa
step "Adding SSH key to GitHub"
pbcopy < ~/.ssh/id_rsa.pub
echo "Copied public SSH key to the clipboard"
echo -n "Press return to open the browser to GitHub: "
read -r
open "https://github.com/settings/keys"
echo -n "Press return when ready: "
read -r
fi
}
setup_my_repos() {
step "Creating $pj directory"
mkdir -p "$pj"
step "Cloning repositories"
clone_git_repo mk12/dotfiles "$pj/dotfiles"
clone_git_repo mk12/scripts "$pj/scripts"
clone_git_repo_from_bundle journal
clone_git_repo_from_bundle finance
step "Building scripts"
make -C "$pj/scripts"
step "Symlinking dotfiles"
"$pj/dotfiles/link.sh"
}
setup_homebrew_github_api_token() {
step "Setting Homebrew GitHub API Token"
if ! grep -q HOMEBREW_GITHUB_API_TOKEN "$secrets"; then
echo "Create an access token called HOMEBREW_GITHUB_API_TOKEN for X"
echo -n "Press return to open the browser to GitHub: "
read -r
open "https://github.com/settings/tokens/new"
echo -n "Press return once you have copied the token: "
read -r
{ echo -n 'export HOMEBREW_GITHUB_API_TOKEN="'; pbpaste; echo '"'; } \
>> "$secrets"
fi
}
setup_simplenote_backup() {
clone_git_repo Simperium/simperium-python "$pj/simperium-python"
clone_git_repo hiroshi/simplenote-backup "$pj/simplenote-backup"
step "Setting Simplenote API Token"
if ! grep -q SIMPLENOTE_API_TOKEN "$secrets"; then
property="simperium_opts.token"
echo -n "$property" | pbcopy
echo "The string '$property' has been copied to the clipboard"
echo "Paste it into the web console in Simplenote and copy the result"
echo -n "Press return to open the browser to Simplenote: "
read -r
open "https://app.simplenote.com/"
echo -n "Press return once you have copied the token: "
read -r
{ echo -n 'export SIMPLENOTE_API_TOKEN="'; pbpaste; echo '"'; } \
>> "$secrets"
fi
"$pj/scripts/backup.sh" -i
}
setup_homebrew() {
step "Installing Homebrew"
[[ -x "$br/brew" ]] || /usr/bin/ruby -e "$(curl -fsSL \
https://raw.githubusercontent.com/Homebrew/install/master/install)"
step "Installing Homebrew formulas"
for entry in "${homebrew_formulas[@]}"; do
formula=${entry%:*}
binary=${entry#*:}
[[ -x "$br/$binary" ]] || "$br/brew" install "$formula"
done
step "Installing Homebrew casks"
for entry in "${homebrew_casks[@]}"; do
cask=${entry%:*}
app=${entry#*:}
[[ -d "/Applications/$app.app" ]] || "$br/brew" install --cask "$cask"
done
}
setup_rust() {
step "Installing Rust"
[[ -x ~/.cargo/bin/rustup ]] || curl "https://sh.rustup.rs" -sSf | sh
}
setup_python() {
step "Installing Python packages"
"$br/pip3" install pynvim
}
setup_terminfo() {
file=$(mktemp)
step "Installing xterm-kitty.terminfo"
if ! TERMINFO="" infocmp xterm-kitty &> /dev/null; then
if [[ "${TERMINFO:-}" == '/Applications/kitty.app'* ]]; then
infocmp xterm-kitty > "$file"
tic -x -o ~/.terminfo "$file"
else
echo "WARNING: Did not install xterm-kitty.terminfo"
echo "WARNING: Re-run $0 in kitty to install it"
fi
fi
step "Installing tmux-256color.terminfo"
if ! infocmp tmux-256color &> /dev/null; then
prefix=/usr/local/opt/ncurses
[[ -d "$prefix" ]] || "$br/brew" install ncurses
"$prefix/bin/infocmp" -A "$prefix/share/terminfo" tmux-256color \
> "$file"
tic -o ~/.terminfo "$file"
fi
rm -f "$file"
}
setup_tmux() {
step "Installing Tmux plugins"
clone_git_repo tmux-plugins/tpm ~/.tmux/plugins/tpm
}
setup_vim() {
step "Installing Vim plugins"
"$br/vim" +PlugInstall +qall
"$br/nvim" +PlugInstall +qall
}
setup_kitty() {
step "Setting up kitty"
clone_git_repo mk12/base16-kitty "$pj/base16-kitty"
clone_git_repo mk12/base16-solarized-scheme "$pj/base16-solarized-scheme"
if ! [[ -d "$pj/base16-kitty/colors" ]]; then
"$pj/base16-kitty/register.sh" "$pj/base16-solarized-theme"
"$pj/base16-kitty/update.sh"
"$pj/base16-kitty/build.sh"
fi
if ! [[ -f ~/.config/kitty/colors.conf ]]; then
echo "include $HOME/Projects/base16-kitty/colors/base16-onedark.conf" \
> ~/.config/kitty/colors.conf
fi
}
setup_hammerspoon() {
step "Installing SpoonInstall"
url="https://github.com/Hammerspoon/Spoons/raw/master/Spoons/SpoonInstall.spoon.zip"
if ! [[ -d ~/.hammerspoon/Spoons/SpoonInstall.spoon ]]; then
dest=$(mktemp)
curl -fsSL "$url" > "$dest"
unzip "$dest" -d ~/.hammerspoon/Spoons
rm -f "$dest"
fi
}
setup_fish() {
step "Changing login shell to fish"
dscl . -read ~ UserShell | grep -q "$br/fish" \
|| sudo chsh -s "$br/fish" "$USER"
step "Setting fish features"
fish -c "set -U fish_features stderr-nocaret qmark-noglob regex-easyesc"
step "Installing fisher"
fish <<EOS
if not functions -q fisher
curl -sL https://git.io/fisher | source
fisher install jorgebucaran/fisher
fisher update
end
EOS
}
setup_everything() {
setup_xcode_cli
setup_ssh_github
setup_my_repos
setup_homebrew_github_api_token
setup_homebrew
setup_python
setup_rust
setup_terminfo
setup_tmux
setup_vim
setup_kitty
setup_hammerspoon
setup_fish
if [[ "$primary" == true ]]; then
setup_simplenote_backup
fi
step "Finished"
echo "Manual steps:"
echo " 1. Install apps from the App Store."
echo " 2. Configure System Preferences."
echo " 3. Set up Time Machine backups."
}
print_homebrew_info() {
dir=$(mktemp -d)
printf "%s\n" "${homebrew_formulas[@]%:*}" | sort > "$dir/golden"
printf "%s\n" "${homebrew_casks[@]%:*}" | sort > "$dir/golden_cask"
"$br/brew" list --formula | sort > "$dir/list"
"$br/brew" list --cask | sort > "$dir/list_cask"
"$br/brew" leaves | sort > "$dir/leaves"
echo "Uninstalled"
echo "==========="
comm -23 "$dir/golden" "$dir/list"
comm -23 "$dir/golden_cask" "$dir/list_cask" | sed 's/^/cask: /'
echo
echo "Extraneous"
echo "=========="
comm -13 "$dir/golden" "$dir/leaves"
comm -13 "$dir/golden_cask" "$dir/list_cask" | sed 's/^/cask: /'
rm -f "$dir"/{golden{,_cask},list{,_cask},leaves}
rmdir "$dir"
}
main() {
case $mode in
setup) setup_everything ;;
brew) print_homebrew_info ;;
esac
}
while getopts "hbp" opt; do
case $opt in
h) usage; exit 0 ;;
b) mode=brew ;;
p) primary=true ;;
*) exit 1 ;;
esac
done
shift $((OPTIND - 1))
[[ $# -eq 0 ]] || die "too many arguments"
main