install.sh 8.3 KB

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