forked from davidosomething/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot
executable file
·692 lines (574 loc) · 19.7 KB
/
dot
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!/usr/bin/env bash
[[ -z "${DOTFILES}" ]] && echo "Symlink first" && exit 1
. "${DOTFILES}/lib/helpers.sh"
. "${DOTFILES}/lib/pretty.bash"
[[ -d "$PYENV_ROOT" ]] && eval "$(pyenv init -)"
[[ -n "$NVM_DIR" ]] && __dko_source "${NVM_DIR}/nvm.sh"
# ==============================================================================
# Command functions
# ==============================================================================
# ------------------------------------------------------------------------------
# Meta
# ------------------------------------------------------------------------------
__dko_dotfiles__command_not_found() {
__dko_err "Command not found '${1:-''}'"
echo
__dko_dotfiles__usage
exit 1
}
__dko_dotfiles__usage() {
__dko_usage "dot <command>"
echo '
Utility Commands
dotfiles -- update dotfiles (git pull)
secret -- update ~/.secret (git pull)
daily -- secret, Packages / Developer Tools, fzf, nvm, pyenv
Shell Tools
fzf -- update fzf with flags to not update rc scripts
node -- install latest node via nvm
nvm -- update nvm installation
pyenv -- update pyenv installation
Packages / Developer Tools
composer -- update composer and global packages
gem -- update rubygems and global gems for current ruby
go -- golang
pip -- update all versions of pip (OS dependent)
neopy -- update neovim pyenvs
wpcs -- update the WordPress-Coding-Standards git repo in src/wpcs
System: Arch Linux
arch -- update arch packages
System: Debian/Ubuntu
deb -- update apt packages
System: macOS/OS X
brew -- homebrew packages
mac -- repair permissions and check software updates
'
}
__dko_dotfiles__cd() {
cd -- "$DOTFILES" || {
__dko_err "No \$DOTFILES directory"
return 1
}
}
__dko_dotfiles__update() {
__dko_status "Updating dotfiles"
local lockfile="${LDOTDIR}/dotfiles.lock"
# shellcheck disable=SC2064
trap "rm -f \"$lockfile\"" EXIT
touch "$lockfile"
(
__dko_dotfiles__cd || exit 1
git pull --rebase || exit 1
git log --no-merges --abbrev-commit --oneline ORIG_HEAD..
) || {
__dko_err "Error updating dotfiles"
return 1
}
__dko_ok "Successfully updated dotfiles"
}
__dko_dotfiles__update_secret() {
__dko_status "Updating ~/.secret/"
(
cd -- "${HOME}/.secret" ||
{ __dko_err "No ~/.secret directory" && exit 1; }
git pull --rebase --recurse-submodules || exit 1
git log --no-merges --abbrev-commit --oneline ORIG_HEAD..
) || __dko_err "Failed to update ~/.secret/"
}
__dko_dotfiles__update_daily() {
__dko_dotfiles__update_secret
if [[ -x "/usr/local/opt/fzf/install" ]] || [[ -d "${HOME}/.fzf" ]]; then
__dko_dotfiles__update_fzf
else
__dko_warn "fzf is not installed"
fi
if __dko_dotfiles__node__require_nvm --silent; then
__dko_dotfiles__node__update_nvm
else
__dko_warn "NVM not found, skipping update NVM"
fi
if __dko_dotfiles__go__require_go --silent; then
__dko_dotfiles__go__update_go_packages
else
__dko_warn "Go not found, skipping update Go dependencies"
fi
if ! __dko_dotfiles__ruby__require_chruby --silent; then
__dko_warn "Chruby not found, skipping update ruby gems"
elif ! __dko_dotfiles__ruby__require_rubygems; then
__dko_warn "Rubygems not found, skipping update ruby gems"
else
__dko_dotfiles__ruby__update_gems
fi
if ! __dko_dotfiles__php__require_composer --silent; then
__dko_warn "Composer not found, skipping update composer and packages"
else
__dko_dotfiles__php__update_composer
if __dko_dotfiles__php__require_phpcs --silent; then
__dko_dotfiles__php__update_wpcs
else
__dko_warn "PHPCS not found, skipping update WPCS"
fi
fi
if ! __dko_has "pyenv"; then
__dko_warn "pyenv not found"
else
__dko_dotfiles__py__update_pyenv
__dko_dotfiles__py__update_pip "pip"
__dko_dotfiles__py__update_neovim_python
fi
}
# ------------------------------------------------------------------------------
# Private utilities
# ------------------------------------------------------------------------------
__dko_dotfiles__pyenv_system() {
# switch to brew's python (fallback to system if no brew python)
__dko_has "pyenv" &&
__dko_status "Switching to system python to upgrade brew packages" &&
pyenv shell system || exit 1 &&
python --version
}
# probably don't need this as long as running updates in subshells
__dko_dotfiles__pyenv_global() {
__dko_has "pyenv" &&
__dko_status_ "Switching back to global python" &&
pyenv shell --unset || exit 1
}
# ------------------------------------------------------------------------------
# Externals
# ------------------------------------------------------------------------------
__dko_dotfiles__update_fzf() {
local installer
if [[ -x "/usr/local/opt/fzf/install" ]]; then
__dko_status "fzf was installed via brew, re-running installer"
installer="/usr/local/opt/fzf/install"
elif [[ -d "${HOME}/.fzf" ]]; then
__dko_status "fzf was installed in ~/.fzf"
installer="${HOME}/.fzf/install"
(
cd -- "${HOME}/.fzf" || { __dko_err "Could not cd to ~/.fzf" && exit 1; }
__dko_status "Updating fzf"
git pull || { __dko_err "Could not update ~/.fzf" && exit 1; }
git log --no-merges --abbrev-commit --oneline ORIG_HEAD..
) || return 1
else
__dko_err "fzf is not installed"
return 1
fi
# Install/update shell extensions
if [[ -n "$installer" ]]; then
__dko_status "Updating fzf shell extensions"
"$installer" --bin || return 1
fi
}
# ----------------------------------------------------------------------------
# Ruby: Introspection
# ----------------------------------------------------------------------------
# pass any arg to silence
__dko_dotfiles__ruby__require_chruby() {
[[ -n "$RUBY_VERSION" ]] && return 0
__dko_warn "System ruby detected! Please install and use chruby"
return 1
}
# pass any arg to silence
__dko_dotfiles__ruby__require_rubygems() {
__dko_has "gem" && return 0
__dko_warn "rubygems is not installed"
return 1
}
# ----------------------------------------------------------------------------
# Ruby: Update Gems
# ----------------------------------------------------------------------------
__dko_dotfiles__ruby__update_gems() {
__dko_status "Updating gems"
__dko_dotfiles__ruby__require_chruby || return 1
__dko_dotfiles__ruby__require_rubygems || return 1
__dko_status "Updating RubyGems itself for ruby: ${RUBY_VERSION}"
gem update --system || {
__dko_err "Could not update RubyGems"
return 1
}
gem update || {
__dko_err "Could not update gems"
return 1
}
}
# ----------------------------------------------------------------------------
# Go
# ----------------------------------------------------------------------------
# pass any arg to silence
__dko_dotfiles__go__require_go() {
__dko_has "go" && return 0
__dko_warn "go is not installed"
return 1
}
# pass any arg to silence
__dko_dotfiles__go__require_goenv() {
__dko_has "goenv" && return 0
__dko_warn "goenv is not installed"
return 1
}
__dko_dotfiles__go__update_go_packages() {
__dko_status "Updating go packages"
__dko_dotfiles__go__require_goenv || return 1
__dko_dotfiles__go__require_go || return 1
# in case go was updated recently, the go shim needs to be reset
goenv rehash
go get -u all || {
__dko_err "Could not update go packages"
return 1
}
}
# ----------------------------------------------------------------------------
# Node.js: Introspection
# ----------------------------------------------------------------------------
# pass any arg to skip autoinstall
# pass any arg to silence
__dko_dotfiles__node__require_nvm() {
if [[ -z "$NVM_DIR" ]]; then
__dko_err "\$NVM_DIR is not defined, make sure rc files are linked"
return 1
fi
if (($# == 0)) && [[ ! -d "$NVM_DIR" ]]; then
__dko_status "Installing nvm"
git clone https://github.com/creationix/nvm.git "$NVM_DIR" ||
{ __dko_err "Could not install nvm" && return 1; }
fi
}
# ----------------------------------------------------------------------------
# Node.js: Update node to latest stable
# ----------------------------------------------------------------------------
__dko_dotfiles__update_node() {
__dko_dotfiles__node__require_nvm || return 1
local wanted
wanted="${1:-"$(nvm version-remote --lts)"}"
[[ "$wanted" != "v"* ]] && wanted="v${wanted}"
. "${NVM_DIR}/nvm.sh"
__dko_status "Checking node versions against ${wanted}"
local wanted_with_minor
wanted_with_minor="$(nvm version-remote "$wanted")"
local previous_node
previous_node="$(nvm current)"
if [[ "$wanted_with_minor" == "$previous_node" ]]; then
__dko_ok "Node.js is already up-to-date"
return 0
fi
__dko_status_ "Previous node version was $previous_node"
echo -n "Install and use new node ${wanted_with_minor} as default? [y/N] "
read -r
echo
if [[ "$REPLY" == "y" ]] || [[ "$REPLY" == "Y" ]]; then
__dko_status "Installing node ${wanted_with_minor}..."
if nvm install "$wanted"; then
__dko_status "Updating default alias to ${wanted_with_minor}..."
nvm alias default "$wanted"
__dko_status "Switching to ${wanted_with_minor}..."
nvm use "$wanted"
__dko_status "install-latest-npm"
nvm install-latest-npm
__dko_ok "Node.js and npm updated, shell restart required"
fi
fi
}
# ----------------------------------------------------------------------------
# Node.js: Update NVM
# ----------------------------------------------------------------------------
__dko_dotfiles__node__update_nvm() {
__dko_dotfiles__node__require_nvm || return 1
(
cd -- "$NVM_DIR" || exit 1
__dko_status "Updating nvm"
readonly previous_nvm="$(git describe --abbrev=0 --tags)"
__dko_status_ "Found nvm ${previous_nvm}"
{ git checkout master && git pull --tags; } ||
{ __dko_err "Could not fetch" && exit 1; }
readonly latest_nvm="$(git describe --abbrev=0 --tags)"
# Already up to date
[[ "$previous_nvm" == "$latest_nvm" ]] &&
{ __dko_ok "Already have nvm ${latest_nvm}" && exit 0; }
__dko_status "Fast-forwarding to nvm ${latest_nvm}"
git checkout --quiet --progress "$latest_nvm" ||
{ __dko_err "Could not fast-forward" && exit 1; }
exit 0
) || return 1
}
# ----------------------------------------------------------------------------
# Python
# ----------------------------------------------------------------------------
__dko_dotfiles__py__update_pyenv() {
if [[ -n "$PYENV_ROOT" ]] && [[ -d "${PYENV_ROOT}/.git" ]]; then
__dko_status "Updating pyenv"
(
cd -- "${PYENV_ROOT}" || exit 1
pyenv update
) || return 1
else
__dko_warn "pyenv was not installed using pyenv-installer"
fi
}
# $1 pip command (e.g. `pip2`)
__dko_dotfiles__py__update_pip() {
local pip_command=${1:-pip}
__dko_status "Updating $pip_command"
! __dko_has "$pip_command" &&
__dko_warn "${pip_command} not found" && return 1
"$pip_command" install --upgrade setuptools || return 1
"$pip_command" install --upgrade pip || return 1
"$pip_command" install --upgrade \
--requirement "${DOTFILES}/python/requirements.txt"
}
__dko_dotfiles__py__update_neovim_python() {
! __dko_has "pyenv" &&
__dko_err "pyenv is not installed" && return 1
__dko_status "Updating neovim3" &&
pyenv activate neovim3 &&
__dko_dotfiles__py__update_pip &&
pip install --upgrade pynvim
pyenv deactivate
}
# ----------------------------------------------------------------------------
# PHP
# ----------------------------------------------------------------------------
# pass any arg to silence
__dko_dotfiles__php__require_composer() {
__dko_has "composer" && return 0
__dko_warn "composer is not installed"
return 1
}
__dko_dotfiles__php__update_composer() {
__dko_status "Updating composer"
__dko_dotfiles__php__require_composer || return 1
if [[ -x "/usr/local/bin/composer" ]]; then
__dko_ok "composer was installed via brew (yay)"
else
__dko_status_ "Updating composer itself"
composer self-update || {
__dko_err "Could not update composer"
return 1
}
fi
if [[ -f "$COMPOSER_HOME/composer.json" ]]; then
__dko_status "Updating composer global packages"
composer global update || {
__dko_err "Could not update global packages"
return 1
}
fi
}
# pass any arg to silence
__dko_dotfiles__php__require_phpcs() {
__dko_has "phpcs" && return 0
__dko_warn "phpcs is not installed. Install via composer global require"
__dko_warn_ "and run again, or set installed_paths manually"
return 1
}
__dko_dotfiles__php__update_wpcs() {
readonly wpcs_repo="https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git"
readonly sources_path="${HOME}/src"
readonly wpcs_path="${sources_path}/wpcs"
# --------------------------------------------------------------------------
# Create and clone wpcs if not exists
# --------------------------------------------------------------------------
__dko_status "Updating wpcs"
__dko_dotfiles__php__require_composer || return 1
__dko_dotfiles__php__require_phpcs || return 1
if [[ ! -d "$wpcs_path" ]]; then
mkdir -p "${sources_path}"
git clone -b master "$wpcs_repo" "$wpcs_path"
else
(
cd -- "$wpcs_path" || exit 1
git pull &&
git log --no-merges --abbrev-commit --oneline ORIG_HEAD..
) || return 1
fi
# --------------------------------------------------------------------------
# Determine installed standards
# --------------------------------------------------------------------------
__dko_status "Looking for standards"
readonly possible=(
"${COMPOSER_HOME}/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards"
"$wpcs_path"
)
local standards=()
for entry in "${possible[@]}"; do
[[ -d "$entry" ]] && echo "Found $entry" && standards+=("$entry")
done
standards_path=$(
IFS=','
echo "${standards[*]}"
)
# --------------------------------------------------------------------------
# Update config
# --------------------------------------------------------------------------
__dko_status "Updating standards path to:"
__dko_echo "${standards_path}"
phpcs --config-set installed_paths "$standards_path"
# List installed standards:
phpcs -i
phpcs --config-set default_standard PSR2
}
# ------------------------------------------------------------------------------
# OS-specific commands
# ------------------------------------------------------------------------------
__dko_dotfiles__linux__update() {
case "$1" in
arch) __dko_dotfiles__linux__arch__update ;;
deb) __dko_dotfiles__linux__deb__update ;;
*)
__dko_dotfiles__command_not_found "$1"
return 1
;;
esac
}
__dko_dotfiles__darwin__update() {
case "$1" in
brew) __dko_dotfiles__darwin__update_brew ;;
mac) __dko_dotfiles__darwin__update_mac ;;
*)
__dko_dotfiles__command_not_found "$1"
return 1
;;
esac
}
# ------------------------------------------------------------------------------
# OS: GNU/Linux: Arch Linux
# ------------------------------------------------------------------------------
__dko_dotfiles__linux__arch__update() {
__dko_status "Arch Linux system update"
if __dko_has "pacaur"; then
# update system
pacaur -Syu
elif __dko_has "aura"; then
aura -Syua
else
pacman -Syu
fi
}
# ------------------------------------------------------------------------------
# OS: GNU/Linux: Debian or Ubuntu
# ------------------------------------------------------------------------------
__dko_dotfiles__linux__deb__update() {
__dko_status "Apt system update"
! __dko_has "apt" &&
__dko_err "apt not found, manually use 'apt-get' for crappy systems" &&
return 1
sudo apt update
# This is for home systems only! Removes unused stuff, same as
# `apt-get dist-upgrade`
sudo apt full-upgrade
}
# ------------------------------------------------------------------------------
# OS: macOS/OS X
# ------------------------------------------------------------------------------
__dko_dotfiles__darwin__update_mac() {
__dko_status "macOS system update"
sudo softwareupdate --install --all
__dko_status "xcode and cli update"
sudo xcode-select --install
__dko_has "mas" && mas upgrade
}
# pass any arg to silence
__dko_dotfiles__darwin__require_latest_xcode() {
local latest_xcode="10.1"
xcodebuild -version | grep -q "$latest_xcode" && return 0
__dko_err "Using an outdated Xcode, please install/switch to latest version."
return 1
}
# pass any arg to silence
__dko_dotfiles__darwin__require_brew() {
__dko_has "brew" && return 0
__dko_warn "Homebrew is not installed"
return 1
}
__dko_dotfiles__darwin__update_brew() {
__dko_dotfiles__darwin__require_latest_xcode || return 1
__dko_dotfiles__darwin__require_brew || return 1
__dko_status "Updating homebrew"
(
# CLEANROOM
# enter dotfiles dir to do this in case user has any gem flags or local
# vendor bundle that will cause use of local gems
__dko_dotfiles__cd || exit 1
__dko_dotfiles__pyenv_system
# Brew some makefiles like macvim use tput for output so need to reset
# from xterm-256color-italic I use in iterm
TERM="xterm-256color"
brew update
# check if needed
local outdated
outdated="$(brew outdated --quiet)"
[[ -z "$outdated" ]] && {
__dko_ok "Packages up-to-date"
exit
}
# Upgrade remaining
__dko_status "Upgrading packages"
brew upgrade
__dko_dotfiles__darwin__update_brew_postupgrade "$outdated"
) && {
__dko_status "Clean up old versions and prune dead symlinks"
brew cleanup
__dko_ok "All clean"
}
}
__dko_dotfiles__darwin__update_brew_postupgrade() {
__dko_status "Running post-upgrade packages"
local outdated="$1"
# link curl
if grep -q "curl" <<<"$outdated"; then
brew unlink curl && brew link --force curl
fi
# re-init goenv
if grep -q "goenv" <<<"$outdated"; then
goenv rehash
fi
# do not use zsh git completion, the bash one is better
if grep -q "git" <<<"$outdated"; then
"${DOTFILES}/bin/dko-fix-git-completion"
fi
# If imagemagick was outdated and php-imagick was not, force a reinstall
# of php-imagick from source (using the new imagemagick)
if grep -q "imagemagick" <<<"$outdated"; then
readonly phpimagick="$(brew ls | grep 'php.*imagick')"
if [[ -n "$phpimagick" ]]; then
__dko_status "Rebuilding ${phpimagick} for new imagemagick"
brew reinstall --build-from-source "$phpimagick"
fi
fi
# Detect if brew's python3 (not pyenv) was outdated
if grep -q "python3" <<<"$outdated"; then
__dko_status "Python3 was outdated, upgrading python3"
brew upgrade python3
fi
__dko_ok "Post-upgrade complete"
}
# ==============================================================================
# Main
# ==============================================================================
# $1 command
__dko_dotfiles() {
local argcount="$#"
[[ "$argcount" == "0" ]] && __dko_dotfiles__usage && return 1
case $1 in
dotfiles) __dko_dotfiles__update ;;
secret) __dko_dotfiles__update_secret ;;
daily) __dko_dotfiles__update_daily ;;
composer) __dko_dotfiles__php__update_composer ;;
fzf) __dko_dotfiles__update_fzf ;;
gem) __dko_dotfiles__ruby__update_gems ;;
go) __dko_dotfiles__go__update_go_packages ;;
node) __dko_dotfiles__update_node "$2" ;;
nvm) __dko_dotfiles__node__update_nvm ;;
pip) __dko_dotfiles__py__update_pip "pip" ;;
pyenv) __dko_dotfiles__py__update_pyenv ;;
neopy) __dko_dotfiles__py__update_neovim_python ;;
wpcs) __dko_dotfiles__php__update_wpcs ;;
*)
case "$OSTYPE" in
linux*) __dko_dotfiles__linux__update "$@" ;;
darwin*) __dko_dotfiles__darwin__update "$@" ;;
esac
;;
esac
}
__dko_dotfiles "$@"