-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
358 lines (335 loc) · 11.7 KB
/
install.sh
File metadata and controls
358 lines (335 loc) · 11.7 KB
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
#!/usr/bin/bash
# COLORS
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
NC=$(tput sgr0)
BOLD=$(tput bold)
# VARIABLES
declare -r GIT_BRANCH="${GIT_BRANCH:-"master"}"
declare -r GIT_REMOTE="${GIT_REMOTE:-leonasdev/.dotfiles}"
declare -r NVIM_DIR="${NVIM_DIR:-"$(which nvim)"}"
declare -r INSTALL_PREFIX="${INSTALL_PREFIX:-"$HOME/.local"}"
declare -r XDG_DATA_HOME="${XDG_DATA_HOME:-"$HOME/.local/share"}"
declare -r XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
declare -r RUNTIME_DIR="${RUNTIME_DIR:-"$XDG_DATA_HOME"}"
declare -r CONFIG_DIR="${CONFIG_DIR:-"$XDG_CONFIG_HOME"}"
declare -r PACK_DIR="$RUNTIME_DIR/nvim/site/pack"
# MAIN
function main() {
sudo echo -e "${BOLD}${BLUE}Welcome to leonasdev's dotfiles installation!\n${NC}"
check_tput_installed
pre_check
check_system_deps
if command -v nvim &>/dev/null; then
check_neovim_version
fi
# check if user wnat backup neovim config, otherwise it will overwrite it
if [ -d "$HOME/.config/nvim" ] && ! [ -z "$(ls -A $HOME/.config/nvim)" ]; then
echo "${BOLD}${YELLOW}Destination path $HOME/.config/neovim already exists and is not an empty directory.${NC}"
while [ true ]; do
echo -e "Backup your current ${BOLD}Neovim${NC} config?"
read -p $'\e[33m[y/n]\e[0m: ' yn
case $yn in
[Yy]* ) isBackup=$(backup_old_config);break;;
[Nn]* ) break;;
* ) echo "${BOLD}Please answer ${YELLOW}y${NC}${BOLD} or ${YELLOW}n${NC}${BOLD}.${NC}";;
esac
done
fi
detect_platform
remove_neovim_config
clone_and_checkout_repo
install_deps
post_install
msg "${BOLD}${GREEN}\n\n\n\n\nInstallation Successful !\n${NC}" 1
if [ "$isBackup" ]; then
echo "${BOLD}${GREEN}You can find your backup config under ${CONFIG_DIR}/nvim.bak${NC}"
fi
echo -e "${BOLD}\nNow you can manage your dotfiles by using\n- ${GREEN}git dotfiles${NC}\ncommand.\n"
echo -e "${BOLD}${YELLOW}\nPlease restart your shell.${NC}\n"
}
function pre_check() {
if [ -d "$HOME/.dotfiles" ] && ! [ -z "$(ls -A $HOME/.dotfiles)" ]; then
echo "${BOLD}${YELLOW}Destination path $HOME/.dotfiles already exists and is not an empty directory.${NC}"
echo "${BOLD}${RED}Installation failed.${NC}"
exit 1
fi
while [ true ]; do
echo -e "**${BOLD}${RED}Installation will override your current ${GREEN}[neovim, fish, oh-my-posh] ${RED}configuration!${NC}**"
echo -e "${BOLD}${YELLOW}Continue installation?"
read -p $'\e[33m[y/n]\e[0m: ' yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit 1;break;;
* ) echo "${BOLD}Please answer ${YELLOW}y${NC}${BOLD} or ${YELLOW}n${NC}${BOLD}.${NC}";;
esac
done
}
function post_install() {
echo -e "${BOLD}${BLUE}Removing install.sh...${NC}"
rm install.sh
echo -e "${BOLD}${GREEN}Done${NC}"
echo -e "${BOLD}${BLUE}Removing README.md...${NC}"
rm README.md
echo -e "${BOLD}${GREEN}Done${NC}"
}
function check_neovim_version() {
msg "${BOLD}Checking Neovim version... ${NC}" "1"
regex="^NVIM v"
nvim_ver=$(nvim --version | grep "$regex")
nvim_ver="${nvim_ver/NVIM v/""}"
nvim_ver="${nvim_ver%.*}"
nvim_ver="${nvim_ver##*.}"
required_ver="10"
echo $nvim_ver
if [ "$required_ver" -gt "$nvim_ver" ]; then
echo -e "${BOLD}${RED}[ERROR]: Neovim version needs to greater then 0.10.0 !${NC}"
exit 1
else
echo -e "${BOLD}${GREEN}Neovim version is greater than 0.10.0${NC}"
fi
}
function clone_and_checkout_repo() {
msg "${BOLD}Cloning & checkout dotfiles... ${NC}" "1"
if ! git clone --branch "$GIT_BRANCH" --bare "https://github.com/${GIT_REMOTE}" "$HOME/.dotfiles"; then
echo "Failed to clone repository. Installation failed."
exit 1
else
git config --global alias.dotfiles '!git --git-dir=$HOME/.dotfiles --work-tree=$HOME'
git dotfiles config --local status.showUntrackedFiles no
git dotfiles checkout -f
fi
echo -e "${GREEN}${BOLD}Done${NC}"
}
function remove_neovim_config() {
cd $HOME
msg "${BOLD}${YELLOW}Removing current Neovim configuration... ${NC}"
rm -rf "$RUNTIME_DIR/nvim"
rm -rf "$CONFIG_DIR/nvim"
echo -e "${GREEN}${BOLD}Done${NC}"
}
function print_missing_dep_msg() {
if [ "$#" -eq 1 ] && [ "$1" == "neovim" ] && [ "$OS" == "Darwin" ]; then
echo -e "${BOLD}${RED}[ERROR]: Unable to find neovim dependency${NC}"
echo -e "${BOLD}Please install it first and re-run the installer.${NC}"
echo -e "${BOLD}You need to install latest nightly version. Use: brew install --HEAD neovim${NC}\n"
elif [ "$#" -eq 1 ] && [ "$1" == "neovim" ]; then
echo -e "${BOLD}${RED}[ERROR]: Unable to find neovim dependency${NC}"
echo -e "${BOLD}Please install it first and re-run the installer.${NC}\n"
echo -e "${BOLD}Highly recommend install Neovim from appimage or build from source to get the latest version.${NC}\n"
elif [ "$#" -eq 1 ]; then
echo -e "${BOLD}${RED}[ERROR]: Unable to find dependency [$1]${NC}"
echo -e "${BOLD}Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1${NC}\n"
else
local cmds
cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
printf "${BOLD}${RED}[ERROR]: Unable to find dependencies [%s]${NC}" "$@"
printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
fi
}
function msg() {
local text="$1"
local flag="$2"
local line="$3"
local div_width="80"
# Render line
if [ "$line" != "0" ]; then
printf "%${div_width}s\n" ' ' | tr ' ' -
fi
# Render text
if [ "$flag" == "1" ]; then
echo -e "$text"
else
echo -n "$text"
fi
}
function backup_old_config() {
msg "${BOLD}Backing up your current Neovim configuration...${NC}" "1"
mkdir -p "$CONFIG_DIR/nvim" "$CONFIG_DIR/nvim.bak"
if command -v rsync &>/dev/null; then
rsync --archive -hh --partial --progress --cvs-exclude \
--modify-window=1 "$CONFIG_DIR/nvim"/ "$CONFIG_DIR/nvim.bak"
else
OS="$(uname -s)"
case "$OS" in
Linux | *BSD)
cp -r "$CONFIG_DIR/nvim/"* "$CONFIG_DIR/nvim.bak/."
;;
Darwin)
cp -R "$CONFIG_DIR/nvim/"* "$CONFIG_DIR/nvim.bak/."
;;
*)
echo "OS $OS is not currently supported."
false
return $?
;;
esac
fi
true
return $?
}
function check_system_deps() {
if ! command -v npm &>/dev/null; then
print_missing_dep_msg "npm"
exit 1
fi
if ! command -v node &>/dev/null; then
print_missing_dep_msg "node"
exit 1
fi
if ! command -v git &>/dev/null; then
print_missing_dep_msg "git"
exit 1
fi
}
function install_deps() {
mkdir -p .cache
echo -e "${BOLD}${BLUE}Updating apt...${NC}"
sudo apt update -qq
echo -e "${GREEN}${BOLD}Done${NC}"
if ! command -v bc &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing bc...${NC}"
sudo apt install -qqy bc
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v nvim &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing Neovim 0.10.0 ...${NC}"
sudo apt install -qqy software-properties-common
sudo add-apt-repository -y ppa:neovim-ppa/unstable
sudo apt update -qq
sudo apt install -qqy neovim
echo -e "${GREEN}${BOLD}Done${NC}"
else
check_neovim_version
fi
if ! command -v curl &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing curl...${NC}"
sudo apt install -qqy curl
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v wget &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing wget...${NC}"
sudo apt install -qqy wget
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v clang &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing clang...${NC}"
sudo apt install -qqy clang
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v make &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing make...${NC}"
sudo apt install -qqy make
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v fdfind &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing fd-find...${NC}"
sudo apt install -qqy fd-find
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v rg &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing ripgrep...${NC}"
sudo apt install -qqy ripgrep
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v xclip &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing xclip...${NC}"
sudo apt install -qqy xclip
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v unzip &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing unzip...${NC}"
sudo apt install -qqy unzip
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v cargo &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing rustup...${NC}"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -qy
source $HOME/.cargo/env
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v go &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing golang...${NC}"
sudo rm -rf /usr/local/go
curl -sSL https://go.dev/dl/go1.21.5.linux-amd64.tar.gz | sudo tar -C /usr/local -xzf -
export PATH=$PATH:/usr/local/go/bin
echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.bashrc
echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.profile
echo "Added /usr/local/go/bin to .bashrc and .profile"
echo -e "${GREEN}${BOLD}Done${NC}"
fi
# if ! command -v tree-sitter &>/dev/null; then
# echo -e "${BOLD}${BLUE}Installing tree-sitter-cli, it may take a little while...${NC}"
# cargo install -q tree-sitter-cli
# echo -e "${GREEN}${BOLD}Done${NC}"
# fi
if ! command -v eza &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing eza... (could take a while)${NC}"
cargo install -q eza
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v python3-venv &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing python3-venv...${NC}"
sudo apt install -qqy python3-venv
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v fzf &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing fzf...${NC}"
mkdir -p $HOME/.local/bin
wget -qO- https://github.com/junegunn/fzf/releases/download/v0.70.0/fzf-0.70.0-linux_amd64.tar.gz | tar xzvf - -C $HOME/.local/bin
echo -e "${GREEN}${BOLD}Done${NC}"
fi
if ! command -v oh-my-posh &>/dev/null; then
echo -e "${BOLD}${BLUE}Installing oh-my-posh...${NC}"
sudo wget -q https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
sudo chmod +x /usr/local/bin/oh-my-posh
echo 'eval "$(oh-my-posh init bash --config ~/.config/oh-my-posh/leonasdev.omp.json)"' >> $HOME/.bashrc
echo 'eval "$(oh-my-posh init bash --config ~/.config/oh-my-posh/leonasdev.omp.json)"' >> $HOME/.profile
echo "Added eval oh-my-posh to .bashrc and .profile"
echo -e "${GREEN}${BOLD}Done${NC}"
fi
}
function detect_platform() {
msg "${BOLD}Detecting operating system... ${NC}"
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 install -y"
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 -e "${BOLD}${RED}OS $OS is not currently supported.${NC}"
exit 1
;;
esac
echo -e "${BOLD}\nOS detected: ${BLUE}$OS${NC}"
echo -e "${GREEN}${BOLD}Done${NC}"
}
function check_tput_installed() {
if ! command -v tput &>/dev/null; then
print_missing_dep_msg "tput"
exit 1
fi
}
main