Search code examples
subsonic3nantt4subsonic-active-record

How can I automate the t4 code generation for SubSonic


I'm using SubSonic 3 (ActiveRecord mode) to generate the code for my DAL. It uses T4 templates (.tt) files that as soon as you save, generate the code for you.

I want to automate this as part of my NANT build, but I can't get this to work. I know that MS provide a tool called TextTransform to generate the code from T4 templates, but in the case of the SubSonic templates this doesn't seem to work - I think the templates make some assumptions about the template being run from within Visual Studio, which doesn't seem to work from the command line. The error I get when I try to run it against ActiveRecord.tt is:

Subsonic\ActiveRecord.tt(0,0) : error : Running transformation: System.InvalidCastException: Unable to cast object of type 'Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost' to type 'System.IServiceProvider'. at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.GetCurrentProject() in c:\Users\matt.roberts\AppData\Loc al\Temp\subsonic\Settings.ttinclude:line 103 at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.GetConnectionString(String connectionStringName) in c:\U sers\matt.roberts\AppData\Local\Temp\subsonic\Settings.ttinclude:line 51 at Microsoft.VisualStudio.TextTemplating3d54bbced2424853b667e74a81b9089b. GeneratedTextTransformation.get_ConnectionString() in c:\Users\matt.roberts\AppData\ Local\Temp\subsonic\Settings.ttinclude:line 87

Has anyone managed to automate this generation?

Thanks

Matt.


Solution

  • I wrote a blog entry that covers this ground somewhat:

    Options for running T4 templates from .NET code

    To do what you are trying to do will require some surgery on the SubSonic T4 templates. Specifically, you'll have to (minimally) replace all references to EnvDTE.DTE with something that doesn't require Visual Studio to be running. That may be as simple as hard coding some paths and/or configuration information into your T4 templates if you want to just "get it working".

    It's important to remember that T4 templates are a somewhat thin wrapper around .NET code. If you can do it from .NET, you can very likely do it from T4, you just have to understand how to work within the different set of constraints that T4 provides. Different T4 "hosts" (like Visual Studio vs. TextTransform.exe) behave in different ways and just because a T4 template runs fine under one host doesn't not mean it will run fine under another host.