T4 without Visual Studio?

I'm trying to wireup some code gen templates to my team's automated build process. Our SCM team doesn't want Visual Studio on our build machine (which I have a hard time arguing with).

Is there a way to install the T4 engine without Visual Studio?


Solution 1:

I wrote a cleanly reverse-engineered implementation of a T4 engine for the MonoDevelop IDE. It's open-source, licensed under the permissive MIT/X11 license, so you are free to embed the engine in your app or redistribute it. There's also an implementation of the TextTransform.exe command-line tool available as a dotnet global tool, and some APIs in the Mono.TextTemplating namespace to aid in hosting the engine.

You can get packages from NuGet and the source is on GitHub.

Solution 2:

We're not actually generating code... we're generating Sandcastle scripts and we definitely want that done on the build server. We've taken an approach where we've created a series of custom attributes that we decorate our code with that shapes how the doc is generated. So we have a set of .tt files that reflect our assemblies for these attributes, and generates Sandcastle's input file (.shfb). I could have done it with a custom tool, but T4 fit the bill nicely.

Anyway... I've confirmed that you can run TextTransform.exe without Visual Studio. All you need is the Microsoft.VisualStudio.TextTemplating.dll present. I created a fresh VM, installed .NET 3.5, and copied the following to the file system:

  • TextTransform.exe
  • TextTemplate.ico
  • Microsoft.VisualStudio.TextTemplating.dll
  • Test.tt (something I created)

Test.tt looked like this:

<#@ template language="C#3.5" debug="true" hostspecific="true" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections"#>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Xml" #>
<#@ output  extension=".cs" #>

Test: <#=System.DateTime.Now.ToString()#>....

From the VM I executed Test.tt like so:

C:\TextTransform.exe Test.tt

and Test.cs was created containing

Test: 6/10/2009 5:33:32 PM....

VICTORY!!!!

The hardest part was finding Microsoft.VisualStudio.TextTemplating.dll. I had to fire up FileMon and execute a template on my box which has Visual Studio. FileMon then told me where TextTransform.exe was loading it from. This can be in several places potentially, but I found it in the GAC at C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.dll. In order to browse to that location using Windows Explorer, I had to create a subst like so:

subst X: C:\Windows\assembly

then I could browse to X:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating\9.0.0.0__b03f5f7f11d50a3a\ and copy the dll.

Anyway... thanks for the idea mannish. Hopefully this helps some other poor soul someday :)

Solution 3:

So this is the approach I ended up taking:

I have a development machine with visual studio 2010 sp1 and a build machine (using msbuild).

  1. On the developmental machine install Visual studio 2010 sp1 sdk
  2. On the developmental machine install Microsoft Visual Studio 2010 Visualization & Modeling SDK
  3. On the build machine create a folder
  4. Copy the following files from the development machine to the new folder on the build machine:
    • \Program Files\Microsoft Visual Studio 2010 SDK\VisualStudioIntegration\Common\Assemblies\v4.0\
      • Microsoft.VisualStudio.TextTemplating.10.0.dll
      • Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll
      • Microsoft.VisualStudio.TextTemplating.VSHost.10.0.dll
    • \Program Files\MSBuild\Microsoft\VisualStudio\TextTemplating\v10.0\
      • Microsoft.VisualStudio.TextTemplating.Sdk.Host.10.0.dll
    • \Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\
      • Microsoft.VisualStudio.TextTemplating.Modeling.10.0.dll
    • \Program Files\Common Files\microsoft shared\TextTemplating\10.0\
      • TextTransform.exe
      • TextTransform.ico

Hay presto you should be good to go.


Further reading:

  • For more detailed info see this msdn article
  • And Oleg Sych's excellent blog post on msbuild integration

Solution 4:

I haven't tried it, but I believe you would be able to take the command line tool, drop it on your build server, and transform your templates prior to/during the build.

http://msdn.microsoft.com/en-us/library/bb126461.aspx

Solution 5:

My understanding is that you would still need to have Visual Studio installed. Why swim against the current though? Have you considered generating code at design time as opposed to build time?