install.sh 13 KB

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