install.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_system_deps() {
  153. if ! command -v git &>/dev/null; then
  154. print_missing_dep_msg "git"
  155. exit 1
  156. fi
  157. if ! command -v nvim &>/dev/null; then
  158. print_missing_dep_msg "neovim"
  159. exit 1
  160. fi
  161. }
  162. function __install_nodejs_deps_npm() {
  163. echo "Installing node modules with npm.."
  164. for dep in "${__npm_deps[@]}"; do
  165. if ! npm ls -g "$dep" &>/dev/null; then
  166. printf "installing %s .." "$dep"
  167. npm install -g "$dep"
  168. fi
  169. done
  170. echo "All NodeJS dependencies are successfully installed"
  171. }
  172. function __install_nodejs_deps_yarn() {
  173. echo "Installing node modules with yarn.."
  174. yarn global add "${__npm_deps[@]}"
  175. echo "All NodeJS dependencies are successfully installed"
  176. }
  177. function install_nodejs_deps() {
  178. local -a pkg_managers=("yarn" "npm")
  179. for pkg_manager in "${pkg_managers[@]}"; do
  180. if command -v "$pkg_manager" &>/dev/null; then
  181. eval "__install_nodejs_deps_$pkg_manager"
  182. return
  183. fi
  184. done
  185. print_missing_dep_msg "${pkg_managers[@]}"
  186. exit 1
  187. }
  188. function install_python_deps() {
  189. echo "Verifying that pip is available.."
  190. if ! python3 -m ensurepip &>/dev/null; then
  191. if ! python3 -m pip --version &>/dev/null; then
  192. print_missing_dep_msg "pip"
  193. exit 1
  194. fi
  195. fi
  196. echo "Installing with pip.."
  197. for dep in "${__pip_deps[@]}"; do
  198. python3 -m pip install --user "$dep"
  199. done
  200. echo "All Python dependencies are successfully installed"
  201. }
  202. function __attempt_to_install_with_cargo() {
  203. if command -v cargo &>/dev/null; then
  204. echo "Installing missing Rust dependency with cargo"
  205. cargo install "$1"
  206. else
  207. echo "[WARN]: Unable to find cargo. Make sure to install it to avoid any problems"
  208. exit 1
  209. fi
  210. }
  211. # we try to install the missing one with cargo even though it's unlikely to be found
  212. function install_rust_deps() {
  213. local -a deps=("fd::fd-find" "rg::ripgrep")
  214. for dep in "${deps[@]}"; do
  215. if ! command -v "${dep%%::*}" &>/dev/null; then
  216. __attempt_to_install_with_cargo "${dep##*::}"
  217. fi
  218. done
  219. echo "All Rust dependencies are successfully installed"
  220. }
  221. function backup_old_config() {
  222. for dir in "${__lvim_dirs[@]}"; do
  223. # we create an empty folder for subsequent commands \
  224. # that require an existing directory
  225. mkdir -p "$dir" "$dir.bak"
  226. touch "$dir/ignore"
  227. if command -v rsync &>/dev/null; then
  228. rsync --archive -hh --partial --progress --cvs-exclude \
  229. --modify-window=1 "$dir"/ "$dir.bak"
  230. else
  231. OS="$(uname -s)"
  232. case "$OS" in
  233. Linux | *BSD)
  234. cp -r "$dir/"* "$dir.bak/."
  235. ;;
  236. Darwin)
  237. cp -R "$dir/"* "$dir.bak/."
  238. ;;
  239. *)
  240. echo "OS $OS is not currently supported."
  241. ;;
  242. esac
  243. fi
  244. done
  245. echo "Backup operation complete"
  246. }
  247. function clone_lvim() {
  248. msg "Cloning LunarVim configuration"
  249. if ! git clone --branch "$LV_BRANCH" \
  250. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
  251. echo "Failed to clone repository. Installation failed."
  252. exit 1
  253. fi
  254. }
  255. function link_local_lvim() {
  256. echo "Linking local LunarVim repo"
  257. # Detect whether it's a symlink or a folder
  258. if [ -d "$LUNARVIM_RUNTIME_DIR/lvim" ]; then
  259. echo "Removing old installation files"
  260. rm -rf "$LUNARVIM_RUNTIME_DIR/lvim"
  261. fi
  262. mkdir -p "$LUNARVIM_RUNTIME_DIR"
  263. echo " - $BASEDIR -> $LUNARVIM_RUNTIME_DIR/lvim"
  264. ln -s -f "$BASEDIR" "$LUNARVIM_RUNTIME_DIR/lvim"
  265. }
  266. function setup_shim() {
  267. if [ ! -d "$INSTALL_PREFIX/bin" ]; then
  268. mkdir -p "$INSTALL_PREFIX/bin"
  269. fi
  270. cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
  271. #!/bin/sh
  272. export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
  273. export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
  274. exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
  275. EOF
  276. chmod +x "$INSTALL_PREFIX/bin/lvim"
  277. }
  278. function remove_old_cache_files() {
  279. local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
  280. if [ -e "$packer_cache" ]; then
  281. msg "Removing old packer cache file"
  282. rm -f "$packer_cache"
  283. fi
  284. if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
  285. msg "Removing old startup cache file"
  286. rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
  287. fi
  288. }
  289. function setup_lvim() {
  290. remove_old_cache_files
  291. msg "Installing LunarVim shim"
  292. setup_shim
  293. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  294. echo "Preparing Packer setup"
  295. "$INSTALL_PREFIX/bin/lvim" --headless \
  296. -c 'autocmd User PackerComplete quitall' \
  297. -c 'PackerSync'
  298. echo "Packer setup complete"
  299. }
  300. function update_lvim() {
  301. "$INSTALL_PREFIX/bin/lvim" --headless +'LvimUpdate' +q
  302. }
  303. function print_logo() {
  304. cat <<'EOF'
  305. 88\ 88\
  306. 88 | \__|
  307. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  308. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  309. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  310. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  311. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  312. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  313. EOF
  314. }
  315. main "$@"