install.sh 13 KB

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