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

F/findchocopkg #75

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Creating `Find-ChocolateyPackage` as choco list for remote source will be deprecated in choco CLI 2.0.
- Fixing the `Get-ChocolateyPackage` calls to use `-localOnly` or to use `Find-ChocolateyPackage` instead.
- Modified project with new Sampler template.
- Invoking choco commands now always add `--no-progress` & `--limit-output`.
- Limiting Get-Command choco to the first result as per [#69](https://github.com/chocolatey-community/Chocolatey/issues/69) on all calls.
Expand Down
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BuildWorkflow:

ipmopsdsc1: |
{
Import-Module -Name PSDesiredStateConfiguration -MaximumVersion 1.99 -Passthru
Import-Module -Name PSDesiredStateConfiguration -MaximumVersion 1.99 -Passthru
}
gcpol:
- build_guestconfiguration_packages
Expand Down
2 changes: 1 addition & 1 deletion source/Classes/002.ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ChocolateyPackage
}

Write-Debug -Message (' Searching with ''Get-ChocolateyPackage'' and parameters {0}' -f ($searchVersionParam | ConvertTo-Json -Depth 3))
$refVersionPackage = Get-ChocolateyPackage @searchVersionParam
$refVersionPackage = Find-ChocolateyPackage @searchVersionParam

if ($null -eq $refVersionPackage)
{
Expand Down
2 changes: 1 addition & 1 deletion source/public/Add-ChocolateyPin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Add-ChocolateyPin

process
{
if (-not (Get-ChocolateyPackage -Name $Name -Exact))
if (-not (Get-ChocolateyPackage -LocalOnly -Name $Name -Exact))
{
throw ('Chocolatey Package ''{0}'' cannot be found.' -f $Name)
}
Expand Down
157 changes: 157 additions & 0 deletions source/public/Find-ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

<#
.SYNOPSIS
List the packages from a source.

.DESCRIPTION
This command can list the packages available on the configured source or a specified one.
You can also use this command to search for a specific package, and specific version.

.PARAMETER Name
Name or part of the name of the Package to search on the source(s).

.PARAMETER Version
Version of the package you're looking for.

.PARAMETER IdOnly
Id Only - Only return Package Ids in the list results. Available in 0.1-0.6+.

.PARAMETER Prerelease
Prerelease - Include Prereleases? Defaults to false

.PARAMETER ApprovedOnly
ApprovedOnly - Only return approved packages - this option will filter
out results not from the community repository (https://chocolatey.org/packages). Available in 0.9.10+

.PARAMETER ByIdOnly
ByIdOnly - Only return packages where the id contains the search filter.
Available in 0.9.10+.

.PARAMETER IdStartsWith
IdStartsWith - Only return packages where the id starts with the search
filter. Available in 0.9.10+.

.PARAMETER NoProgress
Do Not Show Progress - Do not show download progress percentages.

.PARAMETER Exact
Exact - Only return packages with this exact name. Available in 0.9.10+.

.PARAMETER Source
Source - Source location for install. Can use special 'webpi' or 'windowsfeatures' sources. Defaults to sources.

.PARAMETER Credential
Credential used with authenticated feeds. Defaults to empty.

.PARAMETER CacheLocation
CacheLocation - Location for download cache, defaults to %TEMP% or value in chocolatey.config file.

.EXAMPLE
Get-ChocolateyPackage -LocalOnly chocolatey

.NOTES
https://github.com/chocolatey/choco/wiki/CommandsList
#>
function Find-ChocolateyPackage
{
[CmdletBinding()]
param
(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Version,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$IdOnly,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$Prerelease,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[Switch]
$ApprovedOnly,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$ByIdOnly,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[Switch]
$IdStartsWith,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$NoProgress,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$Exact,

[Parameter(ValueFromPipelineByPropertyName = $true)]
$Source,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[PSCredential]
$Credential,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[System.String]
$CacheLocation
)

process
{
if (-not ($chocoCmd = @(Get-Command 'choco.exe' -CommandType 'Application' -ErrorAction 'SilentlyContinue')[0]))
{
throw "Chocolatey Software not found."
}

$ChocoArguments = @('search', '-r')
$paramKeys = [Array]::CreateInstance([string], $PSboundparameters.Keys.count)
$PSboundparameters.Keys.CopyTo($paramKeys, 0)
switch ($paramKeys)
{
'verbose'
{
$null = $PSBoundParameters.remove('Verbose')
}
'debug'
{
$null = $PSBoundParameters.remove('debug')
}
'Name'
{
$null = $PSBoundParameters.remove('Name')
}
'Exact'
{
$null = $PSBoundParameters.remove('Exact')
}
}

$ChocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters
Write-Verbose "choco $($ChocoArguments -join ' ')"

if ($ChocoArguments -contains '--verbose')
{
$ChocoArguments = [System.Collections.ArrayList]$ChocoArguments
$ChocoArguments.remove('--verbose')
}

Write-Debug -Message "Running from command without caching."
$ChocoListOutput = &$chocoCmd $ChocoArguments $Name $( if ($Exact)
{
'--exact'
} )

$ChocoListOutput | ConvertFrom-Csv -Delimiter '|' -Header 'Name', 'Version'
}
}
14 changes: 7 additions & 7 deletions source/public/Get-ChocolateyPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ function Get-ChocolateyPackage
[String]
$Name,

[Parameter(
, ValueFromPipelineByPropertyName
)]
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Version,
Expand Down Expand Up @@ -168,14 +166,16 @@ function Get-ChocolateyPackage
return
}

$cachePath = [io.path]::Combine($chocoInstallPath, 'cache', 'GetChocolateyPackageCache.xml')
$cacheFolder = Join-Path -Path $chocoInstallPath -ChildPath 'cache'
$cachePath = Join-Path -Path $cacheFolder -ChildPath 'GetChocolateyPackageCache.xml'

try
{
if (-not (Test-Path -Path $CacheFolder))
if (-not (Test-Path -Path $cacheFolder))
{
$null = New-Item -Type Directory -Path $CacheFolder -Force -ErrorAction Stop
$null = New-Item -Type Directory -Path -Force -ErrorAction Stop
}
if (Test-Path -Path $CachePath)
elseif (Test-Path -Path $CachePath)
{
$CachedFile = Get-Item $CachePath
}
Expand Down
2 changes: 1 addition & 1 deletion source/public/Remove-ChocolateyPin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Remove-ChocolateyPin
throw "Chocolatey Software not found."
}

if (-not (Get-ChocolateyPackage -Name $Name))
if (-not (Get-ChocolateyPackage -LocalOnly -Name $Name))
{
throw "The Pin for Chocolatey Package $Name cannot be found."
}
Expand Down
56 changes: 24 additions & 32 deletions source/public/Test-ChocolateyPackageIsInstalled.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,33 @@ function Test-ChocolateyPackageIsInstalled
{
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
param (
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName)]
param
(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
[String]
$Name,

[Parameter(
ValueFromPipelineByPropertyName
)]
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
[String]
$Version,

[Parameter(
ValueFromPipelineByPropertyName
)]
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$Source,

[Parameter(
ValueFromPipelineByPropertyName
)]
[Parameter(ValueFromPipelineByPropertyName = $true)]
[PSCredential]
$Credential,

[Parameter(
ValueFromPipelineByPropertyName
)]
[System.String]
[Parameter(ValueFromPipelineByPropertyName = $true)]
[String]
$CacheLocation,

[Parameter(
ValueFromPipelineByPropertyName
)]
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$UpdateOnly

)

process
Expand All @@ -85,9 +76,9 @@ function Test-ChocolateyPackageIsInstalled
}

#if version latest verify against sources
if (! ($InstalledPackages = @(Get-ChocolateyPackage -LocalOnly -Name $Name -Exact)) )
if (-not ($InstalledPackages = @(Get-ChocolateyPackage -LocalOnly -Name $Name -Exact)) )
{
Write-Verbose "Could not find Package $Name."
Write-Verbose -Message "Could not find Package $Name."
}

$SearchPackageParams = $PSBoundParameters
Expand All @@ -96,8 +87,9 @@ function Test-ChocolateyPackageIsInstalled

if ($Version -eq 'latest')
{
$ReferenceObject = Get-ChocolateyPackage @SearchPackageParams -Exact
if (!$ReferenceObject)
Write-Verbose -Message ('Finding latest version of package {0}' -f $Name)
$ReferenceObject = Find-ChocolateyPackage @SearchPackageParams -Exact
if (-not $ReferenceObject)
{
throw "Latest version of Package $name not found. Verify that the sources are reachable and package exists."
}
Expand All @@ -107,6 +99,7 @@ function Test-ChocolateyPackageIsInstalled
$ReferenceObject = [PSCustomObject]@{
Name = $Name
}

if ($Version)
{
$ReferenceObject | Add-Member -MemberType NoteProperty -Name version -value $Version
Expand All @@ -115,13 +108,13 @@ function Test-ChocolateyPackageIsInstalled

$PackageFound = $false
$MatchingPackages = $InstalledPackages | Where-Object {
Write-Debug "Testing $($_.Name) against $($ReferenceObject.Name)"
Write-Debug -Message "Testing $($_.Name) against $($ReferenceObject.Name)"
if ($_.Name -eq $ReferenceObject.Name)
{
$PackageFound = $True
Write-Debug "Package Found"

if (!$Version)
if (-not $Version)
{
return $true
}
Expand Down Expand Up @@ -157,10 +150,9 @@ function Test-ChocolateyPackageIsInstalled
$VersionGreaterOrEqual = $False
}

Write-Output (
[PSCustomObject]@{
PackagePresent = $PackageFound
VersionGreaterOrEqual = $VersionGreaterOrEqual
})
[PSCustomObject]@{
PackagePresent = $PackageFound
VersionGreaterOrEqual = $VersionGreaterOrEqual
}
}
}