install.sh 11 KB

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