Last year I did a presentation about Visual Studio’s templates where I demonstrated how to create templates for projects and project items. I also demonstrated how to create a custom wizard that Visual Studio will execute when your template is run. At the time I tried to host T4 in my wizard so I could create some really kick ass templates. Unfortunately, at the time I couldn’t make it work.
Recently, while messing around with CodeSmith I learned that T4 is now more exposed in Visual Studio so it is a bit easier to use. You can just add a file to a project and give it a ‘tt’ extension and VisualStudio will use its T4 engine to expand your template everytime you make make a change and save. Scott Hanselman has a great article that provides an excellent starting point for learning about T4. Scott’s post points to a lot of material published by Oleg Sych who seems to be quite the T4 hero.
As it happens I have CodeSmith pro so I have a choice available to me: Use CodeSmith Projects or use T4 files. CodeSmith Projects can generate multiple outputs from a single template. T4 is part of Visual Studio 2008. For things that need to be regenerated regularly have a CSP or T4 file in a project is pretty cool. Everytime you build or make a change the template runs.
Neither of these solutions address my original effort: Have an easy way to generate 1 time templates via Visual Studio’s add dialogs. There are a number of project artifacts that only need to be generated once. In some cases it is only one time for the entire duration of a project. In other cases every time you add a new item to your project you need something to happen.
Further, we don’t always need code to be generated every time. Sometimes, there are some administrative tasks that need to be performed in response to adding this new item to a project. The point is that this infrastructure partially exists. I’ve just not had time to go back and figure out why my original host failed to work.
If I get time this weekend I might spend some time playing around to figure out what happened. However, today I’m aware of several choices available to me. Instead of using T4 I can use some of the open source generators. I’d be happy to use CodeSmith’s engine but the licensing terms are prohibitive.
In regard to my original sample, knowing me – it’s a simple syntax error in the template I’m testing with and the host and VS integration work just fine.