-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap
executable file
·340 lines (313 loc) · 8.44 KB
/
bootstrap
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
#!/usr/bin/env bash
set -euo pipefail
if [ -n "${CI:-}" ]; then
set -x # For better debugging
fi
# consts
# --------------
ROOT_HOME=/var/root
DOT_HOME=~/dotfiles
CARGO_HOME=~/.cargo
VIMPLUG_FILE=$DOT_HOME/vim/autoload/plug.vim
ZPLUG_HOME=~/.zplug
TPM_HOME=~/.tmux/plugins/tpm
FZF_HOME=~/.fzf
REMOTE_URL="https://github.com/vwrs/dotfiles.git"
CONFIG_HTOP=$HOME/.config/htop
CONFIG_NVIM=$HOME/.config/nvim
# colors
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
fi
# functions
# --------------
has() {
type "$1" > /dev/null 2>&1
}
print_dotfiles() {
echo "$BLUE $BOLD"
cat <<\EOF
_ _ __ ___ ___
/' \/' \ / /'___\ __ /\_ \
/\_/\__// / /\ \__//\_\\//\ \ __ ____
\//\/__/ / /\ \ ,__\/\ \ \ \ \ /'__`\ /',__\
/ /__\ \ \_/\ \ \ \_\ \_/\ __//\__, `\
/_//\_\\ \_\ \ \_\/\____\ \____\/\____/
/_/ \/_/ \/_/ \/_/\/____/\/____/\/___/
EOF
echo "$NORMAL"
}
usage() {
echo "$YELLOW"
cat <<\EOF
Commands:
init (download dotfiles, Homebrew (macOS), vim-plug, zplug and tpm)
font-powerline (install powerline patched fonts)
font-nerd (install SauceCodePro Nerd Font)
deploy (symlink (force override) dotfiles)
deploy_root (symlink dotfiles in super user)
quit
EOF
echo "$NORMAL"
}
init_darwin() {
# Rosetta2 for Apple silicon
if [ "$(uname -m)" = "arm64" ]; then
echo "${BOLD}Installing Rosetta2 ...$NORMAL"
sudo softwareupdate --install-rosetta --agree-to-license
fi
# Homebrew
if [ ! -x "$(which brew)" ]; then
echo "${BOLD}Installing Homebrew ...$NORMAL"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/opt/homebrew/bin/brew shellenv)"
else
echo "${BOLD}Homebrew has already been installed.$NORMAL"
fi
if [ -z "${CI:-}" ]; then
read -p "Install Homebrew packages from Brewfile? [Y/n]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
brew bundle --file=~/dotfiles/Brewfile
fi
fi
echo "${BOLD}Setting defaults ...$NORMAL"
# Key Repeat
defaults write -g InitialKeyRepeat -int 14
defaults write -g KeyRepeat -int 1
# QuickLook
defaults write com.apple.finder QLEnableTextSelection -bool true
defaults write com.apple.finder QLHidePanelOnDeactivate -bool true
# ScreenCapture
defaults write com.apple.screencapture name capture
defaults write com.apple.screencapture include-date -bool false
defaults write com.apple.screencapture location ~/screencapture
# Disable animations
# defaults write com.apple.dock autohide-delay -float 0
# defaults write com.apple.dock autohide-time-modifier -float 0
# defaults delete com.apple.dock expose-animation-duration
# defaults write com.apple.dock springboard-show-duration -int 0
# defaults write com.apple.dock springboard-hide-duration -int 0
defaults write com.apple.finder DisableAllAnimations -bool true
defaults write -g NSInitialToolTipDelay -integer 100
}
init() {
cd "$HOME"
# dotfiles
if [ ! -d $DOT_HOME ]; then
echo "Downloading dotfiles..."
if has "git"; then
if git clone --recursive $REMOTE_URL $DOT_HOME; then
echo "${GREEN}Successfully downloaded dotfiles. ✔︎ $NORMAL"
else
echo "${RED}An unexpected error occurred when trying to install git.$NORMAL"
exit 1
fi
else
echo "${RED}Please install git.$NORMAL"
exit 1
fi
else
echo "${BOLD}dotfiles already exists.$NORMAL"
fi
# macOS
if [ "$(uname)" = "Darwin" ]; then
init_darwin
fi
# Rust
# Install Rust and Cargo for dependencies
# https://doc.rust-lang.org/cargo/getting-started/installation.html
if [ ! -d $CARGO_HOME ]; then
echo "${BOLD}Installing Rust ...$NORMAL"
if ! curl https://sh.rustup.rs -sSf | sh; then
echo "${RED}An unexpected error occurred when trying to install Rust.$NORMAL"
fi
else
echo "${BOLD} $CARGO_HOME already exists.$NORMAL"
fi
# vim-plug
if [ ! -f $VIMPLUG_FILE ]; then
echo "Downloading vim-plug ..."
if curl -fLo $VIMPLUG_FILE --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim; then
echo "${GREEN}Successfully downloaded vim-plug at $VIMPLUG_FILE. ✔︎ $NORMAL"
else
echo "${RED}An unexpected error occurred when trying to install vim-plug.$NORMAL"
exit 1
fi
else
echo "${BOLD}vim-plug already exists.$NORMAL"
fi
# zplug
if [ -x "$(which zsh)" ]; then
if [ ! -d $ZPLUG_HOME ]; then
echo "${BOLD}Downloading zplug ...$NORMAL"
if curl -sL --proto-redir -all,https https://raw.githubusercontent.com/zplug/installer/master/installer.zsh | zsh; then
echo "${GREEN}Successfully downloaded zplug in ${ZPLUG_HOME}. ✔︎ $NORMAL"
else
echo "${RED}An unexpected error occurred when trying to install zplug.$NORMAL"
exit 1
fi
else
echo "${BOLD}zplug already exists.$NORMAL"
fi
else
echo "${RED}zsh is currently not installed.$NORMAL"
fi
# tpm
if [ -x "$(which tmux)" ]; then
if [ ! -d $TPM_HOME ]; then
echo "Downloading tpm ..."
if git clone https://github.com/tmux-plugins/tpm $TPM_HOME; then
echo "${GREEN}Successfully downloaded tpm in $TPM_HOME. ✔︎ $NORMAL"
else
echo "${RED}An unexpected error occurred when trying to install tpm.$NORMAL"
exit 1
fi
else
echo "${BOLD}tpm already exists.$NORMAL"
fi
else
echo "${RED}tmux is currently not installed.$NORMAL"
fi
# fzf
if [ ! -d $FZF_HOME ]; then
echo "${BOLD}Downloading fzf ...$NORMAL"
git clone --depth 1 https://github.com/junegunn/fzf.git $FZF_HOME
if ~/.fzf/install; then
echo "${GREEN}Successfully installed fzf in $FZF_HOME. ✔︎ $NORMAL"
else
echo "${RED}An unexpected error occurred when trying to install fzf.$NORMAL"
fi
else
echo "${BOLD}fzf already exists.$NORMAL"
fi
}
symlink_files() {
if [ ! -d $DOT_HOME ]; then
echo 'dotfiles do not exist.'
exit 1
fi
echo "Symlinking dotfiles..."
[ ! -e "$CONFIG_HTOP" ] && mkdir -p "$CONFIG_HTOP"
[ ! -e "$CONFIG_NVIM" ] && mkdir -p "$CONFIG_NVIM"
cd $DOT_HOME
if for f in *
do
# ignore list
[[ $f = "README.md" ]] && continue
[[ $f = "bootstrap" ]] && continue
if [ "$f" = "htoprc" ]; then
ln -snfv "$DOT_HOME/$f" "$CONFIG_HTOP/$f"
elif [ "$f" = "vimrc" ]; then
ln -snfv "$DOT_HOME/$f" "$1/.$f"
# neovim
echo "vim.cmd('source ~/.vimrc')" >> "$CONFIG_NVIM/init.lua"
else
ln -snfv "$DOT_HOME/$f" "$1/.$f"
fi
done
then
echo "${GREEN}Successfully symlink dotfiles. ✔︎ $NORMAL"
else
echo "${RED}An unexpected error occurred when trying to symlink dotfiles.$NORMAL"
exit 1
fi
echo "${BOLD}To enable zsh, please execute 'chsh'.$NORMAL"
}
powerline_fonts() {
git clone --depth=1 https://github.com/powerline/fonts.git
cd fonts
./install.sh
cd ..
rm -rf fonts
}
nerd_fonts() {
git clone --branch=master --depth=1 https://github.com/ryanoasis/nerd-fonts.git
cd nerd-fonts
./install.sh "$1"
cd ..
rm -rf nerd-fonts
}
show_help() {
echo "$YELLOW"
cat <<EOF
usage: $0 [-a|--all] [-h|--help]
$0 ... run in interactive mode
$0 -a|--all ... run all installation scripts except fonts and symlink dotfiles
$0 -h|--help ... show this message
EOF
echo "$NORMAL"
}
# main
# --------------
main() {
usage
echo -n "${BOLD}command: $NORMAL"
read -r command
case $command in
init)
init
main
;;
font-powerline)
powerline_fonts
main
;;
font-nerd)
nerd_fonts SourceCodePro
main
;;
deploy)
symlink_files "$HOME"
;;
deploy_root)
symlink_files $ROOT_HOME
ln -s "${HOME}/.zplug" "${ROOT_HOME}/.zplug"
;;
quit)
echo "bye!"
exit 0
;;
*)
echo "${RED}bootstrap: command not found.$NORMAL"
main
;;
esac
}
if [ $# = 0 ]; then
print_dotfiles
main
else
case $1 in
-h|--help)
show_help
;;
-a|--all)
init
symlink_files "$HOME"
;;
-*)
echo "$0: unrecognized option \`$1\`"
show_help
exit 1
;;
*)
;;
esac
fi
exit 0