Introduction
The purpose of this simple framework is to develop switches for the devenv command that you can launch from the Visual Studio 2005 prompt. You can do many things from the VS2005 prompt using the devenv command. For example, if you write devenv "name of project/name of solution", without any switch, a new instance of VS is opened with the project/solution loaded. If you write devenv /setup, the environment resources metadata that describes menus, toolbars, and others from VSPackages are merged with the VS environment. Here are all the switches for the devenv command prompt: MSDN.
This framework was born when I had to open a website from a prompt. As far as I know, there isn't an easy way to do this (for example, devenv "website folder"). In the Internet, I found that a method to create a macro, install in VS, and run it with devenv /Command. So, I decided to look for a better way and, in particular, a method that could be useful in future also. Developing a package, for me, was a beautiful idea. So, I searched for some documentation to create custom switches and run them. Here is how to implement custom switches: MSDN.
Prerequisites
- In order to use the VS 2005 version of the framework, you have to install Visual Studio 2005 SDK.
- In order to use the Orcas version of the framework, you have to download Orcas and the Visual Studio Orcas SDK. The Orcas version is the same as the 2005 version, only recompiled. You can download Orcas on a virtual machine (Windows Server 2003) from Microsoft.
- A basic knowledge about VSPackage/add-ins.
Schemes
Here is a class diagram and a sequence diagram to show how the package communicates with the switch framework.
Class Diagram
Sequence Diagram
How to Develop a Custom Switch
In order to create a custom switch, you must inherit a new class from the CustomSwitch
hierarchy. Create a constructor that takes the ISwitchPackage
interface, switch name, and the command prompt parameters, calling the base class. Then, override the Executing
function and implement what you want. To monitor the switch execution, CustomSwitch has two events: BeforeExecuting
and AfterExecuting
.
Here, there is the code example for OpenWebSiteSwitch
:
internal class OpenWebSiteSwitch : CustomSwitch
{
#region Constructors
public OpenWebSiteSwitch(ISwitchPackage package, string name, string[] parameters)
: base(package, name, parameters)
{
}
#endregion
#region Protected Functions
protected override void Executing()
{
VsWebSite.VSWebPackage webPackage =
Package.DTE2.GetObject("WebPackage") as VsWebSite.VSWebPackage;
try
{
webPackage.OpenWebSite(Parameters[0],
VsWebSite.OpenWebsiteOptions.OpenWebsiteOption_None, false);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Exception type: " +
ex.GetType().ToString() + ". WebSite at path {0} cannot be loaded!",
Parameters[0]);
}
}
#endregion
}
To fire events, the Execute
method of the base class CustomSwitch
is implemented as follows:
virtual public void Execute()
{
if (!CheckContext())
throw new SwitchException(Resources.IllegalSwitchOrOption);
if (BeforeExecuting != null)
BeforeExecuting(this, null);
Executing();
if (AfterExecuting != null)
AfterExecuting(this, null);
}
Some operations can be executed only when VS is completely opened, for example, the opening of a project or website. So, you can execute a switch only after the startup completed event.
At the end of the Initialize
function of the package:
EnvDTE80.DTE2 dte2 = GetService(typeof(SDTE)) as EnvDTE80.DTE2;
dte2.Events.DTEEvents.OnStartupComplete += new
EnvDTE._dispDTEEvents_OnStartupCompleteEventHandler(DTEEvents_OnStartupComplete);
In the event:
IVsAppCommandLine commandLine = GetService(typeof(SVsAppCommandLine)) as IVsAppCommandLine;
CustomSwitchEngine customSwitchEngine = new CustomSwitchEngine(this);
Collection<CustomSwitch> customSwitches = customSwitchEngine.GetCustomSwitches(commandLine);
foreach (CustomSwitch customSwitch in customSwitches)
customSwitch.Execute();
IVsAppCommandLine
is the interface that you require for VS and for using the command line arguments.
After implementation of your custom switch, you must update the Registry so Visual Studio recognizes your switches.
In HKLM/SOFTWARE/Microsoft/VisualStudio/8.0/AppCommandLine, you must add a new key with the same name of your switch and add the following values:
Arguments
: number of arguments that the switch can have.
DemandLoad
: 1 if the package is loaded when the switch is present in the command line.
HelpString
: you can specify a help string for /? from the prompt. Example: myswitch /?.
Package
: GUID of the package related to the switch.
To test your switch, you can load the code for the SimpleSwitchFramework (see prerequisites), open the project Properties and, in the Debug tab, add /AboutSwitch after the /rootsuffix Exp. When you press F5, another instance of VS will run, and after startup complete, a message box will appear.
Note: in the source folders, there are two .reg files to update the registry.
Configuration
As you can see, there is a Configuration hierarchy that allows you to load all possible custom switches that VS can recognize. The CustomSwitchEngine
has a property configuration so you can create your custom configuration and use it.