Пример #1
0
        /// <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;
            }
        }
Пример #2
0
        /// <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 initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            OutputWindowHandler.Initialize(GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow);
            _licenseReplacer = new LicenseHeaderReplacer(this);
            _dte             = GetService(typeof(DTE)) as DTE2;
            _addedItems      = new Stack <ProjectItem>();
            var buttonHandlerFactory = new ButtonHandlerFactory(this, _licenseReplacer);

            //register commands
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

            if (mcs != null)
            {
                AddNewSolutionLicenseHeaderDefinitionFileCommand.Initialize(
                    () =>
                {
                    var page2 = (DefaultLicenseHeaderPage)GetDialogPage(typeof(DefaultLicenseHeaderPage));
                    return(page2.LicenseHeaderFileText);
                });
                OpenSolutionLicenseHeaderDefinitionFileCommand.Initialize();
                RemoveSolutionLicenseHeaderDefinitionFileCommand.Initialize();

                _addHeaderCommand    = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeader, AddLicenseHeaderCallback);
                _removeHeaderCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeader, RemoveLicenseHeaderCallback);
                _addHeaderCommand.BeforeQueryStatus += QueryEditCommandStatus;

                _addHeaderToProjectItemCommand      = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeaderToProjectItem, AddLicenseHeaderToProjectItemCallback);
                _removeHeaderFromProjectItemCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdLicenseRemoveHeaderFromProjectItem, RemoveLicenseHeaderFromProjectItemCallback);
                _addHeaderToProjectItemCommand.BeforeQueryStatus += QueryProjectItemCommandStatus;

                _addHeadersToAllFilesInProjectCommand      = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeadersToAllFilesInProject, AddLicenseHeadersToAllFilesInProjectCallback);
                _removeHeadersFromAllFilesInProjectCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeadersFromAllFilesInProject, RemoveLicenseHeadersFromAllFilesInProjectCallback);
                _addHeadersToAllFilesInProjectCommand.BeforeQueryStatus += QueryAllFilesCommandStatus;

                _addNewSolutionHeaderDefinitionFileCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddNewSolutionLicenseHeaderDefinitionFile, AddNewSolutionLicenseHeaderDefinitionFileCallback);
                _openSolutionHeaderDefinitionFileCommand   = RegisterCommand(mcs, PkgCmdIDList.cmdIdOpenSolutionLicenseHeaderDefinitionFile, OpenSolutionLicenseHeaderDefinitionFileCallback);
                _removeSolutionHeaderDefinitionFileCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveSolutionLicenseHeaderDefinitionFile, RemoveSolutionLicenseHeaderDefinitionFileCallback);
                _addNewSolutionHeaderDefinitionFileCommand.BeforeQueryStatus += QuerySolutionCommandStatus;

                RegisterCommand(mcs, PkgCmdIDList.cmdIdAddNewLicenseHeaderDefinitionFileToProject, AddNewLicenseHeaderDefinitionFileToProjectCallback);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdAddExistingLicenseHeaderDefinitionFileToProject, AddExistingLicenseHeaderDefinitionFileToProjectCallback);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdLicenseHeaderOptions, LicenseHeaderOptionsCallback);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeaderToAllFilesInSolution, buttonHandlerFactory.CreateAddLicenseHeaderToAllProjectsButtonHandler().HandleButton);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeaderFromAllFilesInSolution, RemoveLicenseHeaderFromAllFilesInSolutionCallback);
            }


            //register ItemAdded event handler
            var events = _dte.Events as Events2;

            if (events != null)
            {
                _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)
                {
                    //TODO: Add log statement as soon as we have added logging.
                    //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 dont need that feature.
                }

                if (_websiteItemEvents != null)
                {
                    _websiteItemEvents.ItemAdded += ItemAdded;
                }
            }

            //register event handlers for linked commands
            var page = (OptionsPage)GetDialogPage(typeof(OptionsPage));

            if (page != null)
            {
                foreach (var command in page.LinkedCommands)
                {
                    command.Events = _dte.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;
                    }
                }

                page.LinkedCommandsChanged += CommandsChanged;

                //register global event handler for ItemAdded
                _commandEvents = _dte.Events.CommandEvents;
                _commandEvents.BeforeExecute += BeforeAnyCommandExecuted;
            }
        }