install.sh 9.9 KB

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