install.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 main() {
  67. parse_arguments "$@"
  68. cat <<'EOF'
  69. 88\ 88\
  70. 88 | \__|
  71. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  72. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  73. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  74. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  75. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  76. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  77. EOF
  78. __add_separator "80"
  79. echo "Detecting platform for managing any additional neovim dependencies"
  80. detect_platform
  81. check_system_deps
  82. __add_separator "80"
  83. if [ "$ARGS_INSTALL_DEPENDENCIES" -eq 1 ]; then
  84. echo "Would you like to install lunarvim's NodeJS dependencies?"
  85. read -p "[y]es or [n]o (default: no) : " -r answer
  86. [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
  87. echo "Would you like to install lunarvim's Python dependencies?"
  88. read -p "[y]es or [n]o (default: no) : " -r answer
  89. [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
  90. echo "Would you like to install lunarvim's Rust dependencies?"
  91. read -p "[y]es or [n]o (default: no) : " -r answer
  92. [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
  93. fi
  94. __add_separator "80"
  95. echo "Backing up old LunarVim configuration"
  96. backup_old_config
  97. __add_separator "80"
  98. if [ "$ARGS_OVERWRITE" -eq 1 ]; then
  99. for dir in "${__lvim_dirs[@]}"; do
  100. [ -d "$dir" ] && rm -rf "$dir"
  101. done
  102. fi
  103. install_packer
  104. __add_separator "80"
  105. if [ -e "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" ]; then
  106. echo "Updating LunarVim"
  107. update_lvim
  108. else
  109. if [ -n "$ARGS_LOCAL" ]; then
  110. link_local_lvim
  111. else
  112. clone_lvim
  113. fi
  114. setup_lvim
  115. fi
  116. __add_separator "80"
  117. }
  118. function detect_platform() {
  119. OS="$(uname -s)"
  120. case "$OS" in
  121. Linux)
  122. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  123. RECOMMEND_INSTALL="sudo pacman -S"
  124. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  125. RECOMMEND_INSTALL="sudo dnf install -y"
  126. elif [ -f "/etc/gentoo-release" ]; then
  127. RECOMMEND_INSTALL="emerge install -y"
  128. else # assume debian based
  129. RECOMMEND_INSTALL="sudo apt install -y"
  130. fi
  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 succesfully 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 succesfully 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 succesfully 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 succesfully 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)
  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 install_packer() {
  248. if [ -e "$LUNARVIM_PACK_DIR/packer/start/packer.nvim" ]; then
  249. echo "Packer already installed"
  250. else
  251. if ! git clone --depth 1 "https://github.com/wbthomason/packer.nvim" \
  252. "$LUNARVIM_PACK_DIR/packer/start/packer.nvim"; then
  253. echo "Failed to clone Packer. Installation failed."
  254. exit 1
  255. fi
  256. fi
  257. }
  258. function clone_lvim() {
  259. echo "Cloning LunarVim configuration"
  260. if ! git clone --branch "$LV_BRANCH" \
  261. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
  262. echo "Failed to clone repository. Installation failed."
  263. exit 1
  264. fi
  265. }
  266. function link_local_lvim() {
  267. echo "Linking local LunarVim repo"
  268. # Detect whether it's a symlink or a folder
  269. if [ -d "$LUNARVIM_RUNTIME_DIR/lvim" ]; then
  270. echo "Removing old installation files"
  271. rm -rf "$LUNARVIM_RUNTIME_DIR/lvim"
  272. fi
  273. mkdir -p "$LUNARVIM_RUNTIME_DIR"
  274. echo " - $BASEDIR -> $LUNARVIM_RUNTIME_DIR/lvim"
  275. ln -s -f "$BASEDIR" "$LUNARVIM_RUNTIME_DIR/lvim"
  276. }
  277. function setup_shim() {
  278. if [ ! -d "$INSTALL_PREFIX/bin" ]; then
  279. mkdir -p "$INSTALL_PREFIX/bin"
  280. fi
  281. cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
  282. #!/bin/sh
  283. export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
  284. export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
  285. exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
  286. EOF
  287. chmod +x "$INSTALL_PREFIX/bin/lvim"
  288. }
  289. function remove_old_cache_files() {
  290. local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
  291. if [ -e "$packer_cache" ]; then
  292. echo "Removing old packer cache file"
  293. rm -f "$packer_cache"
  294. fi
  295. if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
  296. echo "Removing old startup cache file"
  297. rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
  298. fi
  299. }
  300. function setup_lvim() {
  301. remove_old_cache_files
  302. echo "Installing LunarVim shim"
  303. setup_shim
  304. echo "Preparing Packer setup"
  305. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example-no-ts.lua" \
  306. "$LUNARVIM_CONFIG_DIR/config.lua"
  307. "$INSTALL_PREFIX/bin/lvim" --headless \
  308. -c 'autocmd User PackerComplete quitall' \
  309. -c 'PackerSync'
  310. echo "Packer setup complete"
  311. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  312. echo "Thank you for installing LunarVim!!"
  313. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  314. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  315. }
  316. function update_lvim() {
  317. git -C "$LUNARVIM_RUNTIME_DIR/lvim" fetch --quiet
  318. if ! git -C "$LUNARVIM_RUNTIME_DIR/lvim" diff --quiet "@{upstream}"; then
  319. git -C "$LUNARVIM_RUNTIME_DIR/lvim" merge --ff-only --progress ||
  320. echo "Unable to guarantee data integrity while updating. Please do that manually instead." && exit 1
  321. fi
  322. echo "Clearing up old startup cache"
  323. "$INSTALL_PREFIX/bin/lvim" --headless +LvimCacheReset +q
  324. echo "Your LunarVim installation is now up to date!"
  325. }
  326. function __add_separator() {
  327. local DIV_WIDTH="$1"
  328. printf "%${DIV_WIDTH}s\n" ' ' | tr ' ' -
  329. }
  330. main "$@"