-
Notifications
You must be signed in to change notification settings - Fork 696
/
Copy pathUIActionEngine.cs
1059 lines (908 loc) · 43.8 KB
/
UIActionEngine.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.PackageManagement.Telemetry;
using NuGet.PackageManagement.VisualStudio;
using NuGet.Packaging.Core;
using NuGet.ProjectManagement;
using NuGet.Protocol.Core.Types;
using NuGet.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
using TelemetryPiiProperty = Microsoft.VisualStudio.Telemetry.TelemetryPiiProperty;
using NuGet.ProjectManagement.Projects;
namespace NuGet.PackageManagement.UI
{
/// <summary>
/// Performs package manager actions and controls the UI to display output while the actions are taking place.
/// </summary>
public sealed class UIActionEngine
{
private readonly ISourceRepositoryProvider _sourceProvider;
private readonly NuGetPackageManager _packageManager;
private readonly INuGetLockService _lockService;
private const __VSCREATEWEBBROWSER CreateWebBrowserFlags = __VSCREATEWEBBROWSER.VSCWB_StartCustom | __VSCREATEWEBBROWSER.VSCWB_ReuseExisting | __VSCREATEWEBBROWSER.VSCWB_AutoShow;
private const string CreateWebBrowserOwnerGuidString = "192D4A62-3273-4C4F-9EB4-B53DAAFFCBFB";
/// <summary>
/// Create a UIActionEngine to perform installs/uninstalls
/// </summary>
public UIActionEngine(
ISourceRepositoryProvider sourceProvider,
NuGetPackageManager packageManager,
INuGetLockService lockService)
{
if (sourceProvider == null)
{
throw new ArgumentNullException(nameof(sourceProvider));
}
if (packageManager == null)
{
throw new ArgumentNullException(nameof(packageManager));
}
if (lockService == null)
{
throw new ArgumentNullException(nameof(lockService));
}
_sourceProvider = sourceProvider;
_packageManager = packageManager;
_lockService = lockService;
}
/// <summary>
/// Perform a user action.
/// </summary>
/// <remarks>This needs to be called from a background thread. It may hang on the UI thread.</remarks>
public async Task PerformActionAsync(
INuGetUI uiService,
UserAction userAction,
CancellationToken token)
{
var operationType = NuGetOperationType.Install;
if (userAction.Action == NuGetProjectActionType.Uninstall)
{
operationType = NuGetOperationType.Uninstall;
}
await PerformActionImplAsync(
uiService,
(sourceCacheContext) =>
{
var projects = uiService.Projects;
// Allow prerelease packages only if the target is prerelease
var includePrelease =
userAction.Action == NuGetProjectActionType.Uninstall ||
userAction.Version.IsPrerelease == true;
var includeUnlisted = userAction.Action == NuGetProjectActionType.Uninstall;
var resolutionContext = new ResolutionContext(
uiService.DependencyBehavior,
includePrelease,
includeUnlisted,
VersionConstraints.None,
new GatherCache(),
sourceCacheContext);
return GetActionsAsync(
uiService,
projects,
userAction,
uiService.RemoveDependencies,
uiService.ForceRemove,
resolutionContext,
projectContext: uiService.ProjectContext,
token: token);
},
(actions, sourceCacheContext) =>
{
return ExecuteActionsAsync(actions, uiService.ProjectContext, uiService.CommonOperations, userAction, sourceCacheContext, token);
},
operationType,
userAction,
token);
}
public async Task UpgradeNuGetProjectAsync(INuGetUI uiService, NuGetProject nuGetProject)
{
var context = uiService.UIContext;
// Restore the project before proceeding
var solutionDirectory = context.SolutionManager.SolutionDirectory;
await context.PackageRestoreManager.RestoreMissingPackagesInSolutionAsync(
solutionDirectory,
uiService.ProjectContext,
new LoggerAdapter(uiService.ProjectContext),
CancellationToken.None);
var packagesDependencyInfo = await context.PackageManager.GetInstalledPackagesDependencyInfo(nuGetProject, CancellationToken.None, includeUnresolved: true);
var upgradeInformationWindowModel = new NuGetProjectUpgradeWindowModel((MSBuildNuGetProject)nuGetProject, packagesDependencyInfo.ToList());
var result = uiService.ShowNuGetUpgradeWindow(upgradeInformationWindowModel);
if (!result)
{
// raise upgrade telemetry event with Cancelled status
var packagesCount = upgradeInformationWindowModel.UpgradeDependencyItems.Count;
var upgradeTelemetryEvent = new UpgradeInformationTelemetryEvent();
upgradeTelemetryEvent.SetResult(
uiService.Projects,
NuGetOperationStatus.Cancelled,
packagesCount);
TelemetryActivity.EmitTelemetryEvent(upgradeTelemetryEvent);
return;
}
var progressDialogData = new ProgressDialogData(Resources.NuGetUpgrade_WaitMessage);
string backupPath;
var windowTitle = string.Format(
CultureInfo.CurrentCulture,
Resources.WindowTitle_NuGetMigrator,
NuGetProject.GetUniqueNameOrName(nuGetProject));
using (var progressDialogSession = await context.StartModalProgressDialogAsync(windowTitle, progressDialogData, uiService))
{
backupPath = await PackagesConfigToPackageReferenceMigrator.DoUpgradeAsync(
context,
uiService,
nuGetProject,
upgradeInformationWindowModel.UpgradeDependencyItems,
upgradeInformationWindowModel.NotFoundPackages,
progressDialogSession.Progress,
progressDialogSession.UserCancellationToken);
}
if (!string.IsNullOrEmpty(backupPath))
{
var htmlLogFile = GenerateUpgradeReport(nuGetProject, backupPath, upgradeInformationWindowModel);
Process process = null;
try
{
process = Process.Start(htmlLogFile);
}
catch { }
}
}
private static void OpenUrlInInternalWebBrowser(string url)
{
ThreadHelper.ThrowIfNotOnUIThread();
var webBrowsingService = Package.GetGlobalService(typeof(SVsWebBrowsingService)) as IVsWebBrowsingService;
if (webBrowsingService == null)
{
return;
}
var createWebBrowserOwnerGuid = new Guid(CreateWebBrowserOwnerGuidString);
webBrowsingService.CreateWebBrowser((uint)CreateWebBrowserFlags, ref createWebBrowserOwnerGuid, null, url, null, out var browser, out var frame);
}
private static string GenerateUpgradeReport(NuGetProject nuGetProject, string backupPath, NuGetProjectUpgradeWindowModel upgradeInformationWindowModel)
{
var projectName = NuGetProject.GetUniqueNameOrName(nuGetProject);
using (var upgradeLogger = new UpgradeLogger(projectName, backupPath))
{
var installedAsTopLevel = upgradeInformationWindowModel.UpgradeDependencyItems.Where(t => t.InstallAsTopLevel);
var transitiveDependencies = upgradeInformationWindowModel.TransitiveDependencies.Where(t => !t.InstallAsTopLevel);
foreach (var package in installedAsTopLevel)
{
upgradeLogger.RegisterPackage(projectName, package.Id, package.Version, package.Issues, true);
}
foreach (var package in transitiveDependencies)
{
upgradeLogger.RegisterPackage(projectName, package.Id, package.Version, package.Issues, false);
}
return upgradeLogger.GetHtmlFilePath();
}
}
/// <summary>
/// Perform the multi-package update action.
/// </summary>
/// <remarks>This needs to be called from a background thread. It may hang on the UI thread.</remarks>
public async Task PerformUpdateAsync(
INuGetUI uiService,
List<PackageIdentity> packagesToUpdate,
CancellationToken token)
{
await PerformActionImplAsync(
uiService,
(sourceCacheContext) =>
{
return ResolveActionsForUpdateAsync(
uiService,
packagesToUpdate,
token);
},
async (actions, sourceCacheContext) =>
{
// Get all Nuget projects and actions and call ExecuteNugetProjectActions once for all the projects.
var nugetProjects = actions.Select(action => action.Project);
var nugetActions = actions.Select(action => action.Action);
await _packageManager.ExecuteNuGetProjectActionsAsync(
nugetProjects,
nugetActions,
uiService.ProjectContext,
sourceCacheContext,
token);
},
NuGetOperationType.Update,
userAction: null,
token);
}
/// <summary>
/// Calculates the list of actions needed to perform packages updates.
/// </summary>
/// <param name="uiService">ui service.</param>
/// <param name="packagesToUpdate">The list of packages to update.</param>
/// <param name="token">Cancellation token.</param>
/// <returns>The list of actions.</returns>
private async Task<IReadOnlyList<ResolvedAction>> ResolveActionsForUpdateAsync(
INuGetUI uiService,
List<PackageIdentity> packagesToUpdate,
CancellationToken token)
{
var resolvedActions = new List<ResolvedAction>();
// Keep a single gather cache across projects
var gatherCache = new GatherCache();
var includePrerelease = packagesToUpdate.Where(
package => package.Version.IsPrerelease).Any();
using (var sourceCacheContext = new SourceCacheContext())
{
var resolutionContext = new ResolutionContext(
uiService.DependencyBehavior,
includePrelease: includePrerelease,
includeUnlisted: true,
versionConstraints: VersionConstraints.None,
gatherCache: gatherCache,
sourceCacheContext: sourceCacheContext);
var secondarySources = _sourceProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled);
var actions = await _packageManager.PreviewUpdatePackagesAsync(
packagesToUpdate,
uiService.Projects,
resolutionContext,
uiService.ProjectContext,
uiService.ActiveSources,
secondarySources,
token);
resolvedActions.AddRange(actions.Select(action => new ResolvedAction(action.Project, action))
.ToList());
}
return resolvedActions;
}
/// <summary>
/// The internal implementation to perform user action.
/// </summary>
/// <param name="resolveActionsAsync">A function that returns a task that resolves the user
/// action into project actions.</param>
/// <param name="executeActionsAsync">A function that returns a task that executes
/// the project actions.</param>
private async Task PerformActionImplAsync(
INuGetUI uiService,
Func<SourceCacheContext, Task<IReadOnlyList<ResolvedAction>>> resolveActionsAsync,
Func<IReadOnlyList<ResolvedAction>, SourceCacheContext, Task> executeActionsAsync,
NuGetOperationType operationType,
UserAction userAction,
CancellationToken token)
{
var status = NuGetOperationStatus.Succeeded;
var startTime = DateTimeOffset.Now;
var packageCount = 0;
bool continueAfterPreview = true;
bool acceptedLicense = true;
List<string> removedPackages = null;
HashSet<Tuple<string, string>> existingPackages = new HashSet<Tuple<string, string>>();
List<Tuple<string,string>> addedPackages = null;
List<Tuple<string,string>> updatedPackagesOld = null;
List<Tuple<string,string>> updatedPackagesNew = null;
// Enable granular level telemetry events for nuget ui operation
uiService.ProjectContext.OperationId = Guid.NewGuid();
Stopwatch packageEnumerationTime = new Stopwatch();
packageEnumerationTime.Start();
try
{
// collect the install state of the existing packages
foreach (var project in uiService.Projects)
{
var result = await project.GetInstalledPackagesAsync(token);
foreach (var package in result)
{
Tuple<string, string> packageInfo = new Tuple<string, string>(package.PackageIdentity.Id, (package.PackageIdentity.Version == null ? "" : package.PackageIdentity.Version.ToNormalizedString()));
if (!existingPackages.Contains(packageInfo))
{
existingPackages.Add(packageInfo);
}
}
}
}
catch (Exception)
{
// don't teardown the process if we have a telemetry failure
}
packageEnumerationTime.Stop();
await _lockService.ExecuteNuGetOperationAsync(async () =>
{
try
{
uiService.BeginOperation();
var acceptedFormat = await CheckPackageManagementFormat(uiService, token);
if (!acceptedFormat)
{
return;
}
TelemetryServiceUtility.StartOrResumeTimer();
using (var sourceCacheContext = new SourceCacheContext())
{
var actions = await resolveActionsAsync(sourceCacheContext);
var results = GetPreviewResults(actions);
if (operationType == NuGetOperationType.Uninstall)
{
// removed packages don't have version info
removedPackages = results.SelectMany(result => result.Deleted)
.Select(package => package.Id)
.Distinct()
.ToList();
packageCount = removedPackages.Count;
}
else
{
// log rich info about added packages
addedPackages = results.SelectMany(result => result.Added)
.Select(package => new Tuple<string, string>(package.Id, (package.Version == null ? "" : package.Version.ToNormalizedString())))
.Distinct()
.ToList();
var addCount = addedPackages.Count;
//updated packages can have an old and a new id.
updatedPackagesOld = results.SelectMany(result => result.Updated)
.Select(package => new Tuple<string, string>(package.Old.Id, (package.Old.Version == null ? "" : package.Old.Version.ToNormalizedString())))
.Distinct()
.ToList();
updatedPackagesNew = results.SelectMany(result => result.Updated)
.Select(package => new Tuple<string, string>(package.New.Id, (package.New.Version == null ? "" : package.New.Version.ToNormalizedString())))
.Distinct()
.ToList();
var updateCount = updatedPackagesNew.Count;
// update packages count
packageCount = addCount + updateCount;
if (updateCount > 0)
{
// set operation type to update when there are packages being updated
operationType = NuGetOperationType.Update;
}
}
TelemetryServiceUtility.StopTimer();
// Show the preview window.
if (uiService.DisplayPreviewWindow)
{
var shouldContinue = uiService.PromptForPreviewAcceptance(results);
if (!shouldContinue)
{
continueAfterPreview = false;
return;
}
}
TelemetryServiceUtility.StartOrResumeTimer();
// Show the license acceptance window.
var accepted = await CheckLicenseAcceptanceAsync(uiService, results, token);
TelemetryServiceUtility.StartOrResumeTimer();
if (!accepted)
{
acceptedLicense = false;
return;
}
// Warn about the fact that the "dotnet" TFM is deprecated.
if (uiService.DisplayDeprecatedFrameworkWindow)
{
var shouldContinue = ShouldContinueDueToDotnetDeprecation(uiService, actions, token);
TelemetryServiceUtility.StartOrResumeTimer();
if (!shouldContinue)
{
return;
}
}
if (!token.IsCancellationRequested)
{
// execute the actions
await executeActionsAsync(actions, sourceCacheContext);
// fires ActionsExecuted event to update the UI
uiService.OnActionsExecuted(actions);
}
else
{
status = NuGetOperationStatus.Cancelled;
}
}
}
catch (System.Net.Http.HttpRequestException ex)
{
status = NuGetOperationStatus.Failed;
if (ex.InnerException != null)
{
uiService.ShowError(ex.InnerException);
}
else
{
uiService.ShowError(ex);
}
}
catch (Exception ex)
{
status = NuGetOperationStatus.Failed;
uiService.ShowError(ex);
}
finally
{
TelemetryServiceUtility.StopTimer();
var duration = TelemetryServiceUtility.GetTimerElapsedTime();
uiService.ProjectContext.Log(MessageLevel.Info,
string.Format(CultureInfo.CurrentCulture, Resources.Operation_TotalTime, duration));
uiService.EndOperation();
// don't show "Succeeded" if we actually cancelled...
if ((!continueAfterPreview) || (!acceptedLicense))
{
if (status == NuGetOperationStatus.Succeeded)
{
status = NuGetOperationStatus.Cancelled;
}
}
PackageLoadContext plc = new PackageLoadContext(sourceRepositories: null, isSolution: false, uiService.UIContext);
var frameworks = plc.GetSupportedFrameworks().ToList();
var actionTelemetryEvent = VSTelemetryServiceUtility.GetActionTelemetryEvent(
uiService.ProjectContext.OperationId.ToString(),
uiService.Projects,
operationType,
OperationSource.UI,
startTime,
status,
packageCount,
duration.TotalSeconds);
var nuGetUI = uiService as NuGetUI;
AddUiActionEngineTelemetryProperties(
actionTelemetryEvent,
continueAfterPreview,
acceptedLicense,
userAction,
nuGetUI?.SelectedIndex,
nuGetUI?.RecommendedCount,
nuGetUI?.RecommendPackages,
nuGetUI?.RecommenderVersion,
existingPackages,
addedPackages,
removedPackages,
updatedPackagesOld,
updatedPackagesNew,
frameworks);
actionTelemetryEvent["InstalledPackageEnumerationTimeInMilliseconds"] = packageEnumerationTime.ElapsedMilliseconds;
TelemetryActivity.EmitTelemetryEvent(actionTelemetryEvent);
}
}, token);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "We require lowercase package names in telemetry so that the hashes are consistent")]
private static void AddUiActionEngineTelemetryProperties(
VSActionsTelemetryEvent actionTelemetryEvent,
bool continueAfterPreview,
bool acceptedLicense,
UserAction userAction,
int? selectedIndex,
int? recommendedCount,
bool? recommendPackages,
(string modelVersion, string vsixVersion)? recommenderVersion,
HashSet<Tuple<string, string>> existingPackages,
List<Tuple<string, string>> addedPackages,
List<string> removedPackages,
List<Tuple<string, string>> updatedPackagesOld,
List<Tuple<string, string>> updatedPackagesNew,
List<string> targetFrameworks)
{
TelemetryEvent ToTelemetryPackage(Tuple<string, string> package)
{
var subEvent = new TelemetryEvent(eventName: null);
subEvent.AddPiiData("id", package.Item1?.ToLowerInvariant() ?? "(empty package id)");
subEvent["version"] = package.Item2;
return subEvent;
}
List<TelemetryEvent> ToTelemetryPackageList(List<Tuple<string, string>> packages)
{
var list = new List<TelemetryEvent>(packages.Count);
list.AddRange(packages.Select(ToTelemetryPackage));
return list;
}
// log possible cancel reasons
if (!continueAfterPreview)
{
actionTelemetryEvent["CancelAfterPreview"] = "True";
}
if (!acceptedLicense)
{
actionTelemetryEvent["AcceptedLicense"] = "False";
}
// log the single top level package the user is installing or removing
if (userAction != null)
{
// userAction.Version can be null for deleted packages.
actionTelemetryEvent.ComplexData["SelectedPackage"] = ToTelemetryPackage(new Tuple<string, string>(userAction.PackageId, userAction.Version?.ToNormalizedString() ?? string.Empty));
actionTelemetryEvent["SelectedIndex"] = selectedIndex;
actionTelemetryEvent["RecommendedCount"] = recommendedCount;
actionTelemetryEvent["RecommendPackages"] = recommendPackages;
actionTelemetryEvent["Recommender.ModelVersion"] = recommenderVersion?.modelVersion;
actionTelemetryEvent["Recommender.VsixVersion"] = recommenderVersion?.vsixVersion;
}
// log the installed package state
if (existingPackages?.Count > 0)
{
var packages = new List<TelemetryEvent>();
foreach (var package in existingPackages)
{
packages.Add(ToTelemetryPackage(package));
}
actionTelemetryEvent.ComplexData["ExistingPackages"] = packages;
}
// other packages can be added, removed, or upgraded as part of bulk upgrade or as part of satisfying package dependencies, so log that also
if (addedPackages?.Count > 0)
{
var packages = new List<TelemetryEvent>();
foreach (var package in addedPackages)
{
packages.Add(ToTelemetryPackage(package));
}
actionTelemetryEvent.ComplexData["AddedPackages"] = packages;
}
if (removedPackages?.Count > 0)
{
var packages = new List<TelemetryPiiProperty>();
foreach (var package in removedPackages)
{
packages.Add(new TelemetryPiiProperty(package?.ToLowerInvariant() ?? "(empty package id)"));
}
actionTelemetryEvent.ComplexData["RemovedPackages"] = packages;
}
// two collections for updated packages: pre and post upgrade
if (updatedPackagesNew?.Count > 0)
{
actionTelemetryEvent.ComplexData["UpdatedPackagesNew"] = ToTelemetryPackageList(updatedPackagesNew);
}
if (updatedPackagesOld?.Count > 0)
{
actionTelemetryEvent.ComplexData["UpdatedPackagesOld"] = ToTelemetryPackageList(updatedPackagesOld);
}
// target framworks
if (targetFrameworks?.Count > 0)
{
actionTelemetryEvent["TargetFrameworks"] = string.Join(";", targetFrameworks);
}
}
private async Task<bool> CheckPackageManagementFormat(INuGetUI uiService, CancellationToken token)
{
var potentialProjects = new List<NuGetProject>();
// check if project suppports <PackageReference> items.
// otherwise don't show format selector dialog for this project
var capableProjects = uiService
.Projects
.Where(project =>
project.ProjectStyle == ProjectModel.ProjectStyle.PackagesConfig &&
project.ProjectServices.Capabilities.SupportsPackageReferences);
// get all packages.config based projects with no installed packages
foreach (var project in capableProjects)
{
var installedPackages = await project.GetInstalledPackagesAsync(token);
if (!installedPackages.Any())
{
potentialProjects.Add(project);
}
}
// only show this dialog if there are any new project(s) with no installed packages.
if (potentialProjects.Count > 0)
{
var packageManagementFormat = new PackageManagementFormat(uiService.Settings);
if (!packageManagementFormat.Enabled)
{
// user disabled this prompt either through Tools->options or previous interaction of this dialog.
// now check for default package format, if its set to PackageReference then update the project.
if (packageManagementFormat.SelectedPackageManagementFormat == 1)
{
await uiService.UpdateNuGetProjectToPackageRef(potentialProjects);
}
return true;
}
packageManagementFormat.ProjectNames = potentialProjects
.Select(project => project.GetMetadata<string>(NuGetProjectMetadataKeys.Name))
.OrderBy(name => name, StringComparer.OrdinalIgnoreCase).ToList();
// show dialog for package format selector
var result = uiService.PromptForPackageManagementFormat(packageManagementFormat);
// update nuget projects if user selected PackageReference option
if (result && packageManagementFormat.SelectedPackageManagementFormat == 1)
{
await uiService.UpdateNuGetProjectToPackageRef(potentialProjects);
}
return result;
}
return true;
}
// Returns false if user doesn't accept license agreements.
private async Task<bool> CheckLicenseAcceptanceAsync(
INuGetUI uiService,
IEnumerable<PreviewResult> results,
CancellationToken token)
{
// find all the packages that might need a license acceptance
var licenseCheck = new HashSet<PackageIdentity>(PackageIdentity.Comparer);
foreach (var result in results)
{
foreach (var pkg in result.Added)
{
licenseCheck.Add(pkg);
}
foreach (var pkg in result.Updated)
{
licenseCheck.Add(pkg.New);
}
}
var sources = _sourceProvider.GetRepositories().Where(e => e.PackageSource.IsEnabled);
var licenseMetadata = await GetPackageMetadataAsync(sources, licenseCheck, token);
TelemetryServiceUtility.StopTimer();
// show license agreement
if (licenseMetadata.Any(e => e.RequireLicenseAcceptance))
{
var licenseInfoItems = licenseMetadata
.Where(p => p.RequireLicenseAcceptance)
.Select(e => GeneratePackageLicenseInfo(e));
return uiService.PromptForLicenseAcceptance(licenseInfoItems);
}
return true;
}
private PackageLicenseInfo GeneratePackageLicenseInfo(IPackageSearchMetadata metadata)
{
return new PackageLicenseInfo(
metadata.Identity.Id,
PackageLicenseUtilities.GenerateLicenseLinks(metadata),
metadata.Authors);
}
/// <summary>
/// Warns the user about the fact that the dotnet TFM is deprecated.
/// </summary>
/// <returns>Returns true if the user wants to ignore the warning or if the warning does not apply.</returns>
private bool ShouldContinueDueToDotnetDeprecation(
INuGetUI uiService,
IEnumerable<ResolvedAction> actions,
CancellationToken token)
{
var projects = DotnetDeprecatedPrompt.GetAffectedProjects(actions);
TelemetryServiceUtility.StopTimer();
if (projects.Any())
{
return uiService.WarnAboutDotnetDeprecation(projects);
}
return true;
}
/// <summary>
/// Execute the installs/uninstalls
/// </summary>
private async Task ExecuteActionsAsync(
IEnumerable<ResolvedAction> actions,
INuGetProjectContext projectContext,
ICommonOperations commonOperations,
UserAction userAction,
SourceCacheContext sourceCacheContext,
CancellationToken token)
{
var nuGetProjects = actions.Select(action => action.Project);
var nuGetActions = actions.Select(action => action.Action);
var directInstall = GetDirectInstall(nuGetActions, userAction, commonOperations);
if (directInstall != null)
{
NuGetPackageManager.SetDirectInstall(directInstall, projectContext);
}
await _packageManager.ExecuteNuGetProjectActionsAsync(
nuGetProjects,
nuGetActions,
projectContext,
sourceCacheContext,
token);
NuGetPackageManager.ClearDirectInstall(projectContext);
}
private static PackageIdentity GetDirectInstall(IEnumerable<NuGetProjectAction> nuGetProjectActions,
UserAction userAction,
ICommonOperations commonOperations)
{
if (commonOperations != null
&& userAction != null
&& userAction.Action == NuGetProjectActionType.Install
&& nuGetProjectActions.Any())
{
return new PackageIdentity(userAction.PackageId, userAction.Version);
}
return null;
}
/// <summary>
/// Return the resolve package actions
/// </summary>
private async Task<IReadOnlyList<ResolvedAction>> GetActionsAsync(
INuGetUI uiService,
IEnumerable<NuGetProject> targets,
UserAction userAction,
bool removeDependencies,
bool forceRemove,
ResolutionContext resolutionContext,
INuGetProjectContext projectContext,
CancellationToken token)
{
var results = new List<ResolvedAction>();
Debug.Assert(userAction.PackageId != null, "Package id can never be null in a User action");
if (userAction.Action == NuGetProjectActionType.Install)
{
results.AddRange(await _packageManager.PreviewProjectsInstallPackageAsync(
targets,
new PackageIdentity(userAction.PackageId, userAction.Version),
resolutionContext,
projectContext,
uiService.ActiveSources,
token
));
}
else
{
var uninstallationContext = new UninstallationContext(
removeDependencies: removeDependencies,
forceRemove: forceRemove);
foreach (var target in targets)
{
IEnumerable<NuGetProjectAction> actions;
actions = await _packageManager.PreviewUninstallPackageAsync(
target, userAction.PackageId, uninstallationContext, projectContext, token);
results.AddRange(actions.Select(a => new ResolvedAction(target, a)));
}
}
return results;
}
/// <summary>
/// Convert NuGetProjectActions into PreviewResult types
/// </summary>
private static IReadOnlyList<PreviewResult> GetPreviewResults(IEnumerable<ResolvedAction> projectActions)
{
var results = new List<PreviewResult>();
var expandedActions = new List<ResolvedAction>();
// BuildIntegratedProjectActions contain all project actions rolled up into a single action,
// to display these we need to expand them into the low level actions.
foreach (var action in projectActions)
{
var buildIntegratedAction = action.Action as BuildIntegratedProjectAction;
if (buildIntegratedAction != null)
{
foreach (var buildAction in buildIntegratedAction.GetProjectActions())
{
expandedActions.Add(new ResolvedAction(action.Project, buildAction));
}
}
else
{
// leave the action as is
expandedActions.Add(action);
}
}
// Group actions by project
var actionsByProject = expandedActions.GroupBy(action => action.Project);
// Group actions by operation
foreach (var actions in actionsByProject)
{
var installed = new Dictionary<string, PackageIdentity>(StringComparer.OrdinalIgnoreCase);
var uninstalled = new Dictionary<string, PackageIdentity>(StringComparer.OrdinalIgnoreCase);
var packageIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var action in actions.Select(a => a.Action))
{
var package = action.PackageIdentity;
packageIds.Add(package.Id);
// Create new identities without the dependency graph
if (action.NuGetProjectActionType == NuGetProjectActionType.Install)
{
installed[package.Id] = new PackageIdentity(package.Id, package.Version);
}
else
{
uninstalled[package.Id] = new PackageIdentity(package.Id, package.Version);
}
}
var added = new List<AccessiblePackageIdentity>();
var deleted = new List<AccessiblePackageIdentity>();
var updated = new List<UpdatePreviewResult>();
foreach (var packageId in packageIds)
{
var isInstalled = installed.ContainsKey(packageId);
var isUninstalled = uninstalled.ContainsKey(packageId);
if (isInstalled && isUninstalled)
{
// the package is updated
updated.Add(new UpdatePreviewResult(uninstalled[packageId], installed[packageId]));
installed.Remove(packageId);
}
else if (isInstalled && !isUninstalled)
{
// the package is added
added.Add(new AccessiblePackageIdentity(installed[packageId]));
}
else if (!isInstalled && isUninstalled)
{
// the package is deleted
deleted.Add(new AccessiblePackageIdentity(uninstalled[packageId]));
}
}
var result = new PreviewResult(actions.Key, added, deleted, updated);
results.Add(result);
}
return results;
}
/// <summary>
/// Get the package metadata to see if RequireLicenseAcceptance is true
/// </summary>
private async Task<List<IPackageSearchMetadata>> GetPackageMetadataAsync(
IEnumerable<SourceRepository> sources,
IEnumerable<PackageIdentity> packages,
CancellationToken token)
{
var results = new List<IPackageSearchMetadata>();
// local sources
var localSources = new List<SourceRepository>();
localSources.Add(_packageManager.PackagesFolderSourceRepository);
localSources.AddRange(_packageManager.GlobalPackageFolderRepositories);
var allPackages = packages.ToArray();
using (var sourceCacheContext = new SourceCacheContext())
{
// first check all the packages with local sources.
var completed = (await TaskCombinators.ThrottledAsync(
allPackages,
(p, t) => GetPackageMetadataAsync(localSources, sourceCacheContext, p, t),
token)).Where(metadata => metadata != null).ToArray();
results.AddRange(completed);
if (completed.Length != allPackages.Length)
{
// get remaining package's metadata from remote repositories
var remainingPackages = allPackages.Where(package => !completed.Any(pack => pack.Identity.Equals(package)));
var remoteResults = (await TaskCombinators.ThrottledAsync(
remainingPackages,
(p, t) => GetPackageMetadataAsync(sources, sourceCacheContext, p, t),
token)).Where(metadata => metadata != null).ToArray();
results.AddRange(remoteResults);
}
}
// check if missing metadata for any package
if (allPackages.Length != results.Count)