-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
92 lines (75 loc) · 2.16 KB
/
utils.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
# shellcheck shell=bash
# Helper utilities for `yadm bootstrap` and hooks
VSCODE_EXTENSIONS=~/.config/Code/User/extensions.txt
function confirm() {
read -r -n 1 -p "$1 [Y/n]: "
# Default to yes for empty input
if [[ "$REPLY" == "" ]]; then
return 0
else
echo
fi
# Only 'y' input is valid if something was typed
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
return 0
fi
echo "Operation skipped."
return 1
}
function list_vscode_exts() {
# Linux `code` outputs with CRLF for some reason
code --list-extensions --show-versions | sed 's/\r$//g'
}
function check_vscode_exts() {
command -v code || return 0
diff "$VSCODE_EXTENSIONS" \
<(list_vscode_exts) \
--unchanged-line-format="" \
"$@"
}
function error() {
# Print message in red to stderr
printf '\033[31mERROR:\033[0m ' >&2
printf '%s\n' "$@" >&2
}
function warn() {
# Print message in yellow to stderr
printf '\033[33mWARNING:\033[0m ' >&2
printf '%s\n' "$@" >&2
}
function exit_with_error() {
error "$@"
echo
exit 1
}
function install_vscode_exts() {
xargs printf -- "--install-extension %s " <"$VSCODE_EXTENSIONS" | xargs code
echo
# TODO: some kind of vscode extension "prune" command. Or just uninstall all every time?
if ! check_vscode_exts &>/dev/null; then
# This check may not be needed since `code` errors if it can't install
local UNINSTALLED
UNINSTALLED=$(check_vscode_exts \
--old-line-format=" %L" \
--new-line-format="" \
|| :
)
if [[ -n $UNINSTALLED ]]; then
exit_with_error "Some VSCode extensions were not installed:" "$UNINSTALLED"
fi
local INSTALLED
INSTALLED=$(check_vscode_exts \
--old-line-format="" \
--new-line-format=" %L" \
|| :
)
if [[ -n $INSTALLED ]]; then
warn "Some installed VSCode extensions were not found in $VSCODE_EXTENSIONS:"
echo "$INSTALLED" >&2
echo >&2
fi
fi
}
function is_merging() {
git rev-parse -q --verify MERGE_HEAD &>/dev/null
}