In this post we'll look at extending Visual Studio by creating an Item Template to help with code generation. In the past I've relied heavily upon code snippets for generating chunks of code but item templates help more so when I need to generate an entire code file or several code files at once. Also, parts of the file will need to be dynamic in that I want to give the file a custom class name.
These instructions are for use with Visual Studio 2013.
Prerequisites
Make sure you have VS 2013 SDK installed.
Install the SideWaffle template pack extension.
Steps
1. Create a new VSIX project.
2. Open the Package Manager Console in Visual Studio
3. Execute Install-Package TemplateBuilder –pre
4. Create a folder named ItemTemplates at the root of the project
5. Under that folder create a folder with the name you want to show up in the Add Item dialog box (a good choice would be your company name).
6. Right-click the new Company folder (or whatever you named it) and click Add -> New Item. Select the SideWaffle Item Template and give it an appropriate name.
7. Add the actual file that you want to be created by your template into this new template folder (same level as the readme.txt file that was added). Optionally, if you want more than 1 file to be added just add those also. If you want them to be added to subfolders then create those subfolders here to match.
8. Inside the Definitions folder that it creates will be 4 files with a .vstemplat- extension. Rename all applicable files for your template to have a .vstemplate extension.
9. Edit the .vstemplate file(s) and set DefaultName to the actual filename that you want shown in the dialog window when it asks for a filename to save to. Also, change the Name and Description fields accordingly.
10. In the same file edit the ProjectItem element by removing readme.txt from it's contents. Replace it with the name of the file you added in step 7. If you're adding more than 1 file then just copy and paste this line for every file needed. If the file is in a subfolder then make sure to put the folder name first in the content like this:
<ProjectItem ReplaceParameters="true">css\site.css</ProjectItem>
(For info on replacing parameters, skip down to the last section)
11. Edit the source.extension.vsixmanifest file at the project root. Make sure to edit the Install Targets to the correct version(s) of Visual Studio that you want. For me I edited the version range to be [11.0, 12.0].
12. Edit the Metadata and put values for Product Name, Author, and Description.
13. Now build your project and go to the Debug folder in File Explorer and you should see a .vsix file there that you can deploy. If you just want to test the extension then run it in Debug mode and it will launch a 2nd VS instance that will have the Item Template available.
Replacing parameters in template files
Let's say that you're adding a new CSharp class from this template and you need to know the name that the user entered. You can put tokens in your source file that will get substituted. FYI, the documentation that I found on MSDN didn't work for me for some reason.
I ended up using these two tokens: $fileinputname$ and $rootnamespace$
Intellisense will complain but you should still be able to build the solution. Here is what my sourcefile.cs looks like:
namespace $rootnamespace$ { public class $fileinputname$ { public $fileinputname$() { } } }