-
Notifications
You must be signed in to change notification settings - Fork 462
/
Copy pathinstall.sh
executable file
·289 lines (249 loc) · 7.19 KB
/
install.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
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
#!/usr/bin/env bash
# We don't need return codes for "$(command)", only stdout is needed.
# Allow `[[ -n "$(command)" ]]`, `func "$(command)"`, pipes, etc.
# shellcheck disable=SC2312
set -u
# global vars
DEST_DIR="${HOME}/.config/nvim"
BACKUP_DIR="${DEST_DIR}_backup-$(date +%Y%m%dT%H%M%S)"
CLONE_ATTR=("--progress")
REQUIRED_NVIM_VERSION=0.8
USE_SSH=1
abort() {
printf "%s\n" "$@" >&2
exit 1
}
# Fail fast with a concise message when not using bash
# Single brackets are needed here for POSIX compatibility
# shellcheck disable=SC2292
if [ -z "${BASH_VERSION:-}" ]; then
abort "Bash is required to interpret this script."
fi
# Check if script is run with force-interactive mode in CI
if [[ -n "${CI-}" && -n "${INTERACTIVE-}" ]]; then
abort "Cannot run force-interactive mode in CI."
fi
# string formatters
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_yellow="$(tty_escape "0;33")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
execute() {
if ! "$@"; then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
major_minor() {
echo "${1%%.*}.$(
x="${1#*.}"
echo "${x%%.*}"
)"
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
prompt() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
warn() {
printf "${tty_yellow}Warning${tty_reset}: %s\n" "$(chomp "$1")"
}
warn_ext() {
printf " %s\n" "$(chomp "$1")"
}
getc() {
local save_state
save_state="$(/bin/stty -g)"
/bin/stty raw -echo
IFS='' read -r -n 1 -d '' "$@"
/bin/stty "${save_state}"
}
ring_bell() {
# Use the shell's audible bell.
if [[ -t 1 ]]; then
printf "\a"
fi
}
wait_for_user() {
local c
echo
echo "Press ${tty_bold}RETURN${tty_reset}/${tty_bold}ENTER${tty_reset} to continue or any other key to abort..."
getc c
# we test for \r and \n because some stuff does \r instead
if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]]; then
echo "${tty_red}Aborted.${tty_reset}"
exit 1
fi
}
version_ge() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]
}
prompt_confirm() {
while true; do
read -r -p "$1 [Y/n]: " USR_CHOICE
case "$USR_CHOICE" in
[yY][eE][sS] | [yY])
return 1
;;
[nN][oO] | [nN])
return 0
;;
*)
if [[ -z "$USR_CHOICE" ]]; then
return 1
fi
printf "${tty_red}%s\n\n${tty_reset}" "Invalid input! Please enter one of: '[yY]/[yY][eE][sS] / [nN]/[nN][oO]'"
;;
esac
done
}
check_ssh() {
prompt "Validating SSH connection..."
ssh -T [email protected] &>/dev/null
if ! [ $? -eq 1 ]; then
prompt "We'll use HTTPS to fetch and update plugins."
return 0
else
prompt_confirm "Do you prefer to use SSH to fetch and update plugins? (otherwise HTTPS)"
return $?
fi
}
clone_pref() {
prompt "Checking 'git clone' preferences..."
if ! prompt_confirm "Would you like to perform a shallow clone ('--depth=1')?"; then
CLONE_ATTR+=("--depth=1")
fi
}
is_latest() {
local nvim_version
nvim_version="$(nvim --version | head -n1 | sed -e 's|^[^0-9]*||' -e 's| .*||')"
if version_ge "$(major_minor "${nvim_version##* }")" "$(major_minor "${REQUIRED_NVIM_VERSION}")"; then
return 0
else
return 1
fi
}
# Check if both `INTERACTIVE` and `NONINTERACTIVE` are set
# Always use single-quoted strings with `exp` expressions
# shellcheck disable=SC2016
if [[ -n "${INTERACTIVE-}" && -n "${NONINTERACTIVE-}" ]]; then
abort 'Both `$INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again.'
fi
# Check if script is run non-interactively (e.g. CI)
# If it is run non-interactively we should not prompt for confirmation.
# Always use single-quoted strings with `exp` expressions
# shellcheck disable=SC2016
if [[ -z "${NONINTERACTIVE-}" ]]; then
if [[ -n "${CI-}" ]]; then
warn 'Running in non-interactive mode because `$CI` is set.'
NONINTERACTIVE=1
elif [[ ! -t 0 ]]; then
if [[ -z "${INTERACTIVE-}" ]]; then
warn 'Running in non-interactive mode because `stdin` is not a TTY.'
NONINTERACTIVE=1
else
warn 'Running in interactive mode despite `stdin` not being a TTY because `$INTERACTIVE` is set.'
fi
fi
else
prompt 'Running in non-interactive mode because `$NONINTERACTIVE` is set.'
fi
if ! command -v perl >/dev/null; then
abort "$(
cat <<EOABORT
Perl is required to interpret this script. See:
${tty_underline}https://www.perl.org/get.html${tty_reset}
EOABORT
)"
fi
if ! command -v nvim >/dev/null; then
abort "$(
cat <<EOABORT
You must install NeoVim before installing this Nvim config. See:
${tty_underline}https://github.com/neovim/neovim/wiki/Installing-Neovim${tty_reset}
EOABORT
)"
fi
if ! command -v git >/dev/null; then
abort "$(
cat <<EOABORT
You must install Git before installing this Nvim config. See:
${tty_underline}https://git-scm.com/${tty_reset}
EOABORT
)"
fi
# Always use HTTPS when this script is run non-interactively (e.g. CI)
if [[ -n "${NONINTERACTIVE-}" ]]; then
USE_SSH=0
fi
prompt "This script will install ayamir/nvimdots to:"
echo "${DEST_DIR}"
if [[ -d "${DEST_DIR}" ]]; then
warn "The destination folder: \"${DEST_DIR}\" already exists."
warn_ext "We will make a backup for you at \"${BACKUP_DIR}\"."
fi
if [[ -z "${NONINTERACTIVE-}" ]]; then
ring_bell
wait_for_user
if check_ssh; then
USE_SSH=0
fi
clone_pref
fi
if [[ -d "${DEST_DIR}" ]]; then
execute "mv" "-f" "${DEST_DIR}" "${BACKUP_DIR}"
fi
prompt "Fetching in progress..."
if [[ "$USE_SSH" -eq "1" ]]; then
if is_latest; then
execute "git" "clone" "-b" "main" "${CLONE_ATTR[@]}" "[email protected]:ayamir/nvimdots.git" "${DEST_DIR}"
else
warn "You have outdated Nvim installed (< ${REQUIRED_NVIM_VERSION})."
prompt "Automatically redirecting you to legacy version..."
execute "git" "clone" "-b" "0.7" "${CLONE_ATTR[@]}" "[email protected]:ayamir/nvimdots.git" "${DEST_DIR}"
fi
else
if is_latest; then
execute "git" "clone" "-b" "main" "${CLONE_ATTR[@]}" "https://github.com/ayamir/nvimdots.git" "${DEST_DIR}"
else
warn "You have outdated Nvim installed (< ${REQUIRED_NVIM_VERSION})."
prompt "Automatically redirecting you to legacy version..."
execute "git" "clone" "-b" "0.7" "${CLONE_ATTR[@]}" "https://github.com/ayamir/nvimdots.git" "${DEST_DIR}"
fi
fi
cd "${DEST_DIR}" || return
if [[ "$USE_SSH" -eq "0" ]]; then
prompt "Changing default fetching method to HTTPS..."
execute "perl" "-pi" "-e" "s/\[\"use_ssh\"\] \= true/\[\"use_ssh\"\] \= false/g" "${DEST_DIR}/lua/core/settings.lua"
fi
prompt "Spawning neovim and fetching plugins... (You'll be redirected shortly)"
prompt "If lazy.nvim failed to fetch any plugin(s), maunally execute \`:Lazy sync\` until everything is up-to-date."
cat <<EOS
Thank you for using this set of configuration!
- Project Homepage:
${tty_underline}https://github.com/ayamir/nvimdots${tty_reset}
- Further documentation (including executables you ${tty_bold}must${tty_reset} install for full functionality):
${tty_underline}https://github.com/ayamir/nvimdots/wiki/Prerequisites${tty_reset}
EOS
if [[ -z "${NONINTERACTIVE-}" ]]; then
ring_bell
wait_for_user
nvim
fi