Repository navigation
Expand file tree
/
Copy pathNuGetBaseCommand.cs
More file actions
536 lines (463 loc) · 19 KB
/
Copy pathNuGetBaseCommand.cs
File metadata and controls
536 lines (463 loc) · 19 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using EnvDTE;
using NuGet.VisualStudio;
using NuGet.VisualStudio.Resources;
namespace NuGet.PowerShell.Commands
{
/// <summary>
/// This is the base class for all NuGet cmdlets.
/// </summary>
public abstract class NuGetBaseCommand : PSCmdlet, ILogger, IErrorHandler
{
// User Agent. Do NOT localize
private const string PSCommandsUserAgentClient = "NuGet VS PowerShell Console";
private readonly Lazy<string> _psCommandsUserAgent = new Lazy<string>(
() => HttpUtility.CreateUserAgentString(PSCommandsUserAgentClient, VsVersionHelper.FullVsEdition));
private IVsPackageManager _packageManager;
private readonly ISolutionManager _solutionManager;
private readonly IVsPackageManagerFactory _vsPackageManagerFactory;
private ProgressRecordCollection _progressRecordCache;
private readonly IHttpClientEvents _httpClientEvents;
private bool _overwriteAll, _ignoreAll;
protected NuGetBaseCommand(ISolutionManager solutionManager, IVsPackageManagerFactory vsPackageManagerFactory, IHttpClientEvents httpClientEvents)
{
_solutionManager = solutionManager;
_vsPackageManagerFactory = vsPackageManagerFactory;
_httpClientEvents = httpClientEvents;
}
protected string DefaultUserAgent
{
get
{
return _psCommandsUserAgent.Value;
}
}
private ProgressRecordCollection ProgressRecordCache
{
get
{
if (_progressRecordCache == null)
{
_progressRecordCache = new ProgressRecordCollection();
}
return _progressRecordCache;
}
}
protected IErrorHandler ErrorHandler
{
get
{
return this;
}
}
protected ISolutionManager SolutionManager
{
get
{
return _solutionManager;
}
}
protected IVsPackageManagerFactory PackageManagerFactory
{
get
{
return _vsPackageManagerFactory;
}
}
internal bool IsSyncMode
{
get
{
if (Host == null || Host.PrivateData == null)
{
return false;
}
PSObject privateData = Host.PrivateData;
var syncModeProp = privateData.Properties["IsSyncMode"];
return syncModeProp != null && (bool)syncModeProp.Value;
}
}
/// <summary>
/// Gets an instance of VSPackageManager to be used throughout the execution of this command.
/// </summary>
/// <value>The package manager.</value>
protected internal IVsPackageManager PackageManager
{
get
{
if (_packageManager == null)
{
_packageManager = CreatePackageManager();
}
return _packageManager;
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to display friendly message to the console.")]
protected sealed override void ProcessRecord()
{
try
{
ProcessRecordCore();
}
catch (Exception ex)
{
// unhandled exceptions should be terminating
ErrorHandler.HandleException(ex, terminating: true);
}
finally
{
UnsubscribeEvents();
}
}
protected override void StopProcessing()
{
UnsubscribeEvents();
base.StopProcessing();
}
/// <summary>
/// Derived classess must implement this method instead of ProcessRecord(), which is sealed by NuGetBaseCmdlet.
/// </summary>
protected abstract void ProcessRecordCore();
public void Log(MessageLevel level, string message, params object[] args)
{
string formattedMessage = String.Format(CultureInfo.CurrentCulture, message, args);
LogCore(level, formattedMessage);
}
internal void Execute()
{
BeginProcessing();
ProcessRecord();
EndProcessing();
}
protected override void BeginProcessing()
{
if (_httpClientEvents != null)
{
_httpClientEvents.SendingRequest += OnSendingRequest;
}
}
protected override void EndProcessing()
{
UnsubscribeEvents();
}
protected void SubscribeToProgressEvents()
{
if (!IsSyncMode && _httpClientEvents != null)
{
_httpClientEvents.ProgressAvailable += OnProgressAvailable;
}
}
protected void UnsubscribeFromProgressEvents()
{
if (_httpClientEvents != null)
{
_httpClientEvents.ProgressAvailable -= OnProgressAvailable;
}
}
protected virtual void LogCore(MessageLevel level, string formattedMessage)
{
switch (level)
{
case MessageLevel.Debug:
WriteVerbose(formattedMessage);
break;
case MessageLevel.Warning:
WriteWarning(formattedMessage);
break;
case MessageLevel.Info:
WriteLine(formattedMessage);
break;
case MessageLevel.Error:
WriteError(formattedMessage);
break;
}
}
protected virtual IVsPackageManager CreatePackageManager()
{
if (!SolutionManager.IsSolutionOpen)
{
return null;
}
return PackageManagerFactory.CreatePackageManager();
}
/// <summary>
/// Return all projects in the solution matching the provided names. Wildcards are supported.
/// This method will automatically generate error records for non-wildcarded project names that
/// are not found.
/// </summary>
/// <param name="projectNames">An array of project names that may or may not include wildcards.</param>
/// <returns>Projects matching the project name(s) provided.</returns>
protected IEnumerable<Project> GetProjectsByName(string[] projectNames)
{
var allValidProjectNames = GetAllValidProjectNames().ToList();
foreach (string projectName in projectNames)
{
// if ctrl+c hit, leave immediately
if (Stopping)
{
break;
}
// Treat every name as a wildcard; results in simpler code
var pattern = new WildcardPattern(projectName, WildcardOptions.IgnoreCase);
var matches = from s in allValidProjectNames
where pattern.IsMatch(s)
select _solutionManager.GetProject(s);
int count = 0;
foreach (var project in matches)
{
count++;
yield return project;
}
// We only emit non-terminating error record if a non-wildcarded name was not found.
// This is consistent with built-in cmdlets that support wildcarded search.
// A search with a wildcard that returns nothing should not be considered an error.
if ((count == 0) && !WildcardPattern.ContainsWildcardCharacters(projectName))
{
ErrorHandler.WriteProjectNotFoundError(projectName, terminating: false);
}
}
}
/// <summary>
/// Return all possibly valid project names in the current solution. This includes all
/// unique names and safe names.
/// </summary>
/// <returns></returns>
private IEnumerable<string> GetAllValidProjectNames()
{
var safeNames = _solutionManager.GetProjects().Select(p => _solutionManager.GetProjectSafeName(p));
var uniqueNames = _solutionManager.GetProjects().Select(p => p.GetCustomUniqueName());
return uniqueNames.Concat(safeNames).Distinct();
}
/// <summary>
/// Translate a PSPath into a System.IO.* friendly Win32 path.
/// Does not resolve/glob wildcards.
/// </summary>
/// <param name="psPath">The PowerShell PSPath to translate which may reference PSDrives or have provider-qualified paths which are syntactically invalid for .NET APIs.</param>
/// <param name="path">The translated PSPath in a format understandable to .NET APIs.</param>
/// <param name="exists">Returns null if not tested, or a bool representing path existence.</param>
/// <param name="errorMessage">If translation failed, contains the reason.</param>
/// <returns>True if successfully translated, false if not.</returns>
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "1#", Justification = "Following TryParse pattern in BCL", Target = "path")]
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "2#", Justification = "Following TryParse pattern in BCL", Target = "exists")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "ps", Justification = "ps is a common powershell prefix")]
protected bool TryTranslatePSPath(string psPath, out string path, out bool? exists, out string errorMessage)
{
return PSPathUtility.TryTranslatePSPath(SessionState, psPath, out path, out exists, out errorMessage);
}
/// <summary>
/// Create a package repository from the source by trying to resolve relative paths.
/// </summary>
protected IPackageRepository CreateRepositoryFromSource(IPackageRepositoryFactory repositoryFactory, IPackageSourceProvider sourceProvider, string source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
UriFormatException uriException = null;
string resolvedSource = sourceProvider.ResolveSource(source);
try
{
IPackageRepository repository = repositoryFactory.CreateRepository(resolvedSource);
if (repository != null)
{
return repository;
}
}
catch (UriFormatException ex)
{
// if the source is relative path, it can result in invalid uri exception
uriException = ex;
}
Uri uri;
// if it's not an absolute path, treat it as relative path
if (Uri.TryCreate(source, UriKind.Relative, out uri))
{
string outputPath;
bool? exists;
string errorMessage;
// translate relative path to absolute path
if (TryTranslatePSPath(source, out outputPath, out exists, out errorMessage) && exists == true)
{
return repositoryFactory.CreateRepository(outputPath);
}
else
{
return repositoryFactory.CreateRepository(source);
}
}
else
{
// if this is not a valid relative path either,
// we rethrow the UriFormatException that we caught earlier.
if (uriException != null)
{
throw uriException;
}
}
return null;
}
[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Justification = "This exception is passed to PowerShell. We really don't care about the type of exception here.")]
protected void WriteError(string message)
{
if (!String.IsNullOrEmpty(message))
{
WriteError(new Exception(message));
}
}
protected void WriteError(Exception exception)
{
ErrorHandler.HandleException(exception, terminating: false);
}
void IErrorHandler.WriteProjectNotFoundError(string projectName, bool terminating)
{
var notFoundException =
new ItemNotFoundException(
String.Format(
CultureInfo.CurrentCulture,
Resources.Cmdlet_ProjectNotFound, projectName));
ErrorHandler.HandleError(
new ErrorRecord(
notFoundException,
NuGetErrorId.ProjectNotFound, // This is your locale-agnostic error id.
ErrorCategory.ObjectNotFound,
projectName),
terminating: terminating);
}
void IErrorHandler.ThrowSolutionNotOpenTerminatingError()
{
ErrorHandler.HandleException(
new InvalidOperationException(Resources.Cmdlet_NoSolution),
terminating: true,
errorId: NuGetErrorId.NoActiveSolution,
category: ErrorCategory.InvalidOperation);
}
void IErrorHandler.ThrowNoCompatibleProjectsTerminatingError()
{
ErrorHandler.HandleException(
new InvalidOperationException(Resources.Cmdlet_NoCompatibleProjects),
terminating: true,
errorId: NuGetErrorId.NoCompatibleProjects,
category: ErrorCategory.InvalidOperation);
}
void IErrorHandler.HandleError(ErrorRecord errorRecord, bool terminating)
{
if (terminating)
{
ThrowTerminatingError(errorRecord);
}
else
{
WriteError(errorRecord);
}
}
void IErrorHandler.HandleException(Exception exception, bool terminating,
string errorId, ErrorCategory category, object target)
{
exception = ExceptionUtility.Unwrap(exception);
var error = new ErrorRecord(exception, errorId, category, target);
ErrorHandler.HandleError(error, terminating: terminating);
}
protected void WriteLine(string message = null)
{
if (Host == null)
{
// Host is null when running unit tests. Simply return in this case
return;
}
if (message == null)
{
Host.UI.WriteLine();
}
else
{
Host.UI.WriteLine(message);
}
}
protected void WriteProgress(int activityId, string operation, int percentComplete)
{
if (IsSyncMode)
{
// don't bother to show progress if we are in synchronous mode
return;
}
ProgressRecord progressRecord;
// retrieve the ProgressRecord object for this particular activity id from the cache.
if (ProgressRecordCache.Contains(activityId))
{
progressRecord = ProgressRecordCache[activityId];
}
else
{
progressRecord = new ProgressRecord(activityId, operation, operation);
ProgressRecordCache.Add(progressRecord);
}
progressRecord.CurrentOperation = operation;
progressRecord.PercentComplete = percentComplete;
WriteProgress(progressRecord);
}
private void OnProgressAvailable(object sender, ProgressEventArgs e)
{
WriteProgress(ProgressActivityIds.DownloadPackageId, e.Operation, e.PercentComplete);
}
protected virtual void OnSendingRequest(object sender, WebRequestEventArgs e)
{
HttpUtility.SetUserAgent(e.Request, _psCommandsUserAgent.Value);
}
public virtual FileConflictResolution ResolveFileConflict(string message)
{
if (_overwriteAll)
{
return FileConflictResolution.OverwriteAll;
}
if (_ignoreAll)
{
return FileConflictResolution.IgnoreAll;
}
var choices = new Collection<ChoiceDescription>
{
new ChoiceDescription(Resources.Cmdlet_Yes, Resources.Cmdlet_FileConflictYesHelp),
new ChoiceDescription(Resources.Cmdlet_YesAll, Resources.Cmdlet_FileConflictYesAllHelp),
new ChoiceDescription(Resources.Cmdlet_No, Resources.Cmdlet_FileConflictNoHelp),
new ChoiceDescription(Resources.Cmdlet_NoAll, Resources.Cmdlet_FileConflictNoAllHelp)
};
int choice = Host.UI.PromptForChoice(VsResources.FileConflictTitle, message, choices, defaultChoice: 2);
Debug.Assert(choice >= 0 && choice < 4);
switch (choice)
{
case 0:
return FileConflictResolution.Overwrite;
case 1:
_overwriteAll = true;
return FileConflictResolution.OverwriteAll;
case 2:
return FileConflictResolution.Ignore;
case 3:
_ignoreAll = true;
return FileConflictResolution.IgnoreAll;
}
return FileConflictResolution.Ignore;
}
private void UnsubscribeEvents()
{
if (_httpClientEvents != null)
{
_httpClientEvents.SendingRequest -= OnSendingRequest;
}
}
private class ProgressRecordCollection : KeyedCollection<int, ProgressRecord>
{
protected override int GetKeyForItem(ProgressRecord item)
{
return item.ActivityId;
}
}
}
}