Wednesday, February 6, 2008

Adding VSCT compilation support to Whidbey (VS 2005) projects

This post demonstrates how to add VSCT support for Whidbey (VS 2005) projects.

About a month ago Aaron Marten wrote a post “CTC is dead...Long Live VSCT! (Part 1)” where he announced a new XML-based format called VSCT, which is far easier to work with than CTC. Two weeks ago James Lau announced VS SDK for Orcas Beta 1 (April CTP). One of the improvements of April CTP is that command table configuration (.ctc) files have been deprecated in favor of XML command table (.vsct) files and all samples that used .ctc files have been converted to use .vsct files.

Well, that is good but it is done for Visual Studio Codename Orcas only. The latest VS SDK 4.0 RTM for Visual Studio 2005 announced a little bit earlier still uses CTC but it includes VSCT compiler in pre-release state. You can find it here: C:\Program Files\Visual Studio 2005 SDK\2007.02\Prerelease\.

To demonstrate how to add VSCT support for Whidbey (VS 2005) projects we will use the C# Reference.MenuAndCommands sample included into Visual Studio SDK Version 4.0. You can find it in that directory: C:\Program Files\Visual Studio 2005 SDK\2007.02\VisualStudioIntegration\Samples\IDE\CSharp\Reference.MenuAndCommands\. At the end of this tutorial, we will get this sample working with VSCT instead of CTC. Follow instructions below.

1. Before we will substitute CTC with VSCT let’s see how it looks now. Press F5 to run the MenuAndCommands sample in experimental hive and see some menu items added under main menu item ‘Tools’. Note command ‘C# Command Sample’ – it will be our marker in this tutorial.

Before (with CTC)

2. The first thing we should care about is to obtain VSCT analogue for our CTC file. The easiest way to do that is to build the project to get .cto file compiled from current .ctc file and then create a .vsct file from an existing .cto file using VSCT.exe utility. I have used the following command to perform the conversion from <project_root>\obj\Debug\ directory where two input files PkgCmd.cto and PkgCmd.ctsym are located.

> "C:\Program Files\Visual Studio 2005 SDK\2007.02\Prerelease\VSCT\VSCT.exe" PkgCmd.cto PkgCmd.vsct -SPkgCmd.ctsym

3. Now let’s replace CTC part of the MenuAndCommands sample project with VSCT analogue. Create new folder VSCTComponents under project root directory and copy just generated PkgCmd.vsct file from obj\Debug\ there. Then make it part of the project. To be sure that we have no CTC here let’s remove all from CtcComponents folder except image GenericCmd.bmp, because it is still needed. Let’s also replace the text of the button ‘C# Command Sample’ with ‘C# Command Sample (VSTS)’ to have some differences in resulting UI. See what we have now.

Change the button text to have a visual marker

Note: you can easily add IntelliSense for .vsct files via browsing for C:\Program Files\Visual Studio 2005 SDK\2007.02\Prerelease\VSCT\VSCT.xsd file in ‘Schemas’ property in Properties window (accessible by F4).

4. Rebuild the solution and then execute ‘devenv.exe /rootsuffix Exp /setup’ command to refresh Visual Studio UI. Then press F5 to see the result – all menus disappeared. To get all back in VSCT incarnation of menu items we need to write some code right in MenuAndCommands.csproj project file. To do that unload the project and open it for editing using ‘Edit MenuAndCommands.csproj’ command.

Open project file for manual editing

5. Add the following line importing .targets file as it is shown on the picture below. I have prepared the VSCT_overrides.targets file, so you just need to download it and save in the project root directory.

Import .targets file containing overriden MSBuild targets

6. Now our PkgCmd.vsct file declared as <None Include="VSCTComponents\PkgCmd.vsct" /> in MenuAndCommands.csproj file. Replace this line with the following code. Child node ResourceName specifies the name of CTO object which will be merged into assembly resources with.

Use VSCTCompile tag for .vsct file

7. Save the changes and reload the project. Select ‘Load project normally’ option in the Security Warning dialog which will be appeared. Rebuild the project and see results in output window. There are 4 errors that occurred due to automatic generation.

Four errors of automatic VSCT file generation shown in Visual Studio Output window

Note: make sure you use ‘Normal’ level of MSBuild build output verbosity.

8. You can easily fix them by adding 4 IDSymbol tags under according GuidSymbol tags or just download fixed version of PkgCmd.vsct file I have prepared. Then build the project again and verify that all errors go on.

VSCT file compiled with no errors

9. Rebuild the solution and then execute ‘devenv.exe /rootsuffix Exp /setup’ command to refresh Visual Studio UI. Then press F5 to see the result – menus appeared again. Note the changed text ‘C# Command Sample (VSTS)’.

After (with VSCT)

Keep in touch and happy extending!

1 comment:

Dmitry Pavlov said...

VSX Team has released a new PowerToy named VSCT PowerToy. For VSX developers who extend Visual Studio using the VS SDK, the VSCT (Visual Studio Command Table) PowerToy is a read-only viewer that you can use to explore the commands associated with a VSPackage, and with Visual Studio itself. You can quickly search for any existing commands in the Visual Studio IDE. By browsing through the command groups, GUIDs and IDs, priorities, and other properties of existing commands, you can more easily place and integrate the commands of your own VSPackage.

Read more about VSCT PowerToy on VSX Team blog.