Skip to content

Commit

Permalink
(chocolateyGH-90) Update Get-ChocoInstalledPackage to Cache to Progra…
Browse files Browse the repository at this point in the history
…mData

Get-ChocoInstalledPackage must run and parse choco list -lo for every package managed by DSC

This commit ensures a cChoco folder in ProgramData and exports the custom object to Clixml, and will simply read it from file if last write time is less that 60 Seconds ago.
  • Loading branch information
jrdnr committed Aug 29, 2017
1 parent 52c9d7b commit ef84fb7
Showing 1 changed file with 103 additions and 85 deletions.
188 changes: 103 additions & 85 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ function Set-TargetResource
$Version,
[ValidateNotNullOrEmpty()]
[string]
$Source,
[String]
$Source,
[String]
$chocoParams,
[bool]
[bool]
$AutoUpgrade = $false
)
Write-Verbose -Message 'Start Set-TargetResource'
Expand Down Expand Up @@ -121,11 +121,11 @@ function Test-TargetResource
$Version,
[ValidateNotNullOrEmpty()]
[string]
$Source,
[ValidateNotNullOrEmpty()]
[String]
$Source,
[ValidateNotNullOrEmpty()]
[String]
$chocoParams,
[bool]
[bool]
$AutoUpgrade = $false
)

Expand Down Expand Up @@ -156,7 +156,7 @@ function Test-TargetResource
} else {
$result = $isInstalled
}
}
}

Return $result
}
Expand All @@ -178,7 +178,7 @@ function Test-ChocoInstalled

Function Test-Command
{
[CmdletBinding()]
[CmdletBinding()]
[OutputType([bool])]
Param (
[string]$command = 'choco'
Expand All @@ -193,39 +193,39 @@ Function Test-Command
}
}

function InstallPackage
{
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$pVersion,
[Parameter(Position=3)]
[string]$cParams
)

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')

[string]$chocoinstallparams = '-y'
if ($pParams) {
$chocoinstallparams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoinstallparams += " --version=`"$pVersion`""
}
if ($cParams) {
$chocoinstallparams += " $cParams"
}
Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'"

$packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams"
Write-Verbose -Message "Package output $packageInstallOuput "

#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
function InstallPackage
{
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$pVersion,
[Parameter(Position=3)]
[string]$cParams
)

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')

[string]$chocoinstallparams = '-y'
if ($pParams) {
$chocoinstallparams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoinstallparams += " --version=`"$pVersion`""
}
if ($cParams) {
$chocoinstallparams += " $cParams"
}
Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'"

$packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams"
Write-Verbose -Message "Package output $packageInstallOuput "

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

function UninstallPackage
Expand Down Expand Up @@ -328,49 +328,67 @@ function global:Write-Host
Write-Verbose -Message $Object
}

Function Upgrade-Package {
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')]
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$cParams
)

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Path variables: $env:Path"

[string]$chocoupgradeparams = '-dv -y'
if ($pParams) {
$chocoupgradeparams += " --params=`"$pParams`""
}
if ($cParams) {
$chocoupgradeparams += " $cParams"
}
$cmd = "choco upgrade -dv -y $pName $chocoupgradeparams"
Write-Verbose -Message "Upgrade command: '$cmd'"

if (-not (IsPackageInstalled -pName $pName))
{
throw "$pName is not installed, you cannot upgrade"
}

$packageUpgradeOuput = Invoke-Expression -Command $cmd
$packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ }
}

function Get-ChocoInstalledPackage {
$res = choco list -lo | ForEach-Object {
$Obj = $_ -split '\s'
[pscustomobject]@{
'Name' = $Obj[0]
'Version' = $Obj[1]
}
}
Return $res
Function Upgrade-Package {
[Diagnostics.CodeAnalysis.SuppressMessage('PSUseApprovedVerbs','')]
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
[Parameter(Position=1)]
[string]$pParams,
[Parameter(Position=2)]
[string]$cParams
)

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Path variables: $env:Path"

[string]$chocoupgradeparams = '-dv -y'
if ($pParams) {
$chocoupgradeparams += " --params=`"$pParams`""
}
if ($cParams) {
$chocoupgradeparams += " $cParams"
}
$cmd = "choco upgrade -dv -y $pName $chocoupgradeparams"
Write-Verbose -Message "Upgrade command: '$cmd'"

if (-not (IsPackageInstalled -pName $pName))
{
throw "$pName is not installed, you cannot upgrade"
}

$packageUpgradeOuput = Invoke-Expression -Command $cmd
$packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ }
}

function Get-ChocoInstalledPackage {
$res = $null
$dateMinus = (Get-Date).AddSeconds('-60')

$path = 'C:\ProgramData\cChoco'
$childPath = 'ChocoInstalled.xml'
$ChocoInstallList = Join-Path -Path $path -ChildPath $childPath

$lastWriteTime = (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime

if ( $dateMinus -lt $lastWriteTime ) {
$res = Import-Clixml $ChocoInstallList
}
if (-not ( Test-Path $path )){
New-Item -Path $path -ItemType Directory |Out-Null
}
if (-not $res){
$res = choco list -lo | ForEach-Object {
$Obj = $_ -split '\s'
[pscustomobject]@{
'Name' = $Obj[0]
'Version' = $Obj[1]
}
}
Export-Clixml -path $ChocoInstallList -InputObject $res
}
Return $res
}

Export-ModuleMember -Function *-TargetResource

0 comments on commit ef84fb7

Please sign in to comment.