install.ps1 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #Requires -Version 7.1
  2. $ErrorActionPreference = "Stop" # exit when command fails
  3. # set script variables
  4. $LV_BRANCH = $LV_BRANCH ?? "rolling"
  5. $LV_REMOTE = $LV_REMOTE ?? "lunarvim/lunarvim.git"
  6. $INSTALL_PREFIX = $INSTALL_PREFIX ?? "$HOME\.local"
  7. $env:XDG_DATA_HOME = $env:XDG_DATA_HOME ?? $env:APPDATA
  8. $env:XDG_CONFIG_HOME = $env:XDG_CONFIG_HOME ?? $env:LOCALAPPDATA
  9. $env:XDG_CACHE_HOME = $env:XDG_CACHE_HOME ?? $env:TEMP
  10. $env:LUNARVIM_RUNTIME_DIR = $env:LUNARVIM_RUNTIME_DIR ?? "$env:XDG_DATA_HOME\lunarvim"
  11. $env:LUNARVIM_CONFIG_DIR = $env:LUNARVIM_CONFIG_DIR ?? "$env:XDG_CONFIG_HOME\lvim"
  12. $env:LUNARVIM_CACHE_DIR = $env:LUNARVIM_CACHE_DIR ?? "$env:XDG_CACHE_HOME\lvim"
  13. $env:LUNARVIM_BASE_DIR = $env:LUNARVIM_BASE_DIR ?? "$env:LUNARVIM_RUNTIME_DIR\lvim"
  14. $__lvim_dirs = (
  15. $env:LUNARVIM_BASE_DIR,
  16. $env:LUNARVIM_RUNTIME_DIR,
  17. $env:LUNARVIM_CONFIG_DIR,
  18. $env:LUNARVIM_CACHE_DIR
  19. )
  20. function __add_separator($div_width) {
  21. "-" * $div_width
  22. Write-Output ""
  23. }
  24. function msg($text){
  25. Write-Output $text
  26. __add_separator "80"
  27. }
  28. function main($cliargs) {
  29. print_logo
  30. verify_lvim_dirs
  31. if ($cliargs.Contains("--overwrite")) {
  32. Write-Output "!!Warning!! -> Removing all lunarvim related config because of the --overwrite flag"
  33. $answer = Read-Host "Would you like to continue? [y]es or [n]o "
  34. if ("$answer" -ne "y" -and "$answer" -ne "Y") {
  35. exit 1
  36. }
  37. uninstall_lvim
  38. }
  39. if ($cliargs.Contains("--local") -or $cliargs.Contains("--testing")) {
  40. msg "Using local LunarVim installation"
  41. local_install
  42. exit
  43. }
  44. msg "Checking dependencies.."
  45. check_system_deps
  46. $answer = Read-Host "Would you like to check lunarvim's NodeJS dependencies? [y]es or [n]o (default: no) "
  47. if ("$answer" -eq "y" -or "$answer" -eq "Y") {
  48. install_nodejs_deps
  49. }
  50. $answer = Read-Host "Would you like to check lunarvim's Python dependencies? [y]es or [n]o (default: no) "
  51. if ("$answer" -eq "y" -or "$answer" -eq "Y") {
  52. install_python_deps
  53. }
  54. if (Test-Path "$env:LUNARVIM_BASE_DIR\init.lua" ) {
  55. msg "Updating LunarVim"
  56. validate_lunarvim_files
  57. }
  58. else {
  59. msg "Cloning Lunarvim"
  60. clone_lvim
  61. setup_lvim
  62. }
  63. }
  64. function print_missing_dep_msg($dep) {
  65. Write-Output "[ERROR]: Unable to find dependency [$dep]"
  66. Write-Output "Please install it first and re-run the installer."
  67. }
  68. $winget_package_matrix=@{"git" = "Git.Git"; "nvim" = "nvim.nvim"; "make" = "GnuWin32.Make"; "node" = "OpenJS.NodeJS"; "pip" = "Python.Python.3"}
  69. $scoop_package_matrix=@{"git" = "git"; "nvim" = "neovim-nightly"; "make" = "make"; "node" = "nodejs"; "pip" = "python3"}
  70. function install_system_package($dep) {
  71. if (Get-Command -Name "winget" -ErrorAction SilentlyContinue) {
  72. Write-Output "Attempting to install dependency [$dep] with winget"
  73. $install_cmd = "winget install --interactive $winget_package_matrix[$dep]"
  74. }
  75. elseif (Get-Command -Name "scoop" -ErrorAction SilentlyContinue) {
  76. Write-Output "Attempting to install dependency [$dep] with scoop"
  77. # TODO: check if it's fine to not run it with --global
  78. $install_cmd = "scoop install $scoop_package_matrix[$dep]"
  79. }
  80. else {
  81. print_missing_dep_msg "$dep"
  82. exit 1
  83. }
  84. try {
  85. Invoke-Command $install_cmd -ErrorAction Stop
  86. }
  87. catch {
  88. print_missing_dep_msg "$dep"
  89. exit 1
  90. }
  91. }
  92. function check_system_dep($dep) {
  93. try {
  94. Get-Command -Name $dep -ErrorAction Stop | Out-Null
  95. }
  96. catch {
  97. install_system_package "$dep"
  98. }
  99. }
  100. function check_system_deps() {
  101. check_system_dep "git"
  102. check_system_dep "nvim"
  103. check_system_dep "make"
  104. }
  105. function install_nodejs_deps() {
  106. $dep = "node"
  107. try {
  108. check_system_dep "$dep"
  109. Invoke-Command -ScriptBlock { npm install --global neovim tree-sitter-cli } -ErrorAction Break
  110. }
  111. catch {
  112. print_missing_dep_msg "$dep"
  113. }
  114. }
  115. function install_python_deps() {
  116. $dep = "pip"
  117. try {
  118. check_system_dep "$dep"
  119. Invoke-Command -ScriptBlock { python -m pip install --user pynvim } -ErrorAction Break
  120. }
  121. catch {
  122. print_missing_dep_msg "$dep"
  123. }
  124. }
  125. function backup_old_config() {
  126. $src = "$env:LUNARVIM_CONFIG_DIR"
  127. if (Test-Path $src) {
  128. New-Item "$src.old" -ItemType Directory -Force | Out-Null
  129. Copy-Item -Force -Recurse "$src\*" "$src.old\." | Out-Null
  130. }
  131. msg "Backup operation complete"
  132. }
  133. function local_install() {
  134. verify_lvim_dirs
  135. $repoDir = git rev-parse --show-toplevel
  136. $gitLocalCloneCmd = git clone --progress "$repoDir" "$env:LUNARVIM_BASE_DIR"
  137. Invoke-Command -ErrorAction Stop -ScriptBlock { $gitLocalCloneCmd; setup_lvim }
  138. }
  139. function clone_lvim() {
  140. try {
  141. $gitCloneCmd = git clone --progress --depth 1 --branch "$LV_BRANCH" `
  142. "https://github.com/$LV_REMOTE" `
  143. "$env:LUNARVIM_BASE_DIR"
  144. Invoke-Command -ErrorAction Stop -ScriptBlock { $gitCloneCmd }
  145. }
  146. catch {
  147. msg "Failed to clone repository. Installation failed."
  148. exit 1
  149. }
  150. }
  151. function setup_shim() {
  152. if ((Test-Path "$INSTALL_PREFIX\bin") -eq $false) {
  153. New-Item "$INSTALL_PREFIX\bin" -ItemType Directory | Out-Null
  154. }
  155. Copy-Item -Force "$env:LUNARVIM_BASE_DIR\utils\bin\lvim.ps1" "$INSTALL_PREFIX\bin\lvim.ps1"
  156. }
  157. function uninstall_lvim() {
  158. foreach ($dir in $__lvim_dirs) {
  159. if (Test-Path "$dir") {
  160. Remove-Item -Force -Recurse "$dir"
  161. }
  162. }
  163. }
  164. function verify_lvim_dirs() {
  165. foreach ($dir in $__lvim_dirs) {
  166. if ((Test-Path "$dir") -eq $false) {
  167. New-Item "$dir" -ItemType Directory | Out-Null
  168. }
  169. }
  170. backup_old_config
  171. }
  172. function setup_lvim() {
  173. msg "Installing LunarVim shim"
  174. setup_shim
  175. msg "Installing sample configuration"
  176. if (Test-Path "$env:LUNARVIM_CONFIG_DIR\config.lua") {
  177. Move-Item "$env:LUNARVIM_CONFIG_DIR\config.lua" "$env:LUNARVIM_CONFIG_DIR\config.lua.old"
  178. }
  179. New-Item -ItemType File -Path "$env:LUNARVIM_CONFIG_DIR\config.lua" | Out-Null
  180. $exampleConfig = "$env:LUNARVIM_BASE_DIR\utils\installer\config_win.example.lua"
  181. Copy-Item -Force "$exampleConfig" "$env:LUNARVIM_CONFIG_DIR\config.lua"
  182. # FIXME: this has never worked
  183. # Invoke-Expression "$INSTALL_PREFIX\bin\lvim.ps1 --headless -c 'autocmd User PackerComplete quitall' -c 'PackerSync'"
  184. Write-Host "Make sure to run `:PackerSync` at first launch" -ForegroundColor Green
  185. create_alias
  186. msg "Thank you for installing LunarVim!!"
  187. Write-Output "You can start it by running: $INSTALL_PREFIX\bin\lvim.ps1"
  188. Write-Output "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  189. }
  190. function validate_lunarvim_files() {
  191. Set-Alias lvim "$INSTALL_PREFIX\bin\lvim.ps1"
  192. try {
  193. $verify_version_cmd='if v:errmsg != "" | cquit | else | quit | endif'
  194. Invoke-Command -ScriptBlock { lvim --headless -c 'LvimUpdate' -c "$verify_version_cmd" } -ErrorAction SilentlyContinue
  195. }
  196. catch {
  197. Write-Output "Unable to guarantee data integrity while updating. Please run `:LvimUpdate` manually instead."
  198. exit 1
  199. }
  200. Write-Output "Your LunarVim installation is now up to date!"
  201. }
  202. function create_alias {
  203. try {
  204. $answer = Read-Host $(`
  205. "Would you like to create an alias inside your Powershell profile?`n" + `
  206. "(This enables you to start lvim with the command 'lvim') [y]es or [n]o (default: no)" )
  207. }
  208. catch {
  209. msg "Non-interactive mode detected. Skipping alias creation"
  210. return
  211. }
  212. if ("$answer" -ne "y" -or "$answer" -ne "Y") {
  213. return
  214. }
  215. $lvim_bin="$INSTALL_PREFIX\bin\lvim.ps1"
  216. $lvim_alias = Get-Alias lvim -ErrorAction SilentlyContinue
  217. if ($lvim_alias.Definition == $lvim_bin) {
  218. Write-Output "Alias is already set and will not be reset."
  219. return
  220. }
  221. Add-Content -Path $PROFILE -Value $("Set-Alias lvim $lvim_bin")
  222. Write-Host 'To use the new alias in this window reload your profile with: `. $PROFILE`' -ForegroundColor Green
  223. }
  224. function print_logo(){
  225. Write-Output "
  226. 88\ 88\
  227. 88 | \__|
  228. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  229. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  230. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  231. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  232. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  233. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  234. "
  235. }
  236. main "$args"