1+ #nullable enable
2+ namespace XrmTools . Commands ;
3+
4+ using Community . VisualStudio . Toolkit ;
5+ using Microsoft . VisualStudio . ComponentModelHost ;
6+ using Microsoft . VisualStudio . Shell ;
7+ using System ;
8+ using System . Collections . Generic ;
9+ using System . ComponentModel . Composition ;
10+ using System . Threading . Tasks ;
11+ using XrmTools . Analyzers ;
12+ using XrmTools . Environments ;
13+ using XrmTools . Helpers ;
14+ using XrmTools . Logging . Compatibility ;
15+ using XrmTools . Services ;
16+ using XrmTools . UI ;
17+ using XrmTools . WebApi ;
18+ using XrmTools . Xrm . Repositories ;
19+ using System . Diagnostics . CodeAnalysis ;
20+ using XrmTools . Resources ;
21+
22+ [ Command ( PackageGuids . XrmToolsCmdSetIdString , PackageIds . UnregisterPluginCmdId ) ]
23+ internal sealed class UnregisterCommand : BaseCommand < UnregisterCommand >
24+ {
25+ [ Import ]
26+ internal IWebApiService WebApiService { get ; set ; } = null ! ;
27+
28+ [ Import ]
29+ internal IEnvironmentProvider EnvironmentProvider { get ; set ; } = null ! ;
30+
31+ [ Import ]
32+ internal IXrmMetaDataService MetaDataService { get ; set ; } = null ! ;
33+
34+ [ Import ]
35+ internal IRepositoryFactory RepositoryFactory { get ; set ; } = null ! ;
36+
37+ [ Import ]
38+ internal ILogger < RegisterPluginCommand > Logger { get ; set ; } = null ! ;
39+
40+ [ Import ]
41+ internal IPluginRegistrationService PluginRegistrationService { get ; set ; } = null ! ;
42+
43+ override protected async Task ExecuteAsync ( OleMenuCmdEventArgs e )
44+ {
45+ var activeItem = await VS . Solutions . GetActiveItemAsync ( ) ;
46+ if ( activeItem is null || activeItem . FullPath is null || ! ( activeItem . Type is SolutionItemType . Project or SolutionItemType . PhysicalFile ) ) return ;
47+
48+ var project = activeItem . Type == SolutionItemType . Project ? ( Project ) activeItem : activeItem . FindParent ( SolutionItemType . Project ) as Project ;
49+ if ( project is null )
50+ {
51+ await VS . MessageBox . ShowErrorAsync ( Vsix . Name , "The selected item is not a project or part of a project." ) ;
52+ return ;
53+ }
54+
55+ var ui = new VsPluginRegistrationUI ( ) ;
56+ var confirmed = await ui . ConfirmUnregsiterAssemblyAsync ( project . Name ) ;
57+ if ( ! confirmed ) return ;
58+
59+ await VS . StatusBar . StartAnimationAsync ( StatusAnimation . General ) ;
60+ await VS . StatusBar . ShowMessageAsync ( "Unregistering from Dataverse..." ) ;
61+
62+ try
63+ {
64+ var input = new RegistrationInput (
65+ itemFullPath : activeItem . FullPath ,
66+ isProject : true ,
67+ nugetPackagePath : null ) ;
68+
69+ var result = await PluginRegistrationService ! . UnregisterAsync ( input , ui ) ;
70+
71+ if ( ! result . Succeeded )
72+ {
73+ await VS . StatusBar . EndAnimationAsync ( StatusAnimation . General ) ;
74+ await VS . StatusBar . ShowMessageAsync ( "Unregistration failed." ) ;
75+ await VS . MessageBox . ShowErrorAsync ( Vsix . Name , result . Message ) ;
76+ return ;
77+ }
78+
79+ await VS . StatusBar . ShowMessageAsync ( result . Message ) ;
80+ }
81+ catch ( Exception ex )
82+ {
83+ Logger . LogError ( ex , "An unexpected error occurred during plugin registration." ) ;
84+ await VS . MessageBox . ShowErrorAsync ( Vsix . Name , "Unregistration failed due to an unexpected error. " + ex . Message ) ;
85+ }
86+ finally
87+ {
88+ await VS . StatusBar . EndAnimationAsync ( StatusAnimation . General ) ;
89+ }
90+ }
91+
92+
93+ protected override async Task InitializeCompletedAsync ( )
94+ {
95+ //Command.Supported = false;
96+ try
97+ {
98+ var componentModel = await Package . GetServiceAsync < SComponentModel , IComponentModel > ( ) . ConfigureAwait ( false ) ;
99+ componentModel ? . DefaultCompositionService . SatisfyImportsOnce ( this ) ;
100+ EnsureDependencies ( ) ;
101+ }
102+ catch ( Exception ex )
103+ {
104+ Logger ? . LogError ( ex , "An error occurred while initializing the RegisterPluginCommand." ) ;
105+ await VS . MessageBox . ShowErrorAsync ( Vsix . Name , "An error occurred while initializing the RegisterPluginCommand. " + ex . Message ) ;
106+ return ;
107+ }
108+ }
109+
110+ [ MemberNotNull ( nameof ( Logger ) , nameof ( MetaDataService ) , nameof ( WebApiService ) ,
111+ nameof ( EnvironmentProvider ) , nameof ( RepositoryFactory ) ) ]
112+ private void EnsureDependencies ( )
113+ {
114+ if ( Logger == null ) throw new InvalidOperationException ( string . Format ( Strings . MissingServiceDependency , nameof ( RegisterPluginCommand ) , nameof ( Logger ) ) ) ;
115+ if ( MetaDataService == null ) throw new InvalidOperationException ( string . Format ( Strings . MissingServiceDependency , nameof ( RegisterPluginCommand ) , nameof ( MetaDataService ) ) ) ;
116+ if ( WebApiService == null ) throw new InvalidOperationException ( string . Format ( Strings . MissingServiceDependency , nameof ( RegisterPluginCommand ) , nameof ( WebApiService ) ) ) ;
117+ if ( EnvironmentProvider == null ) throw new InvalidOperationException ( string . Format ( Strings . MissingServiceDependency , nameof ( RegisterPluginCommand ) , nameof ( EnvironmentProvider ) ) ) ;
118+ if ( RepositoryFactory == null ) throw new InvalidOperationException ( string . Format ( Strings . MissingServiceDependency , nameof ( RegisterPluginCommand ) , nameof ( RepositoryFactory ) ) ) ;
119+ if ( PluginRegistrationService == null ) throw new InvalidOperationException ( string . Format ( Strings . MissingServiceDependency , nameof ( RegisterPluginCommand ) , nameof ( PluginRegistrationService ) ) ) ;
120+ }
121+ }
122+ #nullable restore
0 commit comments