install.sh 13 KB

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