From 1f634be3bd5339d9d2f1a2bf960c6999d466a248 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 11 Nov 2023 21:54:29 -0700 Subject: [PATCH] fix(windows): only show PATH update message once --- _webi/install-package.tpl.ps1 | 43 +++++------------------------------ webi/webi-pwsh.ps1 | 33 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/_webi/install-package.tpl.ps1 b/_webi/install-package.tpl.ps1 index d027013..e6984d1 100644 --- a/_webi/install-package.tpl.ps1 +++ b/_webi/install-package.tpl.ps1 @@ -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 diff --git a/webi/webi-pwsh.ps1 b/webi/webi-pwsh.ps1 index a223d28..85a9529 100644 --- a/webi/webi-pwsh.ps1 +++ b/webi/webi-pwsh.ps1 @@ -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