install.sh 11 KB

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