install.sh 12 KB

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