Skip to content

Commit

Permalink
fixes $Env:ChocolateyInstall not set #68
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelcolas committed Apr 2, 2023
1 parent dc2e289 commit f44489a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 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.
57 changes: 32 additions & 25 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 @@ -146,14 +141,14 @@ function Install-ChocolateySoftware
{
'FromFeedUrl'
{
if ($PackageFeedUrl -and ![string]::IsNullOrEmpty($Version))
if ($PackageFeedUrl -and -not [string]::IsNullOrEmpty($Version))
{
Write-Verbose "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,7 +198,7 @@ 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 = @{
Expand Down Expand Up @@ -245,39 +240,51 @@ function Install-ChocolateySoftware
Write-Verbose "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

0 comments on commit f44489a

Please sign in to comment.