Skip to content

Commit

Permalink
Merge pull request #92 from jrdnr/(GH-90)_Cache_InstalledPackages_for…
Browse files Browse the repository at this point in the history
…_faster_run

(GH-90) Implemented Caching Package list to File
  • Loading branch information
ferventcoder authored Dec 1, 2017
2 parents 6b8f5a5 + 8542f18 commit b20d1e4
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 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 @@ -244,6 +244,9 @@ function InstallPackage
$packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams"
Write-Verbose -Message "Package output $packageInstallOuput "

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'

#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
}
Expand Down Expand Up @@ -273,6 +276,9 @@ function UninstallPackage

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

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'

#refresh path varaible in powershell, as choco doesn"t, to pull in git
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
}
Expand Down Expand Up @@ -393,10 +399,31 @@ Function Upgrade-Package {

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

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'
}

function Get-ChocoInstalledPackage {
Return (choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|")
function Get-ChocoInstalledPackage ($action) {
$ChocoInstallLP = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache'
if ( -not (Test-Path $ChocoInstallLP)){
New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null
}
$ChocoInstallList = Join-Path -Path $ChocoInstallLP -ChildPath 'ChocoInstalled.xml'

if ($action -eq 'Purge') {
Remove-Item $ChocoInstallList -Force
$res = $true
} else {
$PackageCacheSec = (Get-Date).AddSeconds('-60')
if ( $PackageCacheSec -lt (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime ) {
$res = Import-Clixml $ChocoInstallList
} else {
choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|" -OutVariable res | Export-Clixml -Path $ChocoInstallList
}
}

Return $res
}

Export-ModuleMember -Function *-TargetResource

0 comments on commit b20d1e4

Please sign in to comment.