install.ps1 8.4 KB

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