コード例 #1
0
        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);
            }
        }
コード例 #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;
            }
        }
コード例 #3
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)
            {
                _addLicenseHeaderCommand    = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeader, AddLicenseHeaderCallback);
                _removeLicenseHeaderCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeader, RemoveLicenseHeaderCallback);
                _addLicenseHeaderCommand.BeforeQueryStatus += QueryEditCommandStatus;

                _addLicenseHeaderToProjectItemCommand      = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeaderToProjectItem, AddLicenseHeaderToProjectItemCallback);
                _removeLicenseHeaderFromProjectItemCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeaderFromProjectItem, RemoveLicenseHeaderFromProjectItemCallback);
                _addLicenseHeaderToProjectItemCommand.BeforeQueryStatus += QueryProjectItemCommandStatus;

                _addLicenseHeadersToAllFilesCommand      = RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeadersToAllFiles, AddLicenseHeadersToAllFilesCallback);
                _removeLicenseHeadersFromAllFilesCommand = RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeadersFromAllFiles, RemoveLicenseHeadersFromAllFilesCallback);
                _addLicenseHeadersToAllFilesCommand.BeforeQueryStatus += QueryAllFilesCommandStatus;

                RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeaderDefinitionFile, AddLicenseHeaderDefinitionFileCallback);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdAddExistingLicenseHeaderDefinitionFile, AddExistingLicenseHeaderDefinitionFileCallback);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdLicenseHeaderOptions, LicenseHeaderOptionsCallback);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdAddLicenseHeaderToAllProjects, buttonHandlerFactory.CreateAddLicenseHeaderToAllProjectsButtonHandler().HandleButton);
                RegisterCommand(mcs, PkgCmdIDList.cmdIdRemoveLicenseHeaderFromAllProjects, RemoveLicenseHeaderFromAllProjectsCallback);
            }



            //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 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;
            }
        }