Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-95) Allow for upgrading of packages #134

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AppVeyor/AppVeyorInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ Write-Host "Installed NuGet version '$($pkg.version)'"
# Install Modules #
#---------------------------------#
[version]$ScriptAnalyzerVersion = '1.8.1'
[version]$PesterVersion = '4.10.1'
Install-Module -Name 'PSScriptAnalyzer' -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $ScriptAnalyzerVersion
Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop
Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $PesterVersion
Install-Module -Name 'xDSCResourceDesigner' -Repository PSGallery -Force -ErrorAction Stop

#---------------------------------#
Expand Down
49 changes: 43 additions & 6 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,28 @@ function Set-TargetResource
$whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Installing / upgrading package from Chocolatey')
if ($whatIfShouldProcess) {
if ($Version) {
Write-Verbose -Message "Uninstalling $Name due to version mis-match"
UninstallPackage -pName $Name -pParams $Params
Write-Verbose -Message "Re-Installing $Name with correct version $versionToInstall"
InstallPackage -pName $Name -pParams $Params -pVersion $versionToInstall -pSource $Source -cParams $chocoParams
# Version check on the existing package to ensure we don't attempt to upgrade in a downgrade situation
$installedPackages = @(Get-ChocoInstalledPackage | Where-object { $_.Name -eq $Name })
$packageCount = $installedPackages.Count
Write-Verbose "Found $packageCount matching package(s) for upgrade"
$canUpgrade = $packageCount -eq 1
if ($canUpgrade) {
[Version] $installedVersion = Get-VersionCore -Version $installedPackages[0].Version
[Version] $targetVersion = Get-VersionCore -Version $Version

$canUpgrade = $installedVersion.CompareTo($targetVersion) -le 0
}

if ($canUpgrade) {
Write-Verbose -Message "Upgrading $Name to version $Version"
Upgrade-Package -pName $Name -pParams $Params -pVersion $Version
}
else {
Write-Verbose -Message "Uninstalling $Name due to version mis-match"
UninstallPackage -pName $Name -pParams $Params
Write-Verbose -Message "Re-Installing $Name with correct version $version"
InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams
}
}
elseif ($MinimumVersion) {
Write-Verbose -Message "Upgrading $Name because installed version is lower that the specified minimum"
Expand Down Expand Up @@ -443,7 +461,7 @@ function global:Write-Host
Write-Verbose -Message $Object
}

Function Upgrade-Package {
function Upgrade-Package {
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')]
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
Expand All @@ -454,7 +472,9 @@ Function Upgrade-Package {
[Parameter(Position=2)]
[string]$pSource,
pauby marked this conversation as resolved.
Show resolved Hide resolved
[Parameter(Position=3)]
[string]$cParams
[string]$cParams,
[Parameter(Position=4)]
[string]$pVersion
Comment on lines +476 to +477
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at line 146 above you've got:

$chocoParams += " --version='$versionToInstall'"
Upgrade-Package -pName $Name -pParams $Params -pSource $Source -cParams $chocoParams

The version parameter is added in the calling code by adding it to -cParams parameter. So I'm unsure why we need a specific parameter here that does nothing more than add it into the Choco parameters?

)

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Expand All @@ -464,6 +484,9 @@ Function Upgrade-Package {
if ($pParams) {
$chocoParams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoParams += " --version=`"$pVersion`""
}
Comment on lines +487 to +489
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code I was talking about above.

if ($pSource) {
$chocoParams += " --source=`"$pSource`""
}
Expand Down Expand Up @@ -550,4 +573,18 @@ function Get-ChocoVersion {
Return $res
}

function Get-VersionCore {
[CmdletBinding()]
param (
[string]$Version
)

[Version] $versionCore = $null
if ($Version -match '(?<Major>[1-9]\d*)(\.(?<Minor>[0-9]*))?(\.(?<Patch>[0-9]*))?(\.(?<Segment>[0-9]*))?([-+].*)?') {
$versionCore = New-Object System.Version($Matches.Major, $Matches.Minor, $Matches.Patch, $Matches.Segment)
}

$versionCore
}
Comment on lines +576 to +588
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't compare prerelease versions so 3.0.0-beta1 and 3.0.0-beta2 are the same. So in this instance the code calling it would actually uninstall 3.0.0-beta1 and install 3.0.0-beta2 rather than upgrade.


Export-ModuleMember -Function *-TargetResource