-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathGetInstalledPSResource.cs
195 lines (171 loc) · 7.19 KB
/
GetInstalledPSResource.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
/// <summary>
/// It retrieves a resource that was installed with Install-PSResource
/// Returns a single resource or multiple resource.
/// </summary>
[Cmdlet(VerbsCommon.Get, "InstalledPSResource")]
[Alias("Get-PSResource")]
[OutputType(typeof(PSResourceInfo))]
public sealed class GetInstalledPSResourceCommand : PSCmdlet
{
#region Members
private VersionRange _versionRange;
private List<string> _pathsToSearch;
#endregion
#region Parameters
/// <summary>
/// Specifies the desired name for the resource to look for.
/// </summary>
[SupportsWildcards]
[Parameter(Position = 0, ValueFromPipeline = true)]
public string[] Name { get; set; }
/// <summary>
/// Specifies the version of the resource to include to look for.
/// </summary>
[SupportsWildcards]
[Parameter]
[ValidateNotNullOrEmpty()]
public string Version { get; set; }
/// <summary>
/// Specifies the path to look in.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string Path { get; set; }
/// <summary>
/// Specifies the scope of installation.
/// </summary>
[Parameter]
public ScopeType Scope { get; set; }
#endregion
#region Methods
protected override void BeginProcessing()
{
// Validate that if a -Version param is passed in that it can be parsed into a NuGet version range.
// an exact version will be formatted into a version range.
if (Version == null)
{
WriteDebug("Searcing for all versions");
_versionRange = VersionRange.All;
}
else if (!Utils.TryParseVersionOrVersionRange(Version, out _versionRange))
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException("Argument for -Version parameter is not in the proper format."),
"IncorrectVersionFormat",
ErrorCategory.
InvalidArgument,
this));
}
// Determine paths to search.
_pathsToSearch = new List<string>();
if (Path != null)
{
WriteDebug($"Provided path is: '{Path}'");
var resolvedPaths = GetResolvedProviderPathFromPSPath(Path, out ProviderInfo provider);
if (resolvedPaths.Count != 1)
{
ThrowTerminatingError(new ErrorRecord(
new PSArgumentException($"Error: Could not resolve provided Path argument '{Path}' into a single path."),
"ErrorInvalidPathArgument",
ErrorCategory.InvalidArgument,
this));
}
var resolvedPath = resolvedPaths[0];
WriteDebug($"Provided resolved path is '{resolvedPath}'");
var versionPaths = Utils.GetSubDirectories(resolvedPath);
if (versionPaths.Length == 0)
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException($"Error cannot find expected subdirectories in provided path: {Path}"),
"PathMissingExpectedSubdirectories",
ErrorCategory.InvalidOperation,
this));
}
_pathsToSearch.AddRange(versionPaths);
}
else
{
// retrieve all possible paths
_pathsToSearch = Utils.GetAllResourcePaths(this, Scope);
}
}
protected override void ProcessRecord()
{
var namesToSearch = Utils.ProcessNameWildcards(Name, removeWildcardEntries: false, out string[] errorMsgs, out bool _);
foreach (string error in errorMsgs)
{
WriteError(new ErrorRecord(
new PSInvalidOperationException(error),
"ErrorFilteringNamesForUnsupportedWildcards",
ErrorCategory.InvalidArgument,
this));
}
// This catches the case where Name wasn't passed in as null or empty,
// but after filtering out unsupported wildcard names in BeginProcessing() there are no elements left in Name.
if (namesToSearch.Length == 0)
{
WriteDebug("Names were not provided or could not be resolved");
return;
}
// SelectPrereleaseOnly is false because we want both stable and prerelease versions all the time..
GetHelper getHelper = new GetHelper(this);
List<string> pkgsFound = new List<string>();
foreach (PSResourceInfo pkg in getHelper.GetPackagesFromPath(
name: namesToSearch,
versionRange: _versionRange,
pathsToSearch: _pathsToSearch,
selectPrereleaseOnly: false))
{
pkgsFound.Add(pkg.Name);
WriteObject(pkg);
}
List<string> pkgsNotFound = new List<string>();
foreach (string name in namesToSearch)
{
if (!pkgsFound.Contains(name, StringComparer.OrdinalIgnoreCase))
{
if (name.Contains('*'))
{
WildcardPattern nameWildCardPattern = new WildcardPattern(name, WildcardOptions.IgnoreCase);
bool foundWildcardMatch = false;
foreach (string pkgFound in pkgsFound)
{
if (nameWildCardPattern.IsMatch(pkgFound))
{
foundWildcardMatch = true;
break;
}
}
if (!foundWildcardMatch)
{
pkgsNotFound.Add(name);
}
}
else
{
pkgsNotFound.Add(name);
}
}
}
if (pkgsNotFound.Count > 0)
{
WriteError(new ErrorRecord(
new ResourceNotFoundException($"No match was found for package '{string.Join(", ", pkgsNotFound)}'."),
"InstalledPackageNotFound",
ErrorCategory.ObjectNotFound,
this));
}
}
#endregion
}
}