Skip to content

Commit

Permalink
Fix/68 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelcolas committed Apr 5, 2023
1 parent d8ffc93 commit 140e12f
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 53 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed

- Removed SideBySide option as per [#61](https://github.com/chocolatey-community/Chocolatey/issues/61).

### Fixed

- Fixed [#68](https://github.com/chocolatey-community/Chocolatey/issues/68) by making sure it's set to the
correct Path.
18 changes: 15 additions & 3 deletions source/Classes/002.ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,19 @@ class ChocolateyPackage
return $currentState
}

$localPackage = Get-ChocolateyPackage -LocalOnly -Name $this.Name -Exact
try
{
$localPackage = Get-ChocolateyPackage -LocalOnly -Name $this.Name -Exact
}
catch
{
Write-Verbose -Message ('Exception Caught:' -f $_)
$localPackage = $null
$currentState.Reasons += @{
code = 'ChocolateyPackage:ChocolateyPackage:ChocolateyError'
phrase = ('Error: {0}.' -f $_)
}
}

if ($null -eq $localPackage)
{
Expand Down Expand Up @@ -195,7 +207,7 @@ class ChocolateyPackage
{
$currentState = $this.Get()

if ($currentState.Reasons.Code.Where({$_ -in @('BelowExpectedVersion','ShouldBeInstalled','ShouldNotBeInstalled','')}))
if ($currentState.Reasons.Code.Where({$_ -in @('BelowExpectedVersion','ShouldBeInstalled','ShouldNotBeInstalled')}))
{
return $false
}
Expand All @@ -208,7 +220,7 @@ class ChocolateyPackage
[void] Set()
{
[ChocolateyPackage] $currentState = $this.Get()
$chocoCommand = Get-Command 'Install-ChocolateyPackage'
$chocoCommand = Get-Command -Name 'Install-ChocolateyPackage'
[hashtable] $chocoCommandParams = @{
Name = $this.Name
Confirm = $false
Expand Down
1 change: 1 addition & 0 deletions source/Classes/003.ChocolateySoftware.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ChocolateySoftware

if ($null -eq $chocoExe)
{
Write-Verbose -Message ('Path: {0}' -f $ENV:Path)
$currentState.Ensure = 'Absent'
}
else
Expand Down
15 changes: 14 additions & 1 deletion source/Classes/004.ChocolateySource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,20 @@ class ChocolateySource
$currentState = [ChocolateySource]::New()
$currentState.Name = $this.Name

$localSource = Get-ChocolateySource -Name $this.Name -ErrorAction 'Ignore'
try
{
$localSource = Get-ChocolateySource -Name $this.Name
}
catch
{
Write-Verbose -Message ('Exception Caught:' -f $_)
$localSource = $null
$currentState.Reasons += @{
code = 'ChocolateySource:ChocolateySource:ChocolateyError'
phrase = ('Error: {0}.' -f $_)
}
}

if ($localSource.Name -eq $this.Name)
{
$currentState.Ensure = 'Present'
Expand Down
1 change: 1 addition & 0 deletions source/WikiSource/Home.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Chocolatey PowerShell module
<sup>*chocolatey v#.#.#*</sup>

Welcome to the Chocolatey PowerShell Module wiki!
41 changes: 41 additions & 0 deletions source/private/Get-ChocolateyInstallPath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

<#
.SYNOPSIS
Gets environment variable ChocolateyInstall from Machine scope.
.DESCRIPTION
This command gets the machine-scoped environment variable 'ChocolateyInstall',
and make sure it's set if the folder is present but variable is not.
If the variable is not set and the choclatey folder can't be found,
the command will write to the error stream.
.EXAMPLE
Get-ChocolateyInstallPath -ErrorAction 'Stop'
#>
function Get-ChocolateyInstallPath
{
[CmdletBinding()]
[OutputType([string])]
param
(
#
)

$chocoInstallPath = [Environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine')
if ([string]::IsNullOrEmpty($chocoPath) -and (Test-Path -Path (Join-Path -Path $env:ProgramData -ChildPath 'Chocolatey')))
{
$chocoInstallPath = Join-Path -Path $env:ProgramData -ChildPath 'Chocolatey'
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoInstallPath, 'Machine')
}
elseif (-not [string]::IsNullOrEmpty($chocoInstallPath))
{
Write-Debug -Message ('ChocolateyInstall path Machine environmen variable already set to ''{0}''.' -f $chocoInstallPath)
}
else
{
Write-Error -Message 'The chocolatey install Machine environment variable couldn''t be found.'
}

return $chocoInstallPath
}
10 changes: 8 additions & 2 deletions source/public/Get-ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ function Get-ChocolateyPackage
(($Name -and $Exact) -or ([string]::IsNullOrEmpty($Name)))
)
{
$CacheFolder = Join-Path -Path $Env:ChocolateyInstall -ChildPath 'cache'
$CachePath = Join-Path -Path $CacheFolder -ChildPath 'GetChocolateyPackageCache.xml'
$chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'SilentlyContinue'
if ([string]::IsNullOrEmpty($chocoInstallPath))
{
Write-Verbose -Message 'Chocolatey Software is not installed on this system.'
return
}

$cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml')
try
{
if (-not (Test-Path -Path $CacheFolder))
Expand Down
7 changes: 4 additions & 3 deletions source/public/Install-ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,12 @@ function Install-ChocolateyPackage
throw "Chocolatey Software not found."
}

$CachePath = [io.path]::Combine($Env:ChocolateyInstall, 'cache', 'GetChocolateyPackageCache.xml')
if ( (Test-Path -Path $CachePath))
$chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'Stop'
$cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml')
if ((Test-Path -Path $cachePath))
{
Write-Debug -Message 'Removing cache begin of Install-ChocolateyPackage'
$null = Remove-Item -Path $CachePath -ErrorAction SilentlyContinue -Force -Confirm:$false
$null = Remove-Item -Path $cachePath -ErrorAction SilentlyContinue -Force -Confirm:$false
Write-Debug -Message 'Removed'
}
}
Expand Down
66 changes: 37 additions & 29 deletions source/public/Install-ChocolateySoftware.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,14 @@
function Install-ChocolateySoftware
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
[CmdletBinding(
DefaultParameterSetName = 'FromFeedUrl'
)]
param (
[Parameter(
ParameterSetName = 'FromPackageUrl'
)]
[CmdletBinding(DefaultParameterSetName = 'FromFeedUrl')]
param
(
[Parameter(ParameterSetName = 'FromPackageUrl')]
[uri]
$ChocolateyPackageUrl,

[Parameter(
ParameterSetName = 'FromFeedUrl'
)]
[Parameter(ParameterSetName = 'FromFeedUrl')]
[uri]
$PackageFeedUrl = 'https://chocolatey.org/api/v2',

Expand Down Expand Up @@ -139,21 +134,21 @@ function Install-ChocolateySoftware
}
catch
{
Write-Warning 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to do one or more of the following: (1) upgrade to .NET Framework 4.5+ and PowerShell v3, (2) specify internal Chocolatey package location (set $env:chocolateyDownloadUrl prior to install or host the package internally), (3) use the Download + PowerShell method of install. See https://chocolatey.org/install for all install options.'
Write-Warning -Message 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to do one or more of the following: (1) upgrade to .NET Framework 4.5+ and PowerShell v3, (2) specify internal Chocolatey package location (set $env:chocolateyDownloadUrl prior to install or host the package internally), (3) use the Download + PowerShell method of install. See https://chocolatey.org/install for all install options.'
}

switch ($PSCmdlet.ParameterSetName)
{
'FromFeedUrl'
{
if ($PackageFeedUrl -and ![string]::IsNullOrEmpty($Version))
if ($PackageFeedUrl -and -not [string]::IsNullOrEmpty($Version))
{
Write-Verbose "Downloading specific version of Chocolatey: $Version"
Write-Verbose -Message "Downloading specific version of Chocolatey: $Version"
$url = "$PackageFeedUrl/package/chocolatey/$Version"
}
else
{
if (![string]::IsNullOrEmpty($PackageFeedUrl))
if (-not [string]::IsNullOrEmpty($PackageFeedUrl))
{
$url = $PackageFeedUrl
}
Expand Down Expand Up @@ -203,20 +198,22 @@ function Install-ChocolateySoftware
$null = New-Item -path $tempDir -ItemType Directory
}

$file = Join-Path $tempDir "chocolatey.zip"
$file = Join-Path -Path $tempDir -ChildPath "chocolatey.zip"
# Download the Chocolatey package
Write-Verbose -Message "Getting Chocolatey from $url."
$GetRemoteFileParams = @{
url = $url
file = $file
}

$GetRemoteFileParamsName = (get-command Get-RemoteFile).parameters.keys
$KeysForRemoteFile = $PSBoundParameters.keys | Where-Object { $_ -in $GetRemoteFileParamsName }
foreach ($key in $KeysForRemoteFile )
{
Write-Debug "`tWith $key :: $($PSBoundParameters[$key])"
$null = $GetRemoteFileParams.Add($key , $PSBoundParameters[$key])
}

$null = Get-RemoteFile @GetRemoteFileParams

# unzip the package
Expand All @@ -242,42 +239,53 @@ function Install-ChocolateySoftware
}

# Call chocolatey install
Write-Verbose "Installing chocolatey on this machine."
Write-Verbose -Message "Installing chocolatey on this machine."
$TempTools = [io.path]::combine($tempDir, 'tools')
# To be able to mock
$chocInstallPS1 = Join-Path $TempTools 'chocolateyInstall.ps1'
$chocInstallPS1 = Join-Path -Path $TempTools -ChildPath 'chocolateyInstall.ps1'

if ($InstallationDirectory)
{
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $InstallationDirectory, 'Machine')
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $InstallationDirectory, 'Process')
}

& $chocInstallPS1 | Write-Debug
Write-Verbose -Message 'Ensuring chocolatey commands are on the path.'
[string] $chocoPath = ''

Write-Verbose 'Ensuring chocolatey commands are on the path.'
$chocoPath = [Environment]::GetEnvironmentVariable('ChocolateyInstall')
if ([string]::IsNullOrEmpty($chocoPath))
if ($chocoPath = [Environment]::GetEnvironmentVariable('ChocolateyInstall','Machine'))
{
$chocoPath = "$env:ALLUSERSPROFILE\Chocolatey"
Write-Debug -Message ('The Machine Environment variable is already set to: ''{0}''.' -f $chocoPath)
}

if (-not (Test-Path -Path $chocoPath))
else
{
$chocoPath = "$env:SYSTEMDRIVE\ProgramData\Chocolatey"
# Checking if it was installed in AllUserProfile/Chocolatey (was default many years ago)
$chocoPath = Join-Path -Path $env:ALLUSERSPROFILE -ChildPath 'Chocolatey'
if (-not (Test-Path -Path $chocoPath))
{
# The AllUserProfile/Chocolatey folder does not exit, let's install in correct default location
$chocoPath = Join-Path -Path $env:ProgramData -ChildPath 'Chocolatey'
}

# Set the Machine-scoped 'ChocolateyInstall' environement variable
Write-Debug -Message ('Setting the Machine & Process Environment variable to: ''{0}''.' -f $chocoPath)
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoPath, 'Machine')
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoPath, 'Process')
}

$chocoExePath = Join-Path -Path $chocoPath -ChildPath 'bin'

if ($($env:Path).ToLower().Contains($($chocoExePath).ToLower()) -eq $false)
if (@($env:Path.ToLower() -split [io.path]::PathSeparator) -notcontains $chocoExePath.ToLower())
{
$env:Path = [Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)
# we can't see the choco bin folder in $env:Path, trying to load from Machine.
$env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine')
}

Write-Verbose 'Ensuring chocolatey.nupkg is in the lib folder'
Write-Verbose -Message 'Ensuring chocolatey.nupkg is in the lib folder'
$chocoPkgDir = Join-Path -Path $chocoPath -ChildPath 'lib\chocolatey'
$nupkg = Join-Path -Path $chocoPkgDir -ChildPath 'chocolatey.nupkg'
$null = [System.IO.Directory]::CreateDirectory($chocoPkgDir)
Copy-Item -Path "$file" -Destination "$nupkg" -Force -ErrorAction SilentlyContinue
Copy-Item -Path $file -Destination $nupkg -Force -ErrorAction 'SilentlyContinue'

if ($ChocoVersion = & "$chocoPath\choco.exe" @('-v'))
{
Expand Down
4 changes: 2 additions & 2 deletions source/public/Test-ChocolateyInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function Test-ChocolateyInstall
if (-not ($chocoCmd = @(Get-Command 'choco.exe' -CommandType 'Application' -ErrorAction 'SilentlyContinue')[0]))
{
if (
!$InstallDir -or
-not $InstallDir -or
$chocoCmd.Path -match [regex]::Escape($InstallDir)
)
{
Expand All @@ -57,7 +57,7 @@ function Test-ChocolateyInstall
}
else
{
Write-Verbose "Chocolatey Software not found."
Write-Verbose -Message 'Chocolatey Software not found.'
return $false
}
}
2 changes: 1 addition & 1 deletion source/public/Test-ChocolateySource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function Test-ChocolateySource
{
if (-not (Get-Command 'choco.exe' -CommandType Application -ErrorAction SilentlyContinue))
{
throw "Chocolatey Software not found."
throw "Chocolatey Software not found. 17"
}

if (-not ($Source = (Get-ChocolateySource -Name $Name)) )
Expand Down
8 changes: 4 additions & 4 deletions source/public/Uninstall-Chocolatey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Uninstall-Chocolatey
[Parameter()]
[AllowNull()]
[System.String]
$InstallDir = $Env:ChocolateyInstall,
$InstallDir = $(Get-ChocolateyInstallPath -ErrorAction 'SilentlyContinue'),

[Parameter(DontShow)]
[switch]
Expand All @@ -41,7 +41,7 @@ function Uninstall-Chocolatey

process
{
#If InstallDir is empty or null, select from whee choco.exe is available
#If InstallDir is empty or null, select from the choco.exe if available
if (-not $InstallDir)
{
Write-Debug -Message "Attempting to find the choco.exe command."
Expand All @@ -59,7 +59,7 @@ function Uninstall-Chocolatey
}
}

Write-Verbose "Chocolatey Installation Folder is $InstallDir"
Write-Verbose -Message "Chocolatey Installation Folder is $InstallDir"
$chocoFiles = @('choco.exe', 'chocolatey.exe', 'cinst.exe', 'cuninst.exe', 'clist.exe', 'cpack.exe', 'cpush.exe',
'cver.exe', 'cup.exe').Foreach{ $_; "$_.old" } #ensure the .old are also removed

Expand Down Expand Up @@ -96,7 +96,7 @@ function Uninstall-Chocolatey

Write-Verbose -Message "Removing $InstallDir from the Path and the ChocolateyInstall Environment variable."
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $null, 'Machine')
$Env:ChocolateyInstall = $null
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $null, 'Process')
$AllPaths = [Environment]::GetEnvironmentVariable('Path', 'machine').split(';').where{
![string]::IsNullOrEmpty($_) -and
$_ -notmatch "^$([regex]::Escape($InstallDir))\\bin$"
Expand Down
9 changes: 6 additions & 3 deletions source/public/Uninstall-ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,15 @@ function Uninstall-ChocolateyPackage
{
throw "Chocolatey Software not found."
}
$CachePath = [io.path]::Combine($Env:ChocolateyInstall, 'cache', 'GetChocolateyPackageCache.xml')
if ( (Test-Path $CachePath))

$chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'Stop'
$cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml')
if ( (Test-Path $cachePath))
{
$null = Remove-Item -Path $CachePath -ErrorAction 'SilentlyContinue' -Confirm:$false
$null = Remove-Item -Path $cachePath -ErrorAction 'SilentlyContinue' -Confirm:$false
}
}

process
{
foreach ($PackageName in $Name)
Expand Down
8 changes: 5 additions & 3 deletions source/public/Update-ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,12 @@ function Update-ChocolateyPackage
{
throw "Chocolatey Software not found."
}
$CachePath = [io.path]::Combine($Env:ChocolateyInstall, 'cache', 'GetChocolateyPackageCache.xml')
if ( (Test-Path $CachePath))

$chocoInstallPath = Get-ChocolateyInstallPath -ErrorAction 'Stop'
$cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml')
if ( (Test-Path $cachePath))
{
$null = Remove-Item -Path $CachePath -ErrorAction 'SilentlyContinue' -Confirm:$false
$null = Remove-Item -Path $cachePath -ErrorAction 'SilentlyContinue' -Confirm:$false
}
}

Expand Down

0 comments on commit 140e12f

Please sign in to comment.