Compiling templates on a build machine without Visual Studio

Nov 29, 2008 at 1:33 PM
I've used fairly simple T4Templates already. I rely on a pre-build event in the project consuming to ensure that the templates are compiled on every build. This works fine on the build machine that uses CruiseControl executing MsBuild rather than via visual studio.

Now I want to start using the T4Toolbox. I am concerned that the pre-build events will not work on the build machine as the compile is performed using MsBuild via CruiseControl and not via Visual Studio. Is my assumption correct? And if so is there any workaround?

Thanks
Christian
Coordinator
Nov 29, 2008 at 6:43 PM
Christian,

The short answer to your question is that T4Toolbox must run inside of Visual Studio. It uses EnvDTE (Visual Studio automation API) to update projects with multiple generated files and check out previously generated files in source control. Some of the templates (such as EnumSqlView) also rely on CodeModel API of Visual Studio.

It is possible to modify T4Toolbox and make the main framework work outside of Visual Studio, possibly at the cost of loosing the benefits of automatic check-outs and management of multiple output files. Could you provide more details about the kind of code are you generating at build time and why you prefer to generate code at build time as opposed to design time? How would the build/code generation process interact with the source control repository?

Oleg

Nov 30, 2008 at 5:19 PM
Thanks for the reply Oleg.

>How would the build/code generation process interact with the source control repository?

I prefer to avoid the situation altogether by not checking in the generated files. I keep the template in the same project that consumes it. Yes I know that supporting a production version of our code when the T4Template tool itself can change is a risk with this approach. However we mitigate this risk by a large suite of unit tests plus we have VM as a full back to build our code using the same version of T4Template tool as the production version.

>why you prefer to generate code at build time as opposed to design time?

Two reasons:
1. we don't checkin the generated code. We therefore need the code to be generated using a Pre-build event.
2. there is in my mind a serious flaw to the T4Template tool: you have to run manually "Run Custom Tool" on each template if an included template changes. We workaround this flaw by using Pre-build events to ensure that the templates are always run and our code re-generated.

>It is possible to modify T4Toolbox and make the main framework work outside of Visual Studio, possibly at the cost of loosing the benefits of automatic check-outs and management of multiple output files.

Well it would increase the tools target audience. However, I'm really just starting out here with T4 templates so I'm not in a position to make suggestions like this.

Would it be possible to list the features that T4Toolbox provides over and above what you get with VS2008? That way I can get a feel for whether there is value in removing the constraint T4Toolbox has on VS.

Thanks
Christian
Coordinator
Dec 9, 2008 at 10:42 AM
Christian,

I'm sorry for the delay. Thanks for the explanation. We certainly prefer different approaches to code generation, but I can see why you prefer to generate code during build.

To answer your question, here is a quick list of features T4 Toolbox provides today on top of plain T4:

* Framework for composite code generators
* Automatic management of output files in the project
* Unit test runner
* Visual Studio project item templates
* (Limited number of) Ready to use code generators

It also has a very limited abstraction layer between the T4 host and the framework, which could be improved to support code generation outside of Visual Studio.

P.S. I understand this is a very dry description. I will post more details on my web site by the end of this week.

Oleg
Coordinator
Dec 22, 2008 at 6:22 PM
Christian,

Here are the details for the bullet list from my previous post: http://www.olegsych.com/2008/12/what-is-in-t4-toolbox/

I'm sorry it took so long; hope you find it useful .

So, do you think you there is enough value in T4 Toolbox for your scenario to invest some effort in removing the Visual Studio constraint?

Oleg
Coordinator
Mar 31, 2009 at 12:13 PM
Christian,

Marcos Romero implemented support for build-time code generation with T4 Toolbox. His approach is based on launching Visual Studio programmatically when transformation is running under the command line host. More details here: http://t4toolbox.codeplex.com/Thread/View.aspx?ThreadId=46207. I would appreciate your feedback.

Oleg
Coordinator
Apr 8, 2009 at 11:39 AM
We are actively discussing how to support build-time code generation in T4 Toolbox here: http://t4toolbox.codeplex.com/Thread/View.aspx?ThreadId=52069. Please join the discussion if you are interested.

Thanks,
Oleg
Aug 5, 2009 at 6:03 PM

christianacca wrote:

...We workaround this flaw by using Pre-build events to ensure that the templates are always run and our code re-generated.

Christian (and all) --

Can you post a sample of the pre-build event the ensures code regeneration?

AFAIK, it seems like it it still the case that "you have to run manually 'Run Custom Tool' on each template", unless I have missed something???

I was thinking of writing a VS.NET macro to do it but, ug, I do not find those much fun-- so, if there is a generic way to say "execute a 'Run Custom Tool' command for all TT files in my project", via a menu-item or a pre-build event or whatever, then that would be great.

What do you think?

-- Mark Kamoski

Aug 5, 2009 at 6:10 PM

All --

Wait a minute.

I just found the little icon in the upper-right of the Project Explorer in Visual Studio-- where the tooltip says "Transform All Templates".

Nice.

That works for me.

When I look back at this thread in retrospect, I see the ambiguity of language or my my reading or both.

That is, the phrase "to do it at build time" can mean (A) "build time, as in when you choose 'Rebuild Solution' in VS.NET" (which is what I was thinking) and it can mean (B) "build time, as in when you are building the Solution and packaging as a deployable unit" (which seems to be what Christian was thinking).

That was my misread, I think. Sorry about that.

I am am good with teh "Transform All Templates"-- however, now that I think about all THAT, I would like to have VS.NET auto-click that button every time one choose "Rebuild Solution".

Also, for connotation (B), above, I still would like to know if and how that can be solved.

Thank you.

-- Mark Kamoski

Coordinator
Aug 5, 2009 at 9:38 PM

Assuming that option (B) referrs to generating code with T4 toolbox on build machine, without visual studio running, there is currently no support for this scenario in the T4 Toolbox. Here is the discussion thread http://t4toolbox.codeplex.com/Thread/View.aspx?ThreadId=52069. However, T4 itself provides a command line tool - TextTransform.exe, which can be used to transform plain T4 templates at build time. Of course you loose the ability to generate multiple files and add them to target projects automatically offered by the T4 Toolbox.

Aug 6, 2009 at 3:46 PM
olegsych wrote:

...However, T4 itself provides a command line tool - TextTransform.exe, which can be used to transform plain T4 templates at build time. Of course you loose the ability to generate multiple files and add them to target projects automatically offered by the T4 Toolbox...

Oleg --

You lost me. I think. Or, if I understand you correctly, then what you are saying about the command-line tool is not what I am trying to find because I must be able to generate multiple files and add them to target projects automatically. I am sorry; but, I am just learning all this now. Please allow me to clarify my question.

Right now, I can click the icon "Transform All Templates" in the Project Explorer in the VS.NET IDE with my project loaded and it will run all my templates. This is great. I do this manually now. It works. I definitely think this is the right-feature and necessary and should remain. However, I think that that "click of the icon" should be somehow automatically run when various other IDE events trigger.

I think what would be ideal is to have an additional preference settings in >Tools, >Options, >TextTemplating, as follows...

User Preference Setting -- Run Transform All Templates before (>Build, >BuildSolution)? y/n

User Preference Setting -- Run Transform All Templates before(>Build, >RebuildSolution)? y/n

User Preference Setting -- Run Transform All Templates before (>Build, >BuildMyProject)? y/n

User Preference Setting -- Run Transform All Templates before (>Build, >RebuildMyProject)? y/n

 

Question 1: Do you performing the kind of auto-run process of Transform All Templates is a good idea?

Question 2: Given that those preferences do not exist now in >Tool, >Options, >, is there a workaround?

 

Please advise.

Thank you.

-- Mark Kamoski

Coordinator
Aug 6, 2009 at 7:33 PM

1) No. Code generation can be time consuming. I prefer to trigger it explicitly.

2) Save the .tt file, Run Custom tool or Transform All Templates to generate code explicitly. You can also use TextTransform.exe from in the build script itself to regenerate the code programmatically. However, T4 toolbox currently doesn't support this host.

Oct 14, 2009 at 11:11 AM

Hi Oleg,

I think that my question is relevant here, please forgive me if it is not. I am wanting something similar to what it appears the previous people were asking about.

Current Scenario:

I have created an assmebly which is installed in the gac and used within Visual Studio to create plugin assemblies to another application. The client can use this to drag and drop custom web form controls to create a screen (ascx in this case). The top most control has a custom action which triggers some custom code. The custom code takes the controls and creates sample xml based on their layout and properties, then a schema is created and added to the plugin for the xml, the using codeDom, it creates a c# model of the schema with lots of added goodies. The custom code is also using the DTE to add files and folders into the plugin.

Problem:

This all works fine. The problem is that if we change the code generation code that is run against their web controls, we have to open each plugin project and re-run the custom action. This can be time consuming and annoying as the number of plugins grows.

What I really want to happen is, as part of the pre-build, a check performed to see if the code generation does need re-running and then run it if it does. I first thought about just using a custom msbuild task, but for the life of me I don't see how to get hold of the DTE from within a msbuild task. I then stumbled upon T4, read over most of your posts, and found the Host param which let me get access to the DTE through a GetService command. I then thought that if I used a very simple template in each custom project that just called into my code and passed in the DTE, this would work.

Do you have any thoughts on this or do you think I am approaching the whole thing wrong? (I would really like to be able to keep most of the code that I have written and would like the code to be able to be called from stand alone msbuild as well as part of the build process from within visual studio)

Any help would be greatly appreciated.

Thanks

Dewy

Coordinator
Oct 15, 2009 at 11:06 PM

If you have a simple template in each custom project, you can transform them all in Visual Studio by clicking "Transform All Templates" button in the Solution Explorer toolbar.