-
Notifications
You must be signed in to change notification settings - Fork 743
Expand file tree
/
Copy pathGetPackageCommand.cs
More file actions
378 lines (325 loc) · 13.7 KB
/
GetPackageCommand.cs
File metadata and controls
378 lines (325 loc) · 13.7 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// 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.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using NuGet.Packaging;
using NuGet.ProjectManagement;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using NuGet.VisualStudio;
using Task = System.Threading.Tasks.Task;
namespace NuGet.PackageManagement.PowerShellCmdlets
{
/// <summary>
/// This command lists the available packages which are either from a package source or installed in the
/// current solution.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Package", DefaultParameterSetName = ParameterAttribute.AllParameterSets)]
[OutputType(typeof(PowerShellPackage))]
public class GetPackageCommand : NuGetPowerShellBaseCommand
{
private const int DefaultFirstValue = 50;
private bool _enablePaging;
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty]
public string Filter { get; set; }
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Project")]
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }
[Parameter(ParameterSetName = "Remote")]
[Parameter(ParameterSetName = "Updates")]
[ValidateNotNullOrEmpty]
public string Source { get; set; }
[Parameter(Mandatory = true, ParameterSetName = "Remote")]
[Alias("Online", "Remote")]
public SwitchParameter ListAvailable { get; set; }
[Parameter(Mandatory = true, ParameterSetName = "Updates")]
public SwitchParameter Updates { get; set; }
[Parameter(ParameterSetName = "Remote")]
[Parameter(ParameterSetName = "Updates")]
public SwitchParameter AllVersions { get; set; }
[Parameter(ParameterSetName = "Remote")]
[ValidateRange(0, Int32.MaxValue)]
public int PageSize { get; set; }
[Parameter]
[Alias("Prerelease")]
public SwitchParameter IncludePrerelease { get; set; }
[Parameter]
[ValidateRange(0, Int32.MaxValue)]
public virtual int First { get; set; }
[Parameter]
[ValidateRange(0, Int32.MaxValue)]
public int Skip { get; set; }
/// <summary>
/// Determines if local repository are not needed to process this command
/// </summary>
protected bool UseRemoteSourceOnly { get; set; }
/// <summary>
/// Determines if a remote repository will be used to process this command.
/// </summary>
protected bool UseRemoteSource { get; set; }
protected virtual bool CollapseVersions { get; set; }
public List<NuGetProject> Projects { get; private set; }
/// <summary>
/// logging time disabled for tab command
/// </summary>
protected override bool IsLoggingTimeDisabled
{
get
{
return true;
}
}
private void Preprocess()
{
UseRemoteSourceOnly = ListAvailable.IsPresent || (!String.IsNullOrEmpty(Source) && !Updates.IsPresent);
UseRemoteSource = ListAvailable.IsPresent || Updates.IsPresent || !String.IsNullOrEmpty(Source);
CollapseVersions = !AllVersions.IsPresent;
UpdateActiveSourceRepository(Source);
NuGetUIThreadHelper.JoinableTaskFactory.Run(async () =>
{
await GetNuGetProjectAsync(ProjectName);
// When ProjectName is not specified, get all of the projects in the solution
if (string.IsNullOrEmpty(ProjectName))
{
Projects = (await VsSolutionManager.GetNuGetProjectsAsync()).ToList();
}
else
{
Projects = new List<NuGetProject> { Project };
}
});
}
protected override void ProcessRecordCore()
{
Preprocess();
// If Remote & Updates set of parameters are not specified, list the installed package.
if (!UseRemoteSource)
{
CheckSolutionState();
var packagesToDisplay = NuGetUIThreadHelper.JoinableTaskFactory.Run(
() => GetInstalledPackagesAsync(Projects, Filter, Skip, First, Token));
WriteInstalledPackages(packagesToDisplay);
}
else
{
if (PageSize != 0)
{
_enablePaging = true;
First = PageSize;
}
else if (First == 0)
{
First = DefaultFirstValue;
}
if (Filter == null)
{
Filter = string.Empty;
}
// Find avaiable packages from the current source and not taking targetframeworks into account.
if (UseRemoteSourceOnly)
{
var errors = new List<string>();
var remotePackages = GetPackagesFromRemoteSource(Filter, IncludePrerelease.IsPresent, errors.Add)
.Skip(Skip);
// If there are any errors and there is only one source, then don't mention the
// fact the no packages were found.
var outputOnEmpty = PrimarySourceRepositories.Count() != 1 && errors.Any();
WritePackagesFromRemoteSource(
remotePackages.Take(First),
outputWarning: true,
outputOnEmpty: outputOnEmpty);
if (_enablePaging)
{
WriteMoreRemotePackagesWithPaging(remotePackages.Skip(First));
}
foreach (var error in errors)
{
LogCore(MessageLevel.Error, error);
}
}
// Get package udpates from the current source and taking targetframeworks into account.
else
{
CheckSolutionState();
NuGetUIThreadHelper.JoinableTaskFactory.Run(WriteUpdatePackagesFromRemoteSourceAsyncInSolutionAsync);
}
}
}
private async Task WriteUpdatePackagesFromRemoteSourceAsyncInSolutionAsync()
{
foreach (var project in Projects)
{
await WriteUpdatePackagesFromRemoteSourceAsync(project);
}
}
/// <summary>
/// Output package updates to current project(s) found from the current remote source
/// </summary>
/// <param name="packagesToDisplay"></param>
private async Task WriteUpdatePackagesFromRemoteSourceAsync(NuGetProject project)
{
var installedPackages = await project.GetInstalledPackagesAsync(Token);
installedPackages = installedPackages.Where(p => !IsAutoReferenced(p));
VersionType versionType;
if (CollapseVersions)
{
versionType = VersionType.Latest;
}
else
{
versionType = VersionType.Updates;
}
var projectHasUpdates = false;
var metadataTasks = installedPackages.Select(installedPackage =>
Task.Run(async () =>
{
var metadata = await GetLatestPackageFromRemoteSourceAsync(installedPackage.PackageIdentity, IncludePrerelease.IsPresent);
if (metadata != null)
{
await metadata.GetVersionsAsync();
}
return metadata;
}));
foreach (var task in installedPackages.Zip(metadataTasks, (p, t) => Tuple.Create(t, p)))
{
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks
var metadata = await task.Item1;
#pragma warning restore VSTHRD003 // Avoid awaiting foreign Tasks
if (metadata != null)
{
var package = PowerShellUpdatePackage.GetPowerShellPackageUpdateView(metadata, task.Item2.PackageIdentity.Version, versionType, project);
var versions = package.Versions ?? Enumerable.Empty<NuGetVersion>();
if (versions.Any())
{
projectHasUpdates = true;
WriteObject(package);
}
}
}
if (!projectHasUpdates)
{
LogCore(MessageLevel.Info, string.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_NoPackageUpdates, project.GetMetadata<string>(NuGetProjectMetadataKeys.Name)));
}
}
/// <summary>
/// Output installed packages to the project(s)
/// </summary>
private void WriteInstalledPackages(Dictionary<NuGetProject, IEnumerable<Packaging.PackageReference>> dictionary)
{
// Get the PowerShellPackageWithProjectView
var view = PowerShellInstalledPackage.GetPowerShellPackageView(dictionary, ConfigSettings);
if (view.Any())
{
WriteObject(view, enumerateCollection: true);
}
else
{
LogCore(MessageLevel.Info, Resources.Cmdlet_NoPackagesInstalled);
}
}
/// <summary>
/// Output packages found from the current remote source
/// </summary>
private void WritePackagesFromRemoteSource(IEnumerable<IPackageSearchMetadata> packages, bool outputWarning, bool outputOnEmpty)
{
// Write warning message for Get-Package -ListAvaialble -Filter being obsolete
// and will be replaced by Find-Package [-Id]
VersionType versionType;
string message;
if (CollapseVersions)
{
versionType = VersionType.Latest;
message = "Find-Package [-Id]";
}
else
{
versionType = VersionType.All;
message = "Find-Package [-Id] -AllVersions";
}
// Output list of PowerShellPackages
if (outputWarning && !string.IsNullOrEmpty(Filter))
{
LogCore(MessageLevel.Warning, string.Format(CultureInfo.CurrentCulture, Resources.Cmdlet_CommandObsolete, message));
}
WritePackages(packages, versionType, outputOnEmpty);
}
/// <summary>
/// Output packages found from the current remote source with specified page size
/// e.g. Get-Package -ListAvailable -PageSize 20
/// </summary>
private void WriteMoreRemotePackagesWithPaging(IEnumerable<IPackageSearchMetadata> packagesToDisplay)
{
// Display more packages with paging
foreach (var page in ToPagedCollection(packagesToDisplay, PageSize).Where(p => p.Any()))
{
// Prompt to user and if want to continue displaying more packages
int command = AskToContinueDisplayPackages();
if (command == 0)
{
// If yes, display the next page of (PageSize) packages
WritePackagesFromRemoteSource(page, outputWarning: false, outputOnEmpty: false);
}
else
{
break;
}
}
}
private static IEnumerable<IEnumerable<TSource>> ToPagedCollection<TSource>(IEnumerable<TSource> source, int pageSize)
{
var nextPage = new List<TSource>();
foreach (var item in source)
{
nextPage.Add(item);
if (nextPage.Count == pageSize)
{
yield return nextPage;
nextPage = new List<TSource>();
}
}
if (nextPage.Any())
{
yield return nextPage;
}
}
private void WritePackages(IEnumerable<IPackageSearchMetadata> packages, VersionType versionType, bool outputOnEmpty)
{
var view = PowerShellRemotePackage.GetPowerShellPackageView(packages, versionType);
if (view.Any() || !outputOnEmpty)
{
WriteObject(view, enumerateCollection: true);
}
else
{
LogCore(MessageLevel.Info, Resources.Cmdlet_GetPackageNoPackageFound);
}
}
private int AskToContinueDisplayPackages()
{
// Add a line before message prompt
WriteLine();
var choices = new Collection<ChoiceDescription>
{
new ChoiceDescription(Resources.Cmdlet_Yes, Resources.Cmdlet_DisplayMorePackagesYesHelp),
new ChoiceDescription(Resources.Cmdlet_No, Resources.Cmdlet_DisplayMorePackagesNoHelp)
};
var choice = Host.UI.PromptForChoice(string.Empty, Resources.Cmdlet_PrompToDisplayMorePackages, choices, defaultChoice: 1);
Debug.Assert(choice >= 0 && choice < 2);
// Add a line after
WriteLine();
return choice;
}
private static bool IsAutoReferenced(PackageReference reference)
{
return (reference as BuildIntegratedPackageReference)?.Dependency?.AutoReferenced == true;
}
}
}