install.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 main() {
  28. cat <<'EOF'
  29. 88\ 88\
  30. 88 | \__|
  31. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  32. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  33. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  34. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  35. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  36. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  37. EOF
  38. __add_separator "80"
  39. echo "Detecting platform for managing any additional neovim dependencies"
  40. detect_platform
  41. if [ -n "$GITHUB_ACTIONS" ]; then
  42. LV_BRANCH="${GITHUB_REF##*/}"
  43. install_packer
  44. setup_lvim
  45. exit 0
  46. fi
  47. check_system_deps
  48. __add_separator "80"
  49. echo "Would you like to install lunarvim's NodeJS dependencies?"
  50. read -p "[y]es or [n]o (default: no) : " -r answer
  51. [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
  52. echo "Would you like to install lunarvim's Python dependencies?"
  53. read -p "[y]es or [n]o (default: no) : " -r answer
  54. [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
  55. echo "Would you like to install lunarvim's Rust dependencies?"
  56. read -p "[y]es or [n]o (default: no) : " -r answer
  57. [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
  58. __add_separator "80"
  59. echo "Backing up old LunarVim configuration"
  60. backup_old_config
  61. __add_separator "80"
  62. case "$@" in
  63. *--overwrite*)
  64. echo "!!Warning!! -> Removing all lunarvim related config \
  65. because of the --overwrite flag"
  66. read -p "Would you like to continue? [y]es or [n]o : " -r answer
  67. [ "$answer" == "${answer#[Yy]}" ] && exit 1
  68. for dir in "${__lvim_dirs[@]}"; do
  69. [ -d "$dir" ] && rm -rf "$dir"
  70. done
  71. ;;
  72. esac
  73. install_packer
  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 ! python3 -m pip --version &>/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. python3 -m pip 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. touch "$dir/ignore"
  176. if command -v rsync &>/dev/null; then
  177. rsync --archive -hh --partial --progress --cvs-exclude \
  178. --modify-window=1 "$dir"/ "$dir.bak"
  179. else
  180. OS="$(uname -s)"
  181. case "$OS" in
  182. Linux)
  183. cp -r "$dir/"* "$dir.bak/."
  184. ;;
  185. Darwin)
  186. cp -R "$dir/"* "$dir.bak/."
  187. ;;
  188. *)
  189. echo "OS $OS is not currently supported."
  190. ;;
  191. esac
  192. fi
  193. done
  194. echo "Backup operation complete"
  195. }
  196. function install_packer() {
  197. if [ -e "$LUNARVIM_PACK_DIR/packer/start/packer.nvim" ]; then
  198. echo "Packer already installed"
  199. else
  200. if ! git clone --depth 1 "https://github.com/wbthomason/packer.nvim" \
  201. "$LUNARVIM_PACK_DIR/packer/start/packer.nvim"; then
  202. echo "Failed to clone Packer. Installation failed."
  203. exit 1
  204. fi
  205. fi
  206. }
  207. function clone_lvim() {
  208. echo "Cloning LunarVim configuration"
  209. if ! git clone --branch "$LV_BRANCH" \
  210. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
  211. echo "Failed to clone repository. Installation failed."
  212. exit 1
  213. fi
  214. }
  215. function setup_shim() {
  216. if [ ! -d "$INSTALL_PREFIX/bin" ]; then
  217. mkdir -p "$INSTALL_PREFIX/bin"
  218. fi
  219. cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
  220. #!/bin/sh
  221. export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
  222. export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
  223. exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
  224. EOF
  225. chmod +x "$INSTALL_PREFIX/bin/lvim"
  226. }
  227. function setup_lvim() {
  228. echo "Installing LunarVim shim"
  229. setup_shim
  230. echo "Preparing Packer setup"
  231. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example-no-ts.lua" \
  232. "$LUNARVIM_CONFIG_DIR/config.lua"
  233. "$INSTALL_PREFIX/bin/lvim" --headless \
  234. -c 'autocmd User PackerComplete quitall' \
  235. -c 'PackerSync'
  236. echo "Packer setup complete"
  237. cp "$LUNARVIM_RUNTIME_DIR/lvim/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  238. echo "Thank you for installing LunarVim!!"
  239. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  240. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  241. }
  242. function update_lvim() {
  243. git -C "$LUNARVIM_RUNTIME_DIR/lvim" fetch --quiet
  244. if ! git -C "$LUNARVIM_RUNTIME_DIR/lvim" diff --quiet "@{upstream}"; then
  245. git -C "$LUNARVIM_RUNTIME_DIR/lvim" merge --ff-only --progress ||
  246. echo "Unable to guarantee data integrity while updating. Please do that manually instead." && exit 1
  247. fi
  248. echo "Clearing up old startup cache"
  249. "$INSTALL_PREFIX/bin/lvim" --headless +LvimCacheReset +q
  250. echo "Your LunarVim installation is now up to date!"
  251. }
  252. function __add_separator() {
  253. local DIV_WIDTH="$1"
  254. printf "%${DIV_WIDTH}s\n" ' ' | tr ' ' -
  255. }
  256. main "$@"