Visual Studio 2012 is really faster than 2010, this is due to an excellent work of the team to maximize performance and because in this release some of the older and less used part of the IDE were removed. One of this part is the Macro editor that is not anymore available in Visual Studio. This is one of the feature I missed most because I’ve used it in the past to automate some basic task, like attach to IIS automatically with a keyboard shortcut. The Macro engine had some limitation, it supported only Visual Basic, it was old code that needs time to be maintained and finally everything you can do with a Macro can be done with Visual Studio Plugin so there was no need to maintain anymore in the product.

If you used macro with VS2010 and you want to use them in VS2012 you need to create an addin, the easiest way is to go for a Visual Studio Package:

image

Figure 1: Creating a Visual Studio Package

Choosing a Project type of “Visual Studio Package” will launch a wizard that asks you some information about the type of extension you want to create, the most important part is choosing the functions that the plugin want to implement, to convert a macro the simplest solution is implement one or more Commands.

image

Figure 2: Choose to implement Menu Command.

The default structure created by the wizard implements a simple command placed under the Tools menu, it can be invoked with the menu or you can assign shortcut to launch with a combination of keys. Once the project is created converting the macro is just a matter of copying the code of the macro inside the plugin and call inside the handler of the command.

Dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;

// Add our command handlers for menu (commands must exist in the .vsct file)
_attachToIIS = new AttachToIIS(Dte);
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
{
    // Create the command for the menu item.
    CommandID menuCommandID = new CommandID(GuidList.guidAlkampferVsix2012CmdSet, (int)PkgCmdIDList.attachToIIS);
    MenuCommand menuItem = new MenuCommand(_attachToIIS.MenuItemCallback, menuCommandID);
    mcs.AddCommand(menuItem);
}

This snippet of code is based on code generated by the wizard, I’ve only added line 1 to retrieve a reference to the DTE2 object needed by my AttachToIIS macro code and to keep the code clean I’ve moved all the code of the macro inside a dedicated class called AttachToIIS. This class has a method called MenuItemCallback that gots called when the user ask to execute the command (from the menu or from a shortcut).

I’ve then converted all of my old VB code to C# and pasted inside the class, then press F5 and an instance of VS2012 Experimental Hive started, permitting me to debug the plugin to verify if everything is ok.

image

Figure 3: My plugin is loaded correctly and the menu is there

The icon is the default one used by the wizard it is an aspect I do not care about because I just want to use my old macro code :), now it is time to use Tools –> Customize menu to assign a shortcut to this command.

image

Figure 4: My new command is loaded and is associated to the Tools namespace, so it is called tools.AttachToIIS

Et voilà, the game is done, the whole process of Macro Conversion took no more than 15 minutes and I’m able to use the same macro code in VS2012. This is my feedback of the process.

Advantages:

  • Creating a VS package is a matter of few clicks and you have a solution completely configure to create your plugin
  • The result is much more manageable, instead of having a bunch of macro code that you need to copy and paste inside the Macro IDE when you reinstall you have a vsix package that can be installed by everyone with a simple double click.
  • You are not forced to use Visual Basic

Disadvantages:

  • It is much simpler to experiment with macro, just write code, press F5 and you are immediately debugging the code, with a plugin when you press F5 another instance of VS starts, and it needs some seconds.

I’ll release the code the next week.

Gian Maria.

Tags: ,

7 Responses to “Converting Visual Studio Macro to Visual Studio plugin”

  1. I was missing my macro that used to do the very same thing. After looking around one developer suggested in another blog that you can create a keyboard shortcut to Attach to Process and then just press a key (e.g. “w”) and press enter to attach to the process. Two more keystrokes but it has some advantages:
    - The macro will not blow up if the IIS process is not running for any reason
    - You can use it to attach other processes as well

    I haven’t missed my macro after creating that shortcut instead.

  2. Yes, you can customize Visual Studio the way you want with plugins :), basically attach to IIS is such a common task that it is useful to enclose it in a command.

    Clearly the command does not blow up if the IIS process is not running, simply it does not attach to the process because it do not find any process named w3wp. ;)

  3. Ricci, have you posted your code yet?

  4. The link is in the next post of the series, but I’ll report here for convenience

    http://sdrv.ms/PPJQCL

    Enjoy :)

    Alk.

  5. Thanks for the example, on the way out the door right now, but do have a request.

    My macros are pretty simple, usually put some code on the page I’m working on.

    Do you know of any examples of doing that in VS 2012??

    appreciate your work.

  6. Actually no, I think you can search inside samples of Visual Studio SDK, it should be quite simple because in visual Studio there is the concept of active document and you should be able to manipulate it from a macro, then once the code works inside the macro, simply create a plugin for VS2012 in Visual Basic with a simple command and you should be able to copy and paste macro code inside the plugin in the MenuitemCallback of the command.

Trackbacks/Pingbacks

  1. Converting Visual Studio Macro to Visual Studio plugin | Visual Studio ALM | Scoop.it