Building a VSIX Deployable Single File Generator

A recent post on the VS Extensibility Forum related to deploying a Single File Generator (also called a “Custom Tool”), inspired me to write a new VSIX Deployable Single File Generator Sample to the MSDN Code gallery.

The existing samples were written a few releases back, and none of them take advantage of VSIX deployment. To create the new sample, I opted to create the initial project using the Visual Studio SDK’s “Visual Studio Package” project template. This ensured I ended up with the project settings and build rules that would automatically generate and include the .pkgdef to register the Single File Generator, and package it into a .VSIX file.

I used VS 2010 here, as I also wanted to also verify that you could build a Single File Generator with an older version of the Visual Studio tools, and then simply tweak the .vsixmanifest to target more recent versions of Visual Studio as well. If you happen to be using a newer version of VS, you can use these same steps to produce a similar project.

  1. Install the Visual Studio SDK that matches the version of VS you have installed. You can find a link to all the published versions in the Welcome post of this blog.
  2. Create a new “Visual Studio Package” project, selecting C# as the language, and then removing/deselecting all other options in the project wizard.
  3. Remove the VSPackage.resx, GlobalSuppressions.cs, and Guids.cs files from project.
  4. Rename the xxxxPackage.cs to something more fitting your project; MyCodeGenerator.cs for example.
  5. Replace the code in MyCodeGenerator.cs with something similar to the code found in the SimpleFileGenerator.cs.
  6. Edit the source.extension.vsixmanifest to include the versions of VS you want to target.
  7. Build and test in the experimental instance using F5.
  8. Use the resulting .VSIX file to deploy your Single File Generator to other machines.

One interesting discovery made while writing this, was that you can successfully target Express versions of Visual Studio, by removing the reference to Microsoft.VisualStudio.MPF from the source.extension.vsixmanifest file. For example:

    <Reference Id="Microsoft.VisualStudio.MPF" MinVersion="10.0">
<Name>Visual Studio MPF</Name>
</Reference>

You’ll still see a warning during the build due to the VSIX containing non-template content. But this can be ignored, and the assembly and .pkgdef for your Single File Generator will be properly installed even for the Express SKUs you list in the .vsixmanifest. For example:

<?xml version="1.0" encoding="utf-8"?>
<Vsix Version="1.0.0" xmlns="https://schemas.microsoft.com/developer/vsx-schema/2010">
<Identifier Id="1263af09-d434-4a54-8c86-4d4000c394ab">
<Name>SimpleFileGenerator</Name>
<Author>Microsoft</Author>
<Version>1.0</Version>
<Description xml:space="preserve">Demo implementation of a Single File Generator</Description>
<Locale>1033</Locale>
<InstalledByMsi>false</InstalledByMsi>
<SupportedProducts>
<VisualStudio Version="10.0">
<Edition>Pro</Edition>
<Edition>Express_All</Edition>
</VisualStudio>
<VisualStudio Version="11.0">
<Edition>Pro</Edition>
<Edition>Express_All</Edition>
</VisualStudio>
<VisualStudio Version="12.0">
<Edition>Pro</Edition>
<Edition>Express_All</Edition>
</VisualStudio>

</SupportedProducts>
<SupportedFrameworkRuntimeEdition MinVersion="4.0" MaxVersion="4.0" />
</Identifier>
<References/>
<Content>
<VsPackage>|%CurrentProject%;PkgdefProjectOutputGroup|</VsPackage>
</Content>
</Vsix>

Note, this is an extremely bare boned sample, and it’s primary purpose is to illustrate how to go about leveraging the VSSDK package project to create a VSIX deployment for a Single File Generator. An internet search will turn up a good number of examples/articles on this topic. But one of my favorites was recently published by Qwertie on Code Project entitled “Writing a Single-File Generator”.