-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGet-ChocolateyPackage.ps1
More file actions
138 lines (115 loc) · 4.18 KB
/
Get-ChocolateyPackage.ps1
File metadata and controls
138 lines (115 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.SYNOPSIS
List the packages installed on the local machine.
.DESCRIPTION
Retrieve the list of packages installed locally or to search for a specific package
installed, with a specific version.
.PARAMETER Name
Name or part of the name of the Package to search for.
.PARAMETER Version
Version of the package you're looking for.
.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 Exact
Exact - Only return packages with this exact name. Available in 0.9.10+.
.PARAMETER ByPassCache
ByPassCache - Bypass the local cache of packages and get the latest list from
.EXAMPLE
Get-ChocolateyPackage -Name chocolatey
.NOTES
https://github.com/chocolatey/choco/wiki/CommandsList
#>
function Get-ChocolateyPackage
{
[CmdletBinding()]
param
(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Name,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Version,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$ByIdOnly,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[Switch]
$IdStartsWith,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]
$Exact,
[Parameter()]
[switch]
$ByPassCache
)
begin
{
# Validate choco is installed or die. Will load the exe path from module cache.
# Should you want to specify an installation directory,
# you have to call Get-ChocolateyCommand -InstallDir <path> -Force to set the cache.
$chocoCmd = Get-ChocolateyCommand
# This command is PowerShell only (avoiding expensive choco list when validating many packages)
if ($PSBoundParameters.ContainsKey('ByPassCache'))
{
$null = $PSBoundParameters.Remove('ByPassCache')
}
}
process
{
$chocoArguments = @('list')
if (-not [string]::IsNullOrEmpty($Name))
{
$chocoArguments += @($Name)
# looks like choco search --name="chocolatey" does not work anymore
}
if ($PSBoundParameters.ContainsKey('Name'))
{
$null = $PSBoundParameters.Remove('Name')
}
$chocoArguments += Get-ChocolateyDefaultArgument @PSBoundParameters
if ($ByPassCache.IsPresent -or -not $Exact.IsPresent)
{
# bypass caching when requested, or when not searching for an exact match
# (when not doing exact match choco is looking through description)
Write-Debug -Message ('{0} {1}' -f $chocoCmd, $($ChocoArguments -join ' '))
&$chocoCmd $chocoArguments | ConvertFrom-Csv -Delimiter '|' -Header 'Name', 'Version'
}
else
{
try
{
$cachedPackages = Get-ChocolateyPackageCache -ErrorAction Stop
if (-not [string]::IsNullOrEmpty($name))
{
$cachedPackages = $cachedPackages.Where({$_.Name -eq $Name})
}
if (-not [string]::IsNullOrEmpty($Version))
{
$cachedPackages = $cachedPackages.Where({$_.Version -like $Version})
}
$cachedPackages
}
catch
{
Write-Debug -Message ('Failed to use cache...{0}' -f $_.Exception.Message)
Write-Debug -Message ('{0} {1}' -f $chocoCmd, $($ChocoArguments -join ' '))
$outputString = &$chocoCmd $chocoArguments
if ([string]::isNullOrEmpty($outputString))
{
Write-Warning -Message ('No output returned from choco command: {0}' -f $chocoCmd)
}
else
{
ConvertFrom-Csv -Delimiter '|' -Header 'Name', 'Version' -InputObject $outputString
}
}
}
}
}