views:

869

answers:

6

I'm generating code dynamically, currently using String.Format and embedding placeholders - but reformatting the C# code for use as a template is a pain, and I think using a T4 template would be better.

However, the code generation will be happening on a running system, so I need to know that I can safely and legally redistribute the Microsoft T4 Engine with my product.

Anyone else done this? Or know the (legal) answer?

+3  A: 

You can redistribute T4 as part of DSLToolsRedist, however, it requires Visual Studio 2005 standard edition or higher to be already installed. I don't believe that T4 can be legally redistributed without Visual Studio at this time. The scenario you described will be directly supported in Visual Studio 2010

Oleg Sych
A: 

I recalled this came up back in 2006 (before I knew what T4 was!) and went searching and found this http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/1ab0bf3f-2810-4adf-bf75-900b98dee8e2/ Unfortunately the gotdotnet link is broken. Tricky to say whethere in that thread they are agreeing that you can legally host T4 outsid of VS and redistribute - or not.

Eric Nelson
+1  A: 

I have the answer and unfortunately it is no as Oleg suggested. (Great work on T4 by the way Oleg). You can not redistribute T4 at this time - it is part of VS. In VS2010 it will be possible to precompile T4 templates and then redistribute those precompiled templates with your application with no T4 dependency to run them.

Eric Nelson
+1  A: 

I have my own template code generator system, compiling the code in a separate appdomain, from before VS2008 came out.

If you're interested, post a comment and I'll post an url to the code.

The code to use my templating engine is here, you can browse to it with a web browser or point a Subversion client at it. Note, the link that starts with here really does stop after the word, but the server-side WMD renderer leaks the link onto the following text.

Note that if you just copy the single file in that namespace, then it won't compile by itself, it requires some stuff in LVK.Delegates and LVK.Scripting, namespaces up a couple of levels from the link above. If you don't want to suck down the whole library, you'll need to extract the pieces it complains about one at a time until it compiles.

There is also a binary version of the library at /LVK_3_5/trunk/Binaries/Debug/LVK in the same repository. If you download that it's as simple as just adding a reference to it, and checking class LVK.Text.Templates.TextTemplate.

Unfortunately I don't have any examples for my library at the moment.

Basically, to use a template:

TextTemplate tt = new TextTemplate();
tt.Source = "... code here, check example file above ...";
tt.Compile();
String output = tt.Generate(singleObjectParameter);

Inside the template, which is basically all code that is inserted in a single method (which means that it's not as good as T4 in terms of being able to add methods easily, but you can use anonymous methods), you'll have access to the data object passed into it as a parameter named data.

So to just output the contents of the passed parameter:

<%= data %>

To repeat it:

<% for (Int32 index = 0; index < 10; index++) { %>
<%= data %>
<% } %>

If you have questions, send them to my email at [email protected].

Lasse V. Karlsen
A: 

It's a real shame, but as Oleg points out, if it's untested then how can you release it.

Lassevk, certainly I'm interested - was thinking about rehashing a template engine I build about 10 years ago, but I'm always up for an easier life!

Rammesses
+4  A: 

It looks like there may soon be another option.

Yesterday, Miguel de Icaza posted about T4 integration in MonoDevelop, so I'm expecting there to be a mono equivalent T4 toolset any time now.

See: http://tirania.org/blog/archive/2009/Mar-10.html

Rammesses
This has actually been released, and you can get the sourcecode from the MonoDevelop repository.A quick compile against .Net and (with a bit of wrangling), you get a fairly easy-to-use T4 engine for your projects. I guess I should write a How-To blog-post on getting it working.
Rammesses