fix(windows): only show PATH update message once

This commit is contained in:
AJ ONeal
2023-11-11 21:54:29 -07:00
parent fe363814a1
commit 1f634be3bd
2 changed files with 39 additions and 37 deletions

View File

@@ -1,8 +1,6 @@
#!/usr/bin/env pwsh
#350 check if windows user run as admin
$OriginalPath = $Env:Path
# this allows us to call ps1 files, which allows us to have spaces in filenames
# ('powershell "$Env:USERPROFILE\test.ps1" foo' will fail if it has a space in
# the path but '& "$Env:USERPROFILE\test.ps1" foo' will work even with a space)
@@ -115,7 +113,7 @@ function webi_path_add($pathname) {
$my_pathname = $my_pathname -ireplace $my_home_re, "%USERPROFILE%"
$all_user_paths = [Environment]::GetEnvironmentVariable("Path", "User")
$user_paths = $all_user_paths -Split (';')
$user_paths = "${all_user_paths}".Trim(';').Split(';')
$exists_in_path = $false
foreach ($user_path in $user_paths) {
# C:\Users\me\bin => %USERPROFILE%/bin
@@ -127,42 +125,18 @@ function webi_path_add($pathname) {
}
}
if (-Not $exists_in_path) {
$all_user_paths = $pathname + ";" + $all_user_paths
$all_user_paths = "${pathname};${all_user_paths}".Trim(';')
[Environment]::SetEnvironmentVariable("Path", $all_user_paths, "User")
$null = Sync-EnvPath
}
}
function webi_path_add_followup($pathname) {
$UpdateUserPath = "`$UserPath = [Environment]::GetEnvironmentVariable('Path', 'User')"
$UpdateMachinePath = "`$MachinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')"
Write-Host ''
Write-Host '***********************************' -ForegroundColor yellow -BackgroundColor black
Write-Host '* IMPORTANT -- READ ME *' -ForegroundColor yellow -BackgroundColor black
Write-Host '* (run the PATH commands below) *' -ForegroundColor yellow -BackgroundColor black
Write-Host '***********************************' -ForegroundColor yellow -BackgroundColor black
Write-Host ''
Write-Host ""
Write-Host "Copy, paste, and run the appropriate commands to update your PATH:"
Write-Host ""
Write-Host "cmd.exe:"
Write-Host " (close and reopen the terminal)" -ForegroundColor yellow -BackgroundColor black
Write-Host ""
Write-Host "PowerShell:"
Write-Host " $UpdateUserPath" -ForegroundColor yellow -BackgroundColor black
Write-Host " $UpdateMachinePath" -ForegroundColor yellow -BackgroundColor black
Write-Host " `$Env:Path = `"`${UserPath};`${MachinePath}`"" -ForegroundColor yellow -BackgroundColor black
Write-Host " (or close and reopen the terminal, or reboot)"
Write-Host ""
}
function Sync-EnvPath {
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
$MachinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$null = [Environment]::SetEnvironmentVariable("Path", "${UserPath};${MachinePath}")
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User").Trim(';')
$MachinePath = [Environment]::GetEnvironmentVariable("Path", "Machine").Trim(';')
$Env:Path = "${UserPath};${MachinePath}"
"${UserPath};${MachinePath}"
[Environment]::SetEnvironmentVariable("Path", $Env:Path)
$Env:Path
}
$Env:WEBI_UA = Get-UserAgent
@@ -177,10 +151,5 @@ webi_path_add ~/.local/bin
webi_path_add ~/.local/bin
$CurrentPath = Sync-EnvPath
IF ($OriginalPath -ne $CurrentPath) {
webi_path_add_followup
}
# Done
Pop-Location

View File

@@ -101,6 +101,30 @@ function Get-UserAgent {
"PowerShell+curl Windows/10+ $my_arch msvc"
}
function Show-HowToUpdateEnv {
$UpdateUserPath = "`$UserPath = [Environment]::GetEnvironmentVariable('Path', 'User')"
$UpdateMachinePath = "`$MachinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine')"
Write-Host ''
Write-Host '***********************************' -ForegroundColor yellow -BackgroundColor black
Write-Host '* IMPORTANT -- READ ME *' -ForegroundColor yellow -BackgroundColor black
Write-Host '* (run the PATH commands below) *' -ForegroundColor yellow -BackgroundColor black
Write-Host '***********************************' -ForegroundColor yellow -BackgroundColor black
Write-Host ''
Write-Host ""
Write-Host "Copy, paste, and run the appropriate commands to update your PATH:"
Write-Host ""
Write-Host "cmd.exe:"
Write-Host " (close and reopen the terminal)" -ForegroundColor yellow -BackgroundColor black
Write-Host ""
Write-Host "PowerShell:"
Write-Host " $UpdateUserPath" -ForegroundColor yellow -BackgroundColor black
Write-Host " $UpdateMachinePath" -ForegroundColor yellow -BackgroundColor black
Write-Host " `$Env:Path = `"`${UserPath};`${MachinePath}`"" -ForegroundColor yellow -BackgroundColor black
Write-Host " (or close and reopen the terminal)"
Write-Host ""
}
# Switch to userprofile
Push-Location $Env:USERPROFILE
@@ -170,4 +194,13 @@ Invoke-DownloadUrl -Force -URL $PKG_URL -Params $UrlParams -Path $PkgInstallPwsh
powershell $HOME\.local\tmp\${exename}.install.ps1
IF ($IsWebiParent) {
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User").Trim(';')
$MachinePath = [Environment]::GetEnvironmentVariable("Path", "Machine").Trim(';')
$Env:Path = "${UserPath};${MachinePath}"
IF ($OriginalPath -ne $Env:Path) {
Show-HowToUpdateEnv
}
}
Pop-Location