@@ -73,63 +73,79 @@ function Set-EnvVar {
7373 Publish-EnvVar
7474}
7575
76- function Test -PathLikeEnvVar {
76+ function Split -PathLikeEnvVar {
7777 param (
78- [string ] $Name ,
78+ [string []] $Pattern ,
7979 [string ]$Path
8080 )
8181
8282 if ($null -eq $Path -and $Path -eq ' ' ) {
83- return $false , $null
83+ return $null , $null
8484 } else {
85- $strippedPath = $Path.Split (' ;' , [System.StringSplitOptions ]::RemoveEmptyEntries).Where ({ $_ -ne $Name }) -join ' ;'
86- return ($strippedPath -ne $Path ), $strippedPath
85+ $splitPattern = $Pattern.Split (' ;' , [System.StringSplitOptions ]::RemoveEmptyEntries)
86+ $splitPath = $Path.Split (' ;' , [System.StringSplitOptions ]::RemoveEmptyEntries)
87+ $inPath = @ ()
88+ foreach ($p in $splitPattern ) {
89+ $inPath += $splitPath.Where ({ $_ -like $p })
90+ $splitPath = $splitPath.Where ({ $_ -notlike $p })
91+ }
92+ return ($inPath -join ' ;' ), ($splitPath -join ' ;' )
8793 }
8894}
8995
9096function Add-Path {
9197 param (
92- [string ]$Path ,
98+ [string []]$Path ,
99+ [string ]$TargetEnvVar = ' PATH' ,
93100 [switch ]$Global ,
94- [switch ]$Force
101+ [switch ]$Force ,
102+ [switch ]$Quiet
95103 )
96104
97- if (! $Path.Contains (' %' )) {
98- $Path = Get-AbsolutePath $Path
99- }
100105 # future sessions
101- $inPath , $strippedPath = Test -PathLikeEnvVar $Path (Get-EnvVar - Name ' PATH ' - Global:$Global )
106+ $inPath , $strippedPath = Split -PathLikeEnvVar $Path (Get-EnvVar - Name $TargetEnvVar - Global:$Global )
102107 if (! $inPath -or $Force ) {
103- Write-Output " Adding $ ( friendly_path $Path ) to $ ( if ($Global ) {' global' } else {' your' }) path."
104- Set-EnvVar - Name ' PATH' - Value (@ ($Path , $strippedPath ) -join ' ;' ) - Global:$Global
108+ if (! $Quiet ) {
109+ $Path | ForEach-Object {
110+ Write-Host " Adding $ ( friendly_path $_ ) to $ ( if ($Global ) {' global' } else {' your' }) path."
111+ }
112+ }
113+ Set-EnvVar - Name $TargetEnvVar - Value ((@ ($Path ) + $strippedPath ) -join ' ;' ) - Global:$Global
105114 }
106115 # current session
107- $inPath , $strippedPath = Test -PathLikeEnvVar $Path $env: PATH
116+ $inPath , $strippedPath = Split -PathLikeEnvVar $Path $env: PATH
108117 if (! $inPath -or $Force ) {
109- $env: PATH = @ ($Path , $strippedPath ) -join ' ;'
118+ $env: PATH = ( @ ($Path ) + $strippedPath ) -join ' ;'
110119 }
111120}
112121
113122function Remove-Path {
114123 param (
115- [string ]$Path ,
116- [switch ]$Global
124+ [string []]$Path ,
125+ [string ]$TargetEnvVar = ' PATH' ,
126+ [switch ]$Global ,
127+ [switch ]$Quiet ,
128+ [switch ]$PassThru
117129 )
118130
119- if (! $Path.Contains (' %' )) {
120- $Path = Get-AbsolutePath $Path
121- }
122131 # future sessions
123- $inPath , $strippedPath = Test -PathLikeEnvVar $Path (Get-EnvVar - Name ' PATH ' - Global:$Global )
132+ $inPath , $strippedPath = Split -PathLikeEnvVar $Path (Get-EnvVar - Name $TargetEnvVar - Global:$Global )
124133 if ($inPath ) {
125- Write-Output " Removing $ ( friendly_path $Path ) from $ ( if ($Global ) {' global' } else {' your' }) path."
126- Set-EnvVar - Name ' PATH' - Value $strippedPath - Global:$Global
134+ if (! $Quiet ) {
135+ $Path | ForEach-Object {
136+ Write-Host " Removing $ ( friendly_path $_ ) from $ ( if ($Global ) {' global' } else {' your' }) path."
137+ }
138+ }
139+ Set-EnvVar - Name $TargetEnvVar - Value $strippedPath - Global:$Global
127140 }
128141 # current session
129- $inPath , $strippedPath = Test -PathLikeEnvVar $Path $env: PATH
130- if ($inPath ) {
142+ $inSessionPath , $strippedPath = Split -PathLikeEnvVar $Path $env: PATH
143+ if ($inSessionPath ) {
131144 $env: PATH = $strippedPath
132145 }
146+ if ($PassThru ) {
147+ return $inPath
148+ }
133149}
134150
135151# # Deprecated functions
@@ -145,8 +161,8 @@ function env($name, $global, $val) {
145161}
146162
147163function strip_path ($orig_path , $dir ) {
148- Show-DeprecatedWarning $MyInvocation ' Test -PathLikeEnvVar'
149- Test -PathLikeEnvVar - Name $dir - Path $orig_path
164+ Show-DeprecatedWarning $MyInvocation ' Split -PathLikeEnvVar'
165+ Split -PathLikeEnvVar - Name $dir - Path $orig_path
150166}
151167
152168function add_first_in_path ($dir , $global ) {
0 commit comments