@@ -581,12 +581,18 @@ function fullpath($path) {
581581 $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath ($path )
582582}
583583function friendly_path ($path ) {
584- $h = (Get-PsProvider ' FileSystem' ).home; if (! $h.endswith (' \' )) { $h += ' \' }
585- if ($h -eq ' \' ) { return $path }
586- return " $path " -replace ([regex ]::escape($h )), " ~\"
584+ $h = (Get-PSProvider ' FileSystem' ).Home
585+ if (! $h.EndsWith (' \' )) {
586+ $h += ' \'
587+ }
588+ if ($h -eq ' \' ) {
589+ return $path
590+ } else {
591+ return $path -replace ([Regex ]::Escape($h )), ' ~\'
592+ }
587593}
588594function is_local ($path ) {
589- ($path -notmatch ' ^https?://' ) -and (test-path $path )
595+ ($path -notmatch ' ^https?://' ) -and (Test-Path $path )
590596}
591597
592598# operations
@@ -715,57 +721,6 @@ function Invoke-ExternalCommand {
715721 return $true
716722}
717723
718- function Publish-Env {
719- if (-not (" Win32.NativeMethods" -as [Type ])) {
720- Add-Type - Namespace Win32 - Name NativeMethods - MemberDefinition @"
721- [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
722- public static extern IntPtr SendMessageTimeout(
723- IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
724- uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
725- "@
726- }
727-
728- $HWND_BROADCAST = [IntPtr ] 0xffff ;
729- $WM_SETTINGCHANGE = 0x1a ;
730- $result = [UIntPtr ]::Zero
731-
732- [Win32.Nativemethods ]::SendMessageTimeout($HWND_BROADCAST ,
733- $WM_SETTINGCHANGE ,
734- [UIntPtr ]::Zero,
735- " Environment" ,
736- 2 ,
737- 5000 ,
738- [ref ] $result
739- ) | Out-Null
740- }
741-
742- function env ($name , $global , $val = ' __get' ) {
743- $RegisterKey = if ($global ) {
744- Get-Item - Path ' HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
745- } else {
746- Get-Item - Path ' HKCU:'
747- }
748- $EnvRegisterKey = $RegisterKey.OpenSubKey (' Environment' , $val -ne ' __get' )
749-
750- if ($val -eq ' __get' ) {
751- $RegistryValueOption = [Microsoft.Win32.RegistryValueOptions ]::DoNotExpandEnvironmentNames
752- $EnvRegisterKey.GetValue ($name , $null , $RegistryValueOption )
753- } elseif ($val -eq $null ) {
754- try { $EnvRegisterKey.DeleteValue ($name ) } catch { }
755- Publish-Env
756- } else {
757- $RegistryValueKind = if ($val.Contains (' %' )) {
758- [Microsoft.Win32.RegistryValueKind ]::ExpandString
759- } elseif ($EnvRegisterKey.GetValue ($name )) {
760- $EnvRegisterKey.GetValueKind ($name )
761- } else {
762- [Microsoft.Win32.RegistryValueKind ]::String
763- }
764- $EnvRegisterKey.SetValue ($name , $val , $RegistryValueKind )
765- Publish-Env
766- }
767- }
768-
769724function isFileLocked ([string ]$path ) {
770725 $file = New-Object System.IO.FileInfo $path
771726
@@ -873,7 +828,7 @@ function warn_on_overwrite($shim, $path) {
873828function shim ($path , $global , $name , $arg ) {
874829 if (! (Test-Path $path )) { abort " Can't shim '$ ( fname $path ) ': couldn't find '$path '." }
875830 $abs_shimdir = ensure (shimdir $global )
876- ensure_in_path $abs_shimdir $global
831+ Add-Path - Path $abs_shimdir - Global: $global
877832 if (! $name ) { $name = strip_ext (fname $path ) }
878833
879834 $shim = " $abs_shimdir \$ ( $name.tolower ()) "
@@ -1010,26 +965,6 @@ function get_shim_path() {
1010965 return $shim_path
1011966}
1012967
1013- function search_in_path ($target ) {
1014- $path = (env ' PATH' $false ) + " ;" + (env ' PATH' $true )
1015- foreach ($dir in $path.split (' ;' )) {
1016- if (test-path " $dir \$target " - pathType leaf) {
1017- return " $dir \$target "
1018- }
1019- }
1020- }
1021-
1022- function ensure_in_path ($dir , $global ) {
1023- $path = env ' PATH' $global
1024- $dir = fullpath $dir
1025- if ($path -notmatch [regex ]::escape($dir )) {
1026- write-output " Adding $ ( friendly_path $dir ) to $ ( if ($global ){' global' }else {' your' }) path."
1027-
1028- env ' PATH' $global " $dir ;$path " # for future sessions...
1029- $env: PATH = " $dir ;$env: PATH " # for this session
1030- }
1031- }
1032-
1033968function Get-DefaultArchitecture {
1034969 $arch = get_config DEFAULT_ARCHITECTURE
1035970 $system = if (${env: ProgramFiles(Arm)} ) {
@@ -1104,45 +1039,6 @@ function Confirm-InstallationStatus {
11041039 return , $Installed
11051040}
11061041
1107- function strip_path ($orig_path , $dir ) {
1108- if ($null -eq $orig_path ) { $orig_path = ' ' }
1109- $stripped = [string ]::join(' ;' , @ ( $orig_path.split (' ;' ) | Where-Object { $_ -and $_ -ne $dir } ))
1110- return ($stripped -ne $orig_path ), $stripped
1111- }
1112-
1113- function add_first_in_path ($dir , $global ) {
1114- $dir = fullpath $dir
1115-
1116- # future sessions
1117- $null , $currpath = strip_path (env ' path' $global ) $dir
1118- env ' path' $global " $dir ;$currpath "
1119-
1120- # this session
1121- $null , $env: PATH = strip_path $env: PATH $dir
1122- $env: PATH = " $dir ;$env: PATH "
1123- }
1124-
1125- function remove_from_path ($dir , $global ) {
1126- $dir = fullpath $dir
1127-
1128- # future sessions
1129- $was_in_path , $newpath = strip_path (env ' path' $global ) $dir
1130- if ($was_in_path ) {
1131- Write-Output " Removing $ ( friendly_path $dir ) from your path."
1132- env ' path' $global $newpath
1133- }
1134-
1135- # current session
1136- $was_in_path , $newpath = strip_path $env: PATH $dir
1137- if ($was_in_path ) { $env: PATH = $newpath }
1138- }
1139-
1140- function ensure_robocopy_in_path {
1141- if (! (Test-CommandAvailable robocopy)) {
1142- shim " C:\Windows\System32\Robocopy.exe" $false
1143- }
1144- }
1145-
11461042function wraptext ($text , $width ) {
11471043 if (! $width ) { $width = $host.ui.rawui.buffersize.width };
11481044 $width -= 1 # be conservative: doesn't seem to print the last char
0 commit comments