views:

132

answers:

1

Imagine a folder structure that contains more than 200 solution files for projects that all belong to one software. Most of them generate shared libraries that are referenced in other projects that are contained in own solutions.

Wouldn't it be wonderful if there was a possibility of right clicking such a referenced assembly in the solution explorer and then having a context menu item such as "Open solution" and "Open solution in a new Visual Studio instance" (something like this)?

For this to work, the addin should be configured with some sort of base directory. From there it needs to create a collection of all available solution files and the contained projects. When a reference is right clicked, it should scan the project files for the corresponding output and - if found - present the menu items for directly opening the corresponding solution (maybe this information could be prefetched, too).

Well... did anyone of you already create such an addin? Does anyone happen to know an addin that already does something like this?

If neither of those questions is answered with "yes": Can anyone point me to a direction of how to extend the solution explorers context menu based on set preconditions?

A: 

Afaik you can't do this with a VS MEF extension, but it's probably not all that difficult to do with a VS Add-in.

  1. Use MZTools samples/templates to write the part that would add your menu to the appropriate command bar (this is the context menu pop-up which includes the Solution Explorer's commands). The ones I hooked into were Solution and Project. I imagine there is also a commandBar called Reference. In the following code ApplicationObject is of type EnvDTE Here's some code to search for the command bar you need:

        private void IterateAllCommandBars( )
    {
        var commandBars =(CommandBars)ApplicationObject.CommandBars;
        Debug.Indent( );
        foreach (CommandBar commandBar in commandBars)
        {
            Debug.WriteLine(commandBar.Name);
        }
        Debug.Unindent( );
    }
    
  2. When your extension is loaded (typically in OnStartupComplete) you can then look for your preconditions and set up a dictionary that maps reference to solution path.

  3. Use the QueryStatus call that is invoked on your add in to enable/disable the menu based on which reference is selected if any. For Example I get the selected project if any:

    private static Project GetProject(DTE applicationObject)
    {
        if (applicationObject.Solution==null||applicationObject.Solution.Projects==null||applicationObject.Solution.Projects.Count<1)
            return null;
        if (applicationObject.SelectedItems.Count==1&&applicationObject.SelectedItems.Item(1).Project!=null)
            return applicationObject.SelectedItems.Item(1).Project;
        return null;
    }
    
  4. Execute the command you want to invoke when the user clicks a valid option. I'm not sure how or if you could have it open the solution in the existing VS instance, but opening in a new VS would be easy. Process.Start(ProjectFullPath); or that could be SolutionFullPath.

Maslow
Apparently someone has written a MEF mapper so that you can write MEF components to extend VS menus (also solution explorer): http://coolthingoftheday.blogspot.com/2010/06/managed-menu-extension-mme-makes-adding.html
Maslow