So far in this series, we have created all the pieces for a Visual Studio Project Template for a unit-testing project.
- In the introduction, we outlined the purpose and requirements for our Project Template.
- In Part 1, we used the File -> Export Template command to create an unadorned template from an ordinary unit-testing project.
- In Part 2, we wrote a custom program that did the same thing as File -> Export Template, plus it introduced some template paramters to the source.
- In Part 3, we prepared a class derived from Microsoft.VisualStudio.TemplateWizard.IWizard that populated the template parameters, as well as making other adjustments.
Now we're ready to put it all together.
There are a couple of ways to deploy a template. You can upload it to the online code gallery but for internal use it probably makes more sense to put a VSIX file on a file share. That's what we'll do in this post, with a few added features.
Contents of the VSIX File
A VSIX file is nothing more than a ZIP file with a different extension. Here's what ours will have in it.
- The ProjectTemplates directory contains the ZIP from File -> Export Template (or, in our case, from our custom exporter program).
- [Content_Types].xml is required by the Open Packaging Convention. For each file extension (e.g., dll), it tells the content type (e.g., application/octet-stream).
- Extension.vsixmanifest is -- you guessed it -- the manifest for the extension. Among other things, it tells Visual Studio that we want to use a custom Wizard.
- Fws.VS2012.ProjectWizard.dll is that Wizard.
- License.txt contains the license terms that will appear on the installation dialog.
That's what's in a VSIX. Now how do we create one?
Creating the VSIX Project
A VSIX file is usually the build result of a VSIX project. That project type appears on the New Project dialog here:
What you'll get will look something like this.
Pretty bare-bones, right? Visual Studio will also open that vsixmanifest file for you in a special Design window:
While that window is up, we might as well enter something in the Author field. (It is required and the project will not build without it.)
Completing the VSIX project consists of adding things to this manifest, and usually to the project as well. We've done that in the sample code that you can download here ProjectTemplateTutorial.zip (271.98 kb), where you'll find a project called Fws.VS2012.UnitTestProject.Extension.
Let's see how to create each item in the project, and add it to the manifest.
Incorporating the .vstemplate
Recall from the earlier post on Visual Studio's native File -> Export Template feature that one product of the export is a .vstemplate file (in the exported ZIP). We need one of those for the VSIX, too. It's just an XML file, so it's easy enough to code by hand. Or, you can cheat and do a manual File -> Export Template to get you started. Either way, you want something that looks like this.
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<Name>Fascinated With Software Unit Test Project</Name>
<Description>A Unit Test project that conforms to FWS standards.</Description>
<Project TargetFileName="Fws.VS2012.UnitTestProject.Template.csproj" File="Fws.VS2012.UnitTestProject.Template.csproj" ReplaceParameters="true">
<Folder Name="Properties" TargetFolderName="Properties">
<ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
<Assembly>Fws.VS2012.ProjectWizard, Version=126.96.36.199, Culture=Neutral, PublicKeyToken=a417c6f2c239ee82</Assembly>
Incorporating the Wizard
If you read that .vstemplate closely, you saw that the last part of it names the Wizard assembly we created in the last post. That's the first step to incorporate the wizard.
The second step is to add it to the Project References in the usual way. Although the extension does not call the Wizard, we need the project reference in order for the next step to work.
Finally, we add the Wizard's assembly to the vsixmanifest. Double-click on the source.extension.vsixmanifest in the project to invoke the designer. Click on Assets and then on New. Fill out the window as shown here.
Incorporating a License File
We want the VSIX installer to display license terms, so we put them in a text file, License.txt, which we include in the project.
We add the file name to the Metadata tab of the vsixmanifest editor.
Incorporating an Icon
This works the same as incorporating the license file. We simply create the file as part of the project, and then add it to the manifest.
Incorporating the Exported Template
Let's not forget the most important piece -- the ZIP file that contains the exported template! We want our custom exporting program to export to the ProjectTemplate\Test folder in the project. The \Test part is what will make our Visual Studio extension appear in the Test category when we say Add -> New Project.
So how are we going to make this happen with no manual intervention? We will use the pre-build event!
Huh? Let's break that down.
- $(ProjectDir)..\..\TemplateExporter\$(OutDir)\Fws.VS2012.TemplateExporter.exe - This executes our custom exporter.
- $(ProjectDir)..\Template\ - The exporter's first command-line argument is the directory of the template project. It's a sister of the VSIX project, in the Template directory next to the VSIX project's Extension directory.
- Fws.VS2012.UnitTestProject.Template.csproj - The name of the project file.
- $(ProjectDir) - The directory that contains the icon file and .vstemplate file.
- $(ProjectDir)ProjectTemplates\Test - The directory to which the template will be exported.
That will put UnitTestProject.zip in the ProjectTemplates\Test directory. Now we need to get it from there into the VSIX. As when we brought in the Wizard assemby, we use the Assets tab of the vsixmanifest editor. This time, we complete the asset dialog thus:
Checking the Build Order
The Extension project depends on everything else we've created so far, but the dependency on the Template Exporter is hidden away in the pre-build event. Therefore, we must make sure that the Extension project builds after all the others. We right-click on the solution and choose Project Dependencies and then Build Order. As it happens, everything looks good,
Building, Deploying and Installing
Now with a quick F6 we build the entire project. The resulting VSIX will be in the bin\Release directory.
We can copy it from there to any convenient location for the rest of the development team to use. Wherever it ends up, a double-click will launch the installation process.
The license terms ("freely available to anyone") come from the License.txt file in the Extension project.
Press Install and then restart all open Visual Studio instances.
Using the Template
At long last we are ready to use the template. With a solution open, we can right-click on the solution and then choose Add -> New Project. And there it is!
After we press OK, we'll get a fresh unit-testing project that conforms to the standards we outlined in the introduction.