2022-02-03 08:59:25 +01:00
# Requires -Version 7.1
2021-10-03 16:13:46 +02:00
$ErrorActionPreference = " Stop " # exit when command fails
2023-02-14 08:23:41 +01:00
if ( $PSVersionTable . PSVersion -lt 7.1 ) {
Write-Error " Powershell version needs to be greater than 7.1! "
}
2021-10-03 16:13:46 +02:00
# set script variables
2022-11-04 17:56:33 +01:00
$LV_BRANCH = $LV_BRANCH ? ? " master "
2022-03-10 09:54:38 +01:00
$LV_REMOTE = $LV_REMOTE ? ? " lunarvim/lunarvim.git "
$INSTALL_PREFIX = $INSTALL_PREFIX ? ? " $HOME \.local "
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
$env:XDG_DATA_HOME = $env:XDG_DATA_HOME ? ? $env:APPDATA
$env:XDG_CONFIG_HOME = $env:XDG_CONFIG_HOME ? ? $env:LOCALAPPDATA
$env:XDG_CACHE_HOME = $env:XDG_CACHE_HOME ? ? $env:TEMP
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
$env:LUNARVIM_RUNTIME_DIR = $env:LUNARVIM_RUNTIME_DIR ? ? " $env:XDG_DATA_HOME \lunarvim "
$env:LUNARVIM_CONFIG_DIR = $env:LUNARVIM_CONFIG_DIR ? ? " $env:XDG_CONFIG_HOME \lvim "
$env:LUNARVIM_CACHE_DIR = $env:LUNARVIM_CACHE_DIR ? ? " $env:XDG_CACHE_HOME \lvim "
$env:LUNARVIM_BASE_DIR = $env:LUNARVIM_BASE_DIR ? ? " $env:LUNARVIM_RUNTIME_DIR \lvim "
2021-10-03 16:13:46 +02:00
$__lvim_dirs = (
2022-03-10 09:54:38 +01:00
$env:LUNARVIM_BASE_DIR ,
$env:LUNARVIM_RUNTIME_DIR ,
$env:LUNARVIM_CONFIG_DIR ,
$env:LUNARVIM_CACHE_DIR
2021-10-03 16:13:46 +02:00
)
2022-03-10 09:54:38 +01:00
function __add_separator($div_width ) {
" - " * $div_width
Write-Output " "
}
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
function msg($text ) {
Write-Output $text
2021-10-03 16:13:46 +02:00
__add_separator " 80 "
2022-03-10 09:54:38 +01:00
}
function main($cliargs ) {
print_logo
verify_lvim_dirs
if ( $cliargs . Contains ( " --overwrite " ) ) {
Write-Output " !!Warning!! -> Removing all lunarvim related config because of the --overwrite flag "
$answer = Read-Host " Would you like to continue? [y]es or [n]o "
if ( " $answer " -ne " y " -and " $answer " -ne " Y " ) {
exit 1
}
uninstall_lvim
}
if ( $cliargs . Contains ( " --local " ) -or $cliargs . Contains ( " --testing " ) ) {
msg " Using local LunarVim installation "
local_install
2021-10-03 16:13:46 +02:00
exit
}
2022-03-10 09:54:38 +01:00
msg " Checking dependencies.. "
2021-10-03 16:13:46 +02:00
check_system_deps
2022-03-10 09:54:38 +01:00
$answer = Read-Host " Would you like to check lunarvim's NodeJS dependencies? [y]es or [n]o (default: no) "
2021-10-03 16:13:46 +02:00
if ( " $answer " -eq " y " -or " $answer " -eq " Y " ) {
install_nodejs_deps
2022-03-10 09:54:38 +01:00
}
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
$answer = Read-Host " Would you like to check lunarvim's Python dependencies? [y]es or [n]o (default: no) "
2021-10-03 16:13:46 +02:00
if ( " $answer " -eq " y " -or " $answer " -eq " Y " ) {
install_python_deps
2022-03-10 09:54:38 +01:00
}
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
if ( Test-Path " $env:LUNARVIM_BASE_DIR \init.lua " ) {
msg " Updating LunarVim "
validate_lunarvim_files
2021-10-03 16:13:46 +02:00
}
else {
2022-03-10 09:54:38 +01:00
msg " Cloning Lunarvim "
clone_lvim
2021-10-03 16:13:46 +02:00
setup_lvim
}
}
function print_missing_dep_msg($dep ) {
Write-Output " [ERROR]: Unable to find dependency [ $dep ] "
2022-03-10 09:54:38 +01:00
Write-Output " Please install it first and re-run the installer. "
2021-10-03 16:13:46 +02:00
}
2024-02-05 19:25:45 +01:00
$winget_package_matrix = @ { " git " = " Git.Git " ; " nvim " = " Neovim.Neovim " ; " make " = " GnuWin32.Make " ; " node " = " OpenJS.NodeJS " ; " pip " = " Python.Python.3.11 " }
$winget_additional_arguments_matrix = @ { " git " = " --source winget --interactive " ; " nvim " = " --interactive " ; " make " = " --interactive " ; " node " = " " ; " pip " = " " }
$scoop_package_matrix = @ { " git " = " git " ; " nvim " = " neovim " ; " make " = " make " ; " node " = " nodejs " ; " pip " = " python " }
2022-03-10 09:54:38 +01:00
2021-10-03 16:13:46 +02:00
function install_system_package($dep ) {
2024-02-05 19:25:45 +01:00
# Make installers sometimes have a problem when adding make to path
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! "
2021-10-03 16:13:46 +02:00
if ( Get-Command -Name " winget " -ErrorAction SilentlyContinue ) {
2022-03-10 09:54:38 +01:00
Write-Output " Attempting to install dependency [ $dep ] with winget "
2024-02-05 19:25:45 +01:00
$command = " winget "
$command_arguments = " -e --id $( $winget_package_matrix [ $dep ] ) $( $winget_additional_arguments_matrix [ $dep ] ) " . Trim ( ) -split ' '
2021-10-03 16:13:46 +02:00
}
elseif ( Get-Command -Name " scoop " -ErrorAction SilentlyContinue ) {
2022-03-10 09:54:38 +01:00
Write-Output " Attempting to install dependency [ $dep ] with scoop "
2021-10-03 16:13:46 +02:00
# TODO: check if it's fine to not run it with --global
2024-02-05 19:25:45 +01:00
$command = " scoop "
$command_arguments = " $( $scoop_package_matrix [ $dep ] ) " . Trim ( ) -split ' '
2021-10-03 16:13:46 +02:00
}
else {
print_missing_dep_msg " $dep "
exit 1
}
try {
2024-02-05 19:25:45 +01:00
& $command install $command_arguments
# Refresh the path after installation
$env:Path = [ System.Environment ] :: GetEnvironmentVariable ( " Path " , " Machine " ) + " ; " + [ System.Environment ] :: GetEnvironmentVariable ( " Path " , " User " )
} catch {
Write-Output " An error occurred: $_ "
exit 1
2021-10-03 16:13:46 +02:00
}
}
function check_system_dep($dep ) {
2022-03-10 09:54:38 +01:00
try {
Get-Command -Name $dep -ErrorAction Stop | Out-Null
2021-10-03 16:13:46 +02:00
}
2022-03-10 09:54:38 +01:00
catch {
2021-10-03 16:13:46 +02:00
install_system_package " $dep "
}
}
function check_system_deps ( ) {
check_system_dep " git "
check_system_dep " nvim "
2022-03-10 09:54:38 +01:00
check_system_dep " make "
2021-10-03 16:13:46 +02:00
}
function install_nodejs_deps ( ) {
2022-02-03 08:59:25 +01:00
$dep = " node "
2021-10-03 16:13:46 +02:00
try {
2022-02-03 08:59:25 +01:00
check_system_dep " $dep "
Invoke-Command -ScriptBlock { npm install - -global neovim tree-sitter -cli } -ErrorAction Break
2021-10-03 16:13:46 +02:00
}
catch {
print_missing_dep_msg " $dep "
}
}
function install_python_deps ( ) {
2022-02-03 08:59:25 +01:00
$dep = " pip "
2021-10-03 16:13:46 +02:00
try {
2022-02-03 08:59:25 +01:00
check_system_dep " $dep "
Invoke-Command -ScriptBlock { python -m pip install - -user pynvim } -ErrorAction Break
2021-10-03 16:13:46 +02:00
}
catch {
print_missing_dep_msg " $dep "
}
}
function backup_old_config ( ) {
2022-03-10 09:54:38 +01:00
$src = " $env:LUNARVIM_CONFIG_DIR "
if ( Test-Path $src ) {
New-Item " $src .old " -ItemType Directory -Force | Out-Null
Copy-Item -Force -Recurse " $src \* " " $src .old\. " | Out-Null
2021-10-03 16:13:46 +02:00
}
2022-03-10 09:54:38 +01:00
msg " Backup operation complete "
2021-10-03 16:13:46 +02:00
}
2022-03-10 09:54:38 +01:00
function local_install ( ) {
verify_lvim_dirs
$repoDir = git rev-parse - -show -toplevel
$gitLocalCloneCmd = git clone - -progress " $repoDir " " $env:LUNARVIM_BASE_DIR "
Invoke-Command -ErrorAction Stop -ScriptBlock { $gitLocalCloneCmd ; setup_lvim }
2021-10-03 16:13:46 +02:00
}
function clone_lvim ( ) {
try {
2022-03-10 09:54:38 +01:00
$gitCloneCmd = git clone - -progress - -depth 1 - -branch " $LV_BRANCH " `
" https://github.com/ $LV_REMOTE " `
" $env:LUNARVIM_BASE_DIR "
Invoke-Command -ErrorAction Stop -ScriptBlock { $gitCloneCmd }
2021-10-03 16:13:46 +02:00
}
catch {
2022-03-10 09:54:38 +01:00
msg " Failed to clone repository. Installation failed. "
2021-10-03 16:13:46 +02:00
exit 1
}
}
function setup_shim ( ) {
if ( ( Test-Path " $INSTALL_PREFIX \bin " ) -eq $false ) {
2022-03-10 09:54:38 +01:00
New-Item " $INSTALL_PREFIX \bin " -ItemType Directory | Out-Null
2021-10-03 16:13:46 +02:00
}
2022-03-10 09:54:38 +01:00
Copy-Item -Force " $env:LUNARVIM_BASE_DIR \utils\bin\lvim.ps1 " " $INSTALL_PREFIX \bin\lvim.ps1 "
}
2021-12-09 11:51:37 +01:00
2022-03-10 09:54:38 +01:00
function uninstall_lvim ( ) {
foreach ( $dir in $__lvim_dirs ) {
if ( Test-Path " $dir " ) {
Remove-Item -Force -Recurse " $dir "
2021-12-09 11:51:37 +01:00
}
}
2022-03-10 09:54:38 +01:00
}
2021-12-09 11:51:37 +01:00
2022-03-10 09:54:38 +01:00
function verify_lvim_dirs ( ) {
2021-12-09 11:51:37 +01:00
foreach ( $dir in $__lvim_dirs ) {
if ( ( Test-Path " $dir " ) -eq $false ) {
2022-03-10 09:54:38 +01:00
New-Item " $dir " -ItemType Directory | Out-Null
2021-12-09 11:51:37 +01:00
}
}
2022-03-10 09:54:38 +01:00
backup_old_config
2021-12-09 11:51:37 +01:00
}
2022-03-10 09:54:38 +01:00
2021-10-03 16:13:46 +02:00
function setup_lvim ( ) {
2022-03-10 09:54:38 +01:00
msg " Installing LunarVim shim "
2021-10-03 16:13:46 +02:00
setup_shim
2022-03-10 09:54:38 +01:00
msg " Installing sample configuration "
2021-10-03 16:13:46 +02:00
2021-10-24 12:56:56 +02:00
if ( Test-Path " $env:LUNARVIM_CONFIG_DIR \config.lua " ) {
2022-03-10 09:54:38 +01:00
Move-Item " $env:LUNARVIM_CONFIG_DIR \config.lua " " $env:LUNARVIM_CONFIG_DIR \config.lua.old "
2021-10-24 12:56:56 +02:00
}
2022-03-10 09:54:38 +01:00
New-Item -ItemType File -Path " $env:LUNARVIM_CONFIG_DIR \config.lua " | Out-Null
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
$exampleConfig = " $env:LUNARVIM_BASE_DIR \utils\installer\config_win.example.lua "
Copy-Item -Force " $exampleConfig " " $env:LUNARVIM_CONFIG_DIR \config.lua "
2021-10-03 16:13:46 +02:00
2023-01-10 21:18:17 +01:00
Write-Host " Make sure to run ` :Lazy sync ` at first launch " -ForegroundColor Green
2022-03-10 09:54:38 +01:00
create_alias
msg " Thank you for installing LunarVim!! "
2021-10-03 16:13:46 +02:00
Write-Output " You can start it by running: $INSTALL_PREFIX \bin\lvim.ps1 "
Write-Output " Do not forget to use a font with glyphs (icons) support [https://github.com/ryanoasis/nerd-fonts] "
}
2022-03-10 09:54:38 +01:00
function validate_lunarvim_files ( ) {
Set-Alias lvim " $INSTALL_PREFIX \bin\lvim.ps1 "
2021-10-03 16:13:46 +02:00
try {
2023-04-07 14:09:02 +02:00
$verify_version_cmd = " if !empty(v:errmsg) | cquit | else | quit | endif "
2022-03-10 09:54:38 +01:00
Invoke-Command -ScriptBlock { lvim - -headless -c 'LvimUpdate' -c " $verify_version_cmd " } -ErrorAction SilentlyContinue
2021-10-03 16:13:46 +02:00
}
catch {
2022-03-10 09:54:38 +01:00
Write-Output " Unable to guarantee data integrity while updating. Please run ` :LvimUpdate ` manually instead. "
2021-10-03 16:13:46 +02:00
exit 1
}
Write-Output " Your LunarVim installation is now up to date! "
}
function create_alias {
2022-03-10 09:54:38 +01:00
try {
$answer = Read-Host $ ( `
" Would you like to create an alias inside your Powershell profile? `n " + `
" (This enables you to start lvim with the command 'lvim') [y]es or [n]o (default: no) " )
}
catch {
msg " Non-interactive mode detected. Skipping alias creation "
return
}
2021-10-03 16:13:46 +02:00
2022-03-10 09:54:38 +01:00
if ( " $answer " -ne " y " -or " $answer " -ne " Y " ) {
return
2021-12-09 11:51:37 +01:00
}
2022-03-10 09:54:38 +01:00
$lvim_bin = " $INSTALL_PREFIX \bin\lvim.ps1 "
$lvim_alias = Get-Alias lvim -ErrorAction SilentlyContinue
2022-03-20 11:56:28 +01:00
if ( $lvim_alias . Definition -eq $lvim_bin ) {
2021-12-09 11:51:37 +01:00
Write-Output " Alias is already set and will not be reset. "
2022-03-10 09:54:38 +01:00
return
2021-12-09 11:51:37 +01:00
}
2022-03-10 09:54:38 +01:00
2022-08-09 13:54:25 +02:00
try {
Get-Content $PROFILE -ErrorAction Stop
}
catch {
New-Item -Path $PROFILE -ItemType " file " -Force
}
2022-11-05 10:50:23 +01:00
Add-Content -Path $PROFILE -Value $ ( " `r `n Set-Alias lvim ' $lvim_bin ' " )
2022-03-10 09:54:38 +01:00
Write-Host 'To use the new alias in this window reload your profile with: `. $PROFILE`' -ForegroundColor Green
}
function print_logo ( ) {
Write-Output "
88 \ 88 \
88 | \ __ |
88 | 88 \ 88 \ 888888 $ \ 888888 \ 888888 \ 88 \ 88 \ 88 \ 888888 \ 8888 \
88 | 88 | 88 | 88 __88 \ \ ____88 \ 88 __88 \ \ 88 \ 88 | 88 | 88 _88 _88 \
88 | 88 | 88 | 88 | 88 | 888888 $ | 88 | \ __ | \ 88 \ 88 / 88 | 88 / 88 / 88 |
88 | 88 | 88 | 88 | 88 | 88 __88 | 88 | \ 88 $ / 88 | 88 | 88 | 88 |
88 | \ 888888 | 88 | 88 | \ 888888 $ | 88 | \ $ / 88 | 88 | 88 | 88 |
\ __ | \ ______ / \ __ | \ __ | \ _______ | \ __ | \ _ / \ __ | \ __ | \ __ | \ __ |
"
2021-10-03 16:13:46 +02:00
}
main " $args "