install.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #!/usr/bin/env bash
  2. set -eo pipefail
  3. #Set branch to master unless specified by the user
  4. declare LV_BRANCH="${LV_BRANCH:-"master"}"
  5. declare -r LV_REMOTE="${LV_REMOTE:-lunarvim/lunarvim.git}"
  6. declare -r INSTALL_PREFIX="${INSTALL_PREFIX:-"$HOME/.local"}"
  7. declare -r XDG_DATA_HOME="${XDG_DATA_HOME:-"$HOME/.local/share"}"
  8. declare -r XDG_CACHE_HOME="${XDG_CACHE_HOME:-"$HOME/.cache"}"
  9. declare -r XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
  10. declare -r LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-"$XDG_DATA_HOME/lunarvim"}"
  11. declare -r LUNARVIM_CONFIG_DIR="${LUNARVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/lvim"}"
  12. # TODO: Use a dedicated cache directory #1256
  13. declare -r LUNARVIM_CACHE_DIR="$XDG_CACHE_HOME/nvim"
  14. declare BASEDIR
  15. BASEDIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
  16. BASEDIR="$(dirname -- "$(dirname -- "$BASEDIR")")"
  17. readonly BASEDIR
  18. declare ARGS_LOCAL=0
  19. declare ARGS_OVERWRITE=0
  20. declare ARGS_INSTALL_DEPENDENCIES=1
  21. declare -a __lvim_dirs=(
  22. "$LUNARVIM_CONFIG_DIR"
  23. "$LUNARVIM_RUNTIME_DIR"
  24. "$LUNARVIM_CACHE_DIR"
  25. )
  26. declare -a __npm_deps=(
  27. "neovim"
  28. "tree-sitter-cli"
  29. )
  30. declare -a __pip_deps=(
  31. "pynvim"
  32. )
  33. function usage() {
  34. echo "Usage: install.sh [<options>]"
  35. echo ""
  36. echo "Options:"
  37. echo " -h, --help Print this help message"
  38. echo " -l, --local Install local copy of LunarVim"
  39. echo " --overwrite Overwrite previous LunarVim configuration (a backup is always performed first)"
  40. echo " --[no]-install-dependencies Whether to prompt to install external dependencies (will prompt by default)"
  41. }
  42. function parse_arguments() {
  43. while [ "$#" -gt 0 ]; do
  44. case "$1" in
  45. -l | --local)
  46. ARGS_LOCAL=1
  47. ;;
  48. --overwrite)
  49. ARGS_OVERWRITE=1
  50. ;;
  51. --install-dependencies)
  52. ARGS_INSTALL_DEPENDENCIES=1
  53. ;;
  54. --no-install-dependencies)
  55. ARGS_INSTALL_DEPENDENCIES=0
  56. ;;
  57. -h | --help)
  58. usage
  59. exit 0
  60. ;;
  61. esac
  62. shift
  63. done
  64. }
  65. function msg() {
  66. local text="$1"
  67. local div_width="80"
  68. printf "%${div_width}s\n" ' ' | tr ' ' -
  69. printf "%s\n" "$text"
  70. }
  71. function main() {
  72. parse_arguments "$@"
  73. print_logo
  74. msg "Detecting platform for managing any additional neovim dependencies"
  75. detect_platform
  76. check_system_deps
  77. if [ "$ARGS_INSTALL_DEPENDENCIES" -eq 1 ]; then
  78. msg "Would you like to install LunarVim's NodeJS dependencies?"
  79. read -p "[y]es or [n]o (default: no) : " -r answer
  80. [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
  81. msg "Would you like to install LunarVim's Python dependencies?"
  82. read -p "[y]es or [n]o (default: no) : " -r answer
  83. [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
  84. msg "Would you like to install LunarVim's Rust dependencies?"
  85. read -p "[y]es or [n]o (default: no) : " -r answer
  86. [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
  87. fi
  88. msg "Backing up old LunarVim configuration"
  89. backup_old_config
  90. verify_lvim_dirs
  91. if [ -e "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" ]; then
  92. update_lvim
  93. else
  94. if [ "$ARGS_LOCAL" -eq 1 ]; then
  95. link_local_lvim
  96. else
  97. clone_lvim
  98. fi
  99. setup_lvim
  100. fi
  101. msg "Thank you for installing LunarVim!!"
  102. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  103. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  104. }
  105. function detect_platform() {
  106. OS="$(uname -s)"
  107. case "$OS" in
  108. Linux)
  109. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  110. RECOMMEND_INSTALL="sudo pacman -S"
  111. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  112. RECOMMEND_INSTALL="sudo dnf install -y"
  113. elif [ -f "/etc/gentoo-release" ]; then
  114. RECOMMEND_INSTALL="emerge install -y"
  115. else # assume debian based
  116. RECOMMEND_INSTALL="sudo apt install -y"
  117. fi
  118. ;;
  119. FreeBSD)
  120. RECOMMEND_INSTALL="sudo pkg install -y"
  121. ;;
  122. NetBSD)
  123. RECOMMEND_INSTALL="sudo pkgin install"
  124. ;;
  125. OpenBSD)
  126. RECOMMEND_INSTALL="doas pkg_add"
  127. ;;
  128. Darwin)
  129. RECOMMEND_INSTALL="brew install"
  130. ;;
  131. *)
  132. echo "OS $OS is not currently supported."
  133. exit 1
  134. ;;
  135. esac
  136. }
  137. function print_missing_dep_msg() {
  138. if [ "$#" -eq 1 ]; then
  139. echo "[ERROR]: Unable to find dependency [$1]"
  140. echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
  141. else
  142. local cmds
  143. cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
  144. printf "[ERROR]: Unable to find dependencies [%s]" "$@"
  145. printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
  146. fi
  147. }
  148. function check_neovim_min_version() {
  149. # TODO: consider locking the requirement to 0.6+
  150. local verify_version_cmd='if !has("nvim-0.5.1") | cquit | else | quit | endif'
  151. # exit with an error if min_version not found
  152. if ! nvim --headless -u NONE -c "$verify_version_cmd"; then
  153. echo "[ERROR]: LunarVim requires at least Neovim v0.5.1 or higher"
  154. exit 1
  155. fi
  156. }
  157. function check_system_deps() {
  158. if ! command -v git &>/dev/null; then
  159. print_missing_dep_msg "git"
  160. exit 1
  161. fi
  162. if ! command -v nvim &>/dev/null; then
  163. print_missing_dep_msg "neovim"
  164. exit 1
  165. fi
  166. check_neovim_min_version
  167. }
  168. function __install_nodejs_deps_npm() {
  169. echo "Installing node modules with npm.."
  170. for dep in "${__npm_deps[@]}"; do
  171. if ! npm ls -g "$dep" &>/dev/null; then
  172. printf "installing %s .." "$dep"
  173. npm install -g "$dep"
  174. fi
  175. done
  176. echo "All NodeJS dependencies are successfully installed"
  177. }
  178. function __install_nodejs_deps_yarn() {
  179. echo "Installing node modules with yarn.."
  180. yarn global add "${__npm_deps[@]}"
  181. echo "All NodeJS dependencies are successfully installed"
  182. }
  183. function install_nodejs_deps() {
  184. local -a pkg_managers=("yarn" "npm")
  185. for pkg_manager in "${pkg_managers[@]}"; do
  186. if command -v "$pkg_manager" &>/dev/null; then
  187. eval "__install_nodejs_deps_$pkg_manager"
  188. return
  189. fi
  190. done
  191. print_missing_dep_msg "${pkg_managers[@]}"
  192. exit 1
  193. }
  194. function install_python_deps() {
  195. echo "Verifying that pip is available.."
  196. if ! python3 -m ensurepip &>/dev/null; then
  197. if ! python3 -m pip --version &>/dev/null; then
  198. print_missing_dep_msg "pip"
  199. exit 1
  200. fi
  201. fi
  202. echo "Installing with pip.."
  203. for dep in "${__pip_deps[@]}"; do
  204. python3 -m pip install --user "$dep"
  205. done
  206. echo "All Python dependencies are successfully installed"
  207. }
  208. function __attempt_to_install_with_cargo() {
  209. if command -v cargo &>/dev/null; then
  210. echo "Installing missing Rust dependency with cargo"
  211. cargo install "$1"
  212. else
  213. echo "[WARN]: Unable to find cargo. Make sure to install it to avoid any problems"
  214. exit 1
  215. fi
  216. }
  217. # we try to install the missing one with cargo even though it's unlikely to be found
  218. function install_rust_deps() {
  219. local -a deps=("fd::fd-find" "rg::ripgrep")
  220. for dep in "${deps[@]}"; do
  221. if ! command -v "${dep%%::*}" &>/dev/null; then
  222. __attempt_to_install_with_cargo "${dep##*::}"
  223. fi
  224. done
  225. echo "All Rust dependencies are successfully installed"
  226. }
  227. function verify_lvim_dirs() {
  228. if [ "$ARGS_OVERWRITE" -eq 1 ]; then
  229. for dir in "${__lvim_dirs[@]}"; do
  230. [ -d "$dir" ] && rm -rf "$dir"
  231. done
  232. fi
  233. for dir in "${__lvim_dirs[@]}"; do
  234. mkdir -p "$dir"
  235. done
  236. }
  237. function backup_old_config() {
  238. for dir in "${__lvim_dirs[@]}"; do
  239. if [ ! -d "$dir" ]; then
  240. continue
  241. fi
  242. mkdir -p "$dir.bak"
  243. touch "$dir/ignore"
  244. if command -v rsync &>/dev/null; then
  245. rsync --archive -hh --partial --progress --cvs-exclude \
  246. --modify-window=1 "$dir"/ "$dir.bak"
  247. else
  248. OS="$(uname -s)"
  249. case "$OS" in
  250. Linux | *BSD)
  251. cp -r "$dir/"* "$dir.bak/."
  252. ;;
  253. Darwin)
  254. cp -R "$dir/"* "$dir.bak/."
  255. ;;
  256. *)
  257. echo "OS $OS is not currently supported."
  258. ;;
  259. esac
  260. fi
  261. done
  262. echo "Backup operation complete"
  263. }
  264. function clone_lvim() {
  265. msg "Cloning LunarVim configuration"
  266. if ! git clone --branch "$LV_BRANCH" \
  267. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
  268. echo "Failed to clone repository. Installation failed."
  269. exit 1
  270. fi
  271. }
  272. function link_local_lvim() {
  273. echo "Linking local LunarVim repo"
  274. # Detect whether it's a symlink or a folder
  275. if [ -d "$LUNARVIM_RUNTIME_DIR/lvim" ]; then
  276. echo "Removing old installation files"
  277. rm -rf "$LUNARVIM_RUNTIME_DIR/lvim"
  278. fi
  279. echo " - $BASEDIR -> $LUNARVIM_RUNTIME_DIR/lvim"
  280. ln -s -f "$BASEDIR" "$LUNARVIM_RUNTIME_DIR/lvim"
  281. }
  282. function setup_shim() {
  283. if [ ! -d "$INSTALL_PREFIX/bin" ]; then
  284. mkdir -p "$INSTALL_PREFIX/bin"
  285. fi
  286. cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
  287. #!/bin/sh
  288. export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
  289. export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
  290. exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
  291. EOF
  292. chmod +x "$INSTALL_PREFIX/bin/lvim"
  293. }
  294. function remove_old_cache_files() {
  295. local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
  296. if [ -e "$packer_cache" ]; then
  297. msg "Removing old packer cache file"
  298. rm -f "$packer_cache"
  299. fi
  300. if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
  301. msg "Removing old startup cache file"
  302. rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
  303. fi
  304. }
  305. function setup_lvim() {
  306. remove_old_cache_files
  307. msg "Installing LunarVim shim"
  308. setup_shim
  309. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  310. echo "Preparing Packer setup"
  311. "$INSTALL_PREFIX/bin/lvim" --headless \
  312. -c 'autocmd User PackerComplete quitall' \
  313. -c 'PackerSync'
  314. echo "Packer setup complete"
  315. }
  316. function update_lvim() {
  317. "$INSTALL_PREFIX/bin/lvim" --headless +'LvimUpdate' +q
  318. }
  319. function print_logo() {
  320. cat <<'EOF'
  321. 88\ 88\
  322. 88 | \__|
  323. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  324. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  325. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  326. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  327. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  328. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  329. EOF
  330. }
  331. main "$@"