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-44) Pass the Source correctly to choco #84

Merged
Merged
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
42 changes: 30 additions & 12 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ function Set-TargetResource
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 -cParams $chocoParams
InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams
} elseif ($AutoUpgrade) {
Write-Verbose -Message "Upgrading $Name due to version mis-match"
Upgrade-Package -pName $Name -pParams $Params
Upgrade-Package -pName $Name -pParams $Params -pSource $Source
}
}
}
} else {
$whatIfShouldProcess = $pscmdlet.ShouldProcess("$Name", 'Install package from Chocolatey')
if ($whatIfShouldProcess) {
InstallPackage -pName $Name -pParams $Params -pVersion $Version -cParams $chocoParams
InstallPackage -pName $Name -pParams $Params -pVersion $Version -pSource $Source -cParams $chocoParams
}
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ function Test-TargetResource
Write-Verbose -Message "Checking if $Name is installed"

if ($AutoUpgrade -and $isInstalled) {
$result = Test-LatestVersionInstalled -pName $Name
$result = Test-LatestVersionInstalled -pName $Name -pSource $Source
} else {
$result = $isInstalled
}
Expand Down Expand Up @@ -204,6 +204,8 @@ function InstallPackage
[Parameter(Position=2)]
[string]$pVersion,
[Parameter(Position=3)]
[string]$pSource,
[Parameter(Position=4)]
[string]$cParams
)

Expand All @@ -216,6 +218,9 @@ function InstallPackage
if ($pVersion) {
$chocoinstallparams += " --version=`"$pVersion`""
}
if ($pSource) {
$chocoinstallparams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoinstallparams += " $cParams"
}
Expand Down Expand Up @@ -251,14 +256,12 @@ function UninstallPackage
$packageUninstallOuput = choco uninstall $pName --params="$pParams" -y
}


Write-Verbose -Message "Package uninstall output $packageUninstallOuput "

#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
}


function IsPackageInstalled
{
param(
Expand Down Expand Up @@ -292,16 +295,26 @@ function IsPackageInstalled
}

Function Test-LatestVersionInstalled {
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
Copy link
Member

Choose a reason for hiding this comment

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

??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the use of Invoke-Expression to Test-LatestVersionInstalled, since the other internal functions (InstallPackage / Upgrade-Package), seem to use Invoke-Expression as well and that specific PSScriptAnalyzer rule will cause the build to fail.
I don't mind changing all uses of Invoke-Expression to Start-Process in this pull request, but this might be better to do in a different commit, just let me know.

param(
[Parameter(Position=0,Mandatory)]
[string]$pName
[Parameter(Mandatory)]
[string]$pName,
[Parameter(Mandatory)]
[string]$pSource
)
Write-Verbose -Message "Testing if $pName can be upgraded"

$queryres = choco upgrade $pName --noop | Select-String -Pattern $pName
$queryres | ForEach-Object {Write-Verbose -Message $_}
[string]$chocoupgradeparams = '--noop'
if ($pSource) {
$chocoupgradeparams += " --source=`"$pSource`""
}

Write-Verbose -Message "Testing if $pName can be upgraded: 'choco upgrade $pName $chocoupgradeparams'"

$packageUpgradeOuput = Invoke-Expression -Command "choco upgrade $pName $chocoupgradeparams"
$packageUpgradeOuput | ForEach-Object {Write-Verbose -Message $_}

if ($queryres -match "$pName.*is the latest version available based on your source") {
if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") {
return $true
}
return $false
Expand Down Expand Up @@ -337,6 +350,8 @@ Function Upgrade-Package {
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$pSource,
[Parameter(Position=3)]
[string]$cParams
)

Expand All @@ -347,10 +362,13 @@ Function Upgrade-Package {
if ($pParams) {
$chocoupgradeparams += " --params=`"$pParams`""
}
if ($pSource) {
$chocoupgradeparams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoupgradeparams += " $cParams"
}
$cmd = "choco upgrade -dv -y $pName $chocoupgradeparams"
$cmd = "choco upgrade $pName $chocoupgradeparams"
Copy link
Member

Choose a reason for hiding this comment

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

👍 on removing duplicates

Write-Verbose -Message "Upgrade command: '$cmd'"

if (-not (IsPackageInstalled -pName $pName))
Expand Down