forked from NvChad/NvChad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·362 lines (313 loc) · 9.67 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
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
#!/usr/bin/env bash
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
REPO="https://github.com/siduck76/NvChad.git"
BACKUP_PATH="/tmp/nvchad/"
TMP_NVCHAD="/tmp/nvchad_updates/"
skip=0
nvchad_path="$HOME/.config/nvim/"
do_startup="true"
dependencies=(
"git"
)
preserved_files=(
"lua/mappings.lua"
"lua/user_config.lua"
)
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
prompt() {
case ${1} in
"-s" | "--success")
printf "${GREEN}%s${NC}\n" "${2}"
;;
"-e" | "--error")
printf "${RED}%s${NC}\n" "${2}"
;;
"-w" | "--warning")
printf "${ORANGE}%s${NC}\n" "${2}"
;;
"-i" | "--info")
printf "${BLUE}%s${NC}\n" "${2}"
;;
*)
printf "${GREEN}%s${NC}\n" "${2}"
;;
esac
}
_usage() {
printf "%s" \
"Usage: ./${0##*/} [ options ]
-h, --help -> Show this help.
-i, --install -> Install the config.
-r, --remove -> Remove the config.
-u, --update -> Update the existing config without removing existing stuff.
-s, --skip-startup -> Skip starting up nvim after installing the config.
-c, --clean-backups -> Remove backups (if any) from your system.
-p=*, --path=* -> Path to NvChad. Relevant for installing and updating.
"
}
_remove() {
read -p "Are you sure you want to uninstall NvChad? This will 'flush' dirs such as $HOME/.config/nvim/. (y/n): " u_reply
if [[ "${u_reply}" == "y" ]]; then
printf " + %s\n" "$(prompt -w "Removing config -> ($HOME/.config/nvim/)")"
rm -rf "$HOME/.config/nvim/"
printf " + %s\n" "$(prompt -w "Removing miscellaneous -> ($HOME/.local/share/nvim/)")"
rm -rf "$HOME/.local/share/nvim/"
printf " + %s\n" "$(prompt -w "Removing cache -> ($HOME/.cache/nvim/)")"
rm -rf "$HOME/.cache/nvim/"
elif [[ "${u_reply}" == "n" ]]; then
prompt -i "Ok, enjoy NvChad :)"
else
prompt -e "Error: you must type either 'n' for no or 'y' for yes."
fi
}
_check_dependencies() {
local err
for i in "${dependencies[@]}"; do
if ! command -v "${i}" &>/dev/null; then
prompt -e "Error: You need to install the dependency '${i}'"
err="true"
fi
done
if [[ "${err}" == "true" ]]; then
exit 1
fi
}
_fetch() {
printf " + %s\n" "$(prompt -i "Cleaning workspace")"
rm -rf ${nvchad_path}
printf " + %s\n" "$(prompt -i "Fetching repo")"
git clone -n ${REPO} --depth 1 "${nvchad_path}"
cd "${nvchad_path}" || return
printf " + %s\n" "$(prompt -i "Checking out core")"
git checkout HEAD lua/
printf " + %s\n" "$(prompt -i "Checking out init file")"
git checkout HEAD init.lua
}
_check_prev_conf() {
if [[ -d "${nvchad_path}" ]]; then
mkdir -p "${BACKUP_PATH}"
amount_files=$(ls "${BACKUP_PATH}" | wc -l)
printf " + %s\n" "$(prompt -i "Previous config found, backing it up (/tmp/nvchad/backup_${amount_files})")"
cp -r "${nvchad_path}" "${BACKUP_PATH}backup_${amount_files}"
fi
}
_check_nvim_version() {
version_string="$(nvim --version | grep -Eo 'NVIM.*v[0-9.]+' -m 1 | grep -Eo '[0-9.]+')" || return 1
printf "%s\n" "${version_string}" | while IFS=. read -r num1 num2 num3; do
{ [ "${num1}" -ge 0 ] && [ "${num2}" -ge 5 ] && [ "${num3}" -ge 0 ]; } || return 1
done
}
_startup() {
printf " + %s\n" "$(prompt -i "Checking nvim version")"
if _NVIM="$(command -v nvim)"; then
if _check_nvim_version; then
printf " + %s\n" "$(prompt -i "NeoVim will open now")"
sleep 1
"${_NVIM}" +'autocmd User PackerComplete ++once lua print "Waiting for PackerCompile.." vim.cmd "PackerCompile"' \
+'autocmd User PackerCompileDone ++once quitall' \
+'lua print "Wait for PackerUpdate and PackerCompile to complete.." require "pluginList" vim.cmd "PackerUpdate"'
"${_NVIM}"
else
prompt -e "Error: Neovim is installed, but version is lower than 0.5.x, install Neovim >= 5.x and then run nvim & do :PackerSync"
fi
else
prompt -e "Error: Neovim is not installed, install Neovim >= 5.x and then run neovim & do :PackerSync"
fi
}
_install() {
prompt -w "-> Checking dependencies..."
_check_dependencies
prompt -w "-> Checking for previous confs..."
_check_prev_conf
prompt -w "-> Cloning..."
_fetch
if [[ "$do_startup" == "true" ]]; then
prompt -w "-> Loading nvim..."
_startup
fi
}
_clean_backups() {
prompt -w "-> Looking for backups..."
if [[ -d "${BACKUP_PATH}" ]]; then
if [[ -z "$(ls -A ${BACKUP_PATH})" ]]; then
printf " + %s\n" "$(prompt -w "No backups were found")"
else
printf " + %s\n" "$(prompt -i "Backups were found, removing them...")"
rm -rf "${BACKUP_PATH}"
fi
else
printf " + %s\n" "$(prompt -w "No backups were found")"
fi
}
_update() {
prompt -w "-> Checking if conf is eligible for being updated..."
if [[ -z "$(ls -A ${nvchad_path})" ]]; then
printf " + %s\n" "$(prompt -e "Error: there is nothing to update")"
else
printf " + %s\n" "$(prompt -i "Updating config")"
mkdir -p ${TMP_NVCHAD}
for to_preservef in "${!preserved_files[@]}"; do
file="${nvchad_path}${preserved_files[to_preservef]}"
printf " + %s\n" "$(prompt -i "saving file: ${preserved_files[to_preservef]}")"
if [[ -e "${file}" ]]; then
mv "${file}" "${TMP_NVCHAD}"
fi
done
do_startup="false"
_install
for to_preservef in "${!preserved_files[@]}"; do
destination_file="${nvchad_path}${preserved_files[to_preservef]}"
file_name=$(basename "${preserved_files[to_preservef]}")
location_path=$(dirname "${destination_file}")
stored_file="${TMP_NVCHAD}${file_name}"
printf " + %s\n" "$(prompt -i "restoring file: ${preserved_files[to_preservef]}")"
if [[ -e "${stored_file}" ]]; then
rm -rf "${destination_file}"
mkdir -p "${location_path}"
mv "${stored_file}" "${destination_file}"
fi
done
rm -rf "${TMP_NVCHAD}"
fi
}
_skip_ahead() {
amount=$1
skip=$((skip + amount))
}
_reorder_array() {
local arr=("$@")
for i in "${!arr[@]}"; do
new_array+=("${arr[i]}")
done
arr=("${new_array[@]}")
unset new_array
echo "${arr[@]}"
}
_parse_args() {
local func_args=$1
local argv=("$@")
unset 'argv[0]' # becuase arg1 is $func_arg
for i in "${!argv[@]}"; do new_array+=("${argv[i]}"); done
argv=("${new_array[@]}")
unset new_array
local argc=${#argv[@]}
_skip_ahead() {
amount=$1
skip=$((skip + amount))
}
_clean_arg() {
arg=$1
if [[ "$arg" == "--"* ]]; then
echo "${arg:2}"
elif [[ "$arg" == "-"* ]]; then
echo "${arg:1}"
fi
}
for j in "${!argv[@]}"; do
if [[ ${skip} -gt 0 ]]; then
left=$((argc - j))
while [[ ${skip} > ${left} ]]; do ((skip--)); done
skip=$((skip - 1))
continue
fi
case ${argv[j]} in
--*) # End of all opt
case ${argv[j]} in
--) # End of all options.
break
;;
*)
# eval "${func_args}" "$(_clean_arg "${argv[j]}")" "$j"
eval "${func_args}" "${argv[j]}" "$j"
;;
esac
;;
-*)
if [[ ${#argv[j]} -le 2 ]]; then
eval "${func_args}" "${argv[j]}" "$j"
else
tangled_args=$(_clean_arg "${argv[j]}")
for ((k = 0; k < ${#tangled_args}; k++)); do
eval "${func_args}" "-${tangled_args:$k:1}" "$j"
done
fi
;;
*)
eval "${func_args}" "${argv[j]}" "$j"
;;
esac
done
}
main() {
local argvs=("$@")
local argc=${#argvs[@]}
local counter=0
_set_params() {
argc=${#argvs[@]}
counter=0
}
_free_arg() {
local index=$1
unset 'argvs[index-counter]'
argvs=($(_reorder_array "${argvs[@]}"))
((counter++))
}
assert_aditional_args() {
var=$1 # flag
index=$2 # flag's index
case ${var} in
-p=* | --path=*)
nvchad_path="${var#*=}"
_free_arg "${index}"
;;
-s | --skip-startup)
do_startup="false"
_free_arg "${index}"
;;
esac
}
assert_args() {
var=$1 # flag
index=$2 # flag's index
case ${var} in
-h | --help)
_usage
;;
-i | --install)
prompt -i "Installing NvChad..."
_install
;;
-r | --remove)
prompt -i "Removing NvChad..."
_remove
;;
-u | --update)
prompt -i "Updating NvChad..."
_update
;;
-c | --clean-backups)
prompt -i "Cleaning NvChad backups..."
_clean_backups
;;
*)
prompt -w "Warning: --unknown command '${var}'"
;;
esac
}
_set_params
_parse_args "assert_aditional_args" "${argvs[@]}"
_set_params
_parse_args "assert_args" "${argvs[@]}"
}
init() {
if [ $# -eq 0 ]; then
prompt -e "ERROR: This script needs at least one argument"
else
main "${@}"
fi
}
init "${@}"