/
dotfiles
executable file
·440 lines (345 loc) · 8.2 KB
/
dotfiles
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/bin/bash
set -e
# Switch to correct folder
if [ -z "$DOTFILES" ]
then
echo "Not yet set up. Sourcing zprofile"
# shellcheck source=zsh/.config/zsh/zprofile.zsh
source "${0%/*}/zsh/.config/zsh/zprofile.zsh"
# TODO: Migrate this script to zsh
# Hack for bash in this script
PATH="$(zsh -c "source ${0%/*}/zsh/.config/zsh/zprofile.zsh && "'echo $PATH')"
fi
brew_dir='/opt/homebrew'
# Possible configurations {{{
all_config=( brew gem npm pip )
all_dirs=()
pushd "$DOTFILES" >/dev/null
for folder in */
do
if [[ ! -e "$folder.nostow" ]]; then
all_config+=("${folder%/}")
all_dirs+=("${folder%/}")
fi
done
if [ "$1" = "--list" ]; then
echo "${all_config[*]}"
exit
elif [ "$1" = "--list-dirs" ]; then
echo "${all_dirs[*]}"
exit
fi
popd >/dev/null
# }}}
# Helper functions {{{
function quit_setup() {
[ -n "$1" ] && echo "Unknown Config: $1"
echo "Usage:"
echo " dotfile-setup 'mode'"
echo "Modes: stow, install & update"
echo "Configurations (optional):"
echo "${all_config[*]}"
exit 1
}
mode=-1
function is_install() {
[ $mode -eq 1 ]
}
function is_update() {
[ $mode -eq 2 ]
}
function should_configure() {
[ $mode -ge 1 ] && has_config "$1" && printf '\n==================== %s ====================\n' "$1"
}
function has_mode() {
[ $mode -ge 0 ]
}
function set_mode() {
[ $mode -eq -1 ] || quit_setup
mode=$1
}
config=()
function set_config() {
config+=("$1")
}
function has_config() {
[[ " ${config[*]} " = *" ${1} "* ]]
}
# }}}
while [ -n "$1" ]
do
case "$1" in
stow)
set_mode 0;;
install)
set_mode 1;;
update)
set_mode 2;;
*)
if has_mode; then
[[ " ${all_config[*]} " = *" ${1} "* ]] || quit_setup "$1"
set_config "$1"
else
set +e
shimmer --quiet 'dotfiles-' "$@"
ret=$?
if [[ $ret -ge 254 ]]; then
quit_setup
else
exit $ret
fi
fi
;;
esac
shift
done
cd "$DOTFILES" || exit
has_mode || quit_setup
# Set all configs if none specified
[ ${#config[@]} -eq 0 ] && config=( "${all_config[*]}" )
# Setup environment {{{
# Install homebrew if not available
if ! hash brew &>/dev/null; then
mkdir -p /usr/local/bin
url='https://raw.githubusercontent.com/Homebrew/install/master/install.sh'
echo 'Install homebrew before continuing with this setup.'
echo "Do you wish to install brew now? ($url)"
read -rp "(y/n): " yn
case $yn in
[Yy]) /bin/bash -c "$(curl -fsSL $url)";;
*) exit 1;;
esac
fi
if is_install; then
# Install mas via brew, if not installed.
if ! hash mas &>/dev/null; then
echo 'Installing brew "mas"'
brew install mas
fi
# Install gnu-stow via brew, if not installed.
if ! hash stow &>/dev/null; then
echo 'Installing brew "stow"'
brew install stow
fi
brew analytics off
fi
# }}}
# INSTALLATION
# Install brews {{{
function brew_install() {
if [ -f "$1" ]
then
for formula in $(command cat "$1" | grep -E "args.*'HEAD'" | sed "s/brew '\(.*\)',.*/\1/"); do
brew upgrade "$formula" --fetch-HEAD || true
done
echo "$2"
brew bundle install --no-lock --file "$1"
fi
}
if should_configure brew
then
brew_install "$DOTFILES/Brewfile" 'Installing brews'
brew_install "$DOTFILES/private/.Brewfile" 'Installing private brews'
brew cleanup
fi
# uninstall git completions as zsh completions are way better!
rm $brew_dir/share/zsh/site-functions/_git 2>/dev/null || true
# }}}
# Install gems {{{
if should_configure gem
then
if is_install
then
echo "Installing missing ruby with rbenv"
# installs ruby version in ~/.ruby-version
if [[ -e "$HOME/.ruby-version" ]]; then
rbenv install --skip-existing
else
rbenv install --skip-existing "$(< "$DOTFILES/ruby/.ruby-version")"
fi
echo 'Installing gems'
bundle install
fi
if is_update
then
echo 'Updating gems'
bundle update
fi
fi
if hash rbenv &>/dev/null
then
mkdir -p "$XDG_DATA_HOME/zsh/generated"
rbenv init - --no-rehash zsh > "$XDG_DATA_HOME/zsh/generated/rbenv.zsh"
rbenv rehash
fi
# }}}
# Install npms {{{
if should_configure npm
then
echo 'Installing npms'
npm install --global npm
#shellcheck disable=SC2046
npm install --global $(command cat "$DOTFILES/node_modules")
if is_update
then
echo 'Updating npms'
#shellcheck disable=SC2046
npm update --global $(command cat "$DOTFILES/node_modules")
fi
fi
# }}}
# Install pips {{{
if should_configure pip
then
if is_install
then
echo 'Installing pips'
pip3 install -r "$DOTFILES/pip3-requirements"
fi
if is_update
then
echo 'Updating pips'
pip3 install -U -r "$DOTFILES/pip3-requirements"
fi
fi
# }}}
# CONFIGURATION
# ALACRITTY {{{
if should_configure alacritty
then
if is_install
then
# Set font rendering for non retina
defaults write -g CGFontRenderingFontSmoothingDisabled -bool NO
fi
echo 'Updating alacritty completion.'
aria2c https://raw.githubusercontent.com/alacritty/alacritty/master/extra/completions/_alacritty -d "$XDG_DATA_HOME/zsh/generated/functions" --console-log-level=warn --allow-overwrite
fi
# }}}
# BAT {{{
if should_configure bat
then
# recompile with theme
bat cache --build
fi
# }}}
# IOS {{{
if should_configure ios
then
if is_install
then
echo 'Set Xcode to show build times.'
defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES
fi
echo 'Generate "xcodes" completion.'
mkdir -p "$XDG_DATA_HOME/zsh/generated/functions"
xcodes --generate-completion-script > "$XDG_DATA_HOME/zsh/generated/functions/_xcodes"
fi
# }}}
# NVIM {{{
if should_configure nvim
then
# Currently no output from vim.
# nvim_update_file="$(mktemp)"
# trap 'rm "$nvim_update_file"' EXIT
if is_install
then
mkdir -p "$XDG_DATA_HOME/nvim/undo"
if [ ! -d "$XDG_DATA_HOME/nvim/site/pack/packer/start/packer.nvim" ]
then
git clone https://github.com/wbthomason/packer.nvim.git "$XDG_DATA_HOME/nvim/site/pack/packer/start/packer.nvim"
fi
echo 'Installing vim plugins'
nvim -c 'autocmd User PackerComplete quitall' -c 'lua require "my.packer"' -c 'PackerSync'
for file in vim/spell/*.add; do
echo 'Compiling vim Spellfiles'
nvim "+mkspell! $file" +qa
done
fi
if is_update
then
echo 'Updating vim plugins'
nvim -c 'autocmd User PackerComplete quitall' -c 'lua require "my.packer"' -c 'PackerSync'
# nvim --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync'
fi
# cat "$nvim_update_file"
echo 'Updating vim-pack tags'
"$DOTFILES"/nvim/.local/bin/neovim-packtags
echo 'Installing luacheck'
luarocks install luacheck
fi
# }}}
# OSX {{{
if is_install && should_configure osx
then
echo 'Set Dock to show instantly with mouse hover.'
defaults write com.apple.dock autohide-delay -float 0
defaults write com.apple.dock autohide-time-modifier -float 0.4
killall Dock
fi
# }}}
# TLDR {{{
if hash tldr &>/dev/null; then
tldr --update
fi
# }}}
# ZSH {{{
function zgenom_comp() {
zsh -c "source $XDG_DATA_HOME/zgenom/zgenom.zsh && zgenom compile $1"
}
if should_configure zsh
then
stow zsh
if is_install
then
mkdir -p "$XDG_CACHE_HOME/zsh"
mkdir -p "$XDG_DATA_HOME/zsh/generated"
if [[ ! -e "$XDG_DATA_HOME/zgenom/.git" ]]; then
git clone https://github.com/jandamm/zgenom.git "$XDG_DATA_HOME/zgenom"
fi
echo 'Installing zsh plugins'
zsh -c "source $DOTFILES/zsh/.config/zsh/.zshrc"
fi
if is_update
then
echo 'Cleaning zsh plugins'
zsh -c "source $DOTFILES/zsh/.config/zsh/init.zsh &>/dev/null && source $XDG_DATA_HOME/zgenom/zgenom.zsh && zgenom clean"
echo 'Updating zgenom'
zsh -c "source $XDG_DATA_HOME/zgenom/zgenom.zsh && zgenom selfupdate"
echo 'Updating zsh plugins'
zsh -c "source $XDG_DATA_HOME/zgenom/zgenom.zsh && zgenom update"
echo 'Recreate zsh plugins cache'
zsh -c "source $DOTFILES/zsh/.config/zsh/init.zsh"
fi
fi
# }}}
for c in ${config[*]}
do
if [[ -d "$c" ]]; then
if [[ -z "$stowing" ]]; then
echo '' && echo 'Putting files into place for:'
stowing=yes
fi
echo " - $c"
dotfiles-stow "$c"
fi
done
if has_config zsh
then
echo '' && echo 'Recompile zsh dotfiles'
zgenom_comp "$HOME/.zshenv"
zgenom_comp "$XDG_CONFIG_HOME/zsh"
zgenom_comp "$XDG_DATA_HOME/zsh/generated"
fi
if is_install
then
echo '' && echo 'Do you want to use zsh from homebrew as your default shell?'
read -rp "(y/n): " yn
case $yn in
[Yy])
sudo sh -c "echo '$brew_dir/bin/zsh' >> /etc/shells"
chsh -s $brew_dir/bin/zsh
;;
esac
fi
# vim:set foldmethod=marker: