install.ps1 8.6 KB

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