Automatically Transform T4 Templates on Build - Comes with support for hostspecific EnvDTE accessing templates and support for multiple output files

Today released V1.9.64 of tangible T4 Editor for VS2010 – PRO EDITION which now allows you to also transform specific T4 templates in your project on build. Here is a short walktrough that shows that this feature also supports templates that access EnvDTE e.g. to create multiple output files.

Let’s try the basics

1. I have installed tangible T4 Editor PRO EDITION

2. I created a C# project and added a Hello file to the project with the following content

<#@ template debug="true" hostSpecific="true" #>
<# this.Write(System.DateTime.Now.ToString()); #>

3. Now I can set this simple template to transform on build – so each time the project is build it is run:


4. Let’s hit “Build” and see what is reported in the output window:


Now let’s do multiple output files within this auto-transform process

3. Create a second file TempalteFileManager.ttinclude. Then right click the Editor to open the Conext menu and select “Insert Code from Gallery”.  
  Then press the button “Search online templates” to find it. then insert its contents from the gallery.


4. Now go back to and change its contents to

<#@ template debug="true" hostSpecific="true" #>
<#@ include file="TemplateFileManager.ttinclude" #>
<# var fm=TemplateFileManager.Create(this);
for (int i=1;i<3;i++){
    fm.StartNewFile("Class" + i + ".cs");
fm.Process(true); #>

5. And run it … cool i finally got this nice multiple output thing and transform on build running


Even more advanced: Generate into subdirectories and other projects:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".txt" #>
<#@ include file="TemplateFileManager.CS.ttinclude" #>
<# string[] fileNames = new string[] { "class4.cs", "class5.cs", "class6.cs" };
var manager = TemplateFileManager.Create(this);
manager.CanOverrideExistingFile = false;
this.WriteLine("// Class is assigned to template ");
manager.StartNewFile("Class2.cs", folderName:"CodeFolder");
this.WriteLine("// Class is created in subfolder ");
manager.StartNewFile("Class3.cs", projectName:"ClassLibrary");
this.WriteLine("// Class is generated in Project ClassLibrary");
foreach (string item in fileNames) {
        manager.StartNewFile(item, projectName:"ClassLibrary", folderName:"SubFolder");
        this.WriteLine("// Classes are created in Project ClassLibrary/SubFolder ");
Abbildung 6

Many thanks to Björn Borgsdorf and Rene Leupold who made this thing possible!

Happy Birthday Björn!

1 comment:

thargy said...

Folders do not work on VS2012, possible because of changes to DTE?