37

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?

kellyb
  • 1,391
  • 1
  • 11
  • 18

6 Answers6

41

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.

Mikayla Hutchinson
  • 16,113
  • 2
  • 44
  • 50
  • +1 to this suggestion. Compile the MonoDevelop TextTransform executable and ship it with Mono.TextTemplating.dll as it's lone dependency. No wacky VS install issues, no tweaking build servers, no wacky VS redistribution licensing issues. – Ethan J. Brown May 23 '12 at 14:12
  • Can you point me to somewhere where the licence and the code are linked other than this answer? There doesn't seem to be anything in the repo, or in the copy of MonoDevelop that I installed. – Martin Eden Jan 10 '14 at 14:19
  • The license is MIT/X11. You can see it in the headers of the files e.g. https://github.com/mono/monodevelop/blob/master/main/src/addins/TextTemplating/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs – Mikayla Hutchinson Jan 10 '14 at 21:03
  • @BrainSlugs83 Don't be so unappreciative and rude. Ask mhutch how many minutes he spent writing this implementation that he "won't get back" just to help out ingrates like you. – x0n Apr 07 '14 at 19:16
  • @x0n -- didn't realize it was unappreciative or rude -- it's a fact, the binaries aren't available for download (and they're not trivial to build either) -- and it's clearly not compatible with the full set of what T4 can do... – BrainSlugs83 Sep 10 '14 at 08:13
  • @mhutch yeah, I'll see if I can get to it in the next couple of weeks. -- also, can you verify the link you posted is correct? -- it looks like that's for logging bugs with Xamarin? – BrainSlugs83 Sep 10 '14 at 08:14
  • (FWIW, I'm only using the "file" and "once" attributes in my include directives...) – BrainSlugs83 Sep 10 '14 at 08:19
36

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 :)

kellyb
  • 1,391
  • 1
  • 11
  • 18
  • 4
    Can't get this to work, did you have to do anything special on the build server (registering the dll) or was it just a case of paste and go? Thanks for any advice – Wilfred Knievel May 18 '11 at 11:09
9

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:


Community
  • 1
  • 1
Wilfred Knievel
  • 3,225
  • 1
  • 26
  • 33
  • I did this but the generated CS files have the assembly default namespace instead of the actual full namespace. did you get over this? – Tomer W Dec 27 '17 at 12:22
5

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

nkirkes
  • 2,635
  • 2
  • 21
  • 36
4

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?

Oleg Sych
  • 217
  • 1
  • 1
  • 1
    We changed the EULAs of VS2010 SP1 to make it clear that it is acceptable to redistribute these files to a build server (only a build server) – GarethJ Sep 22 '11 at 01:57
0

For 12 you only need to copy

C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\12.0

Michael Blake
  • 2,068
  • 2
  • 18
  • 31