forked from NuGet/NuGet.Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNuGetPackage.cs
1329 lines (1120 loc) · 58.6 KB
/
NuGetPackage.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.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE;
using Microsoft;
using Microsoft.ServiceHub.Framework;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
using NuGet.Options;
using NuGet.PackageManagement;
using NuGet.PackageManagement.UI;
using NuGet.PackageManagement.VisualStudio;
using NuGet.ProjectManagement;
using NuGet.VisualStudio;
using NuGet.VisualStudio.Common;
using NuGet.VisualStudio.Common.Telemetry;
using NuGet.VisualStudio.Internal.Contracts;
using NuGet.VisualStudio.Telemetry;
using NuGetConsole;
using NuGetConsole.Implementation;
using ContractsNuGetServices = NuGet.VisualStudio.Contracts.NuGetServices;
using ISettings = NuGet.Configuration.ISettings;
using ProvideBrokeredServiceAttribute = Microsoft.VisualStudio.Shell.ServiceBroker.ProvideBrokeredServiceAttribute;
using Resx = NuGet.PackageManagement.UI.Resources;
using ServiceAudience = Microsoft.VisualStudio.Shell.ServiceBroker.ServiceAudience;
using Task = System.Threading.Tasks.Task;
using UI = NuGet.PackageManagement.UI;
namespace NuGetVSExtension
{
/// <summary>
/// This is the class that implements the package exposed by this assembly.
/// </summary>
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", ProductVersion)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideToolWindow(typeof(PowerConsoleToolWindow),
Style = VsDockStyle.Tabbed,
Window = "{34E76E81-EE4A-11D0-AE2E-00A0C90FFFC3}", // this is the guid of the Output tool window, which is present in both VS and VWD
Orientation = ToolWindowOrientation.Right)]
[ProvideOptionPage(typeof(PackageSourceOptionsPage), "NuGet Package Manager", "Package Sources", 113, 114, true)]
[ProvideOptionPage(typeof(GeneralOptionPage), "NuGet Package Manager", "General", 113, 115, true)]
[ProvideSearchProvider(typeof(NuGetSearchProvider), "NuGet Search")]
// UI Context rule for a project that could be upgraded to PackageReference from packages.config based project.
// Only exception is this UI context doesn't get enabled for right-click on Reference since there is no extension point on references
// to know if there is packages.config file in this project hierarchy. So first-time right click on reference in a new VS instance
// will not show Migrator option.
[ProvideUIContextRule(GuidList.guidUpgradeableProjectLoadedString,
"UpgradeableProjectLoaded",
"SolutionExistsAndFullyLoaded & PackagesConfigBasedProjectLoaded",
new[] { "SolutionExistsAndFullyLoaded", "PackagesConfigBasedProjectLoaded" },
new[] { VSConstants.UICONTEXT.SolutionExistsAndFullyLoaded_string,
"HierSingleSelectionName:packages.config"})]
[ProvideAutoLoad(GuidList.guidUpgradeableProjectLoadedString, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(GuidList.guidAutoLoadNuGetString, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(VSConstants.UICONTEXT.ProjectRetargeting_string, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOrProjectUpgrading_string, PackageAutoLoadFlags.BackgroundLoad)]
[FontAndColorsRegistration("Package Manager Console", NuGetConsole.GuidList.GuidPackageManagerConsoleFontAndColorCategoryString, "{" + GuidList.guidNuGetPkgString + "}")]
[ProvideBrokeredService(ContractsNuGetServices.NuGetProjectServiceName, ContractsNuGetServices.Version1, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.SourceProviderServiceName, BrokeredServicesUtilities.SourceProviderServiceVersion, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.SourceProviderServiceName, BrokeredServicesUtilities.SourceProviderServiceVersion_1_0_1, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.SolutionManagerServiceName, BrokeredServicesUtilities.SolutionManagerServiceVersion, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.ProjectManagerServiceName, BrokeredServicesUtilities.ProjectManagerServiceVersion, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.ProjectUpgraderServiceName, BrokeredServicesUtilities.ProjectUpgraderServiceVersion, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.PackageFileServiceName, BrokeredServicesUtilities.PackageFileServiceVersion, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[ProvideBrokeredService(BrokeredServicesUtilities.SearchServiceName, BrokeredServicesUtilities.SearchServiceVersion, Audience = ServiceAudience.Local | ServiceAudience.RemoteExclusiveClient)]
[Guid(GuidList.guidNuGetPkgString)]
public sealed partial class NuGetPackage : AsyncPackage, IVsPackageExtensionProvider, IVsPersistSolutionOpts
{
private const string F1KeywordValuePmUI = "VS.NuGet.PackageManager.UI";
private AsyncLazy<IVsMonitorSelection> _vsMonitorSelection;
private IVsMonitorSelection VsMonitorSelection => ThreadHelper.JoinableTaskFactory.Run(_vsMonitorSelection.GetValueAsync);
private readonly ReentrantSemaphore _semaphore = ReentrantSemaphore.Create(1, NuGetUIThreadHelper.JoinableTaskFactory.Context, ReentrantSemaphore.ReentrancyMode.Freeform);
private DTE _dte;
private OleMenuCommand _managePackageDialogCommand;
private OleMenuCommand _managePackageForSolutionDialogCommand;
private OleMenuCommandService _mcs;
private uint _solutionExistsAndFullyLoadedContextCookie;
private uint _solutionNotBuildingAndNotDebuggingContextCookie;
private uint _solutionExistsCookie;
private bool _powerConsoleCommandExecuting;
private bool _initialized;
private NuGetPowerShellUsageCollector _nuGetPowerShellUsageCollector;
public NuGetPackage()
{
ServiceLocator.InitializePackageServiceProvider(this);
}
[Import]
private Lazy<IConsoleStatus> ConsoleStatus { get; set; }
[Import]
private Lazy<IDeleteOnRestartManager> DeleteOnRestartManager { get; set; }
[Import]
private Lazy<INuGetUILogger> OutputConsoleLogger { get; set; }
[Import]
private Lazy<INuGetProjectContext> ProjectContext { get; set; }
[Import]
private Lazy<ISettings> Settings { get; set; }
[Import]
private Lazy<IVsSolutionManager> SolutionManager { get; set; }
[Import]
private Lazy<SolutionUserOptions> SolutionUserOptions { get; set; }
/// <summary>
/// This initializes the IVSSourceControlTracker, even though SourceControlTracker is unused.
/// </summary>
[Import]
private Lazy<IVsSourceControlTracker> SourceControlTracker { get; set; }
[Import]
private Lazy<INuGetUIFactory> UIFactory { get; set; }
private IDisposable ProjectRetargetingHandler { get; set; }
private IDisposable ProjectUpgradeHandler { get; set; }
[Import]
private Lazy<IServiceBrokerProvider> ServiceBrokerProvider { get; set; }
[Import]
private Lazy<INuGetExperimentationService> NuGetExperimentationService { get; set; }
/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
NuGetVSTelemetryService.Initialize();
_nuGetPowerShellUsageCollector = new NuGetPowerShellUsageCollector();
await base.InitializeAsync(cancellationToken, progress);
// Add our command handlers for menu (commands must exist in the .vsct file)
await AddMenuCommandHandlersAsync();
// This instantiates a decoupled ICommand instance responsible to locate and display output pane by a UI control
UI.Commands.ShowErrorsCommand = new ShowErrorsCommand(this);
_vsMonitorSelection = new AsyncLazy<IVsMonitorSelection>(
async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// get the UI context cookie for the debugging mode
var vsMonitorSelection = await this.GetServiceAsync<IVsMonitorSelection, IVsMonitorSelection>();
Assumes.Present(vsMonitorSelection);
// get the solution not building and not debugging cookie
var guidCmdUI = VSConstants.UICONTEXT.SolutionExistsAndFullyLoaded_guid;
vsMonitorSelection.GetCmdUIContextCookie(
ref guidCmdUI, out _solutionExistsAndFullyLoadedContextCookie);
guidCmdUI = VSConstants.UICONTEXT.SolutionExistsAndNotBuildingAndNotDebugging_guid;
vsMonitorSelection.GetCmdUIContextCookie(
ref guidCmdUI, out _solutionNotBuildingAndNotDebuggingContextCookie);
guidCmdUI = VSConstants.UICONTEXT.SolutionExists_guid;
vsMonitorSelection.GetCmdUIContextCookie(
ref guidCmdUI, out _solutionExistsCookie);
return vsMonitorSelection;
},
ThreadHelper.JoinableTaskFactory);
await NuGetBrokeredServiceFactory.ProfferServicesAsync(this);
VsShellUtilities.ShutdownToken.Register(InstanceCloseTelemetryEmitter.OnShutdown);
var componentModel = await this.GetFreeThreadedServiceAsync<SComponentModel, IComponentModel>();
Assumes.Present(componentModel);
componentModel.DefaultCompositionService.SatisfyImportsOnce(this);
}
/// <summary>
/// Initialize solution experiences for this package and also add required event handlers.
/// </summary>
private async Task InitializeSolutionExperiencesAsync()
{
await _semaphore.ExecuteAsync(async () =>
{
if (_initialized)
{
return;
}
var componentModel = await this.GetFreeThreadedServiceAsync<SComponentModel, IComponentModel>();
Assumes.Present(componentModel);
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
SolutionManager.Value.AfterNuGetProjectRenamed += SolutionManager_NuGetProjectRenamed;
Brushes.LoadVsBrushes(NuGetExperimentationService.Value);
_dte = await this.GetDTEAsync();
Assumes.Present(_dte);
if (SolutionManager.Value.NuGetProjectContext == null)
{
SolutionManager.Value.NuGetProjectContext = ProjectContext.Value;
}
// when NuGet loads, if the current solution has some package
// folders marked for deletion (because a previous uninstalltion didn't succeed),
// delete them now.
if (await SolutionManager.Value.IsSolutionOpenAsync())
{
await DeleteOnRestartManager.Value.DeleteMarkedPackageDirectoriesAsync(ProjectContext.Value);
}
IVsTrackProjectRetargeting vsTrackProjectRetargeting = await this.GetServiceAsync<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting>();
IVsMonitorSelection vsMonitorSelection = await this.GetServiceAsync<IVsMonitorSelection, IVsMonitorSelection>(throwOnFailure: false);
ProjectRetargetingHandler = new ProjectRetargetingHandler(
_dte,
SolutionManager.Value,
this,
componentModel,
vsTrackProjectRetargeting,
vsMonitorSelection);
IVsSolution2 vsSolution2 = await this.GetServiceAsync<SVsSolution, IVsSolution2>();
ProjectUpgradeHandler = new ProjectUpgradeHandler(
SolutionManager.Value,
vsSolution2);
SolutionUserOptions.Value.LoadSettings();
_initialized = true;
});
}
private void SolutionManager_NuGetProjectRenamed(object sender, NuGetProjectEventArgs e)
{
NuGetUIThreadHelper.JoinableTaskFactory.Run(async () =>
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var project = await SolutionManager.Value.GetVsProjectAdapterAsync(
await SolutionManager.Value.GetNuGetProjectSafeNameAsync(e.NuGetProject));
var windowFrame = await FindExistingWindowFrameAsync(project.Project);
if (windowFrame != null)
{
windowFrame.SetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, string.Format(
CultureInfo.CurrentCulture,
Resx.Label_NuGetWindowCaption,
project.ProjectName));
}
});
}
private async Task AddMenuCommandHandlersAsync()
{
_mcs = await this.GetServiceAsync<IMenuCommandService, OleMenuCommandService>();
if (null != _mcs)
{
// Switch to Main Thread before calling AddCommand which calls GetService() which should
// always be called on UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// menu command for upgrading packages.config files to PackageReference - References context menu
var upgradeNuGetProjectCommandID = new CommandID(GuidList.guidNuGetDialogCmdSet, PkgCmdIDList.cmdidUpgradeNuGetProject);
var upgradeNuGetProjectCommand = new OleMenuCommand(ExecuteUpgradeNuGetProjectCommand, null,
BeforeQueryStatusForUpgradeNuGetProject, upgradeNuGetProjectCommandID);
_mcs.AddCommand(upgradeNuGetProjectCommand);
// menu command for upgrading packages.config files to PackageReference - packages.config context menu
var upgradePackagesConfigCommandID = new CommandID(GuidList.guidNuGetDialogCmdSet, PkgCmdIDList.cmdidUpgradePackagesConfig);
var upgradePackagesConfigCommand = new OleMenuCommand(ExecuteUpgradeNuGetProjectCommand, null,
BeforeQueryStatusForUpgradePackagesConfig, upgradePackagesConfigCommandID);
_mcs.AddCommand(upgradePackagesConfigCommand);
// menu command for opening Package Manager Console
var toolwndCommandID = new CommandID(GuidList.guidNuGetConsoleCmdSet, PkgCmdIDList.cmdidPowerConsole);
var powerConsoleExecuteCommand = new OleMenuCommand(ExecutePowerConsoleCommand, null, BeforeQueryStatusForPowerConsole, toolwndCommandID);
// '$' - This indicates that the input line other than the argument forms a single argument string with no autocompletion
// Autocompletion for filename(s) is supported for option 'p' or 'd' which is not applicable for this command
powerConsoleExecuteCommand.ParametersDescription = "$";
_mcs.AddCommand(powerConsoleExecuteCommand);
// menu command for opening NuGet Debug Console
// Remove debug console from Tools menu for 3.0.0-beta.
//CommandID debugWndCommandID = new CommandID(GuidList.guidNuGetDebugConsoleCmdSet, PkgCmdIDList.cmdidDebugConsole);
//OleMenuCommand debugConsoleExecuteCommand = new OleMenuCommand(ShowDebugConsole, null, null, debugWndCommandID);
// '$' - This indicates that the input line other than the argument forms a single argument string with no autocompletion
// Autocompletion for filename(s) is supported for option 'p' or 'd' which is not applicable for this command
//debugConsoleExecuteCommand.ParametersDescription = "$";
//_mcs.AddCommand(debugConsoleExecuteCommand);
// menu command for opening Manage NuGet packages dialog
var managePackageDialogCommandID = new CommandID(GuidList.guidNuGetDialogCmdSet, PkgCmdIDList.cmdidAddPackageDialog);
_managePackageDialogCommand = new OleMenuCommand(ShowManageLibraryPackageDialog, null, BeforeQueryStatusForAddPackageDialog, managePackageDialogCommandID);
// '$' - This indicates that the input line other than the argument forms a single argument string with no autocompletion
// Autocompletion for filename(s) is supported for option 'p' or 'd' which is not applicable for this command
_managePackageDialogCommand.ParametersDescription = "$";
_mcs.AddCommand(_managePackageDialogCommand);
// menu command for opening "Manage NuGet packages for solution" dialog
var managePackageForSolutionDialogCommandID = new CommandID(GuidList.guidNuGetDialogCmdSet, PkgCmdIDList.cmdidAddPackageDialogForSolution);
_managePackageForSolutionDialogCommand = new OleMenuCommand(ShowManageLibraryPackageForSolutionDialog, null, BeforeQueryStatusForAddPackageForSolutionDialog, managePackageForSolutionDialogCommandID);
// '$' - This indicates that the input line other than the argument forms a single argument string with no autocompletion
// Autocompletion for filename(s) is supported for option 'p' or 'd' which is not applicable for this command
_managePackageForSolutionDialogCommand.ParametersDescription = "$";
_mcs.AddCommand(_managePackageForSolutionDialogCommand);
// menu command for opening Package Source settings options page
var settingsCommandID = new CommandID(GuidList.guidNuGetConsoleCmdSet, PkgCmdIDList.cmdidSourceSettings);
var settingsMenuCommand = new OleMenuCommand(ShowPackageSourcesOptionPage, settingsCommandID);
_mcs.AddCommand(settingsMenuCommand);
// menu command for opening General options page
var generalSettingsCommandID = new CommandID(GuidList.guidNuGetToolsGroupCmdSet, PkgCmdIDList.cmdIdGeneralSettings);
var generalSettingsCommand = new OleMenuCommand(ShowGeneralSettingsOptionPage, generalSettingsCommandID);
_mcs.AddCommand(generalSettingsCommand);
// menu command for the Update option on each package or a selection of packages
var updatePackageDialogCommandID = new CommandID(GuidList.guidNuGetDialogCmdSet, PkgCmdIDList.cmdidUpdatePackage);
var updatePackageDialogCommand = new OleMenuCommand(ShowUpdatePackageDialog, changeHandler: null, BeforeQueryStatusForAddPackageDialog, updatePackageDialogCommandID);
updatePackageDialogCommand.ParametersDescription = "$";
_mcs.AddCommand(updatePackageDialogCommand);
// menu command for the Update All option on the Dependencies -> Packages node
var updatePackagesDialogCommandID = new CommandID(GuidList.guidNuGetDialogCmdSet, PkgCmdIDList.cmdidUpdatePackages);
var updatePackagesDialogCommand = new OleMenuCommand(ShowUpdatePackagesDialog, changeHandler: null, BeforeQueryStatusForAddPackageDialog, updatePackagesDialogCommandID);
updatePackageDialogCommand.ParametersDescription = "$";
_mcs.AddCommand(updatePackagesDialogCommand);
}
}
private void ExecutePowerConsoleCommand(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (ShouldInitializeSolutionExperiences())
{
NuGetUIThreadHelper.JoinableTaskFactory.Run(InitializeSolutionExperiencesAsync);
}
// Get the instance number 0 of this tool window. This window is single instance so this instance
// is actually the only one.
// The last flag is set to true so that if the tool window does not exists it will be created.
ToolWindowPane window = FindToolWindow(typeof(PowerConsoleToolWindow), id: 0, create: true);
if ((null == window)
|| (null == window.Frame))
{
throw new NotSupportedException(Resources.CanNotCreateWindow);
}
var windowFrame = (IVsWindowFrame)window.Frame;
ErrorHandler.ThrowOnFailure(windowFrame.Show());
// Parse the arguments to determine the command and arguments to be passed to IHost
// passed which is of type OleMenuCmdEventArgs
string command = null;
var eventArgs = e as OleMenuCmdEventArgs;
if (eventArgs != null
&& eventArgs.InValue != null)
{
command = eventArgs.InValue as string;
}
// If the command string is null or empty, simply launch the console and return
if (!string.IsNullOrEmpty(command))
{
var powerConsoleService = (IPowerConsoleService)window;
if (powerConsoleService.Execute(command, null))
{
_powerConsoleCommandExecuting = true;
powerConsoleService.ExecuteEnd += PowerConsoleService_ExecuteEnd;
}
}
}
private void PowerConsoleService_ExecuteEnd(object sender, EventArgs e)
{
_powerConsoleCommandExecuting = false;
}
private async Task<IVsWindowFrame> FindExistingWindowFrameAsync(Project project)
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
foreach (var windowFrame in VsUtility.GetDocumentWindows(uiShell))
{
object docView;
var hr = windowFrame.GetProperty(
(int)__VSFPROPID.VSFPROPID_DocView,
out docView);
if (hr == VSConstants.S_OK
&& docView is PackageManagerWindowPane)
{
var packageManagerWindowPane = (PackageManagerWindowPane)docView;
if (packageManagerWindowPane.Model.IsSolution)
{
// the window is the solution package manager
continue;
}
var projects = packageManagerWindowPane.Model.Context.Projects;
if (projects.Count() != 1)
{
continue;
}
IProjectContextInfo existingProject = projects.First();
IServiceBroker serviceBroker = await ServiceBrokerProvider.Value.GetAsync();
IProjectMetadataContextInfo projectMetadata = await existingProject.GetMetadataAsync(
serviceBroker,
CancellationToken.None);
if (string.Equals(projectMetadata.Name, project.Name, StringComparison.OrdinalIgnoreCase))
{
return windowFrame;
}
}
}
return null;
}
private async Task<IVsWindowFrame> CreateNewWindowFrameAsync(Project project)
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var vsProject = await project.ToVsHierarchyAsync();
var documentName = project.FullName;
// Find existing hierarchy and item id of the document window if it's
// already registered.
var rdt = await this.GetServiceAsync<IVsRunningDocumentTable, IVsRunningDocumentTable>();
Assumes.Present(rdt);
IVsHierarchy hier;
uint itemId;
var docData = IntPtr.Zero;
int hr;
try
{
uint cookie;
hr = rdt.FindAndLockDocument(
(uint)_VSRDTFLAGS.RDT_NoLock,
documentName,
out hier,
out itemId,
out docData,
out cookie);
if (hr != VSConstants.S_OK)
{
// the docuemnt window is not registered yet. So use the project as the
// hierarchy.
hier = vsProject;
itemId = (uint)VSConstants.VSITEMID.Root;
}
}
finally
{
if (docData != IntPtr.Zero)
{
Marshal.Release(docData);
docData = IntPtr.Zero;
}
}
// Create the doc window using the hiearchy & item id.
return await CreateDocWindowAsync(project, documentName, hier, itemId);
}
private async Task<IVsWindowFrame> CreateDocWindowAsync(
Project project,
string documentName,
IVsHierarchy hier,
uint itemId)
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var windowFlags =
(uint)_VSRDTFLAGS.RDT_DontAddToMRU |
(uint)_VSRDTFLAGS.RDT_DontSaveAs;
if (!await SolutionManager.Value.IsSolutionAvailableAsync())
{
throw new InvalidOperationException(Resources.SolutionIsNotSaved);
}
var uniqueName = project.GetUniqueName();
var nugetProject = await SolutionManager.Value.GetNuGetProjectAsync(uniqueName);
// If we failed to generate a cache entry in the solution manager something went wrong.
if (nugetProject == null)
{
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, Resources.ProjectHasAnInvalidNuGetConfiguration, project.Name));
}
// load packages.config. This makes sure that an exception will get thrown if there
// are problems with packages.config, such as duplicate packages. When an exception
// is thrown, an error dialog will pop up and this doc window will not be created.
_ = await nugetProject.GetInstalledPackagesAsync(CancellationToken.None);
IServiceBroker serviceBroker = await ServiceBrokerProvider.Value.GetAsync();
IProjectContextInfo contextInfo = await ProjectContextInfo.CreateAsync(nugetProject, CancellationToken.None);
INuGetUI uiController = await UIFactory.Value.CreateAsync(serviceBroker, contextInfo);
// This model takes ownership of --- and Dispose() responsibility for --- the INuGetUI instance.
var model = new PackageManagerModel(
uiController,
isSolution: false,
editorFactoryGuid: GuidList.guidNuGetEditorType);
PackageManagerControl control = await PackageManagerControl.CreateAsync(model, OutputConsoleLogger.Value);
var windowPane = new PackageManagerWindowPane(control);
var guidEditorType = GuidList.guidNuGetEditorType;
var guidCommandUI = Guid.Empty;
var caption = string.Format(
CultureInfo.CurrentCulture,
Resx.Label_NuGetWindowCaption,
project.Name);
IVsWindowFrame windowFrame;
var uiShell = await this.GetServiceAsync<SVsUIShell, IVsUIShell>();
Assumes.Present(uiShell);
var ppunkDocView = IntPtr.Zero;
var ppunkDocData = IntPtr.Zero;
var hr = 0;
try
{
ppunkDocView = Marshal.GetIUnknownForObject(windowPane);
ppunkDocData = Marshal.GetIUnknownForObject(model);
hr = uiShell.CreateDocumentWindow(
windowFlags,
documentName,
(IVsUIHierarchy)hier,
itemId,
ppunkDocView,
ppunkDocData,
ref guidEditorType,
null,
ref guidCommandUI,
null,
caption,
string.Empty,
null,
out windowFrame);
if (windowFrame != null)
{
WindowFrameHelper.AddF1HelpKeyword(windowFrame, keywordValue: F1KeywordValuePmUI);
WindowFrameHelper.DisableWindowAutoReopen(windowFrame);
}
}
finally
{
if (ppunkDocView != IntPtr.Zero)
{
Marshal.Release(ppunkDocData);
}
if (ppunkDocData != IntPtr.Zero)
{
Marshal.Release(ppunkDocView);
}
}
ErrorHandler.ThrowOnFailure(hr);
return windowFrame;
}
private void ExecuteUpgradeNuGetProjectCommand(object sender, EventArgs e)
{
NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await ExecuteUpgradeNuGetProjectCommandAsync(sender, e);
})
.PostOnFailure(nameof(NuGetPackage), nameof(ExecuteUpgradeNuGetProjectCommand));
}
private async Task ExecuteUpgradeNuGetProjectCommandAsync(object sender, EventArgs e)
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (ShouldInitializeSolutionExperiences())
{
await InitializeSolutionExperiencesAsync();
}
Project project = VsMonitorSelection.GetActiveProject();
if (!await NuGetProjectUpgradeUtility.IsNuGetProjectUpgradeableAsync(nuGetProject: null, project))
{
MessageHelper.ShowWarningMessage(Resources.ProjectMigrateErrorMessage, Resources.ErrorDialogBoxTitle);
return;
}
string uniqueName = await project.GetCustomUniqueNameAsync();
// Close NuGet Package Manager if it is open for this project
IVsWindowFrame windowFrame = await FindExistingWindowFrameAsync(project);
windowFrame?.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty);
IServiceBroker serviceBroker = await ServiceBrokerProvider.Value.GetAsync();
NuGetProject nuGetProject = await SolutionManager.Value.GetNuGetProjectAsync(uniqueName);
IProjectContextInfo projectContextInfo = await ProjectContextInfo.CreateAsync(nuGetProject, CancellationToken.None);
using (INuGetUI uiController = await UIFactory.Value.CreateAsync(serviceBroker, projectContextInfo))
{
await uiController.UIContext.UIActionEngine.UpgradeNuGetProjectAsync(uiController, projectContextInfo);
uiController.UIContext.UserSettingsManager.PersistSettings();
}
}
private void ShowManageLibraryPackageDialog(object sender, EventArgs e)
{
string parameterString = (e as OleMenuCmdEventArgs)?.InValue as string;
NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async delegate
{
await ShowManageLibraryPackageDialogAsync(GetSearchText(parameterString));
}).PostOnFailure(nameof(NuGetPackage), nameof(ShowManageLibraryPackageDialog));
}
private async Task ShowManageLibraryPackageDialogAsync(string searchText, ShowUpdatePackageOptions updatePackageOptions = null)
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (ShouldInitializeSolutionExperiences())
{
await InitializeSolutionExperiencesAsync();
}
// *** temp code
Project project = VsMonitorSelection.GetActiveProject();
if (project != null
&&
!project.IsUnloaded()
&&
await EnvDTEProjectUtility.IsSupportedAsync(project))
{
IVsWindowFrame windowFrame = await FindExistingWindowFrameAsync(project);
if (windowFrame == null)
{
windowFrame = await CreateNewWindowFrameAsync(project);
}
if (windowFrame != null)
{
ShowUpdatePackages(windowFrame, updatePackageOptions);
Search(windowFrame, searchText);
windowFrame.Show();
}
}
else
{
// show error message when no supported project is selected.
var projectName = project != null ? project.Name : string.Empty;
var errorMessage = string.IsNullOrEmpty(projectName)
? Resources.NoProjectSelected
: string.Format(CultureInfo.CurrentCulture, Resources.DTE_ProjectUnsupported, projectName);
MessageHelper.ShowWarningMessage(errorMessage, Resources.ErrorDialogBoxTitle);
}
}
private void ShowUpdatePackageDialog(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
var updatePackages = ShowUpdatePackageOptions.UpdatePackages(GetSelectedPackages());
string parameterString = (e as OleMenuCmdEventArgs)?.InValue as string;
NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async delegate
{
await ShowManageLibraryPackageDialogAsync(GetSearchText(parameterString), updatePackages);
}).PostOnFailure(nameof(NuGetPackage), nameof(ShowUpdatePackageDialog));
}
private void ShowUpdatePackagesDialog(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
string parameterString = (e as OleMenuCmdEventArgs)?.InValue as string;
NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async delegate
{
await ShowManageLibraryPackageDialogAsync(GetSearchText(parameterString), ShowUpdatePackageOptions.UpdateAllPackages());
}).PostOnFailure(nameof(NuGetPackage), nameof(ShowUpdatePackagesDialog));
}
private async Task<IVsWindowFrame> FindExistingSolutionWindowFrameAsync()
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var uiShell = await this.GetServiceAsync<SVsUIShell, IVsUIShell>();
foreach (var windowFrame in VsUtility.GetDocumentWindows(uiShell))
{
object property;
var hr = windowFrame.GetProperty(
(int)__VSFPROPID.VSFPROPID_DocData,
out property);
var packageManagerControl = VsUtility.GetPackageManagerControl(windowFrame);
if (hr == VSConstants.S_OK
&&
property is IVsSolution
&&
packageManagerControl != null)
{
return windowFrame;
}
}
return null;
}
private static string GetSearchText(string parameterString)
{
if (parameterString == null)
{
return null;
}
// The parameterString looks like:
// "jquery /searchin:online"
// or just "jquery"
parameterString = parameterString.Trim();
var lastIndexOfSearchInSwitch = parameterString.LastIndexOf("/searchin:", StringComparison.OrdinalIgnoreCase);
if (lastIndexOfSearchInSwitch == -1)
{
return parameterString;
}
return parameterString.Substring(0, lastIndexOfSearchInSwitch);
}
/// <summary>
/// Gets the names of the package or packages that are selected in the VsHierarchy.
/// </summary>
/// <returns>
/// A string array containing the list of selected packages in the same hierarchy.
/// If only one package is selected, the array will contain one package name, otherwise, it will be a list of package names.
/// If the selected packages are across multiple hierarchies or if no packages are selected, an empty array will be returned.
/// This method will return <c>null</c> if it fails to retrieve the selected hierarchy.
/// </returns>
private string[] GetSelectedPackages()
{
ThreadHelper.ThrowIfNotOnUIThread();
IntPtr hierarchyPtr = IntPtr.Zero;
IntPtr selectionContainerPtr = IntPtr.Zero;
try
{
if (!ErrorHandler.Succeeded(VsMonitorSelection.GetCurrentSelection(out hierarchyPtr, out uint itemId, out IVsMultiItemSelect multiItemSelect, out selectionContainerPtr))
|| (itemId == VSConstants.VSITEMID_NIL && hierarchyPtr != IntPtr.Zero))
{
return null;
}
var packages = new List<string>();
if (itemId != VSConstants.VSITEMID_SELECTION)
{
// This is a single item selection.
if (Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)) is IVsHierarchy hierarchy
&& TryGetPackageNameFromHierarchy(hierarchy, itemId, out string packageName))
{
packages.Add(packageName);
}
}
else if (multiItemSelect != null)
{
// This is a multiple item selection.
VSITEMSELECTION[] vsItemSelections = multiItemSelect.GetSelectedItemsInSingleHierachy();
if (vsItemSelections != null)
{
foreach (VSITEMSELECTION sel in vsItemSelections)
{
if (sel.pHier != null && TryGetPackageNameFromHierarchy(sel.pHier, sel.itemid, out string packageName))
{
packages.Add(packageName);
}
}
}
}
return packages.ToArray();
}
finally
{
if (hierarchyPtr != IntPtr.Zero)
{
Marshal.Release(hierarchyPtr);
}
if (selectionContainerPtr != IntPtr.Zero)
{
Marshal.Release(selectionContainerPtr);
}
}
}
private static bool TryGetPackageNameFromHierarchy(IVsHierarchy hierarchy, uint itemId, out string packageName)
{
Assumes.NotNull(hierarchy);
ThreadHelper.ThrowIfNotOnUIThread();
int hr = hierarchy.GetProperty(itemId, (int)__VSHPROPID7.VSHPROPID_ProjectTreeCapabilities, out object treeCapabilities);
packageName = ParsePackageNameFromTreeCapabilities(treeCapabilities as string);
return hr == VSConstants.S_OK && packageName != null;
}
private static string ParsePackageNameFromTreeCapabilities(string treeCapabilities)
{
const string packageNamePrefix = "$ID:";
if (treeCapabilities == null)
{
return null;
}
string[] capabilities = treeCapabilities.Split(' ');
string packageName = capabilities.SingleOrDefault(p => p.StartsWith(packageNamePrefix, StringComparison.Ordinal));
return packageName?.Substring(packageNamePrefix.Length);
}
private async Task<IVsWindowFrame> CreateDocWindowForSolutionAsync()
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
IVsWindowFrame windowFrame = null;
var solution = await this.GetServiceAsync<SVsSolution, IVsSolution>();
var uiShell = await this.GetServiceAsync<SVsUIShell, IVsUIShell>();
var windowFlags =
(uint)_VSRDTFLAGS.RDT_DontAddToMRU |
(uint)_VSRDTFLAGS.RDT_DontSaveAs;
// when VSSolutionManager is already initialized, then use the existing APIs to check pre-conditions.
if (!await SolutionManager.Value.IsSolutionAvailableAsync())
{
throw new InvalidOperationException(Resources.SolutionIsNotSaved);
}
IServiceBroker serviceBroker = await ServiceBrokerProvider.Value.GetAsync();
IReadOnlyCollection<IProjectContextInfo> projectContexts;
using (INuGetProjectManagerService projectManagerService = await serviceBroker.GetProxyAsync<INuGetProjectManagerService>(
NuGetServices.ProjectManagerService))
{
Assumes.NotNull(projectManagerService);
projectContexts = await projectManagerService.GetProjectsAsync(CancellationToken.None);
if (projectContexts.Count == 0)
{
MessageHelper.ShowWarningMessage(Resources.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
return null;
}
}
INuGetUI uiController = await UIFactory.Value.CreateAsync(serviceBroker, projectContexts.ToArray());
var solutionName = (string)_dte.Solution.Properties.Item("Name").Value;
// This model takes ownership of --- and Dispose() responsibility for --- the INuGetUI instance.
var model = new PackageManagerModel(
uiController,
isSolution: true,
editorFactoryGuid: GuidList.guidNuGetEditorType)
{
SolutionName = solutionName
};
PackageManagerControl control = await PackageManagerControl.CreateAsync(model, OutputConsoleLogger.Value);
var windowPane = new PackageManagerWindowPane(control);
var guidEditorType = GuidList.guidNuGetEditorType;
var guidCommandUI = Guid.Empty;
var caption = Resx.Label_SolutionNuGetWindowCaption;
var documentName = await SolutionManager.Value.GetSolutionFilePathAsync();
var ppunkDocView = IntPtr.Zero;
var ppunkDocData = IntPtr.Zero;
var hr = 0;
try
{
ppunkDocView = Marshal.GetIUnknownForObject(windowPane);
ppunkDocData = Marshal.GetIUnknownForObject(model);
hr = uiShell.CreateDocumentWindow(
windowFlags,
documentName,
(IVsUIHierarchy)solution,
(uint)VSConstants.VSITEMID.Root,
ppunkDocView,
ppunkDocData,
ref guidEditorType,
null,
ref guidCommandUI,
null,
caption,
string.Empty,
null,
out windowFrame);
if (windowFrame != null)
{
WindowFrameHelper.AddF1HelpKeyword(windowFrame, keywordValue: F1KeywordValuePmUI);
WindowFrameHelper.DisableWindowAutoReopen(windowFrame);
}
}
finally
{
if (ppunkDocView != IntPtr.Zero)
{
Marshal.Release(ppunkDocData);
}
if (ppunkDocData != IntPtr.Zero)
{
Marshal.Release(ppunkDocView);
}
}
ErrorHandler.ThrowOnFailure(hr);
return windowFrame;
}
private void ShowManageLibraryPackageForSolutionDialog(object sender, EventArgs e)
{
NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async delegate
{
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (ShouldInitializeSolutionExperiences())
{
await InitializeSolutionExperiencesAsync();
}
var windowFrame = await FindExistingSolutionWindowFrameAsync();
if (windowFrame == null)
{
// Create the window frame
windowFrame = await CreateDocWindowForSolutionAsync();
}
if (windowFrame != null)
{
// process search string
string parameterString = null;
var args = e as OleMenuCmdEventArgs;
if (args != null)
{
parameterString = args.InValue as string;
}
var searchText = GetSearchText(parameterString);
Search(windowFrame, searchText);
windowFrame.Show();
}
}).PostOnFailure(nameof(NuGetPackage), nameof(ShowManageLibraryPackageForSolutionDialog));
}
/// <summary>
/// Search for packages using the searchText.
/// </summary>
/// <param name="windowFrame">A window frame that hosts the PackageManagerControl.</param>
/// <param name="searchText">Search text.</param>
private void Search(IVsWindowFrame windowFrame, string searchText)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (string.IsNullOrWhiteSpace(searchText))
{
return;
}
var packageManagerControl = VsUtility.GetPackageManagerControl(windowFrame);
if (packageManagerControl != null)
{
if (packageManagerControl.ActiveFilter != UI.ItemFilter.All)
{
packageManagerControl.ActiveFilter = UI.ItemFilter.All;
}