install.sh 12 KB

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