install.sh 10 KB

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