T4 Templating without Visual Studio

I’m currently appointed to an older project to make some new features. I soon realized that this project is developed in a pre-Nuget era, so all dependencies were copied into a 3rd party folder and included in source control. The entire solution includes 29 projects, so there were a larger number of dependencies to this solution. I decided to get rid of all the dependencies that is available on Nuget. These included NHibernate, MVVM Light, log4net, Castle Windsor, Auto Mapper, Rhino Mocks, NUnit, and the Silverlight Toolkit. I noticed that the Silverlight Toolkit is available as individual components, instead of just installing them all. It took a few hours to get all dependencies to work across the solution, but pretty trivial. Besides these dependencies, there was a dependency to Texttransform.exe.

T4 Templating and Visual Studio

Texttransform.exe is part of T4 templating and is installed together with Visual Studio. In this case a developer has copied the Texttransform.exe from the its default installation path in \Program Files (x86)\Common Files\Microsoft Shared\Text Templating\ to the 3rd party folder in the solution folder. The problem with this approach is that the version in the solution folder is the Visual Studio 2010 version. Running Texttransform.exe from Visual Studio 2012, provides an error. I therefore removed the copy from the solution folder and wrote a relative path in the pre-build event to the default installation.

"%COMMONPROGRAMFILES(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"

The $(VisualStudioVersion) variable provides the current version of Visual Studio, so this approach works on both Visual Studio 2010 and Visual Studio 2012.

I’m not going to cover T4 templating in detail in this post, but I recommend reading Scott Hanselman’s post on this issue. Basically, T4 is code generation. It can generate all kinds of text files, not just code.

Since T4 is installed together with Visual Studio, it gives us quite a challenge to get this to work with Team Foundation Server (TFS) (which is the management decision of source control at my current employer), since the $(VisualStudioVersion) variable is not present on the build server.

T4 Templating and Team Foundation Server

To get the necessary dlls I needed to install the Visual Studio SDK 2012 on my development machine and the copy the following files to the build server:

It is important to keep the same directory hierarchy. Due to these issues with T4 Templating and TFS, Microsoft has changed their EULA for Visual Studio making it license-wise legal to copy the files to a build server.

Without making too much custom TFS handling of this, I going to the send the VisualStudioVersion variable, mentioned above, to the TFS from Visual Studio through a MSBuild argument. This is done in your Build Definition, under the Process tab, like this:

image

Now TFS is able to find texttransform.exe and do the T4 templating.

Instead of this kind of hack, MSBuild ought to support T4 templating natively. Like MSBuild is able to perform configuration transformations, it should support T4 templating as well.


comments powered by Disqus