install.ps1 9.5 KB

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