install.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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:-"master"}"
  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. declare -r LUNARVIM_BASE_DIR="${LUNARVIM_BASE_DIR:-"$LUNARVIM_RUNTIME_DIR/lvim"}"
  13. # TODO: Use a dedicated cache directory #1256
  14. declare -r LUNARVIM_CACHE_DIR="$XDG_CACHE_HOME/nvim"
  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 Whether 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 msg() {
  67. local text="$1"
  68. local div_width="80"
  69. printf "%${div_width}s\n" ' ' | tr ' ' -
  70. printf "%s\n" "$text"
  71. }
  72. function main() {
  73. parse_arguments "$@"
  74. print_logo
  75. msg "Detecting platform for managing any additional neovim dependencies"
  76. detect_platform
  77. check_system_deps
  78. if [ "$ARGS_INSTALL_DEPENDENCIES" -eq 1 ]; then
  79. msg "Would you like to install LunarVim's NodeJS dependencies?"
  80. read -p "[y]es or [n]o (default: no) : " -r answer
  81. [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
  82. msg "Would you like to install LunarVim's Python dependencies?"
  83. read -p "[y]es or [n]o (default: no) : " -r answer
  84. [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
  85. msg "Would you like to install LunarVim's Rust dependencies?"
  86. read -p "[y]es or [n]o (default: no) : " -r answer
  87. [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
  88. fi
  89. backup_old_config
  90. verify_lvim_dirs
  91. if [ "$ARGS_LOCAL" -eq 1 ]; then
  92. link_local_lvim
  93. elif [ -d "$LUNARVIM_BASE_DIR" ]; then
  94. validate_lunarvim_files
  95. else
  96. clone_lvim
  97. fi
  98. setup_lvim
  99. msg "Thank you for installing LunarVim!!"
  100. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  101. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  102. }
  103. function detect_platform() {
  104. OS="$(uname -s)"
  105. case "$OS" in
  106. Linux)
  107. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  108. RECOMMEND_INSTALL="sudo pacman -S"
  109. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  110. RECOMMEND_INSTALL="sudo dnf install -y"
  111. elif [ -f "/etc/gentoo-release" ]; then
  112. RECOMMEND_INSTALL="emerge install -y"
  113. else # assume debian based
  114. RECOMMEND_INSTALL="sudo apt install -y"
  115. fi
  116. ;;
  117. FreeBSD)
  118. RECOMMEND_INSTALL="sudo pkg install -y"
  119. ;;
  120. NetBSD)
  121. RECOMMEND_INSTALL="sudo pkgin install"
  122. ;;
  123. OpenBSD)
  124. RECOMMEND_INSTALL="doas pkg_add"
  125. ;;
  126. Darwin)
  127. RECOMMEND_INSTALL="brew install"
  128. ;;
  129. *)
  130. echo "OS $OS is not currently supported."
  131. exit 1
  132. ;;
  133. esac
  134. }
  135. function print_missing_dep_msg() {
  136. if [ "$#" -eq 1 ]; then
  137. echo "[ERROR]: Unable to find dependency [$1]"
  138. echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
  139. else
  140. local cmds
  141. cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
  142. printf "[ERROR]: Unable to find dependencies [%s]" "$@"
  143. printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
  144. fi
  145. }
  146. function check_neovim_min_version() {
  147. local verify_version_cmd='if !has("nvim-0.6.1") | cquit | else | quit | endif'
  148. # exit with an error if min_version not found
  149. if ! nvim --headless -u NONE -c "$verify_version_cmd"; then
  150. echo "[ERROR]: LunarVim requires at least Neovim v0.6.1 or higher"
  151. exit 1
  152. fi
  153. }
  154. function validate_lunarvim_files() {
  155. local verify_version_cmd='if v:errmsg != "" | cquit | else | quit | endif'
  156. if ! "$INSTALL_PREFIX/bin/lvim" --headless -c 'LvimUpdate' -c "$verify_version_cmd" &>/dev/null; then
  157. msg "Removing old installation files"
  158. rm -rf "$LUNARVIM_BASE_DIR"
  159. clone_lvim
  160. fi
  161. }
  162. function check_system_deps() {
  163. if ! command -v git &>/dev/null; then
  164. print_missing_dep_msg "git"
  165. exit 1
  166. fi
  167. if ! command -v nvim &>/dev/null; then
  168. print_missing_dep_msg "neovim"
  169. exit 1
  170. fi
  171. check_neovim_min_version
  172. }
  173. function __install_nodejs_deps_pnpm() {
  174. echo "Installing node modules with pnpm.."
  175. pnpm install -g "${__npm_deps[@]}"
  176. echo "All NodeJS dependencies are successfully installed"
  177. }
  178. function __install_nodejs_deps_npm() {
  179. echo "Installing node modules with npm.."
  180. for dep in "${__npm_deps[@]}"; do
  181. if ! npm ls -g "$dep" &>/dev/null; then
  182. printf "installing %s .." "$dep"
  183. npm install -g "$dep"
  184. fi
  185. done
  186. echo "All NodeJS dependencies are successfully installed"
  187. }
  188. function __install_nodejs_deps_yarn() {
  189. echo "Installing node modules with yarn.."
  190. yarn global add "${__npm_deps[@]}"
  191. echo "All NodeJS dependencies are successfully installed"
  192. }
  193. function __validate_node_installation() {
  194. local pkg_manager="$1"
  195. local manager_home
  196. if ! command -v "$pkg_manager" &>/dev/null; then
  197. return 1
  198. fi
  199. if [ "$pkg_manager" == "npm" ]; then
  200. manager_home="$(npm config get prefix 2>/dev/null)"
  201. elif [ "$pkg_manager" == "pnpm" ]; then
  202. manager_home="$(pnpm config get prefix 2>/dev/null)"
  203. else
  204. manager_home="$(yarn global bin 2>/dev/null)"
  205. fi
  206. if [ ! -d "$manager_home" ] || [ ! -w "$manager_home" ]; then
  207. echo "[ERROR] Unable to install using [$pkg_manager] without administrative privileges."
  208. return 1
  209. fi
  210. return 0
  211. }
  212. function install_nodejs_deps() {
  213. local -a pkg_managers=("pnpm" "yarn" "npm")
  214. for pkg_manager in "${pkg_managers[@]}"; do
  215. if __validate_node_installation "$pkg_manager"; then
  216. eval "__install_nodejs_deps_$pkg_manager"
  217. return
  218. fi
  219. done
  220. print_missing_dep_msg "${pkg_managers[@]}"
  221. exit 1
  222. }
  223. function install_python_deps() {
  224. echo "Verifying that pip is available.."
  225. if ! python3 -m ensurepip &>/dev/null; then
  226. if ! python3 -m pip --version &>/dev/null; then
  227. print_missing_dep_msg "pip"
  228. exit 1
  229. fi
  230. fi
  231. echo "Installing with pip.."
  232. for dep in "${__pip_deps[@]}"; do
  233. python3 -m pip install --user "$dep"
  234. done
  235. echo "All Python dependencies are successfully installed"
  236. }
  237. function __attempt_to_install_with_cargo() {
  238. if command -v cargo &>/dev/null; then
  239. echo "Installing missing Rust dependency with cargo"
  240. cargo install "$1"
  241. else
  242. echo "[WARN]: Unable to find cargo. Make sure to install it to avoid any problems"
  243. exit 1
  244. fi
  245. }
  246. # we try to install the missing one with cargo even though it's unlikely to be found
  247. function install_rust_deps() {
  248. local -a deps=("fd::fd-find" "rg::ripgrep")
  249. for dep in "${deps[@]}"; do
  250. if ! command -v "${dep%%::*}" &>/dev/null; then
  251. __attempt_to_install_with_cargo "${dep##*::}"
  252. fi
  253. done
  254. echo "All Rust dependencies are successfully installed"
  255. }
  256. function verify_lvim_dirs() {
  257. if [ "$ARGS_OVERWRITE" -eq 1 ]; then
  258. for dir in "${__lvim_dirs[@]}"; do
  259. [ -d "$dir" ] && rm -rf "$dir"
  260. done
  261. fi
  262. for dir in "${__lvim_dirs[@]}"; do
  263. mkdir -p "$dir"
  264. done
  265. }
  266. function backup_old_config() {
  267. for dir in "${__lvim_dirs[@]}"; do
  268. if [ ! -d "$dir" ]; then
  269. continue
  270. fi
  271. mkdir -p "$dir.bak"
  272. touch "$dir/ignore"
  273. msg "Backing up old $dir to $dir.bak"
  274. if command -v rsync &>/dev/null; then
  275. rsync --archive -hh --stats --partial --copy-links --cvs-exclude "$dir"/ "$dir.bak"
  276. else
  277. OS="$(uname -s)"
  278. case "$OS" in
  279. Linux | *BSD)
  280. cp -r "$dir/"* "$dir.bak/."
  281. ;;
  282. Darwin)
  283. cp -R "$dir/"* "$dir.bak/."
  284. ;;
  285. *)
  286. echo "OS $OS is not currently supported."
  287. ;;
  288. esac
  289. fi
  290. done
  291. msg "Backup operation complete"
  292. }
  293. function clone_lvim() {
  294. msg "Cloning LunarVim configuration"
  295. if ! git clone --branch "$LV_BRANCH" \
  296. --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_BASE_DIR"; then
  297. echo "Failed to clone repository. Installation failed."
  298. exit 1
  299. fi
  300. }
  301. function link_local_lvim() {
  302. echo "Linking local LunarVim repo"
  303. # Detect whether it's a symlink or a folder
  304. if [ -d "$LUNARVIM_BASE_DIR" ]; then
  305. echo "Removing old installation files"
  306. rm -rf "$LUNARVIM_BASE_DIR"
  307. fi
  308. echo " - $BASEDIR -> $LUNARVIM_BASE_DIR"
  309. ln -s -f "$BASEDIR" "$LUNARVIM_BASE_DIR"
  310. }
  311. function setup_shim() {
  312. make -C "$LUNARVIM_BASE_DIR" install-bin
  313. }
  314. function remove_old_cache_files() {
  315. local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
  316. if [ -e "$packer_cache" ]; then
  317. msg "Removing old packer cache file"
  318. rm -f "$packer_cache"
  319. fi
  320. if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
  321. msg "Removing old startup cache file"
  322. rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
  323. fi
  324. }
  325. function setup_lvim() {
  326. remove_old_cache_files
  327. msg "Installing LunarVim shim"
  328. setup_shim
  329. cp "$LUNARVIM_BASE_DIR/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  330. echo "Preparing Packer setup"
  331. "$INSTALL_PREFIX/bin/lvim" --headless \
  332. -c 'autocmd User PackerComplete quitall' \
  333. -c 'PackerSync'
  334. echo "Packer setup complete"
  335. }
  336. function print_logo() {
  337. cat <<'EOF'
  338. 88\ 88\
  339. 88 | \__|
  340. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  341. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  342. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  343. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  344. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  345. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  346. EOF
  347. }
  348. main "$@"