install.ps1 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #Requires -Version 7.1
  2. $ErrorActionPreference = "Stop" # exit when command fails
  3. # set script variables
  4. $LV_BRANCH = ($LV_BRANCH, "rolling", 1 -ne $null)[0]
  5. $LV_REMOTE = ($LV_REMOTE, "lunarvim/lunarvim.git", 1 -ne $null)[0]
  6. $INSTALL_PREFIX = ($INSTALL_PREFIX, "$HOME\.local", 1 -ne $null)[0]
  7. $env:XDG_DATA_HOME = ($env:XDG_DATA_HOME, "$env:APPDATA", 1 -ne $null)[0]
  8. $env:XDG_CONFIG_HOME = ($env:XDG_CONFIG_HOME, "$env:LOCALAPPDATA", 1 -ne $null)[0]
  9. $env:XDG_CACHE_HOME = ($env:XDG_CACHE_HOME, "$env:TEMP", 1 -ne $null)[0]
  10. $env:LUNARVIM_RUNTIME_DIR = ($env:LUNARVIM_RUNTIME_DIR, "$env:XDG_DATA_HOME\lunarvim", 1 -ne $null)[0]
  11. $env:LUNARVIM_CONFIG_DIR = ($env:LUNARVIM_CONFIG_DIR, "$env:XDG_CONFIG_HOME\lvim", 1 -ne $null)[0]
  12. $env:LUNARVIM_CACHE_DIR = ($env:LUNARVIM_CACHE_DIR, "$env:XDG_CACHE_HOME\lvim", 1 -ne $null)[0]
  13. $__lvim_dirs = (
  14. "$env:LUNARVIM_CONFIG_DIR",
  15. "$env:LUNARVIM_RUNTIME_DIR",
  16. "$env:LUNARVIM_CACHE_DIR"
  17. )
  18. function main($cliargs) {
  19. Write-Output "
  20. 88\ 88\
  21. 88 | \__|
  22. 88 |88\ 88\ 888888$\ 888888\ 888888\ 88\ 88\ 88\ 888888\8888\
  23. 88 |88 | 88 |88 __88\ \____88\ 88 __88\\88\ 88 |88 |88 _88 _88\
  24. 88 |88 | 88 |88 | 88 | 888888$ |88 | \__|\88\88 / 88 |88 / 88 / 88 |
  25. 88 |88 | 88 |88 | 88 |88 __88 |88 | \88$ / 88 |88 | 88 | 88 |
  26. 88 |\888888 |88 | 88 |\888888$ |88 | \$ / 88 |88 | 88 | 88 |
  27. \__| \______/ \__| \__| \_______|\__| \_/ \__|\__| \__| \__|
  28. "
  29. __add_separator "80"
  30. # skip this in a Github workflow
  31. if ( $null -eq "$GITHUB_ACTIONS" ) {
  32. install_packer
  33. setup_shim
  34. exit
  35. }
  36. __add_separator "80"
  37. check_system_deps
  38. Write-Output "Would you like to check lunarvim's NodeJS dependencies?"
  39. $answer = Read-Host "[y]es or [n]o (default: no) "
  40. if ("$answer" -eq "y" -or "$answer" -eq "Y") {
  41. install_nodejs_deps
  42. }
  43. Write-Output "Would you like to check lunarvim's Python dependencies?"
  44. $answer = Read-Host "[y]es or [n]o (default: no) "
  45. if ("$answer" -eq "y" -or "$answer" -eq "Y") {
  46. install_python_deps
  47. }
  48. __add_separator "80"
  49. Write-Output "Backing up old LunarVim configuration"
  50. backup_old_config
  51. __add_separator "80"
  52. verify_lvim_dirs
  53. if (Test-Path "$env:LUNARVIM_RUNTIME_DIR\site\pack\packer\start\packer.nvim") {
  54. Write-Output "Packer already installed"
  55. }
  56. else {
  57. install_packer
  58. }
  59. __add_separator "80"
  60. if (Test-Path "$env:LUNARVIM_RUNTIME_DIR\lvim\init.lua" ) {
  61. Write-Output "Updating LunarVim"
  62. update_lvim
  63. }
  64. else {
  65. if ($cliargs.Contains("--testing")) {
  66. copy_local_lvim_repository
  67. }
  68. else {
  69. clone_lvim
  70. }
  71. setup_lvim
  72. }
  73. __add_separator "80"
  74. }
  75. function print_missing_dep_msg($dep) {
  76. Write-Output "[ERROR]: Unable to find dependency [$dep]"
  77. Write-Output "Please install it first and re-run the installer. Try: $RECOMMEND_INSTALL $dep"
  78. }
  79. function install_system_package($dep) {
  80. if (Get-Command -Name "winget" -ErrorAction SilentlyContinue) {
  81. Write-Output "[INFO]: Attempting to install dependency [$dep] with winget"
  82. $install_cmd = "winget install --interactive"
  83. }
  84. elseif (Get-Command -Name "scoop" -ErrorAction SilentlyContinue) {
  85. Write-Output "[INFO]: Attempting to install dependency [$dep] with scoop"
  86. # TODO: check if it's fine to not run it with --global
  87. $install_cmd = "scoop install"
  88. }
  89. else {
  90. print_missing_dep_msg "$dep"
  91. exit 1
  92. }
  93. try {
  94. Invoke-Command $install_cmd $dep -ErrorAction Stop
  95. }
  96. catch {
  97. print_missing_dep_msg "$dep"
  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. Write-Output "[INFO]: Checking dependencies.."
  111. check_system_dep "git"
  112. check_system_dep "nvim"
  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. foreach ($dir in $__lvim_dirs) {
  136. # we create an empty folder for subsequent commands \
  137. # that require an existing directory
  138. if ( Test-Path "$dir") {
  139. New-Item "$dir.bak" -ItemType Directory -Force
  140. Copy-Item -Recurse "$dir\*" "$dir.bak\."
  141. }
  142. }
  143. Write-Output "Backup operation complete"
  144. }
  145. function install_packer() {
  146. Invoke-Command -ErrorAction Stop -ScriptBlock { git clone --progress --depth 1 "https://github.com/wbthomason/packer.nvim" "$env:LUNARVIM_RUNTIME_DIR\site\pack\packer\start\packer.nvim" }
  147. }
  148. function copy_local_lvim_repository() {
  149. Write-Output "Copy local LunarVim configuration"
  150. Copy-Item -Path "$((Get-Item $PWD).Parent.Parent.FullName)" -Destination "$env:LUNARVIM_RUNTIME_DIR/lvim" -Recurse
  151. }
  152. function clone_lvim() {
  153. Write-Output "Cloning LunarVim configuration"
  154. try {
  155. Invoke-Command -ErrorAction Stop -ScriptBlock { git clone --progress --branch "$LV_BRANCH" --depth 1 "https://github.com/$LV_REMOTE" "$env:LUNARVIM_RUNTIME_DIR/lvim" }
  156. }
  157. catch {
  158. Write-Output "Failed to clone repository. Installation failed."
  159. exit 1
  160. }
  161. }
  162. function setup_shim() {
  163. if ((Test-Path "$INSTALL_PREFIX\bin") -eq $false) {
  164. New-Item "$INSTALL_PREFIX\bin" -ItemType Directory
  165. }
  166. Copy-Item "$env:LUNARVIM_RUNTIME_DIR\lvim\utils\bin\lvim.ps1" -Destination "$INSTALL_PREFIX\bin\lvim.ps1" -Force
  167. }
  168. function verify_lvim_dirs() {
  169. if ($cliargs.Contains("--overwrite")) {
  170. Write-Output "!!Warning!! -> Removing all lunarvim related config because of the --overwrite flag"
  171. $answer = Read-Host "Would you like to continue? [y]es or [n]o "
  172. if ("$answer" -ne "y" -and "$answer" -ne "Y") {
  173. exit 1
  174. }
  175. foreach ($dir in $__lvim_dirs) {
  176. if (Test-Path "$dir") {
  177. Remove-Item -Force -Recurse "$dir"
  178. }
  179. }
  180. }
  181. foreach ($dir in $__lvim_dirs) {
  182. if ((Test-Path "$dir") -eq $false) {
  183. New-Item "$dir" -ItemType Directory
  184. }
  185. }
  186. }
  187. function setup_lvim() {
  188. Write-Output "Installing LunarVim shim"
  189. setup_shim
  190. Write-Output "Preparing Packer setup"
  191. if (Test-Path "$env:LUNARVIM_CONFIG_DIR\config.lua") {
  192. Remove-Item -Force "$env:LUNARVIM_CONFIG_DIR\config.lua"
  193. }
  194. Out-File -FilePath "$env:LUNARVIM_CONFIG_DIR\config.lua"
  195. Write-Output "Packer setup complete"
  196. __add_separator "80"
  197. Copy-Item "$env:LUNARVIM_RUNTIME_DIR\lvim\utils\installer\config.example.lua" "$env:LUNARVIM_CONFIG_DIR\config.lua"
  198. $answer = Read-Host $(`
  199. "Would you like to create an alias inside your Powershell profile?`n" + `
  200. "(This enables you to start lvim with the command 'lvim') [y]es or [n]o (default: no)" )
  201. if ("$answer" -eq "y" -and "$answer" -eq "Y") {
  202. create_alias
  203. }
  204. __add_separator "80"
  205. Write-Output "Thank you for installing LunarVim!!"
  206. Write-Output "You can start it by running: $INSTALL_PREFIX\bin\lvim.ps1"
  207. Write-Output "Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts]"
  208. }
  209. function update_lvim() {
  210. try {
  211. Invoke-Command git -C "$env:LUNARVIM_RUNTIME_DIR/lvim" status -uno
  212. }
  213. catch {
  214. git -C "$env:LUNARVIM_RUNTIME_DIR/lvim" pull --ff-only --progress -or
  215. Write-Output "Unable to guarantee data integrity while updating. Please do that manually instead."
  216. exit 1
  217. }
  218. Write-Output "Your LunarVim installation is now up to date!"
  219. }
  220. function __add_separator($div_width) {
  221. "-" * $div_width
  222. Write-Output ""
  223. }
  224. function create_alias {
  225. if ($null -eq $(Get-Alias | Select-String "lvim")) {
  226. Add-Content -Path $PROFILE -Value $( -join @('Set-Alias lvim "', "$INSTALL_PREFIX", '\bin\lvim.ps1"'))
  227. Write-Output ""
  228. Write-Host 'To use the new alias in this window reload your profile with ". $PROFILE".' -ForegroundColor Yellow
  229. }
  230. else {
  231. Write-Output "Alias is already set and will not be reset."
  232. }
  233. }
  234. main "$args"