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-83) Turn off download progress #101

Merged
merged 9 commits into from
Jul 28, 2019
118 changes: 86 additions & 32 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -226,33 +226,39 @@ function InstallPackage

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

[string]$chocoinstallparams = '-y'
[string]$chocoParams = '-y'
if ($pParams) {
$chocoinstallparams += " --params=`"$pParams`""
$chocoParams += " --params=`"$pParams`""
}
if ($pVersion) {
$chocoinstallparams += " --version=`"$pVersion`""
$chocoParams += " --version=`"$pVersion`""
}
if ($pSource) {
$chocoinstallparams += " --source=`"$pSource`""
$chocoParams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoinstallparams += " $cParams"
$chocoParams += " $cParams"
}
# Check if Chocolatey version is Greater than 0.10.4, and add --no-progress
if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){
$chocoParams += " --no-progress"
}
Write-Verbose -Message "Install command: 'choco install $pName $chocoinstallparams'"

$packageInstallOuput = Invoke-Expression -Command "choco install $pName $chocoinstallparams"
Write-Verbose -Message "Package output $packageInstallOuput "
$cmd = "choco install $pName $chocoParams"
Write-Verbose -Message "Install command: '$cmd'"
$packageInstallOuput = Invoke-Expression -Command $cmd
Write-Verbose -Message "Package output $packageInstallOuput"

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'
Get-ChocoInstalledPackage -Purge

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

function UninstallPackage
{
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingInvokeExpression','')]
param(
[Parameter(Position=0,Mandatory)]
[string]$pName,
Expand All @@ -262,22 +268,26 @@ function UninstallPackage

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

#Todo: Refactor
if (-not ($pParams))
{
Write-Verbose -Message 'Uninstalling Package Standard'
$packageUninstallOuput = choco uninstall $pName -y
[string]$chocoParams = "-y"
if ($pParams) {
$chocoParams += " --params=`"$pParams`""
}
elseif ($pParams)
{
Write-Verbose -Message "Uninstalling Package with params $pParams"
$packageUninstallOuput = choco uninstall $pName --params="$pParams" -y
if ($pVersion) {
$chocoParams += " --version=`"$pVersion`""
}
# Check if Chocolatey version is Greater than 0.10.4, and add --no-progress
if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){
$chocoParams += " --no-progress"
}

$cmd = "choco uninstall $pName $chocoParams"
Write-Verbose -Message "Uninstalling $pName with: '$cmd'"
$packageUninstallOuput = Invoke-Expression -Command $cmd

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

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'
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 @@ -325,14 +335,15 @@ Function Test-LatestVersionInstalled {
)
Write-Verbose -Message "Testing if $pName can be upgraded"

[string]$chocoupgradeparams = '--noop'
[string]$chocoParams = '--noop'
if ($pSource) {
$chocoupgradeparams += " --source=`"$pSource`""
$chocoParams += " --source=`"$pSource`""
}

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

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

if ($packageUpgradeOuput -match "$pName.*is the latest version available based on your source") {
Expand Down Expand Up @@ -379,17 +390,22 @@ Function Upgrade-Package {
$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')
Write-Verbose -Message "Path variables: $env:Path"

[string]$chocoupgradeparams = '-dv -y'
[string]$chocoParams = '-dv -y'
if ($pParams) {
$chocoupgradeparams += " --params=`"$pParams`""
$chocoParams += " --params=`"$pParams`""
}
if ($pSource) {
$chocoupgradeparams += " --source=`"$pSource`""
$chocoParams += " --source=`"$pSource`""
}
if ($cParams) {
$chocoupgradeparams += " $cParams"
$chocoParams += " $cParams"
}
# Check if Chocolatey version is Greater than 0.10.4, and add --no-progress
if ((Get-ChocoVersion) -ge [System.Version]('0.10.4')){
$chocoParams += " --no-progress"
}
$cmd = "choco upgrade $pName $chocoupgradeparams"

$cmd = "choco upgrade $pName $chocoParams"
Write-Verbose -Message "Upgrade command: '$cmd'"

if (-not (IsPackageInstalled -pName $pName))
Expand All @@ -401,29 +417,67 @@ Function Upgrade-Package {
$packageUpgradeOuput | ForEach-Object { Write-Verbose -Message $_ }

# Clear Package Cache
Get-ChocoInstalledPackage 'Purge'
Get-ChocoInstalledPackage -Purge
}

function Get-ChocoInstalledPackage ($action) {
function Get-ChocoInstalledPackage {
[CmdletBinding()]
param (
[switch]$Purge,
[switch]$NoCache
)

$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') {
if ($Purge.IsPresent) {
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
$res = choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|"
if ( -not $NoCache){
$res | Export-Clixml -Path $ChocoInstallList
}
}
}

Return $res
}

function Get-ChocoVersion {
[CmdletBinding()]
param (
[switch]$Purge,
[switch]$NoCache
)

$chocoInstallCache = Join-Path -Path $env:ChocolateyInstall -ChildPath 'cache'
if ( -not (Test-Path $chocoInstallCache)){
New-Item -Name 'cache' -Path $env:ChocolateyInstall -ItemType Directory | Out-Null
}
$chocoVersion = Join-Path -Path $chocoInstallCache -ChildPath 'ChocoVersion.xml'

if ($Purge.IsPresent) {
Remove-Item $chocoVersion -Force
$res = $true
} else {
$cacheSec = (Get-Date).AddSeconds('-60')
if ( $cacheSec -lt (Get-Item $chocoVersion -ErrorAction SilentlyContinue).LastWriteTime ) {
$res = Import-Clixml $chocoVersion
} else {
$cmd = choco -v
$res = [System.Version]($cmd.Split('-')[0])
$res | Export-Clixml -Path $chocoVersion
}
}
Return $res
}

Export-ModuleMember -Function *-TargetResource