install.ps1 8.4 KB

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