-
Notifications
You must be signed in to change notification settings - Fork 743
Expand file tree
/
Copy pathFindPackageCommand.cs
More file actions
216 lines (181 loc) · 7.18 KB
/
FindPackageCommand.cs
File metadata and controls
216 lines (181 loc) · 7.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.VisualStudio.Threading;
using NuGet.ProjectManagement;
using NuGet.Versioning;
using NuGet.VisualStudio;
namespace NuGet.PackageManagement.PowerShellCmdlets
{
/// <summary>
/// FindPackage is similar to GetPackage -ListAvailable, but have the following difference:
/// Without -StartWith present, it find packages by keyword anywhere in the package Id, description or summary.
/// With -StartWith present, it only returns packages with Ids starting with the specified string.
/// </summary>
[Cmdlet(VerbsCommon.Find, "Package")]
[OutputType(typeof(PowerShellPackage))]
public class FindPackageCommand : NuGetPowerShellBaseCommand
{
// NOTE: Number of packages returned by api.nuget.org is static and is 20
// Display the same number of results with other endpoints, such as nuget.org/api/v2, as well
private const int MaxReturnedPackages = 20;
[Parameter(ValueFromPipelineByPropertyName = true, Position = 0)]
public string Id { get; set; }
[Parameter]
[ValidateNotNullOrEmpty]
public string Version { get; set; }
[Parameter]
[ValidateNotNullOrEmpty]
public virtual string Source { get; set; }
[Parameter]
[Alias("Prerelease")]
public SwitchParameter IncludePrerelease { get; set; }
[Parameter]
public SwitchParameter AllVersions { get; set; }
/// <summary>
/// Determines if an exact Id match would be performed with the search results. By default, FindPackage
/// returns all packages that contain Filter value in package ID, description or summary.
/// </summary>
[Parameter]
public SwitchParameter ExactMatch { get; set; }
/// <summary>
/// Find packages by AutoComplete endpoint, starting with Id.
/// Also used for tab expansion.
/// </summary>
[Parameter]
public SwitchParameter StartWith { get; set; }
[Parameter]
[ValidateRange(0, int.MaxValue)]
public virtual int First { get; set; }
[Parameter]
[ValidateRange(0, int.MaxValue)]
public int Skip { get; set; }
protected void Preprocess()
{
// Since this is also used for intellisense, we need to limit the number of packages that we return. Otherwise,
// typing InstallPackage TAB would download the entire feed.
if (First == 0)
{
First = MaxReturnedPackages;
}
if (Id == null)
{
Id = string.Empty;
}
if (Version == null)
{
Version = string.Empty;
}
UpdateActiveSourceRepository(Source);
}
protected override void ProcessRecordCore()
{
Preprocess();
if (StartWith.IsPresent)
{
FindPackageStartWithId(excludeVersionInfo: false);
}
else
{
FindPackagesByPSSearchService();
}
}
private void FindPackagesByPSSearchService()
{
var errors = new List<string>();
var remotePackages = GetPackagesFromRemoteSource(Id, IncludePrerelease.IsPresent, errors.Add);
if (ExactMatch.IsPresent)
{
remotePackages = remotePackages.Where(p => string.Equals(p.Identity.Id, Id, StringComparison.OrdinalIgnoreCase));
}
remotePackages = remotePackages.Skip(Skip).Take(First);
VersionType versionType;
if (AllVersions.IsPresent)
{
versionType = VersionType.All;
}
else
{
versionType = VersionType.Latest;
}
var view = PowerShellRemotePackage.GetPowerShellPackageView(remotePackages, versionType);
foreach (var package in view)
{
// Just start the task and don't wait for it to complete
package.AsyncLazyVersions.GetValueAsync();
}
if (view.Any())
{
WriteObject(view, enumerateCollection: true);
}
foreach (var error in errors)
{
LogCore(MessageLevel.Error, error);
}
}
protected void FindPackageStartWithId(bool excludeVersionInfo)
{
var packageIds = NuGetUIThreadHelper.JoinableTaskFactory.Run(
() => GetPackageIdsFromRemoteSourceAsync(Id, IncludePrerelease.IsPresent));
Token.ThrowIfCancellationRequested();
packageIds = packageIds?.Skip(Skip).Take(First) ?? Enumerable.Empty<string>();
if (excludeVersionInfo)
{
var packages = packageIds.Select(id => new PowerShellPackage { Id = id });
WriteObject(packages, enumerateCollection: true);
return;
}
if (!ExactMatch.IsPresent)
{
var packages = new List<PowerShellPackage>();
foreach (var id in packageIds)
{
var package = GetPowerShellPackageFromRemoteSource(id);
// Just start the task and don't wait for it to complete
package.AsyncLazyVersions.GetValueAsync();
packages.Add(package);
}
WriteObject(packages, enumerateCollection: true);
}
else
{
if (packageIds.Any())
{
var packageId = packageIds.FirstOrDefault(p => string.Equals(p, Id, StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrEmpty(packageId))
{
var package = GetPowerShellPackageFromRemoteSource(packageId);
// Just start the task and don't wait for it to complete
package.AsyncLazyVersions.GetValueAsync();
WriteObject(package);
}
}
}
}
/// <summary>
/// Get IPowerShellPackage from the remote package source
/// </summary>
private PowerShellPackage GetPowerShellPackageFromRemoteSource(string id)
{
var asyncLazyVersions = new AsyncLazy<IEnumerable<NuGetVersion>>(
() => GetPackageVersionsFromRemoteSourceAsync(id, Version, IncludePrerelease.IsPresent),
NuGetUIThreadHelper.JoinableTaskFactory);
var package = new PowerShellPackage();
package.Id = id;
package.AsyncLazyVersions = asyncLazyVersions;
if (AllVersions.IsPresent)
{
package.AllVersions = true;
}
else
{
package.AllVersions = false;
}
return package;
}
}
}