-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This includes a modified version of the Uninstall-ChocolateyPath script from chocolatey/choco#1663
- Loading branch information
Showing
12 changed files
with
333 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright © 2018 Chocolatey Software, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# https://github.com/chocolatey/choco/pull/1663 | ||
# https://github.com/chocolatey/choco/blob/b89e64e9fd599c3244290d62096e0e7ab0c91742/src/chocolatey.resources/helpers/functions/EnvPathFunctions.ps1 | ||
|
||
# For internal use by Install-ChocolateyPath and Uninstall-ChocolateyPath. | ||
|
||
function Parse-EnvPathList([string] $rawPathVariableValue) { | ||
# Using regex (for performance) which correctly splits at each semicolon unless the semicolon is inside double quotes. | ||
# Unlike semicolons, quotes are not allowed inside paths so there is thankfully no need to unescape them. | ||
# (Verified using Windows 10’s environment variable editor.) | ||
# Blank path entries are preserved, such as those caused by a trailing semicolon. | ||
# This enables reserializing without gratuitous reformatting. | ||
$paths = $rawPathVariableValue -split '(?<=\G(?:[^;"]|"[^"]*")*);' | ||
|
||
# Remove quotes from each path if they are present | ||
for ($i = 0; $i -lt $paths.Length; $i++) { | ||
$path = $paths[$i] | ||
if ($path.Length -ge 2 -and $path.StartsWith('"', [StringComparison]::Ordinal) -and $path.EndsWith('"', [StringComparison]::Ordinal)) { | ||
$paths[$i] = $path.Substring(1, $path.Length - 2) | ||
} | ||
} | ||
|
||
return $paths | ||
} | ||
|
||
function Format-EnvPathList([string[]] $paths) { | ||
# Don’t mutate the original (externally visible if the argument is not type-coerced), | ||
# but don’t clone if mutation is unnecessary. | ||
$createdDefensiveCopy = $false | ||
|
||
# Add quotes to each path if necessary | ||
for ($i = 0; $i -lt $paths.Length; $i++) { | ||
$path = $paths[$i] | ||
if ($path -ne $null -and $path.Contains(';')) { | ||
if (-not $createdDefensiveCopy) { | ||
$createdDefensiveCopy = $true | ||
$paths = $paths.Clone() | ||
} | ||
$paths[$i] = '"' + $path + '"' | ||
} | ||
} | ||
|
||
return $paths -join ';' | ||
} | ||
|
||
function IndexOf-EnvPath([System.Collections.Generic.List[string]] $list, [string] $value) { | ||
$list.FindIndex({ | ||
$value.Equals($args[0], [StringComparison]::OrdinalIgnoreCase) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright © 2017 Chocolatey Software, Inc. | ||
# Copyright © 2015 - 2017 RealDimensions Software, LLC | ||
# Copyright © 2011 - 2015 RealDimensions Software, LLC & original authors/contributors from https://github.com/chocolatey/chocolatey | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" | ||
$modulePath = Join-Path "${toolsDir}" 'EnvPathFunctions-GH1663.ps1' | ||
Import-Module "${modulePath}" | ||
|
||
# https://github.com/chocolatey/choco/pull/1663 | ||
# https://github.com/chocolatey/choco/blob/b89e64e9fd599c3244290d62096e0e7ab0c91742/src/chocolatey.resources/helpers/functions/Install-ChocolateyPath.ps1 | ||
|
||
function Install-ChocolateyPath-GH1663 { | ||
<# | ||
.SYNOPSIS | ||
**NOTE:** Administrative Access Required when `-PathType 'Machine'.` | ||
This puts a directory to the PATH environment variable. | ||
.DESCRIPTION | ||
Ensures that the given path is present in the given type of PATH | ||
environment variable as well as in the current session. | ||
.NOTES | ||
This command will assert UAC/Admin privileges on the machine if | ||
`-PathType 'Machine'`. | ||
This is used when the application/tool is not being linked by Chocolatey | ||
(not in the lib folder). | ||
.INPUTS | ||
None | ||
.OUTPUTS | ||
None | ||
.PARAMETER PathToInstall | ||
The exact path to ensure in the environment PATH. | ||
.PARAMETER PathType | ||
Which PATH to add it to. If specifying `Machine`, this requires admin | ||
privileges to run correctly. | ||
.PARAMETER IgnoredArguments | ||
Allows splatting with arguments that do not apply. Do not use directly. | ||
.EXAMPLE | ||
Install-ChocolateyPath -PathToInstall "$($env:SystemDrive)\tools\gittfs" | ||
.EXAMPLE | ||
Install-ChocolateyPath "$($env:SystemDrive)\Program Files\MySQL\MySQL Server 5.5\bin" -PathType 'Machine' | ||
.LINK | ||
Uninstall-ChocolateyPath | ||
.LINK | ||
Install-ChocolateyEnvironmentVariable | ||
.LINK | ||
Get-EnvironmentVariable | ||
.LINK | ||
Set-EnvironmentVariable | ||
.LINK | ||
Get-ToolsLocation | ||
#> | ||
param( | ||
[parameter(Mandatory=$true, Position=0)][string] $pathToInstall, | ||
[parameter(Mandatory=$false, Position=1)][System.EnvironmentVariableTarget] $pathType = [System.EnvironmentVariableTarget]::User, | ||
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments | ||
) | ||
|
||
Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters | ||
## Called from chocolateysetup.psm1 - wrap any Write-Host in try/catch | ||
|
||
$paths = Parse-EnvPathList (Get-EnvironmentVariable -Name 'PATH' -Scope $pathType -PreserveVariables) | ||
if ((IndexOf-EnvPath $paths $pathToInstall) -eq -1) { | ||
try { | ||
Write-Host "PATH environment variable does not have $pathToInstall in it. Adding..." | ||
} catch { | ||
Write-Verbose "PATH environment variable does not have $pathToInstall in it. Adding..." | ||
} | ||
|
||
if ($pathType -eq [EnvironmentVariableTarget]::Machine -and -not (Test-ProcessAdminRights)) { | ||
$psArgs = "Install-ChocolateyPath -pathToInstall `'$pathToInstall`' -pathType `'$pathType`'" | ||
Start-ChocolateyProcessAsAdmin "$psArgs" | ||
} else { | ||
$paths += $pathToInstall | ||
Set-EnvironmentVariable -Name 'PATH' -Value $(Format-EnvPathList $paths) -Scope $pathType | ||
} | ||
} | ||
|
||
# Make change immediately available | ||
$paths = Parse-EnvPathList $env:PATH | ||
if ((IndexOf-EnvPath $paths $pathToInstall) -eq -1) { | ||
$paths += $pathToInstall | ||
$env:Path = Format-EnvPathList $paths | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright © 2018 Chocolatey Software, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" | ||
$modulePath = Join-Path "${toolsDir}" 'EnvPathFunctions-GH1663.ps1' | ||
Import-Module "${modulePath}" | ||
|
||
# https://github.com/chocolatey/choco/pull/1663 | ||
# https://github.com/chocolatey/choco/blob/b89e64e9fd599c3244290d62096e0e7ab0c91742/src/chocolatey.resources/helpers/functions/Uninstall-ChocolateyPath.ps1 | ||
|
||
function Uninstall-ChocolateyPath-GH1663 { | ||
<# | ||
.SYNOPSIS | ||
**NOTE:** Administrative Access Required when `-PathType 'Machine'.` | ||
This puts a directory to the PATH environment variable. | ||
.DESCRIPTION | ||
Ensures that the given path is not present in the given type of PATH | ||
environment variable as well as in the current session. | ||
.NOTES | ||
This command will assert UAC/Admin privileges on the machine if | ||
`-PathType 'Machine'`. | ||
This is used when the application/tool is not being linked by Chocolatey | ||
(not in the lib folder). | ||
.PARAMETER PathToUninstall | ||
The exact path to remove from the environment PATH. | ||
.PARAMETER PathType | ||
Which PATH to add it to. If specifying `Machine`, this requires admin | ||
privileges to run correctly. | ||
.PARAMETER IgnoredArguments | ||
Allows splatting with arguments that do not apply. Do not use directly. | ||
.EXAMPLE | ||
Uninstall-ChocolateyPath -PathToUninstall "$($env:SystemDrive)\tools\gittfs" | ||
.EXAMPLE | ||
Uninstall-ChocolateyPath "$($env:SystemDrive)\Program Files\MySQL\MySQL Server 5.5\bin" -PathType 'Machine' | ||
.LINK | ||
Install-ChocolateyPath | ||
.LINK | ||
Get-EnvironmentVariable | ||
.LINK | ||
Set-EnvironmentVariable | ||
.LINK | ||
Get-ToolsLocation | ||
#> | ||
param( | ||
[parameter(Mandatory=$true, Position=0)][string] $pathToUninstall, | ||
[parameter(Mandatory=$false, Position=1)][System.EnvironmentVariableTarget] $pathType = [System.EnvironmentVariableTarget]::User, | ||
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments | ||
) | ||
|
||
Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters | ||
|
||
$paths = Parse-EnvPathList (Get-EnvironmentVariable -Name 'PATH' -Scope $pathType -PreserveVariables) | ||
$removeIndex = (IndexOf-EnvPath $paths $pathToUninstall) | ||
if ($removeIndex -ge 0) { | ||
Write-Host "Found $pathToUninstall in PATH environment variable. Removing..." | ||
|
||
if ($pathType -eq [EnvironmentVariableTarget]::Machine -and -not (Test-ProcessAdminRights)) { | ||
Write-Error "Failed: This requires administrator rights." | ||
} else { | ||
$paths = [System.Collections.ArrayList] $paths | ||
$paths.RemoveAt($removeIndex) | ||
Set-EnvironmentVariable -Name 'PATH' -Value $(Format-EnvPathList $paths) -Scope $pathType | ||
} | ||
} | ||
|
||
# Make change immediately available | ||
$paths = Parse-EnvPathList $env:PATH | ||
if ($removeIndex -ge 0) { | ||
$paths = [System.Collections.ArrayList] $paths | ||
$paths.RemoveAt($removeIndex) | ||
$env:Path = Format-EnvPathList $paths | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
$ErrorActionPreference = 'Stop'; | ||
|
||
$packageName = "${ENV:ChocolateyPackageName}" | ||
$nameSplit = ${packageName}.Split('-') | ||
$toolchain = "msvc" | ||
|
||
if (${packageName}.Contains("devel")) { | ||
Write-Output "No PATH changes required for devel package. Exiting..." | ||
Exit | ||
} | ||
|
||
# gstreamer, gstreamer-devel, gstreamer-mingw, or gstreamer-mingw-devel | ||
if (${nameSplit}.Length -gt 1 -and ${nameSplit}[1] -ne "devel") { | ||
$toolchain = ${nameSplit}[1] | ||
} | ||
|
||
# ENV:ChocolateyForceX86 appears to never be set for uninstall | ||
# No --x86 flag exists on the command | ||
if (${ENV:OS_IS64BIT} -And -Not ${ENV:ChocolateyForceX86}) { | ||
$pathForUninstall = "%GSTREAMER_1_0_ROOT_$(${toolchain}.ToUpper())_X86_64%\bin" | ||
} else { | ||
$pathForUninstall = "%GSTREAMER_1_0_ROOT_$(${toolchain}.ToUpper())_X86%\bin" | ||
} | ||
|
||
if (-not (Get-Command 'Uninstall-ChocolateyPath' -errorAction SilentlyContinue)) { | ||
Write-Output "Using Uninstall-ChocolateyPath-GH1663 function"; | ||
|
||
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" | ||
$modulePath = Join-Path "${toolsDir}" 'Uninstall-ChocolateyPath-GH1663.ps1' | ||
Import-Module "${modulePath}" | ||
|
||
Uninstall-ChocolateyPath-GH1663 "${pathForUninstall}" "User" | ||
} | ||
else { | ||
Write-Debug "Using native Uninstall-ChocolateyPath function"; | ||
|
||
Uninstall-ChocolateyPath "${pathForUninstall}" "User" | ||
} |
Oops, something went wrong.