Pablo Galiano : TTxGen on MSDN Code Gallery

Subscriptions

<October 2008>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

News

Subscribe to Pablo Galiano by Email

Post Categories

TTxGen on MSDN Code Gallery

TTxGen is a generic single file generator based on the text templating engine.

What is that???, Some background:

Single file generators or custom tools are COM components registered with Visual Studio that generate code.

To implement a custom tool you basically need to implement the IVsSingleFileGenerator interface. For more detail you can read kzu's post 

A custom tool also needs to be registered and associated to file a extension.

The IVsSingleFileGenerator interface declares two methods:

int DefaultExtension (
	out string pbstrDefaultExtension
)

This method is used to specify the extension of the generated file.

int Generate (
	[InAttribute] string wszInputFilePath,
	[InAttribute] string bstrInputFileContents,
	[InAttribute] string wszDefaultNamespace,
	[OutAttribute] IntPtr[] rgbOutputFileContents,
	out uint pcbOutput,
	[InAttribute] IVsGeneratorProgress pGenerateProgress
)

This method is used to generate the code that we want. It receives the name of the file associated with custom tool, the content of the file and it returns a Byte[] with the generated code.

One serious drawback is that we need to implement, register and deploy one custom tool for each representation of custom code that we want to generate.

Ok that's all for custom tools, now the funny part...

As you may know the text templating (t4) engine is now part of Visual Studio 2008. Based on this I created an implementation of a generic single file generator using t4.

The idea here is to have the same context as in custom tools. For that I have created a custom directive processor that injects the context needed on the tt:

<#@ output extension=".xml" #>
<#@ ParentFileInjector processor="TTxGenDirectiveProcessor" requires="fileName='Sample.xml'" #>
<#= this.ParentFileName #>
<#= this.ParentFileContent #>

If we want to access the parent file name we can use the ParentFileName variable, and for the content we can use the ParentFileContent.

TTxGen comes with some tooling to create a xGen template with the proper directives and context to start generating code:

scn1

scn2

The Create xGen Template command unfold a tt template, default the extension to the extension of the parent file and it automatically replace the requires argument.

 

You can download TTxGen from codegallery.

 

Pablo

posted on Monday, February 04, 2008 6:10 PM by pga

# re: TTxGen on MSDN Code Gallery @ Thursday, February 28, 2008 6:50 AM

Take a look at the discussion at http://code.msdn.microsoft.com/TTxGen/Thread/View.aspx?ThreadId=90

there is a small bug in your installer.

Benjamin S

# re: TTxGen on MSDN Code Gallery @ Sunday, April 13, 2008 8:49 AM

oW87Wg <a href="http://mmuvniiwpuhn.com/">mmuvniiwpuhn</a>, [url=http://cusvinwxdzll.com/]cusvinwxdzll[/url], [link=http://ntugonkypxpy.com/]ntugonkypxpy[/link], http://aornbqrmcyjq.com/

itonbldr

# re: TTxGen on MSDN Code Gallery @ Thursday, May 01, 2008 10:20 PM

rBC38U <a href="http://paftyiflztpm.com/">paftyiflztpm</a>, [url=http://ecwxxytczygs.com/]ecwxxytczygs[/url], [link=http://jvhouqaxemus.com/]jvhouqaxemus[/link], http://pxgjmnjsedwm.com/

zwdhwurkmgj

# re: TTxGen on MSDN Code Gallery @ Wednesday, June 11, 2008 11:23 AM

Very nice... has anyone gotten it to work in a C++ project?

Greg Hamlin

# No intellisense for .tt file extension ? @ Sunday, July 27, 2008 5:48 PM

How can i have the intellisense for .tt files ?

Thanks,

Johnny

# Intellisense with T4Editor @ Sunday, July 27, 2008 5:53 PM

I just install your T4Editor and it's working... Thank you. Johnny

Johnny

# Intellisense with T4Editor. Not enough &#233;_&#232; @ Sunday, July 27, 2008 6:11 PM

Hi, I tried to create a wrapper from an existing class with TTxGen and I dont know what are the public things of the "this" keyword. Or how I take the Properties of the existing class to be able to wrap them. Could you help me ?
Thanks,
Johnny

Johnny