-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·285 lines (237 loc) · 8.89 KB
/
setup.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
#!/usr/bin/env bash
set -eo pipefail
declare HV_BRANCH="main" # stable branch
declare -r HV_REMOTE="f1zm0/Hypervim.git"
declare -r XDG_DATA_HOME="$HOME/.local/share"
declare -r XDG_CACHE_HOME="$HOME/.cache"
declare -r XDG_CONFIG_HOME="$HOME/.config"
declare -r HYPERVIM_CONFIG_DIR="$XDG_CONFIG_HOME/hvim"
declare -r HYPERVIM_CACHE_DIR="$XDG_CACHE_HOME/hvim"
declare -r HYPERVIM_BASE_DIR="$XDG_CONFIG_HOME/hvim"
declare -r INSTALL_PREFIX="$HOME/.local"
declare NEOVIM_MIN_VERSION=0.8
function usage() {
echo "Usage: setup.sh [<options>]"
echo ""
echo "Options:"
echo " -h, --help Print this help message"
echo " --branch Specify a specific branch."
}
function parse_arguments() {
while [ "$#" -gt 0 ]; do
case "$1" in
--branch)
HV_BRANCH="$2"
;;
-h | --help)
usage
exit 0
;;
esac
shift
done
}
function msg() {
local text="$1"
local div_width="80"
printf "%${div_width}s\n" ' ' | tr ' ' -
printf "%s\n" "$text"
}
function confirm() {
local question="$1"
while true; do
msg "$question"
read -p "[y]es or [n]o (default: no) : " -r answer
case "$answer" in
y | Y | yes | YES | Yes | YEs | YeS | yES | yeS | yEs)
return 0
;;
n | N | no | NO | No | nO | *[[:blank:]]* | "")
return 1
;;
*)
msg "Please answer [y]es or [n]o."
;;
esac
done
}
function detect_platform() {
OS="$(uname -s)"
case "$OS" in
Linux)
if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
RECOMMEND_INSTALL="sudo pacman -S"
elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
RECOMMEND_INSTALL="sudo dnf install -y"
elif [ -f "/etc/gentoo-release" ]; then
RECOMMEND_INSTALL="emerge -tv"
else # assume debian based
RECOMMEND_INSTALL="sudo apt install -y"
fi
;;
FreeBSD)
RECOMMEND_INSTALL="sudo pkg install -y"
;;
NetBSD)
RECOMMEND_INSTALL="sudo pkgin install"
;;
OpenBSD)
RECOMMEND_INSTALL="doas pkg_add"
;;
Darwin)
RECOMMEND_INSTALL="brew install"
;;
*)
echo "OS $OS is not currently supported."
exit 1
;;
esac
}
function print_missing_dep_msg() {
if [ "$#" -eq 1 ]; then
echo "[ERROR]: Unable to find dependency [$1]"
echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
else
local cmds
cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
printf "[ERROR]: Unable to find dependencies [%s]" "$@"
printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
fi
}
function check_neovim_min_version() {
local verify_version_cmd="if !has(\"nvim-${NEOVIM_MIN_VERSION}\") | cquit | else | quit | endif"
# exit with an error if min_version not found
if ! nvim --headless -u NONE -c "$verify_version_cmd"; then
echo "[ERROR]: Hypervim requires at least Neovim v${NEOVIM_MIN_VERSION} or higher"
exit 1
fi
}
function check_nodejs_version() {
local node_major_version
node_major_version=$(node --version | cut -d. -f1 | sed 's/v//')
echo "Detected node version: $node_major_version"
if [ "$node_major_version" -ge 18 ]; then
echo "[WARNING]: Copilot requires NodeJS version < 18. Downgrade version if you intend to use Copilot"
fi
}
function check_system_deps() {
local deps_list=("curl" "git" "node" "npm" "yarn" "make" "cc" "fzf" "unzip")
for dep in "${deps_list[@]}"; do
if ! command -v "$dep" &>/dev/null; then
print_missing_dep_msg "$dep"
exit 1
fi
done
check_neovim_min_version
}
function backup_old_config() {
local src="$HYPERVIM_CONFIG_DIR"
if [ ! -d "$src" ]; then
return
fi
mkdir -p "$src.old"
touch "$src/ignore"
msg "Backing up old $src to $src.old"
if command -v rsync &>/dev/null; then
rsync --archive -hh --stats --partial --copy-links --cvs-exclude "$src"/ "$src.old"
else
OS="$(uname -s)"
case "$OS" in
Linux | *BSD)
cp -r "$src/"* "$src.old/."
;;
Darwin)
cp -R "$src/"* "$src.old/."
;;
*)
echo "OS $OS is not currently supported."
;;
esac
fi
msg "Backup operation complete"
}
function clone_hvim() {
msg "Cloning Hypervim configuration ..."
if [[ -d "$HYPERVIM_BASE_DIR" ]]; then
if confirm "$HYPERVIM_BASE_DIR already exists. Do you want to remove it by doing another git clone?"; then
echo "Removing existing ${HYPERVIM_BASE_DIR} directory."
rm -rf "$HYPERVIM_BASE_DIR"
else
msg "Ok. Exiting."
exit 1
fi
fi
if ! git clone --branch "$HV_BRANCH" "https://github.com/$HV_REMOTE" "$HYPERVIM_BASE_DIR"; then
echo "Failed to clone repository. Installation failed."
exit 1
fi
}
function create_executable() {
local dst="$INSTALL_PREFIX/bin/hvim"
if [[ ! -d "$INSTALL_PREFIX/bin" ]]; then
mkdir -p "$INSTALL_PREFIX/bin"
fi
if [[ -f $dst ]]; then
rm -rf "$dst"
fi
cat <<EOF >"$dst"
#!/bin/sh
export HYPERVIM_CONFIG_DIR="${HYPERVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/hvim"}"
export HYPERVIM_CACHE_DIR="${HYPERVIM_CACHE_DIR:-"$XDG_CACHE_HOME/hvim"}"
if [ \$# -eq 0 ]; then
exec nvim -u "$HYPERVIM_CONFIG_DIR/init.lua"
else
exec nvim -u "$HYPERVIM_CONFIG_DIR/init.lua" "\$@"
fi
EOF
chmod +x "$dst"
}
function remove_old_cache_files() {
local packer_cache="$HYPERVIM_CONFIG_DIR/plugin/packer_compiled.lua"
if [ -e "$packer_cache" ]; then
msg "Removing old packer cache file"
rm -f "$packer_cache"
fi
if [ -e "$HYPERVIM_CACHE_DIR/luacache" ] || [ -e "$HYPERVIM_CACHE_DIR/hvim_cache" ]; then
msg "Removing old startup cache file"
rm -f "$HYPERVIM_CACHE_DIR/{luacache,hvim_cache}"
fi
}
function print_logo() {
cat <<'EOF'
⠀⠀⠀⠀⠀⢀⣤⣴⡦⠀⠀⠀⠀⠀⢀⣴⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣼⣿⣿⠇⠀⠀⠀⠀⢀⣿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⣿⣿⡿⠀⠀⠀⠀⠀⣼⡿⠀⠀⠀⠀⠀⢀⣀⣠⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣦⠀⠀⠀⠀⠀⠀⣠⠞⠁⠀⠀⠀⠀⠀⠀⠀⢀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢘⣻⣻⠃⠀⠀⠀⠀⣸⣿⠃⠀⣴⣆⢀⣴⣿⡿⠁⣠⣴⣶⣶⣤⣤⣤⣤⣤⣴⡆⣠⣶⣶⣶⣶⣶⣶⣶⣶⡶⢆⣤⣶⣶⣶⣶⣶⣶⣶⣶⣌⢻⣿⣇⠀⠀⠀⣠⡾⠻⠿⠿⢿⣿⣿⣿⠟⠀⢸⣿⣿⣿⡀⠀⢀⣤⣶⠀⠀⠀
⠀⠀⠀⢠⣿⣽⣿⣶⣶⣶⣶⣶⣿⣿⣦⣄⠙⠛⣿⣿⠟⠀⣴⣿⣿⣥⣤⣶⣶⡾⠿⠛⢋⣼⣿⣿⣉⣉⣉⣡⣤⣄⠀⢀⣾⣿⣤⣤⣴⣶⠿⠟⠛⠉⠁⠈⣿⣿⡀⢠⣾⡿⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⣿⣿⠈⣿⣇⢠⣾⡿⣿⡆⠀⠀
⠀⠀⠀⣾⣿⡿⠁⠀⠀⠀⢸⣿⣿⠏⠁⠀⢀⣾⣿⠋⠀⣻⣿⡿⠛⠋⠉⠁⠀⠀⠀⣠⣬⣿⣿⠿⠛⠛⠛⠉⠉⠁⢀⣾⡿⠛⠿⢿⣿⣷⣶⣤⣤⣀⠀⠀⠸⣿⣷⣿⡾⠁⠀⠀⠀⠀⣼⡿⠀⠀⠀⢀⣿⡇⠀⢿⣿⣿⠛⠀⢿⣧⠀⠀
⠀⠀⣸⣿⣿⠁⠀⠀⠀⢀⣿⣿⠏⠀⠀⣠⣿⡿⠁⠀⣼⣿⠟⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣻⣥⣰⣶⣶⣾⡿⠿⠁⣼⡿⠁⠀⠀⠀⠀⠈⠉⠙⠛⠁⠀⠀⠀⢿⣿⡟⠁⠀⣠⣤⣤⣤⣿⣧⣤⡄⠀⣸⣿⠀⠀⢸⡿⠏⠀⠀⠸⣿⡀⠀
⠀⢀⣿⣿⠃⠀⠀⠀⠀⣸⣿⡟⠀⠀⣴⠟⠋⠀⢀⣾⡿⠋⠀⠀⠀⠀⠀⠀⠀⠘⠻⠿⠿⠛⠛⠋⠉⠁⠀⠀⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠋⠀⠀⠀⠘⠛⠛⠋⠉⠉⠉⠉⠀⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀
⠀⡼⠉⠀⠀⠀⠀⠀⠀⠈⠛⠁⠀⠞⠃⠀⠀⠠⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠃
EOF
}
function setup_hvim() {
remove_old_cache_files
msg "Creating Hypervim executable"
create_executable
msg "Installing packer and sync plugins"
"$INSTALL_PREFIX/bin/hvim" --headless \
-c 'autocmd User PackerComplete quitall' \
-c 'PackerSync'
echo "Hypervim setup complete"
}
function main() {
parse_arguments "$@"
print_logo
detect_platform
check_system_deps
check_nodejs_version
backup_old_config
clone_hvim
setup_hvim
msg "Thank you for installing Hypervim."
echo "You can start it by running: hvim"
echo "[make sure $INSTALL_PREFIX/bin is in \$PATH]"
}
main "$@"