private void RemoveLicenseHeaderFromAllFilesInSolutionCallback(object sender, EventArgs e) { Solution solution = _dte.Solution; IVsStatusbar statusBar = (IVsStatusbar)GetService(typeof(SVsStatusbar)); var removeLicenseHeaderFromAllProjects = new RemoveLicenseHeaderFromAllFilesInSolutionCommand(statusBar, _licenseReplacer); bool resharperSuspended = CommandUtility.ExecuteCommandIfExists("ReSharper_Suspend", _dte); try { removeLicenseHeaderFromAllProjects.Execute(solution); } catch (Exception exception) { MessageBoxHelper.Information( string.Format( "The command '{0}' failed with the exception '{1}'. See Visual Studio Output Window for Details.", removeLicenseHeaderFromAllProjects.GetCommandName(), exception.Message)); OutputWindowHandler.WriteMessage(exception.ToString()); } if (resharperSuspended) { CommandUtility.ExecuteCommand("ReSharper_Resume", _dte); } }
/// <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) { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_OnAssemblyResolve; JoinableTaskFactory = ThreadHelper.JoinableTaskFactory; LicenseHeaderExtractor = new LicenseHeaderExtractor(); await base.InitializeAsync(cancellationToken, progress); await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); Dte2 = await GetServiceAsync(typeof(DTE)) as DTE2; Assumes.Present(Dte2); CreateAndConfigureFileAppender(Path.GetFileNameWithoutExtension(Dte2?.Solution.FullName)); await CreateAndConfigureOutputPaneAppenderAsync(); s_log.Info("Logger has been initialized"); _addedItems = new Stack <ProjectItem>(); await AddHeaderToProjectItemCommand.InitializeAsync(this); await RemoveHeaderFromProjectItemCommand.InitializeAsync(this); await AddLicenseHeaderToAllFilesInSolutionCommand.InitializeAsync(this); await RemoveLicenseHeaderFromAllFilesInSolutionCommand.InitializeAsync(this); await AddNewSolutionLicenseHeaderDefinitionFileCommand.InitializeAsync(this, Dte2?.Solution, () => DefaultLicenseHeaderPageModel.LicenseHeaderFileText); await OpenSolutionLicenseHeaderDefinitionFileCommand.InitializeAsync(this); await RemoveSolutionLicenseHeaderDefinitionFileCommand.InitializeAsync(this); await AddLicenseHeaderToAllFilesInProjectCommand.InitializeAsync(this); await RemoveLicenseHeaderFromAllFilesInProjectCommand.InitializeAsync(this); await AddNewLicenseHeaderDefinitionFileToProjectCommand.InitializeAsync(this); await AddExistingLicenseHeaderDefinitionFileToProjectCommand.InitializeAsync(this); await LicenseHeaderOptionsCommand.InitializeAsync(this); await AddLicenseHeaderToAllFilesInFolderCommand.InitializeAsync(this); await RemoveLicenseHeaderFromAllFilesInFolderCommand.InitializeAsync(this); await AddExistingLicenseHeaderDefinitionFileToFolderCommand.InitializeAsync(this); await AddNewLicenseHeaderDefinitionFileToFolderCommand.InitializeAsync(this); await AddLicenseHeaderEditorAdvancedMenuCommand.InitializeAsync(this); await RemoveLicenseHeaderEditorAdvancedMenuCommand.InitializeAsync(this); //register ItemAdded event handler if (Dte2?.Events is Events2 events) { _projectItemEvents = events.ProjectItemsEvents; //we need to keep a reference, otherwise the object is garbage collected and the event won't be fired _projectItemEvents.ItemAdded += ItemAdded; //Register to WebsiteItemEvents for Website Projects to work //Reference: https://social.msdn.microsoft.com/Forums/en-US/dde7d858-2440-43f9-bbdc-3e1b815d4d1e/itemadded-itemremoved-and-itemrenamed-events-not-firing-in-web-projects?forum=vsx //Concerns, that the ItemAdded Event gets called on unrelated events, like closing the solution or opening folder, could not be reproduced try { _websiteItemEvents = events.GetObject("WebSiteItemsEvents") as ProjectItemsEvents; } catch (Exception ex) { //This probably only throws an exception if no WebSite component is installed on the machine. //If no WebSite component is installed, they are probably not using a WebSite Project and therefore don't need that feature. s_log.Error("No WebSite component is installed on the machine: ", ex); } if (_websiteItemEvents != null) { _websiteItemEvents.ItemAdded += ItemAdded; } } // migrate options from registry to config file await MigrateOptionsAsync(); //register event handlers for linked commands var page = GeneralOptionsPageModel; if (page != null) { foreach (var command in page.LinkedCommands) { command.Events = Dte2.Events.CommandEvents[command.Guid, command.Id]; switch (command.ExecutionTime) { case ExecutionTime.Before: command.Events.BeforeExecute += BeforeLinkedCommandExecuted; break; case ExecutionTime.After: command.Events.AfterExecute += AfterLinkedCommandExecuted; break; default: throw new ArgumentOutOfRangeException(); } } page.LinkedCommandsChanged += CommandsChanged; //register global event handler for ItemAdded _commandEvents = Dte2.Events.CommandEvents; _commandEvents.BeforeExecute += BeforeAnyCommandExecuted; } }