install.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/usr/bin/env bash
  2. set -eo pipefail
  3. #Set branch to master unless specified by the user
  4. declare -r 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. # TODO: Use a dedicated cache directory #1256
  11. declare -r NEOVIM_CACHE_DIR="$XDG_CACHE_HOME/nvim"
  12. declare -r LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-"$XDG_DATA_HOME/lunarvim"}"
  13. declare -r LUNARVIM_CONFIG_DIR="${LUNARVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/lvim"}"
  14. declare -a __lvim_dirs=(
  15. "$LUNARVIM_CONFIG_DIR"
  16. "$LUNARVIM_RUNTIME_DIR"
  17. "$NEOVIM_CACHE_DIR" # for now this is shared with neovim
  18. )
  19. declare -a __npm_deps=(
  20. "neovim"
  21. "tree-sitter-cli"
  22. )
  23. declare -a __pip_deps=(
  24. "pynvim"
  25. )
  26. function main() {
  27. cat <<'EOF'
  28. 88\ 88\
  29. 88 | \__|
  30. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  31. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  32. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  33. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  34. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  35. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  36. EOF
  37. __add_separator "80"
  38. echo "Detecting platform for managing any additional neovim dependencies"
  39. detect_platform
  40. # skip this in a Github workflow
  41. if [ -z "$GITHUB_ACTIONS" ]; then
  42. check_system_deps
  43. __add_separator "80"
  44. echo "Would you like to check lunarvim's NodeJS dependencies?"
  45. read -p "[y]es or [n]o (default: no) : " -r answer
  46. [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
  47. echo "Would you like to check lunarvim's Python dependencies?"
  48. read -p "[y]es or [n]o (default: no) : " -r answer
  49. [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
  50. echo "Would you like to check lunarvim's Rust dependencies?"
  51. read -p "[y]es or [n]o (default: no) : " -r answer
  52. [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
  53. __add_separator "80"
  54. echo "Backing up old LunarVim configuration"
  55. backup_old_config
  56. __add_separator "80"
  57. fi
  58. case "$@" in
  59. *--overwrite*)
  60. echo "!!Warning!! -> Removing all lunarvim related config \
  61. because of the --overwrite flag"
  62. read -p "Would you like to continue? [y]es or [n]o : " -r answer
  63. [ "$answer" == "${answer#[Yy]}" ] && exit 1
  64. for dir in "${__lvim_dirs[@]}"; do
  65. [ -d "$dir" ] && rm -rf "$dir"
  66. done
  67. ;;
  68. esac
  69. if [ -e "$LUNARVIM_RUNTIME_DIR/site/pack/packer/start/packer.nvim" ]; then
  70. echo "Packer already installed"
  71. else
  72. install_packer
  73. fi
  74. __add_separator "80"
  75. if [ -e "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" ]; then
  76. echo "Updating LunarVim"
  77. update_lvim
  78. else
  79. clone_lvim
  80. setup_lvim
  81. fi
  82. __add_separator "80"
  83. }
  84. function detect_platform() {
  85. OS="$(uname -s)"
  86. case "$OS" in
  87. Linux)
  88. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  89. RECOMMEND_INSTALL="sudo pacman -S"
  90. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  91. RECOMMEND_INSTALL="sudo dnf install -y"
  92. elif [ -f "/etc/gentoo-release" ]; then
  93. RECOMMEND_INSTALL="emerge install -y"
  94. else # assume debian based
  95. RECOMMEND_INSTALL="sudo apt install -y"
  96. fi
  97. ;;
  98. Darwin)
  99. RECOMMEND_INSTALL="brew install"
  100. ;;
  101. *)
  102. echo "OS $OS is not currently supported."
  103. exit 1
  104. ;;
  105. esac
  106. }
  107. function print_missing_dep_msg() {
  108. echo "[ERROR]: Unable to find dependency [$1]"
  109. echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
  110. }
  111. function check_dep() {
  112. if ! command -v "$1" &>/dev/null; then
  113. print_missing_dep_msg "$1"
  114. exit 1
  115. fi
  116. }
  117. function check_system_deps() {
  118. if ! command -v git &>/dev/null; then
  119. print_missing_dep_msg "git"
  120. exit 1
  121. fi
  122. if ! command -v nvim &>/dev/null; then
  123. print_missing_dep_msg "neovim"
  124. exit 1
  125. fi
  126. }
  127. function install_nodejs_deps() {
  128. check_dep "npm"
  129. echo "Installing node modules with npm.."
  130. for dep in "${__npm_deps[@]}"; do
  131. if ! npm ls -g "$dep" &>/dev/null; then
  132. printf "installing %s .." "$dep"
  133. npm install -g "$dep"
  134. fi
  135. done
  136. echo "All NodeJS dependencies are succesfully installed"
  137. }
  138. function install_python_deps() {
  139. echo "Verifying that pip is available.."
  140. if ! python3 -m ensurepip &>/dev/null; then
  141. if ! command -v pip3 &>/dev/null; then
  142. print_missing_dep_msg "pip"
  143. exit 1
  144. fi
  145. fi
  146. echo "Installing with pip.."
  147. for dep in "${__pip_deps[@]}"; do
  148. pip3 install --user "$dep"
  149. done
  150. echo "All Python dependencies are succesfully installed"
  151. }
  152. function __attempt_to_install_with_cargo() {
  153. if ! command -v cargo &>/dev/null; then
  154. echo "Installing missing Rust dependency with cargo"
  155. cargo install "$1"
  156. else
  157. echo "[WARN]: Unable to find fd. Make sure to install it to avoid any problems"
  158. fi
  159. }
  160. # we try to install the missing one with cargo even though it's unlikely to be found
  161. function install_rust_deps() {
  162. if ! command -v fd &>/dev/null; then
  163. __attempt_to_install_with_cargo "fd-find"
  164. fi
  165. if ! command -v rg &>/dev/null; then
  166. __attempt_to_install_with_cargo "ripgrep"
  167. fi
  168. echo "All Rust dependencies are succesfully installed"
  169. }
  170. function backup_old_config() {
  171. for dir in "${__lvim_dirs[@]}"; do
  172. # we create an empty folder for subsequent commands \
  173. # that require an existing directory
  174. mkdir -p "$dir" "$dir.bak"
  175. if command -v rsync &>/dev/null; then
  176. rsync --archive -hh --partial --progress \
  177. --modify-window=1 "$dir"/ "$dir.bak"
  178. else
  179. cp -R "$dir/*" "$dir.bak/."
  180. fi
  181. done
  182. echo "Backup operation complete"
  183. }
  184. function install_packer() {
  185. git clone --progress --depth 1 https://github.com/wbthomason/packer.nvim \
  186. "$LUNARVIM_RUNTIME_DIR/site/pack/packer/start/packer.nvim"
  187. }
  188. function clone_lvim() {
  189. echo "Cloning LunarVim configuration"
  190. if ! git clone --progress --branch "$LV_BRANCH" \
  191. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
  192. echo "Failed to clone repository. Installation failed."
  193. exit 1
  194. fi
  195. }
  196. function setup_shim() {
  197. if [ ! -d "$INSTALL_PREFIX/bin" ]; then
  198. mkdir -p "$INSTALL_PREFIX/bin"
  199. fi
  200. cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
  201. #!/bin/sh
  202. export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
  203. export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
  204. exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
  205. EOF
  206. chmod +x "$INSTALL_PREFIX/bin/lvim"
  207. }
  208. function setup_lvim() {
  209. echo "Installing LunarVim shim"
  210. setup_shim
  211. echo "Preparing Packer setup"
  212. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example-no-ts.lua" \
  213. "$LUNARVIM_CONFIG_DIR/config.lua"
  214. nvim -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" --headless \
  215. +'autocmd User PackerComplete sleep 100m | qall' \
  216. +PackerInstall
  217. nvim -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" --headless \
  218. +'autocmd User PackerComplete sleep 100m | qall' \
  219. +PackerSync
  220. echo "Packer setup complete"
  221. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  222. echo "Thank you for installing LunarVim!!"
  223. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  224. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  225. }
  226. function update_lvim() {
  227. if ! git -C "$LUNARVIM_RUNTIME_DIR/lvim" status -uno &>/dev/null; then
  228. git -C "$LUNARVIM_RUNTIME_DIR/lvim" pull --ff-only --progress ||
  229. echo "Unable to guarantee data integrity while updating. Please do that manually instead." && exit 1
  230. fi
  231. echo "Your LunarVim installation is now up to date!"
  232. }
  233. function __add_separator() {
  234. local DIV_WIDTH="$1"
  235. printf "%${DIV_WIDTH}s\n" ' ' | tr ' ' -
  236. }
  237. main "$@"