install.sh 13 KB

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