install.sh 11 KB

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