install.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_CACHE_DIR="${LUNARVIM_CACHE_DIR:-"$XDG_CACHE_HOME/lvim"}"
  13. declare -r LUNARVIM_BASE_DIR="${LUNARVIM_BASE_DIR:-"$LUNARVIM_RUNTIME_DIR/lvim"}"
  14. declare -r LUNARVIM_LOG_LEVEL="${LUNARVIM_LOG_LEVEL:-warn}"
  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 INTERACTIVE_MODE=1
  23. declare ADDITIONAL_WARNINGS=""
  24. declare -a __lvim_dirs=(
  25. "$LUNARVIM_CONFIG_DIR"
  26. "$LUNARVIM_RUNTIME_DIR"
  27. "$LUNARVIM_CACHE_DIR"
  28. )
  29. declare -a __npm_deps=(
  30. "neovim"
  31. "tree-sitter-cli"
  32. )
  33. declare -a __pip_deps=(
  34. "pynvim"
  35. )
  36. function usage() {
  37. echo "Usage: install.sh [<options>]"
  38. echo ""
  39. echo "Options:"
  40. echo " -h, --help Print this help message"
  41. echo " -l, --local Install local copy of LunarVim"
  42. echo " -y, --yes Disable confirmation prompts (answer yes to all questions)"
  43. echo " --overwrite Overwrite previous LunarVim configuration (a backup is always performed first)"
  44. echo " --[no-]install-dependencies Whether to automatically install external dependencies (will prompt by default)"
  45. }
  46. function parse_arguments() {
  47. while [ "$#" -gt 0 ]; do
  48. case "$1" in
  49. -l | --local)
  50. ARGS_LOCAL=1
  51. ;;
  52. --overwrite)
  53. ARGS_OVERWRITE=1
  54. ;;
  55. -y | --yes)
  56. INTERACTIVE_MODE=0
  57. ;;
  58. --install-dependencies)
  59. ARGS_INSTALL_DEPENDENCIES=1
  60. ;;
  61. --no-install-dependencies)
  62. ARGS_INSTALL_DEPENDENCIES=0
  63. ;;
  64. -h | --help)
  65. usage
  66. exit 0
  67. ;;
  68. esac
  69. shift
  70. done
  71. }
  72. function msg() {
  73. local text="$1"
  74. local div_width="80"
  75. printf "%${div_width}s\n" ' ' | tr ' ' -
  76. printf "%s\n" "$text"
  77. }
  78. function confirm() {
  79. local question="$1"
  80. while true; do
  81. msg "$question"
  82. read -p "[y]es or [n]o (default: no) : " -r answer
  83. case "$answer" in
  84. y | Y | yes | YES | Yes)
  85. return 0
  86. ;;
  87. n | N | no | NO | No | *[[:blank:]]* | "")
  88. return 1
  89. ;;
  90. *)
  91. msg "Please answer [y]es or [n]o."
  92. ;;
  93. esac
  94. done
  95. }
  96. function main() {
  97. parse_arguments "$@"
  98. print_logo
  99. msg "Detecting platform for managing any additional neovim dependencies"
  100. detect_platform
  101. check_system_deps
  102. if [ "$ARGS_INSTALL_DEPENDENCIES" -eq 1 ]; then
  103. if [ "$INTERACTIVE_MODE" -eq 1 ]; then
  104. if confirm "Would you like to install LunarVim's NodeJS dependencies?"; then
  105. install_nodejs_deps
  106. fi
  107. if confirm "Would you like to install LunarVim's Python dependencies?"; then
  108. install_python_deps
  109. fi
  110. if confirm "Would you like to install LunarVim's Rust dependencies?"; then
  111. install_rust_deps
  112. fi
  113. else
  114. install_nodejs_deps
  115. install_python_deps
  116. install_rust_deps
  117. fi
  118. fi
  119. backup_old_config
  120. verify_lvim_dirs
  121. if [ "$ARGS_LOCAL" -eq 1 ]; then
  122. link_local_lvim
  123. elif [ -d "$LUNARVIM_BASE_DIR" ]; then
  124. validate_lunarvim_files
  125. else
  126. clone_lvim
  127. fi
  128. setup_lvim
  129. msg "$ADDITIONAL_WARNINGS"
  130. msg "Thank you for installing LunarVim!!"
  131. echo "You can start it by running: $INSTALL_PREFIX/bin/lvim"
  132. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  133. }
  134. function detect_platform() {
  135. OS="$(uname -s)"
  136. case "$OS" in
  137. Linux)
  138. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  139. RECOMMEND_INSTALL="sudo pacman -S"
  140. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  141. RECOMMEND_INSTALL="sudo dnf install -y"
  142. elif [ -f "/etc/gentoo-release" ]; then
  143. RECOMMEND_INSTALL="emerge -tv"
  144. else # assume debian based
  145. RECOMMEND_INSTALL="sudo apt install -y"
  146. fi
  147. ;;
  148. FreeBSD)
  149. RECOMMEND_INSTALL="sudo pkg install -y"
  150. ;;
  151. NetBSD)
  152. RECOMMEND_INSTALL="sudo pkgin install"
  153. ;;
  154. OpenBSD)
  155. RECOMMEND_INSTALL="doas pkg_add"
  156. ;;
  157. Darwin)
  158. RECOMMEND_INSTALL="brew install"
  159. ;;
  160. *)
  161. echo "OS $OS is not currently supported."
  162. exit 1
  163. ;;
  164. esac
  165. }
  166. function print_missing_dep_msg() {
  167. if [ "$#" -eq 1 ]; then
  168. echo "[ERROR]: Unable to find dependency [$1]"
  169. echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
  170. else
  171. local cmds
  172. cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
  173. printf "[ERROR]: Unable to find dependencies [%s]" "$@"
  174. printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
  175. fi
  176. }
  177. function check_neovim_min_version() {
  178. local verify_version_cmd='if !has("nvim-0.8") | cquit | else | quit | endif'
  179. # exit with an error if min_version not found
  180. if ! nvim --headless -u NONE -c "$verify_version_cmd"; then
  181. echo "[ERROR]: LunarVim requires at least Neovim v0.8 or higher"
  182. exit 1
  183. fi
  184. }
  185. function verify_core_plugins() {
  186. msg "Verifying core plugins"
  187. if ! bash "$LUNARVIM_BASE_DIR/utils/ci/verify_plugins.sh"; then
  188. echo "[ERROR]: Unable to verify plugins, make sure to manually run ':PackerSync' when starting lvim for the first time."
  189. exit 1
  190. fi
  191. echo "Verification complete!"
  192. }
  193. function validate_lunarvim_files() {
  194. local verify_version_cmd='if v:errmsg != "" | cquit | else | quit | endif'
  195. if ! "$INSTALL_PREFIX/bin/lvim" --headless -c 'LvimUpdate' -c "$verify_version_cmd" &>/dev/null; then
  196. msg "Removing old installation files"
  197. rm -rf "$LUNARVIM_BASE_DIR"
  198. clone_lvim
  199. fi
  200. }
  201. function validate_install_prefix() {
  202. local prefix="$1"
  203. case $PATH in
  204. *"$prefix/bin"*)
  205. return
  206. ;;
  207. esac
  208. local profile="$HOME/.profile"
  209. test -z "$ZSH_VERSION" && profile="$HOME/.zshenv"
  210. ADDITIONAL_WARNINGS="[WARN] the folder $prefix/bin is not on PATH, consider adding 'export PATH=$prefix/bin:\$PATH' to your $profile"
  211. # avoid problems when calling any verify_* function
  212. export PATH="$prefix/bin:$PATH"
  213. }
  214. function check_system_deps() {
  215. validate_install_prefix "$INSTALL_PREFIX"
  216. if ! command -v git &>/dev/null; then
  217. print_missing_dep_msg "git"
  218. exit 1
  219. fi
  220. if ! command -v nvim &>/dev/null; then
  221. print_missing_dep_msg "neovim"
  222. exit 1
  223. fi
  224. check_neovim_min_version
  225. }
  226. function __install_nodejs_deps_pnpm() {
  227. echo "Installing node modules with pnpm.."
  228. pnpm install -g "${__npm_deps[@]}"
  229. echo "All NodeJS dependencies are successfully installed"
  230. }
  231. function __install_nodejs_deps_npm() {
  232. echo "Installing node modules with npm.."
  233. for dep in "${__npm_deps[@]}"; do
  234. if ! npm ls -g "$dep" &>/dev/null; then
  235. printf "installing %s .." "$dep"
  236. npm install -g "$dep"
  237. fi
  238. done
  239. echo "All NodeJS dependencies are successfully installed"
  240. }
  241. function __install_nodejs_deps_yarn() {
  242. echo "Installing node modules with yarn.."
  243. yarn global add "${__npm_deps[@]}"
  244. echo "All NodeJS dependencies are successfully installed"
  245. }
  246. function __validate_node_installation() {
  247. local pkg_manager="$1"
  248. local manager_home
  249. if ! command -v "$pkg_manager" &>/dev/null; then
  250. return 1
  251. fi
  252. if [ "$pkg_manager" == "npm" ]; then
  253. manager_home="$(npm config get prefix 2>/dev/null)"
  254. elif [ "$pkg_manager" == "pnpm" ]; then
  255. manager_home="$(pnpm config get prefix 2>/dev/null)"
  256. else
  257. manager_home="$(yarn global bin 2>/dev/null)"
  258. fi
  259. if [ ! -d "$manager_home" ] || [ ! -w "$manager_home" ]; then
  260. return 1
  261. fi
  262. return 0
  263. }
  264. function install_nodejs_deps() {
  265. local -a pkg_managers=("pnpm" "yarn" "npm")
  266. for pkg_manager in "${pkg_managers[@]}"; do
  267. if __validate_node_installation "$pkg_manager"; then
  268. eval "__install_nodejs_deps_$pkg_manager"
  269. return
  270. fi
  271. done
  272. echo "[WARN]: skipping installing optional nodejs dependencies due to insufficient permissions."
  273. echo "check how to solve it: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally"
  274. }
  275. function install_python_deps() {
  276. echo "Verifying that pip is available.."
  277. if ! python3 -m ensurepip >/dev/null; then
  278. if ! python3 -m pip --version &>/dev/null; then
  279. echo "[WARN]: skipping installing optional python dependencies"
  280. return 1
  281. fi
  282. fi
  283. echo "Installing with pip.."
  284. for dep in "${__pip_deps[@]}"; do
  285. python3 -m pip install --user "$dep" || return 1
  286. done
  287. echo "All Python dependencies are successfully installed"
  288. }
  289. function __attempt_to_install_with_cargo() {
  290. if command -v cargo &>/dev/null; then
  291. echo "Installing missing Rust dependency with cargo"
  292. cargo install "$1"
  293. else
  294. echo "[WARN]: Unable to find cargo. Make sure to install it to avoid any problems"
  295. exit 1
  296. fi
  297. }
  298. # we try to install the missing one with cargo even though it's unlikely to be found
  299. function install_rust_deps() {
  300. local -a deps=("fd::fd-find" "rg::ripgrep")
  301. for dep in "${deps[@]}"; do
  302. if ! command -v "${dep%%::*}" &>/dev/null; then
  303. __attempt_to_install_with_cargo "${dep##*::}"
  304. fi
  305. done
  306. echo "All Rust dependencies are successfully installed"
  307. }
  308. function verify_lvim_dirs() {
  309. if [ "$ARGS_OVERWRITE" -eq 1 ]; then
  310. for dir in "${__lvim_dirs[@]}"; do
  311. [ -d "$dir" ] && rm -rf "$dir"
  312. done
  313. fi
  314. for dir in "${__lvim_dirs[@]}"; do
  315. mkdir -p "$dir"
  316. done
  317. }
  318. function backup_old_config() {
  319. local src="$LUNARVIM_CONFIG_DIR"
  320. if [ ! -d "$src" ]; then
  321. return
  322. fi
  323. mkdir -p "$src.old"
  324. touch "$src/ignore"
  325. msg "Backing up old $src to $src.old"
  326. if command -v rsync &>/dev/null; then
  327. rsync --archive -hh --stats --partial --copy-links --cvs-exclude "$src"/ "$src.old"
  328. else
  329. OS="$(uname -s)"
  330. case "$OS" in
  331. Linux | *BSD)
  332. cp -r "$src/"* "$src.old/."
  333. ;;
  334. Darwin)
  335. cp -R "$src/"* "$src.old/."
  336. ;;
  337. *)
  338. echo "OS $OS is not currently supported."
  339. ;;
  340. esac
  341. fi
  342. msg "Backup operation complete"
  343. }
  344. function clone_lvim() {
  345. msg "Cloning LunarVim configuration"
  346. if ! git clone --branch "$LV_BRANCH" \
  347. "https://github.com/${LV_REMOTE}" "$LUNARVIM_BASE_DIR"; then
  348. echo "Failed to clone repository. Installation failed."
  349. exit 1
  350. fi
  351. }
  352. function link_local_lvim() {
  353. echo "Linking local LunarVim repo"
  354. # Detect whether it's a symlink or a folder
  355. if [ -d "$LUNARVIM_BASE_DIR" ]; then
  356. echo "Removing old installation files"
  357. rm -rf "$LUNARVIM_BASE_DIR"
  358. fi
  359. echo " - $BASEDIR -> $LUNARVIM_BASE_DIR"
  360. ln -s -f "$BASEDIR" "$LUNARVIM_BASE_DIR"
  361. }
  362. function setup_shim() {
  363. make -C "$LUNARVIM_BASE_DIR" install-bin
  364. }
  365. function remove_old_cache_files() {
  366. local packer_cache="$LUNARVIM_CONFIG_DIR/plugin/packer_compiled.lua"
  367. if [ -e "$packer_cache" ]; then
  368. msg "Removing old packer cache file"
  369. rm -f "$packer_cache"
  370. fi
  371. if [ -e "$LUNARVIM_CACHE_DIR/luacache" ] || [ -e "$LUNARVIM_CACHE_DIR/lvim_cache" ]; then
  372. msg "Removing old startup cache file"
  373. rm -f "$LUNARVIM_CACHE_DIR/{luacache,lvim_cache}"
  374. fi
  375. }
  376. function setup_lvim() {
  377. remove_old_cache_files
  378. msg "Installing LunarVim shim"
  379. setup_shim
  380. [ ! -f "$LUNARVIM_CONFIG_DIR/config.lua" ] \
  381. && cp "$LUNARVIM_BASE_DIR/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  382. echo "Preparing Packer setup"
  383. "$INSTALL_PREFIX/bin/lvim" --headless \
  384. -c "lua require('lvim.core.log'):set_level([[$LUNARVIM_LOG_LEVEL]])" \
  385. -c 'autocmd User PackerComplete quitall' \
  386. -c 'PackerSync'
  387. echo "Packer setup complete"
  388. verify_core_plugins
  389. }
  390. function print_logo() {
  391. cat <<'EOF'
  392. 88\ 88\
  393. 88 | \__|
  394. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  395. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  396. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  397. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  398. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  399. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  400. EOF
  401. }
  402. main "$@"