-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·354 lines (312 loc) · 10.9 KB
/
bootstrap.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
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
#!/usr/bin/env bash
main() {
ask_for_sudo
install_xcode_command_line_tools # to get "git", needed for clone_dotfiles_repo
clone_dotfiles_repo
install_homebrew
install_packages_with_brewfile
# change_shell_to_fish
# install_pip_packages
# install_npm_packages
# install_vscode_extensions
setup_symlinks
setup_macOS_defaults
# update_login_items
}
DOTFILES_REPO=~/personal/dotfiles
function ask_for_sudo() {
echo_info "Prompting for sudo password"
if sudo --validate; then
# Keep-alive
while true; do
sudo --non-interactive true
sleep 10
kill -0 "$$" || exit
done 2>/dev/null &
echo_success "Sudo password updated"
else
error "Sudo password update failed"
exit 1
fi
}
function install_xcode_command_line_tools() {
echo_info "Installing Xcode command line tools"
if softwareupdate --history | grep --silent "Command Line Tools"; then
echo_success "Xcode command line tools already exists"
else
xcode-select --install
read -n 1 -s -r -p "Press any key once installation is complete"
if softwareupdate --history | grep --silent "Command Line Tools"; then
echo_success "Xcode command line tools installation succeeded"
else
error "Xcode command line tools installation failed"
exit 1
fi
fi
}
function install_homebrew() {
echo_info "Installing Homebrew"
if hash brew 2>/dev/null; then
echo_success "Homebrew already exists"
else
url=https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
echo ${url}
if yes | /bin/bash -c "$(curl -fsSL ${url})"; then
echo_success "Homebrew installation succeeded"
else
error "Homebrew installation failed"
exit 1
fi
fi
}
function install_packages_with_brewfile() {
echo_info "Installing Brewfile packages"
TAP=${DOTFILES_REPO}/brew/Brewfile_tap
BREW=${DOTFILES_REPO}/brew/Brewfile_brew
CASK=${DOTFILES_REPO}/brew/Brewfile_cask
MAS=${DOTFILES_REPO}/brew/Brewfile_mas
if hash parallel 2>/dev/null; then
echo_substep "parallel already exists"
else
if brew install parallel &>/dev/null; then
printf 'will cite' | parallel --citation &>/dev/null
echo_substep "parallel installation succeeded"
else
error "parallel installation failed"
exit 1
fi
fi
if (
echo $TAP
echo $BREW
echo $CASK
echo $MAS
) | parallel --verbose --linebuffer -j 4 brew bundle check --file={} &>/dev/null; then
echo_success "Brewfile packages are already installed"
else
if brew bundle --file="$TAP"; then
echo_substep "Brewfile_tap installation succeeded"
export HOMEBREW_CASK_OPTS="--no-quarantine"
if (
echo $BREW
echo $CASK
echo $MAS
) | parallel --verbose --linebuffer -j 3 brew bundle --file={}; then
echo_success "Brewfile packages installation succeeded"
else
error "Brewfile packages installation failed"
exit 1
fi
else
error "Brewfile_tap installation failed"
exit 1
fi
fi
}
function change_shell_to_fish() {
echo_info "Fish shell setup"
if grep --quiet fish <<<"$SHELL"; then
echo_success "Fish shell already exists"
else
user=$(whoami)
echo_substep "Adding Fish executable to /etc/shells"
if grep --fixed-strings --line-regexp --quiet \
"/usr/local/bin/fish" /etc/shells; then
echo_substep "Fish executable already exists in /etc/shells"
else
if echo /usr/local/bin/fish | sudo tee -a /etc/shells >/dev/null; then
echo_substep "Fish executable successfully added to /etc/shells"
else
error "Failed to add Fish executable to /etc/shells"
exit 1
fi
fi
echo_substep "Switching shell to Fish for \"${user}\""
if sudo chsh -s /usr/local/bin/fish "$user"; then
echo_success "Fish shell successfully set for \"${user}\""
else
error "Please try setting Fish shell again"
fi
fi
}
# function install_pip_packages() {
# echo_info "Installing pip packages"
# REQUIREMENTS_FILE=${DOTFILES_REPO}/pip/requirements.txt
# if pip3 install -r "$REQUIREMENTS_FILE" 1>/dev/null; then
# echo_success "pip packages successfully installed"
# else
# error "pip packages installation failed"
# exit 1
# fi
# }
# function install_npm_packages() {
# npm_packages=(fkill-cli)
# # npm_packages=(fkill-cli vtop karma-cli resume-cli)
# echo_info "Installing npm packages \"${npm_packages[*]}\""
# node_list_outcome=$(npm list -g --depth=0)
# for package_to_install in "${npm_packages[@]}"; do
# if echo "$node_list_outcome" |
# grep --ignore-case "$package_to_install" &>/dev/null; then
# echo_substep "\"${package_to_install}\" already exists"
# else
# if npm install --global "$package_to_install"; then
# echo_substep "Package \"${package_to_install}\" installation succeeded"
# else
# error "Package \"${package_to_install}\" installation failed"
# exit 1
# fi
# fi
# done
# echo_success "node packages successfully installed"
# }
# function install_vscode_extensions() {
# extensions=(
# idleberg.applescript
# betterthantomorrow.calva
# esbenp.prettier-vscode
# ms-azuretools.vscode-docker
# dbaeumer.vscode-eslint
# skyapps.fish-vscode
# eamodio.gitlens
# ms-vsliveshare.vsliveshare
# shd101wyy.markdown-preview-enhanced
# prettier
# humao.rest-client
# foxundermoon.shell-format
# jock.svg
# vscodevim.vim
# # borkdude.clj-kondo
# # avli.clojure
# )
# echo_info "Installing vscode extensions"
# extensions_outcome=$(code --list-extensions)
# for extension_to_install in "${extensions[@]}"; do
# if echo "$extensions_outcome" |
# grep --ignore-case "$extension_to_install" &>/dev/null; then
# echo_substep "\"${extension_to_install}\" already exists"
# else
# if code --install-extension "$extension_to_install"; then
# echo_substep "Extension \"${extension_to_install}\" installation succeeded"
# else
# error "Extension \"${extension_to_install}\" installation failed"
# exit 1
# fi
# fi
# done
# echo_success "vscode extensions successfully installed"
# }
function clone_dotfiles_repo() {
echo_info "Cloning dotfiles repository into ${DOTFILES_REPO}"
if test -e $DOTFILES_REPO; then
echo_substep "${DOTFILES_REPO} already exists"
pull_latest $DOTFILES_REPO
echo_success "Pull successful in ${DOTFILES_REPO} repository"
else
url=https://github.com/Trosterud/dotfiles.git
if git clone "$url" $DOTFILES_REPO &&
git -C $DOTFILES_REPO remote set-url origin [email protected]:Trosterud/dotfiles.git; then
echo_success "Dotfiles repository cloned into ${DOTFILES_REPO}"
else
error "Dotfiles repository cloning failed"
exit 1
fi
fi
}
function pull_latest() {
echo_substep "Pulling latest changes in ${1} repository"
if git -C $1 pull origin master &>/dev/null; then
return
else
error "Please pull latest changes in ${1} repository manually"
fi
}
function setup_symlinks() {
APPLICATION_SUPPORT=~/Library/Application\ Support
echo_info "Setting up symlinks"
symlink "zshrc" ${DOTFILES_REPO}/.zshrc ~/.zshrc
# symlink "git" ${DOTFILES_REPO}/git/gitconfig ~/.gitconfig
# Disable shell login message
# symlink "hushlogin" /dev/null ~/.hushlogin
# symlink "fish:completions" ${DOTFILES_REPO}/fish/completions ~/.config/fish/completions
# symlink "fish:functions" ${DOTFILES_REPO}/fish/functions ~/.config/fish/functions
# symlink "fish:config.fish" ${DOTFILES_REPO}/fish/config.fish ~/.config/fish/config.fish
# symlink "fish:abbreviations.fish" ${DOTFILES_REPO}/fish/abbreviations.fish ~/.config/fish/abbreviations.fish
# symlink "fish:oh_my_fish" ${DOTFILES_REPO}/fish/oh_my_fish ~/.config/omf
# Visual Studio Code
# symlink "vscode:settings.json" ${DOTFILES_REPO}/vscode/settings.json ~/Library/Application\ Support/Code/User/settings.json
# # symlink "vscode:keybindings.json" ${DOTFILES_REPO}/vscode/keybindings.json /Users/pawelgrzybek/Library/Application\ Support/Code/User/keybindings.json
# # symlink "vscode:snippets" ${DOTFILES_REPO}/vscode/snippets/ /Users/pawelgrzybek/Library/Application\ Support/Code/User
echo_success "Symlinks successfully setup"
}
function symlink() {
application=$1
point_to=$2
destination=$3
destination_dir=$(dirname "$destination")
if test ! -e "$destination_dir"; then
echo_substep "Creating ${destination_dir}"
mkdir -p "$destination_dir"
fi
if rm -rf "$destination" && ln -s "$point_to" "$destination"; then
echo_substep "Symlinking for \"${application}\" done"
else
error "Symlinking for \"${application}\" failed"
exit 1
fi
}
function setup_macOS_defaults() {
echo_info "Updating macOS defaults"
current_dir=$(pwd)
cd ${DOTFILES_REPO}/macOS
if bash defaults.sh; then
cd $current_dir
echo_success "macOS defaults updated successfully"
else
cd $current_dir
error "macOS defaults update failed"
exit 1
fi
}
# function update_login_items() {
# echo_info "Updating login items"
# if osascript ${DOTFILES_REPO}/macOS/login_items.applescript &>/dev/null; then
# echo_success "Login items updated successfully "
# else
# error "Login items update failed"
# exit 1
# fi
# }
function coloredEcho() {
local exp="$1"
local color="$2"
local arrow="$3"
if ! [[ $color =~ ^[0-9]$ ]]; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white | *) color=7 ;; # white or invalid color
esac
fi
tput bold
tput setaf "$color"
echo "$arrow $exp"
tput sgr0
}
function echo_info() {
coloredEcho "$1" blue "========>"
}
function echo_substep() {
coloredEcho "$1" magenta "===="
}
function echo_success() {
coloredEcho "$1" green "========>"
}
function error() {
coloredEcho "$1" red "========>"
}
main "$@"