Skip to content

Commit

Permalink
(chocolateyGH-7) Upgrade improvements
Browse files Browse the repository at this point in the history
On upgrade, split the old folder copying from the old folder removal
since the removal is likely to throw errors, we want to give the
message that choco is installed successfully, then attempt to remove
the old folder (which is highly likely to throw errors).
  • Loading branch information
ferventcoder committed Feb 12, 2015
1 parent d793663 commit 050a28a
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions nuget/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
first prior to using choco.
"@ | write-Output

if (-not $allowInsecureRootInstall) {
Remove-OldChocolateyInstall $defaultChocolateyPathOld
}
}

function Set-ChocolateyInstallFolder {
Expand Down Expand Up @@ -171,7 +175,7 @@ param(
$userAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($currentUser.Name, $rights, "Allow")

# this is idempotent
Write-Output "Adding Modify permission for $($currentUser.Name) to '$path'"
Write-Output "Adding Modify permission for $($currentUser.Name) to '$folder'"
$acl.SetAccessRuleProtection($false,$true)
$acl.SetAccessRule($userAccessRule)
Set-Acl $folder $acl
Expand Down Expand Up @@ -199,15 +203,48 @@ param(
$updatedPath = [System.Text.RegularExpressions.Regex]::Replace($path,[System.Text.RegularExpressions.Regex]::Escape($chocolateyExePathOld) + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
if ($updatedPath -ne $path) {
Write-Output "Updating `'$_`' PATH to reflect removal of '$chocolateyPathOld'."
Set-EnvironmentVariable -Name 'Path' -Value $updatedPath -Scope $_
try {
Set-EnvironmentVariable -Name 'Path' -Value $updatedPath -Scope $_ -ErrorAction Stop
} catch {
Write-Warning "Was not able to remove the old environment variable from PATH. You will need to do this manually"
}

}
}

Copy-Item "$chocolateyPathOld\bin\*" "$chocolateyPath\bin" -force -recurse
Copy-Item "$chocolateyPathOld\lib\*" "$chocolateyPath\lib" -force -recurse

$from = "$chocolateyPathOld\bin"
$to ="$chocolateyPath\bin"
$exclude = @("choco.exe", "chocolatey.exe", "cinst.exe", "clist.exe", "cpack.exe", "cpush.exe", "cuninst.exe", "cup.exe", "cver.exe", "RefreshEnv.cmd")
Get-ChildItem -Path $from -recurse -Exclude $exclude |
% {
Write-Debug "Copying $_ `n to $to"
if ($_.PSIsContainer) {
Copy-Item $_ -Destination (Join-Path $to $_.Parent.FullName.Substring($from.length)) -Force -ErrorAction SilentlyContinue
} else {
$fileToMove = (Join-Path $to $_.FullName.Substring($from.length))
try {
Copy-Item $_ -Destination $fileToMove -Exclude $exclude -Force -ErrorAction Stop
}
catch {
Write-Warning "Was not able to move `'$fileToMove`'. You may need to reinstall the shim"
}
}
}
}
}

function Remove-OldChocolateyInstall {
param(
[string]$chocolateyPathOld = "$sysDrive\Chocolatey"
)

if (Test-Path $chocolateyPathOld) {
Write-Warning "This action will result in Log Errors, you can safely ignore those. `n You may need to finish removing '$chocolateyPathOld' manually."
try {
Write-Output "Attempting to remove `'$chocolateyPathOld`'. This may fail if something in the folder is being used or locked."
Remove-Item "$($chocolateyPathOld)" -force -recurse -ErrorAction Stop
Remove-Item "$($chocolateyPathOld)" -force -recurse -ErrorAction Continue
}
catch {
Write-Warning "Was not able to remove `'$chocolateyPathOld`'. You will need to manually remove it."
Expand All @@ -223,7 +260,7 @@ param(
Write-Debug "Removing install files in chocolateyInstall, helpers, redirects, and tools"
"$chocolateyPath\chocolateyInstall", "$chocolateyPath\helpers", "$chocolateyPath\redirects", "$chocolateyPath\tools" | % {
if (Test-Path $_) {
Remove-Item $_ -exclude *.log -recurse -force -ErrorAction SilentlyContinue
Remove-Item $_ -exclude '*.log' -recurse -force -ErrorAction SilentlyContinue
}
}

Expand Down

0 comments on commit 050a28a

Please sign in to comment.