install.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 __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/BunJS dependencies: $(stringify_array "${__npm_deps[@]}")?"; then
  114. install_nodejs_deps
  115. fi
  116. if confirm "Would you like to install LunarVim's Rust dependencies: $(stringify_array "${__rust_deps[@]}")?"; then
  117. install_rust_deps
  118. fi
  119. else
  120. install_nodejs_deps
  121. install_rust_deps
  122. fi
  123. fi
  124. remove_old_cache_files
  125. verify_lvim_dirs
  126. if [ "$ARGS_LOCAL" -eq 1 ]; then
  127. link_local_lvim
  128. else
  129. clone_lvim
  130. fi
  131. setup_lvim
  132. msg "$ADDITIONAL_WARNINGS"
  133. msg "Thank you for installing LunarVim!!"
  134. echo "You can start it by running: $INSTALL_PREFIX/bin/$NVIM_APPNAME"
  135. echo "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  136. }
  137. function detect_platform() {
  138. case "$OS" in
  139. Linux)
  140. if [ -f "/etc/arch-release" ] || [ -f "/etc/artix-release" ]; then
  141. RECOMMEND_INSTALL="sudo pacman -S"
  142. elif [ -f "/etc/fedora-release" ] || [ -f "/etc/redhat-release" ]; then
  143. RECOMMEND_INSTALL="sudo dnf install -y"
  144. elif [ -f "/etc/gentoo-release" ]; then
  145. RECOMMEND_INSTALL="emerge -tv"
  146. else # assume debian based
  147. RECOMMEND_INSTALL="sudo apt install -y"
  148. fi
  149. ;;
  150. FreeBSD)
  151. RECOMMEND_INSTALL="sudo pkg install -y"
  152. ;;
  153. NetBSD)
  154. RECOMMEND_INSTALL="sudo pkgin install"
  155. ;;
  156. OpenBSD)
  157. RECOMMEND_INSTALL="doas pkg_add"
  158. ;;
  159. Darwin)
  160. RECOMMEND_INSTALL="brew install"
  161. ;;
  162. *)
  163. echo "OS $OS is not currently supported."
  164. exit 1
  165. ;;
  166. esac
  167. }
  168. function print_missing_dep_msg() {
  169. if [ "$#" -eq 1 ]; then
  170. echo "[ERROR]: Unable to find dependency [$1]"
  171. echo "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $1"
  172. else
  173. local cmds
  174. cmds=$(for i in "$@"; do echo "$RECOMMEND_INSTALL $i"; done)
  175. printf "[ERROR]: Unable to find dependencies [%s]" "$@"
  176. printf "Please install any one of the dependencies and re-run the installer. Try: \n%s\n" "$cmds"
  177. fi
  178. }
  179. function check_neovim_min_version() {
  180. local verify_version_cmd='if !has("nvim-0.9") | cquit | else | quit | endif'
  181. # exit with an error if min_version not found
  182. if ! nvim --headless -u NONE -c "$verify_version_cmd"; then
  183. echo "[ERROR]: LunarVim requires at least Neovim v0.9 or higher"
  184. exit 1
  185. fi
  186. }
  187. function verify_core_plugins() {
  188. msg "Verifying core plugins"
  189. if ! bash "$LUNARVIM_BASE_DIR/utils/ci/verify_plugins.sh"; then
  190. echo "[ERROR]: Unable to verify plugins, make sure to manually run ':Lazy sync' when starting lvim for the first time."
  191. exit 1
  192. fi
  193. echo "Verification complete!"
  194. }
  195. function validate_install_prefix() {
  196. local prefix="$1"
  197. case $PATH in
  198. *"$prefix/bin"*)
  199. return
  200. ;;
  201. esac
  202. local profile="$HOME/.profile"
  203. test -z "$ZSH_VERSION" && profile="$HOME/.zshenv"
  204. ADDITIONAL_WARNINGS="[WARN] the folder $prefix/bin is not on PATH, consider adding 'export PATH=$prefix/bin:\$PATH' to your $profile"
  205. # avoid problems when calling any verify_* function
  206. export PATH="$prefix/bin:$PATH"
  207. }
  208. function check_system_deps() {
  209. validate_install_prefix "$INSTALL_PREFIX"
  210. if ! command -v git &>/dev/null; then
  211. print_missing_dep_msg "git"
  212. exit 1
  213. fi
  214. if ! command -v nvim &>/dev/null; then
  215. print_missing_dep_msg "neovim"
  216. exit 1
  217. fi
  218. check_neovim_min_version
  219. }
  220. function __install_nodejs_deps_pnpm() {
  221. echo "Installing node modules with pnpm.."
  222. pnpm install -g "${__npm_deps[@]}"
  223. echo "All NodeJS dependencies are successfully installed"
  224. }
  225. function __install_nodejs_deps_npm() {
  226. echo "Installing node modules with npm.."
  227. for dep in "${__npm_deps[@]}"; do
  228. if ! npm ls -g "$dep" &>/dev/null; then
  229. printf "installing %s .." "$dep"
  230. npm install -g "$dep"
  231. fi
  232. done
  233. echo "All NodeJS dependencies are successfully installed"
  234. }
  235. function __install_nodejs_deps_yarn() {
  236. echo "Installing node modules with yarn.."
  237. yarn global add "${__npm_deps[@]}"
  238. echo "All NodeJS dependencies are successfully installed"
  239. }
  240. function __install_nodejs_deps_bun() {
  241. echo "Installing bun modules with bun..."
  242. bun install -g "${__npm_deps[@]}"
  243. echo "All BunJS dependencies are successfully installed"
  244. }
  245. function __validate_node_installation() {
  246. local pkg_manager="$1"
  247. local manager_home
  248. if ! command -v "$pkg_manager" &>/dev/null; then
  249. return 1
  250. fi
  251. if [ "$pkg_manager" == "npm" ]; then
  252. manager_home="$(npm config get prefix 2>/dev/null)"
  253. elif [ "$pkg_manager" == "bun" ]; then
  254. manager_home="$BUN_INSTALL"
  255. elif [ "$pkg_manager" == "pnpm" ]; then
  256. manager_home="$(pnpm config get prefix 2>/dev/null)"
  257. else
  258. manager_home="$(yarn global bin 2>/dev/null)"
  259. fi
  260. if [ ! -d "$manager_home" ] || [ ! -w "$manager_home" ]; then
  261. return 1
  262. fi
  263. return 0
  264. }
  265. function install_nodejs_deps() {
  266. local -a pkg_managers=("pnpm" "bun" "yarn" "npm")
  267. for pkg_manager in "${pkg_managers[@]}"; do
  268. if __validate_node_installation "$pkg_manager"; then
  269. eval "__install_nodejs_deps_$pkg_manager"
  270. return
  271. fi
  272. done
  273. echo "[WARN]: skipping installing optional nodejs dependencies due to insufficient permissions."
  274. echo "check how to solve it: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally"
  275. }
  276. function __attempt_to_install_with_cargo() {
  277. if command -v cargo &>/dev/null; then
  278. echo "Installing missing Rust dependency with cargo"
  279. cargo install "$1"
  280. else
  281. echo "[WARN]: Unable to find cargo. Make sure to install it to avoid any problems"
  282. exit 1
  283. fi
  284. }
  285. # we try to install the missing one with cargo even though it's unlikely to be found
  286. function install_rust_deps() {
  287. for dep in "${__rust_deps[@]}"; do
  288. if ! command -v "${dep%%::*}" &>/dev/null; then
  289. __attempt_to_install_with_cargo "${dep##*::}"
  290. fi
  291. done
  292. echo "All Rust dependencies are successfully installed"
  293. }
  294. function __backup_dir() {
  295. local src="$1"
  296. if [ ! -d "$src" ]; then
  297. return
  298. fi
  299. mkdir -p "$src.old"
  300. msg "Backing up old $src to $src.old"
  301. if command -v rsync &>/dev/null; then
  302. rsync --archive --quiet --backup --partial --copy-links --cvs-exclude "$src"/ "$src.old"
  303. else
  304. case "$OS" in
  305. Darwin)
  306. cp -R "$src/." "$src.old/."
  307. ;;
  308. *)
  309. cp -r "$src/." "$src.old/."
  310. ;;
  311. esac
  312. fi
  313. }
  314. function verify_lvim_dirs() {
  315. for dir in "${__lvim_dirs[@]}"; do
  316. if [ -d "$dir" ]; then
  317. if [ "$ARGS_OVERWRITE" -eq 0 ]; then
  318. __backup_dir "$dir"
  319. fi
  320. rm -rf "$dir"
  321. fi
  322. mkdir -p "$dir"
  323. done
  324. mkdir -p "$LUNARVIM_CONFIG_DIR"
  325. }
  326. function clone_lvim() {
  327. msg "Cloning LunarVim configuration"
  328. if ! git clone --progress --depth 1 --branch "$LV_BRANCH" \
  329. "https://github.com/${LV_REMOTE}" "$LUNARVIM_BASE_DIR"; then
  330. echo "Failed to clone repository. Installation failed."
  331. exit 1
  332. fi
  333. }
  334. function link_local_lvim() {
  335. echo "Linking local LunarVim repo"
  336. # Detect whether it's a symlink or a folder
  337. if [ -d "$LUNARVIM_BASE_DIR" ]; then
  338. msg "Moving old files to ${LUNARVIM_BASE_DIR}.old"
  339. mv "$LUNARVIM_BASE_DIR" "${LUNARVIM_BASE_DIR}".old
  340. fi
  341. echo " - $BASEDIR -> $LUNARVIM_BASE_DIR"
  342. ln -s -f "$BASEDIR" "$LUNARVIM_BASE_DIR"
  343. }
  344. function setup_shim() {
  345. make -C "$LUNARVIM_BASE_DIR" install-bin
  346. }
  347. function remove_old_cache_files() {
  348. local lazy_cache="$LUNARVIM_CACHE_DIR/lazy/cache"
  349. if [ -e "$lazy_cache" ]; then
  350. msg "Removing old lazy cache file"
  351. rm -f "$lazy_cache"
  352. fi
  353. }
  354. function setup_lvim() {
  355. msg "Installing LunarVim shim"
  356. setup_shim
  357. create_desktop_file
  358. [ ! -f "$LUNARVIM_CONFIG_DIR/config.lua" ] \
  359. && cp "$LUNARVIM_BASE_DIR/utils/installer/config.example.lua" "$LUNARVIM_CONFIG_DIR/config.lua"
  360. echo "Preparing Lazy setup"
  361. "$INSTALL_PREFIX/bin/$NVIM_APPNAME" --headless -c 'quitall'
  362. printf "\nLazy setup complete\n"
  363. verify_core_plugins
  364. }
  365. function create_desktop_file() {
  366. # TODO: Any other OSes that use desktop files?
  367. ([ "$OS" != "Linux" ] || ! command -v xdg-desktop-menu &>/dev/null) && return
  368. echo "Creating desktop file"
  369. for d in "$LUNARVIM_BASE_DIR"/utils/desktop/*/; do
  370. size_folder=$(basename "$d")
  371. mkdir -p "$XDG_DATA_HOME/icons/hicolor/$size_folder/apps/"
  372. cp "$LUNARVIM_BASE_DIR/utils/desktop/$size_folder/lvim.svg" "$XDG_DATA_HOME/icons/hicolor/$size_folder/apps"
  373. done
  374. xdg-desktop-menu install --novendor "$LUNARVIM_BASE_DIR/utils/desktop/lvim.desktop" || true
  375. }
  376. function print_logo() {
  377. cat <<'EOF'
  378. 88\ 88\
  379. 88 | \__|
  380. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  381. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  382. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  383. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  384. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  385. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  386. EOF
  387. }
  388. main "$@"