From efb67109fbdacd13757faf033b6ce058c558c809 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 9 May 2023 00:04:59 +0000 Subject: [PATCH] ref!: replace pathman with PowerShell [Environment]::SetEnvironmentVariable --- _webi/template.ps1 | 92 +++++++++++++++++++++++++++++++++++++-------- _webi/webi-pwsh.ps1 | 22 ----------- _webi/webi.bat | 13 +------ deno/install.ps1 | 3 -- git/install.ps1 | 4 +- golang/install.ps1 | 8 +--- node/install.ps1 | 2 +- pathman/install.ps1 | 3 -- sass/install.ps1 | 2 +- sudo/install.ps1 | 3 -- webi/README.md | 4 +- zig/install.ps1 | 2 +- 12 files changed, 84 insertions(+), 74 deletions(-) diff --git a/_webi/template.ps1 b/_webi/template.ps1 index 7be7ec8..54062c4 100644 --- a/_webi/template.ps1 +++ b/_webi/template.ps1 @@ -42,27 +42,87 @@ New-Item -Path .local\opt -ItemType Directory -Force | out-null # {{ baseurl }} # {{ version }} -function webi_add_path +function webi_path_add($pathname) { - Write-Host '' - Write-Host '*****************************' -ForegroundColor red -BackgroundColor white - Write-Host '* IMPORTANT - READ ME *' -ForegroundColor red -BackgroundColor white - Write-Host '*****************************' -ForegroundColor red -BackgroundColor white - Write-Host '' - & "$Env:USERPROFILE\.local\bin\pathman.exe" add "$args[0]" - # Note: not all of these work as expected, so we use the unix-style, which is most consistent - #& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin - #& "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\.local\bin" - #& "$Env:USERPROFILE\.local\bin\pathman.exe" add %USERPROFILE%\.local\bin + # C:\Users\me => C:/Users/me + $my_home = $Env:UserProfile + $my_home = $my_home.replace('\\', '/') + $my_home_re = [regex]::escape($my_home) + + # ~/bin => %USERPROFILE%/bin + $pathname = $pathname.replace('~/', "$Env:UserProfile/") + + # C:\Users\me\bin => %USERPROFILE%/bin + $my_pathname = $pathname.replace('\\', '/') + $my_pathname = $my_pathname -ireplace $my_home_re, "%USERPROFILE%" + + $all_user_paths = [Environment]::GetEnvironmentVariable("Path", "User") + $user_paths = $all_user_paths -Split(';') + $exists_in_path = $false + foreach ($user_path in $user_paths) + { + # C:\Users\me\bin => %USERPROFILE%/bin + $my_user_path = $user_path.replace('\\', '/') + $my_user_path = $my_user_path -ireplace $my_home_re, "%USERPROFILE%" + + if ($my_user_path -ieq $my_pathname) + { + $exists_in_path = $true + } + } + if (-Not $exists_in_path) + { + $all_user_paths = $pathname + ";" + $all_user_paths + [Environment]::SetEnvironmentVariable("Path", $all_user_paths, "User") + } + + $session_paths = $Env:Path -Split(';') + $in_session_path = $false + foreach ($session_path in $session_paths) + { + # C:\Users\me\bin => %USERPROFILE%/bin + $my_session_path = $session_path.replace('\\', '/') + $my_session_path = $my_session_path -ireplace $my_home_re, "%USERPROFILE%" + + if ($my_session_path -ieq $my_pathname) + { + $in_session_path = $true + } + } + + if (-Not $in_session_path) + { + $my_cmd = 'PATH ' + "$pathname" + ';%PATH%' + $my_pwsh = '$Env:Path = "' + "$pathname" + ';$Env:Path"' + + Write-Host '' + Write-Host '**********************************' -ForegroundColor red -BackgroundColor white + Write-Host '* IMPORTANT -- READ ME *' -ForegroundColor red -BackgroundColor white + Write-Host '* (run the PATH command below) *' -ForegroundColor red -BackgroundColor white + Write-Host '**********************************' -ForegroundColor red -BackgroundColor white + Write-Host '' + echo "" + echo "Copy, paste, and run the appropriate command to update your PATH:" + echo "(or close and reopen the terminal, or reboot)" + echo "" + echo "cmd.exe:" + echo " $my_cmd" + echo "" + echo "PowerShell:" + echo " $my_pwsh" + echo "" + } } -# Run pathman to set up the folder -if (Test-Path -Path "$Env:USERPROFILE.local\bin\pathman.exe") -{ - & "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin -} +#$has_local_bin = echo "$Env:PATH" | Select-String -Pattern '\.local.bin' +#if (!$has_local_bin) +#{ + webi_path_add ~/.local/bin +#} {{ installer }} +webi_path_add ~/.local/bin + # Done popd diff --git a/_webi/webi-pwsh.ps1 b/_webi/webi-pwsh.ps1 index 9962259..f3bc3ae 100644 --- a/_webi/webi-pwsh.ps1 +++ b/_webi/webi-pwsh.ps1 @@ -50,28 +50,6 @@ IF($Env:WEBI_HOST -eq $null -or $Env:WEBI_HOST -eq "") $Env:WEBI_HOST = "https://webinstall.dev" } -if (!(Test-Path -Path .local\bin\pathman.exe)) -{ - $PATHMAN_URL = "$Env:WEBI_HOST/api/installers/pathman.ps1?formats=zip,exe,tar" - # Invoke-WebRequest -UserAgent "Windows amd64" "$PATHMAN_URL" -OutFile ".\.local\tmp\pathman.install.ps1" - & curl.exe -fsSL -A "$Env:WEBI_UA" "$PATHMAN_URL" -o .\.local\tmp\pathman.install.ps1 - powershell .\.local\tmp\pathman.install.ps1 -} - -# Run pathman to set up the folder -# (using unix style path because... cmd vs powershell vs whatever) -$has_local_bin = echo "$Env:PATH" | Select-String -Pattern '\.local.bin' -if (!$has_local_bin) -{ - Write-Host '' - Write-Host '**********************************' -ForegroundColor red -BackgroundColor white - Write-Host '* IMPORTANT -- READ ME *' -ForegroundColor red -BackgroundColor white - Write-Host '* (run the PATH command below) *' -ForegroundColor red -BackgroundColor white - Write-Host '**********************************' -ForegroundColor red -BackgroundColor white - Write-Host '' - & "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin -} - # {{ baseurl }} # {{ version }} diff --git a/_webi/webi.bat b/_webi/webi.bat index 6023b92..53f9863 100644 --- a/_webi/webi.bat +++ b/_webi/webi.bat @@ -10,18 +10,7 @@ pushd "%userprofile%" || goto :error mkdir .local\opt || goto :error ) - pushd .local\bin || goto :error - if NOT EXIST pathman.exe ( - echo updating PATH management - powershell $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest https://webinstall.dev/packages/pathman/install.bat -OutFile pathman-setup.bat || goto :error - call .\pathman-setup.bat || goto :error - del pathman-setup.bat || goto :error - rem TODO there's rumor of a windows tool called 'pathman' that does the same thing? - ) - popd || goto :error - .\.local\bin\pathman add ".local\bin" || goto :error - - echo downloading and installing %1 + echo Downloading and installing %1 powershell $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest https://webinstall.dev/packages/%1/install.ps1 -OutFile %1-webinstall.bat || goto :error rem TODO only add if it's not in there already diff --git a/deno/install.ps1 b/deno/install.ps1 index afe3436..ae0e903 100644 --- a/deno/install.ps1 +++ b/deno/install.ps1 @@ -38,6 +38,3 @@ IF (!(Test-Path -Path "$Env:USERPROFILE\.local\opt\$Env:PKG_NAME-v$Env:WEBI_VERS echo "Copying into '$Env:USERPROFILE\.local\bin\$Env:PKG_NAME.exe' from '$Env:USERPROFILE\.local\opt\$Env:PKG_NAME-v$Env:WEBI_VERSION.exe'" Remove-Item -Path "$Env:USERPROFILE\.local\bin\$Env:PKG_NAME.exe" -Recurse -ErrorAction Ignore Copy-Item -Path "$Env:USERPROFILE\.local\opt\$Env:PKG_NAME-v$Env:WEBI_VERSION.exe" -Destination "$Env:USERPROFILE\.local\bin\$Env:PKG_NAME.exe" -Recurse - -# Add to path -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin diff --git a/git/install.ps1 b/git/install.ps1 index 0c35faf..d44ba68 100644 --- a/git/install.ps1 +++ b/git/install.ps1 @@ -53,6 +53,4 @@ Remove-Item -Path "$pkg_dst" -Recurse -ErrorAction Ignore Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse # Add to path -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/opt/git/cmd -#& "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\.local\opt\git\cmd" -#& "$Env:USERPROFILE\.local\bin\pathman.exe" add %USERPROFILE%\.local\opt\git\cmd +webi_path_add ~/.local/opt/git/cmd diff --git a/golang/install.ps1 b/golang/install.ps1 index 1a2ebd7..dbc2612 100644 --- a/golang/install.ps1 +++ b/golang/install.ps1 @@ -85,11 +85,7 @@ echo stringer & "$pkg_dst_cmd" install golang.org/x/tools/cmd/stringer # Add to path -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/opt/go/bin -#& "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\.local\opt\go\bin" -#& "$Env:USERPROFILE\.local\bin\pathman.exe" add %USERPROFILE%\.local\opt\go\bin +webi_path_add ~/.local/opt/go/bin # Special to go: add default GOBIN to PATH -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/go/bin -#& "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\go\bin" -#& "$Env:USERPROFILE\.local\bin\pathman.exe" add %USERPROFILE%\go\bin +webi_path_add ~/go/bin diff --git a/node/install.ps1 b/node/install.ps1 index e7e4a70..f5c9872 100644 --- a/node/install.ps1 +++ b/node/install.ps1 @@ -41,4 +41,4 @@ Remove-Item -Path "$Env:USERPROFILE\.local\opt\$Env:PKG_NAME" -Recurse -ErrorAct Copy-Item -Path "$Env:USERPROFILE\.local\opt\$Env:PKG_NAME-v$Env:WEBI_VERSION" -Destination "$Env:USERPROFILE\.local\opt\$Env:PKG_NAME" -Recurse # Add to path -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/opt/node +webi_path_add ~/.local/opt/node diff --git a/pathman/install.ps1 b/pathman/install.ps1 index 26944ca..866333c 100644 --- a/pathman/install.ps1 +++ b/pathman/install.ps1 @@ -55,6 +55,3 @@ IF (!(Test-Path -Path "$pkg_src_cmd")) echo "Copying into '$pkg_dst_cmd' from '$pkg_src_cmd'" Remove-Item -Path "$pkg_dst_cmd" -Recurse -ErrorAction Ignore | out-null Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse - -# for the special case of pathman bootstrapping itself -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin diff --git a/sass/install.ps1 b/sass/install.ps1 index 12fa350..c911d4f 100644 --- a/sass/install.ps1 +++ b/sass/install.ps1 @@ -52,4 +52,4 @@ echo "Linking '$pkg_dst_dir' from '$pkg_src_dir'" New-Item -ItemType Junction -Path "$pkg_dst_dir" -Target "$pkg_src_dir" > $null # Add to path -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/opt/dart-sass +webi_path_add ~/.local/opt/dart-sass diff --git a/sudo/install.ps1 b/sudo/install.ps1 index 6f0b54d..d14df03 100644 --- a/sudo/install.ps1 +++ b/sudo/install.ps1 @@ -5,8 +5,5 @@ echo "Installing sudo.cmd..." # Couldn't figure out how to get this to work with "here strings", so forgive the ugly, but at least it works Set-Content -Path .local\bin\sudo.cmd -Value "@echo off`r`npowershell -Command ""Start-Process cmd -Verb RunAs -ArgumentList '/c cd /d %CD% && %*'""`r`n@echo on" -# Add to path -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin - echo "Installed to '$Env:USERPROFILE\.local\bin\sudo.cmd'" echo "" diff --git a/webi/README.md b/webi/README.md index cf4030d..57f9b3b 100644 --- a/webi/README.md +++ b/webi/README.md @@ -62,10 +62,8 @@ These are the files that are installed when you use [webinstall.dev](/): ~/.local/opt/pathman-* # Windows -~/.local/bin/webi.cmd +~/.local/bin/webi.bat ~/.local/bin/webi-pwsh.ps1 -~/.local/bin/pathman.exe -~/.local/opt/pathman-* ``` Assuming that you don't use `pathman` for anything else, you can safely remove diff --git a/zig/install.ps1 b/zig/install.ps1 index f26860d..1b41673 100644 --- a/zig/install.ps1 +++ b/zig/install.ps1 @@ -57,4 +57,4 @@ Remove-Item -Path "$pkg_dst" -Recurse -ErrorAction Ignore | out-null Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse # Add to Windows PATH -& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/opt/zig +webi_path_add ~/.local/opt/zig