install.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. if [ "$ARGS_OVERWRITE" -eq 1 ]; then
  91. for dir in "${__lvim_dirs[@]}"; do
  92. [ -d "$dir" ] && rm -rf "$dir"
  93. done
  94. fi
  95. if [ -e "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" ]; then
  96. update_lvim
  97. else
  98. if [ "$ARGS_LOCAL" -eq 1 ]; then
  99. link_local_lvim
  100. else
  101. clone_lvim
  102. fi
  103. setup_lvim
  104. fi
  105. msg "Thank you for installing LunarVim!!"
  106. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  107. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  108. }
  109. function detect_platform() {
  110. OS="$(uname -s)"
  111. case "$OS" in
  112. Linux)
  113. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  114. RECOMMEND_INSTALL="sudo pacman -S"
  115. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  116. RECOMMEND_INSTALL="sudo dnf install -y"
  117. elif [ -f "/etc/gentoo-release" ]; then
  118. RECOMMEND_INSTALL="emerge install -y"
  119. else # assume debian based
  120. RECOMMEND_INSTALL="sudo apt install -y"
  121. fi
  122. ;;
  123. FreeBSD)
  124. RECOMMEND_INSTALL="sudo pkg install -y"
  125. ;;
  126. NetBSD)
  127. RECOMMEND_INSTALL="sudo pkgin install"
  128. ;;
  129. OpenBSD)
  130. RECOMMEND_INSTALL="doas pkg_add"
  131. ;;
  132. Darwin)
  133. RECOMMEND_INSTALL="brew install"
  134. ;;
  135. *)
  136. echo "OS $OS is not currently supported."
  137. exit 1
  138. ;;
  139. esac
  140. }
  141. function print_missing_dep_msg() {
  142. if [ "$#" -eq 1 ]; then
  143. echo "[ERROR]: Unable to find dependency [$1]"
  144. echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
  145. else
  146. local cmds
  147. cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
  148. printf "[ERROR]: Unable to find dependencies [%s]" "$@"
  149. printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
  150. fi
  151. }
  152. function check_neovim_min_version() {
  153. # TODO: consider locking the requirement to 0.6+
  154. local verify_version_cmd='if !has("nvim-0.5.1") | cquit | else | quit | endif'
  155. # exit with an error if min_version not found
  156. if ! nvim --headless -u NONE -c "$verify_version_cmd"; then
  157. echo "[ERROR]: LunarVim requires at least Neovim v0.5.1 or higher"
  158. exit 1
  159. fi
  160. }
  161. function check_system_deps() {
  162. if ! command -v git &>/dev/null; then
  163. print_missing_dep_msg "git"
  164. exit 1
  165. fi
  166. if ! command -v nvim &>/dev/null; then
  167. print_missing_dep_msg "neovim"
  168. exit 1
  169. fi
  170. check_neovim_min_version
  171. }
  172. function __install_nodejs_deps_npm() {
  173. echo "Installing node modules with npm.."
  174. for dep in "${__npm_deps[@]}"; do
  175. if ! npm ls -g "$dep" &>/dev/null; then
  176. printf "installing %s .." "$dep"
  177. npm install -g "$dep"
  178. fi
  179. done
  180. echo "All NodeJS dependencies are successfully installed"
  181. }
  182. function __install_nodejs_deps_yarn() {
  183. echo "Installing node modules with yarn.."
  184. yarn global add "${__npm_deps[@]}"
  185. echo "All NodeJS dependencies are successfully installed"
  186. }
  187. function install_nodejs_deps() {
  188. local -a pkg_managers=("yarn" "npm")
  189. for pkg_manager in "${pkg_managers[@]}"; do
  190. if command -v "$pkg_manager" &>/dev/null; then
  191. eval "__install_nodejs_deps_$pkg_manager"
  192. return
  193. fi
  194. done
  195. print_missing_dep_msg "${pkg_managers[@]}"
  196. exit 1
  197. }
  198. function install_python_deps() {
  199. echo "Verifying that pip is available.."
  200. if ! python3 -m ensurepip &>/dev/null; then
  201. if ! python3 -m pip --version &>/dev/null; then
  202. print_missing_dep_msg "pip"
  203. exit 1
  204. fi
  205. fi
  206. echo "Installing with pip.."
  207. for dep in "${__pip_deps[@]}"; do
  208. python3 -m pip install --user "$dep"
  209. done
  210. echo "All Python dependencies are successfully installed"
  211. }
  212. function __attempt_to_install_with_cargo() {
  213. if command -v cargo &>/dev/null; then
  214. echo "Installing missing Rust dependency with cargo"
  215. cargo install "$1"
  216. else
  217. echo "[WARN]: Unable to find cargo. Make sure to install it to avoid any problems"
  218. exit 1
  219. fi
  220. }
  221. # we try to install the missing one with cargo even though it's unlikely to be found
  222. function install_rust_deps() {
  223. local -a deps=("fd::fd-find" "rg::ripgrep")
  224. for dep in "${deps[@]}"; do
  225. if ! command -v "${dep%%::*}" &>/dev/null; then
  226. __attempt_to_install_with_cargo "${dep##*::}"
  227. fi
  228. done
  229. echo "All Rust dependencies are successfully installed"
  230. }
  231. function backup_old_config() {
  232. for dir in "${__lvim_dirs[@]}"; do
  233. # we create an empty folder for subsequent commands \
  234. # that require an existing directory
  235. mkdir -p "$dir" "$dir.bak"
  236. touch "$dir/ignore"
  237. if command -v rsync &>/dev/null; then
  238. rsync --archive -hh --partial --progress --cvs-exclude \
  239. --modify-window=1 "$dir"/ "$dir.bak"
  240. else
  241. OS="$(uname -s)"
  242. case "$OS" in
  243. Linux | *BSD)
  244. cp -r "$dir/"* "$dir.bak/."
  245. ;;
  246. Darwin)
  247. cp -R "$dir/"* "$dir.bak/."
  248. ;;
  249. *)
  250. echo "OS $OS is not currently supported."
  251. ;;
  252. esac
  253. fi
  254. done
  255. echo "Backup operation complete"
  256. }
  257. function clone_lvim() {
  258. msg "Cloning LunarVim configuration"
  259. if ! git clone --branch "$LV_BRANCH" \
  260. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
  261. echo "Failed to clone repository. Installation failed."
  262. exit 1
  263. fi
  264. }
  265. function link_local_lvim() {
  266. echo "Linking local LunarVim repo"
  267. # Detect whether it's a symlink or a folder
  268. if [ -d "$LUNARVIM_RUNTIME_DIR/lvim" ]; then
  269. echo "Removing old installation files"
  270. rm -rf "$LUNARVIM_RUNTIME_DIR/lvim"
  271. fi
  272. mkdir -p "$LUNARVIM_RUNTIME_DIR"
  273. echo " - $BASEDIR -> $LUNARVIM_RUNTIME_DIR/lvim"
  274. ln -s -f "$BASEDIR" "$LUNARVIM_RUNTIME_DIR/lvim"
  275. }
  276. function setup_shim() {
  277. if [ ! -d "$INSTALL_PREFIX/bin" ]; then
  278. mkdir -p "$INSTALL_PREFIX/bin"
  279. fi
  280. cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
  281. #!/bin/sh
  282. export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
  283. export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
  284. exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
  285. EOF
  286. chmod +x "$INSTALL_PREFIX/bin/lvim"
  287. }
  288. function remove_old_cache_files() {
  289. local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
  290. if [ -e "$packer_cache" ]; then
  291. msg "Removing old packer cache file"
  292. rm -f "$packer_cache"
  293. fi
  294. if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
  295. msg "Removing old startup cache file"
  296. rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
  297. fi
  298. }
  299. function setup_lvim() {
  300. remove_old_cache_files
  301. msg "Installing LunarVim shim"
  302. setup_shim
  303. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  304. echo "Preparing Packer setup"
  305. "$INSTALL_PREFIX/bin/lvim" --headless \
  306. -c 'autocmd User PackerComplete quitall' \
  307. -c 'PackerSync'
  308. echo "Packer setup complete"
  309. }
  310. function update_lvim() {
  311. "$INSTALL_PREFIX/bin/lvim" --headless +'LvimUpdate' +q
  312. }
  313. function print_logo() {
  314. cat <<'EOF'
  315. 88\ 88\
  316. 88 | \__|
  317. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  318. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  319. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  320. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  321. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  322. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  323. EOF
  324. }
  325. main "$@"