Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Extensibility;
- using EnvDTE;
- using EnvDTE80;
- using Microsoft.VisualStudio.CommandBars;
- using System.Resources;
- using System.Reflection;
- using System.Globalization;
- using VSLangProj;
- using System.IO;
- namespace ReferenceDoctor
- {
- /// <summary>The object for implementing an Add-in.</summary>
- /// <seealso class='IDTExtensibility2' />
- public class Connect : IDTExtensibility2, IDTCommandTarget
- {
- private StreamWriter _logWriter;
- /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
- public Connect()
- {
- }
- /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
- /// <param term='application'>Root object of the host application.</param>
- /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
- /// <param term='addInInst'>Object representing this Add-in.</param>
- /// <seealso class='IDTExtensibility2' />
- public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
- {
- _applicationObject = (DTE2)application;
- _addInInstance = (AddIn)addInInst;
- if(connectMode == ext_ConnectMode.ext_cm_UISetup)
- {
- object []contextGUIDS = new object[] { };
- Commands2 commands = (Commands2)_applicationObject.Commands;
- string toolsMenuName = "Tools";
- //Place the command on the tools menu.
- //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
- Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];
- //Find the Tools command bar on the MenuBar command bar:
- CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
- CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
- //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
- // just make sure you also update the QueryStatus/Exec method to include the new command names.
- try
- {
- //Add a command to the Commands collection:
- Command command = commands.AddNamedCommand2(
- _addInInstance,
- "ReferenceDoctor",
- "ReferenceDoctor",
- "Executes the command for ReferenceDoctor",
- true,
- 59,
- ref contextGUIDS,
- (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled,
- (int)vsCommandStyle.vsCommandStylePictAndText,
- vsCommandControlType.vsCommandControlTypeButton);
- //Add a control for the command to the tools menu:
- if((command != null) && (toolsPopup != null))
- {
- command.AddControl(toolsPopup.CommandBar, 1);
- }
- }
- catch(System.ArgumentException)
- {
- //If we are here, then the exception is probably because a command with that name
- // already exists. If so there is no need to recreate the command and we can
- // safely ignore the exception.
- }
- }
- }
- /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
- /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
- /// <param term='custom'>Array of parameters that are host application specific.</param>
- /// <seealso class='IDTExtensibility2' />
- public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
- {
- _logWriter.Close();
- }
- /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
- /// <param term='custom'>Array of parameters that are host application specific.</param>
- /// <seealso class='IDTExtensibility2' />
- public void OnAddInsUpdate(ref Array custom)
- {
- }
- /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
- /// <param term='custom'>Array of parameters that are host application specific.</param>
- /// <seealso class='IDTExtensibility2' />
- public void OnStartupComplete(ref Array custom)
- {
- }
- /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
- /// <param term='custom'>Array of parameters that are host application specific.</param>
- /// <seealso class='IDTExtensibility2' />
- public void OnBeginShutdown(ref Array custom)
- {
- }
- /// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated</summary>
- /// <param term='commandName'>The name of the command to determine state for.</param>
- /// <param term='neededText'>Text that is needed for the command.</param>
- /// <param term='status'>The state of the command in the user interface.</param>
- /// <param term='commandText'>Text requested by the neededText parameter.</param>
- /// <seealso class='Exec' />
- public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
- {
- if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
- {
- if(commandName == "ReferenceDoctor.Connect.ReferenceDoctor")
- {
- status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
- return;
- }
- }
- }
- /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
- /// <param term='commandName'>The name of the command to execute.</param>
- /// <param term='executeOption'>Describes how the command should be run.</param>
- /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
- /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
- /// <param term='handled'>Informs the caller if the command was handled or not.</param>
- /// <seealso class='Exec' />
- public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
- {
- handled = false;
- LogMessage(string.Format("Start investigation at {0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
- if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
- {
- if(commandName == "ReferenceDoctor.Connect.ReferenceDoctor")
- {
- foreach (Project project in _applicationObject.Solution.Projects)
- {
- VSProject vsProject = project.Object as VSProject;
- if (vsProject != null)
- {
- foreach (Reference reference in vsProject.References)
- {
- // if it not project reference
- if (reference.SourceProject == null)
- {
- string refPath = reference.Path;
- string refAssemblyName = Path.GetFileNameWithoutExtension(refPath);
- Project refProject = FindProjectWithinSolution(refAssemblyName);
- // if within solution found project with suitable name
- if (refProject != null)
- {
- try
- {
- //replace reference with project reference
- reference.Remove();
- vsProject.References.AddProject(refProject);
- LogMessage(string.Format(" [{0}] Reference to {1} replaced. ({2} -> {3})", vsProject.Project.Name, refAssemblyName, refPath, refProject.FullName));
- }
- catch (Exception exc)
- {
- LogMessage(string.Format(" [{0}] ERROR during replacing reference to {1} ({2} -> {3}): {4}", vsProject.Project.Name, refAssemblyName, refPath, refProject.FullName, exc.Message));
- }
- }
- }
- }
- }
- }
- LogMessage(string.Format("Completed at {0} {1}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
- //logWriter.WriteLine(string.Format("COMPLETED, {0} at {1}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
- //logWriter.Dispose();
- handled = true;
- }
- }
- }
- private Project FindProjectWithinSolution(string projectName)
- {
- foreach (Project project in _applicationObject.Solution.Projects)
- {
- VSProject vsProject = project.Object as VSProject;
- if (vsProject != null)
- {
- if (string.Compare(vsProject.Project.Name, projectName, ignoreCase: true) == 0)
- {
- return project;
- }
- }
- }
- return null;
- }
- private void LogMessage(string str)
- {
- try
- {
- LogToFile(str);
- }
- catch { }
- try
- {
- LogToOutputWindow(str);
- }
- catch { }
- }
- private void LogToFile(string str)
- {
- if (_logWriter == null)
- {
- _logWriter = new StreamWriter(Path.Combine(new FileInfo(_applicationObject.Solution.FullName).DirectoryName, "ReferenceDoctor.log"), true);
- }
- _logWriter.WriteLine(str);
- _logWriter.Flush();
- }
- private void LogToOutputWindow(string str)
- {
- const string PANE_NAME = "Reference Doctor";
- OutputWindow ow = _applicationObject.ToolWindows.OutputWindow;
- if (!IsPaneExists(PANE_NAME))
- {
- ow.OutputWindowPanes.Add(PANE_NAME);
- }
- OutputWindowPane owP = _applicationObject.ToolWindows.OutputWindow.OutputWindowPanes.Item(PANE_NAME);
- owP.OutputString(str + Environment.NewLine);
- //Window window = _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
- //OutputWindow outputWindow = (OutputWindow)window.Object;
- //outputWindow.ActivePane.Activate();
- //outputWindow.ActivePane.OutputString(str);
- }
- private bool IsPaneExists(string name)
- {
- try
- {
- var pane = _applicationObject.ToolWindows.OutputWindow.OutputWindowPanes.Item(name);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- private DTE2 _applicationObject;
- private AddIn _addInInstance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement