forked from davidosomething/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot
executable file
·497 lines (411 loc) · 13.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
#!/usr/bin/env bash
[[ -z "${DOTFILES}" ]] && echo "Symlink first" && exit 1
. "${DOTFILES}/lib/helpers.sh"
. "${DOTFILES}/lib/pretty.bash"
# ==============================================================================
latest_xcode_major="15"
# ==============================================================================
# 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
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)
pipx -- update pipx-installed cli tools
neovim -- update neovim to latest prerelease build
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 -- software updates
'
}
__dko_dotfiles__cd() {
cd -- "$DOTFILES" || {
__dko_err "No \$DOTFILES directory"
return 1
}
}
__dko_dotfiles__update() {
__dko_status "Updating dotfiles"
local lockfile="${HOME}/.local/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() {
(
cd -- "${HOME}/.secret" 2>/dev/null || {
__dko_warn 'Skipping ~/.secret/ update -- directory not found'
exit 0
}
__dko_status "Updating ~/.secret/"
git pull --rebase --recurse-submodules || exit 1
git log --no-merges --abbrev-commit --oneline ORIG_HEAD..
)
}
__dko_dotfiles__update_daily() {
__dko_dotfiles__update_secret
__dko_dotfiles__go__require_go && __dko_dotfiles__go__update_go_packages
__dko_dotfiles__ruby__require_rubygems && __dko_dotfiles__ruby__update_gems
__dko_has composer && {
__dko_dotfiles__php__update_composer
__dko_has phpcs && __dko_dotfiles__php__update_wpcs
}
}
# ----------------------------------------------------------------------------
# Ruby: Introspection
# ----------------------------------------------------------------------------
# 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_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
}
}
# ----------------------------------------------------------------------------
# Python
# ----------------------------------------------------------------------------
# $1 pip command (e.g. `pip2`)
__dko_dotfiles__py__update_pip() {
__dko_status "Updating pip"
! python -m pip --version >/dev/null 2>&1 &&
__dko_warn "pip not found" && return 1
python -m pip install --upgrade setuptools || return 1
python -m pip install --upgrade wheel || return 1
python -m pip install --upgrade pip
}
__dko_dotfiles__py__update_pipx() {
__dko_status "Updating pipx tools"
! __dko_has pipx &&
__dko_warn "pipx not found" && return 1
pipx upgrade-all
}
__dko_dotfiles__update_neovim() {
if __dko_has brew; then
if __dko_has nvim; then
local version
version="$(nvim --version | head -n1)"
if ! grep dev <<<"$version"; then
brew uninstall neovim
brew install --force --HEAD neovim
else
brew reinstall neovim
fi
fi
elif __dko_has pamac; then
pamac build neovim-git
fi
local res=$?
[ $res -ne 0 ] &&
__dko_err "Could not update to latest neovim prerelease build" &&
return 1
nvim --version
__dko_ok "Updated to latest neovim prerelease build"
}
# ----------------------------------------------------------------------------
# PHP
# ----------------------------------------------------------------------------
__dko_dotfiles__php__update_composer() {
__dko_status "Updating composer"
__dko_has composer || return 1
if [[ -x "${HOMEBREW_PREFIX}/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
}
__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_has composer || return 1
__dko_has phpcs || return 1
if [[ ! -d "$wpcs_path" ]]; then
mkdir -p "${sources_path}"
git clone -b main "$wpcs_repo" "$wpcs_path"
else
(
cd -- "$wpcs_path" || exit 1
git pull --rebase &&
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" ]] && printf 'Found %s\n' "$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" ;;
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" ;;
esac
}
# ------------------------------------------------------------------------------
# OS: GNU/Linux: Arch Linux
# ------------------------------------------------------------------------------
__dko_dotfiles__linux__arch__update() {
__dko_status "Arch Linux system update"
if __dko_has paru; then
paru
elif __dko_has yay; then
yay
elif __dko_has pacaur; then
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"
# softwareupdate --install --all --force
__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 v
# # 14.0.0
# v="$(xcodebuild -version | awk 'NR==1{print $2}')"
# # 14 (trim until .)
# v="${v%%.*}"
# if ((v <= latest_xcode_major)); then
# __dko_ok "Found Xcode >= v${v}"
# return 0
# fi
# __dko_err "Found Xcode ${v}, please install ${latest_xcode_major}.x.x"
# return 1
# }
__dko_dotfiles__darwin__update_brew() {
# __dko_dotfiles__darwin__require_latest_xcode || return 1
__dko_has 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_status "brew update"
brew update || exit 1
# check if needed
local outdated
# outdated="$(brew outdated --quiet)"
# ST: Get outdated items and strip out any that we don't want upgraded
outdated="$( brew outdated --quiet | sed s/'vagrant'// | sed s/'virtualbox-extension-pack'// | sed s/'virtualbox'// | tr '\n' ' ' )"
[[ -z "$outdated" ]] && {
__dko_ok "Packages up-to-date"
exit
}
# Upgrade remaining
__dko_status "brew upgrade"
# ST: Loop outdated items for upgrade, rather than upgrading all
for word in $outdated; do
# We'll manually cleanup later
# HOMEBREW_NO_INSTALL_CLEANUP=1 brew upgrade || exit 1
HOMEBREW_NO_INSTALL_CLEANUP=1 brew upgrade "$word";
done
__dko_ok "Upgrade complete"
__dko_dotfiles__darwin__update_brew_postupgrade "$outdated"
) && {
__dko_status "brew cleanup - clean up old versions and prune dead symlinks"
brew cleanup --verbose
__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
local phpimagick
phpimagick="$(brew list --formula -1 | 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 ;;
gem) __dko_dotfiles__ruby__update_gems ;;
go) __dko_dotfiles__go__update_go_packages ;;
pip) __dko_dotfiles__py__update_pip ;;
pipx) __dko_dotfiles__py__update_pipx ;;
neovim) __dko_dotfiles__update_neovim ;;
neopy) __dko_dotfiles__py__update_neovim_python ;;
wpcs) __dko_dotfiles__php__update_wpcs ;;
*)
case "$OSTYPE" in
linux*) __dko_dotfiles__linux__update "$@" ;;
*arwin*) __dko_dotfiles__darwin__update "$@" ;;
esac
;;
esac
}
__dko_dotfiles "$@"