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 Dec 1, 2017
1 parent 6b8f5a5 commit c7c212b
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017 Chocolatey Software, Inc.
# Copyright (c) 2017 Chocolatey Software, Inc.
# Copyright (c) 2013 - 2017 Lawrence Gripper & original authors/contributors from https://github.com/chocolatey/cChoco
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -396,7 +396,32 @@ Function Upgrade-Package {
}

function Get-ChocoInstalledPackage {
Return (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|")
$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 c7c212b

Please sign in to comment.