it:ad:t4:howto:create_a_code_file

IT:AD:Transformations:T4:HowTo:Create a Code File

After starting a new page

Use IT:AD:Transformations:T4:Syntax:Directives to include what you need:

<#@ include file=”T4Toolbox.tt” #>

Use IT:AD:Transformations:T4:Syntax:CodeBlocks Create the namespace:

namespace <#=codeGenerationTools.EscapeNamespace(namespaceName)#>
{
//Code goes here:
}

Outputing Collapsible Regions

Single File T4 code quickly become unwieldy, multiple T4 files even worse. THerefore, whereas collapsible code sections in C# is a AntiPattern, collapsible code in T4 scripts is helpful.

<#
CodeRegion codeRegion = new CodeRegion(this, 1);
...
region.Begin("Primitive Properties");
#>

//Your code here

<#
codeRegion.End();  //Or ClearIndent();
#>
Note:The region will not be generated if no source code is generated for the region.

Regions are ok…but what really makes code readable are indents/outdents…

<#
PushIndent(”\t”); //or ”\t\t” or "  "
#>
 
//Blah...

<#
PopIndent();
#>

What do you mean PushIndent/PopIndent?!? Where did those method definitions come from?! Who the heck defined those methods?!?

Turns out the while you are in the *.tt file, you are in the context of a class – the TextTransformation class.

So, since you are in the context, anywhere in the page gives you access to a couple of other methods worth knowing about (links point to MSDN documentation pages):

  • ClearIndent: Resets the CurrentIndent to an empty string.
  • Error: Creates a new error to store information about errors that occur during the text template transformation process.
  • Initialize: Infrastructure. Initializes the TextTransformation class.
  • PopIndent: Removes the most recently added text from CurrentIndent.
  • PushIndent: Adds text to CurrentIndent, which is prefixed to each line of the generated text output.
  • TransformText: When overridden in a derived class, generates the text output of the transformation.
  • Warning:Creates a new warning to store information about errors that occur during the text template transformation process.
  • Write(String):Appends a copy of the specified string to the generated text output.
  • Write(String,Object()): equivalent of regex StringFormat…
  • WriteLine(String): Appends a copy of the specified string and the default line terminator to the generated text output.
  • WriteLine(String,Object()): equivalent of StringFormat
The most commonly used will be WriteLine and the indent/output.

[

http://msdn.microsoft.com/en-us/library/bb126474.aspx](http://msdn.microsoft.com/en-us/library/bb126474.aspx)

If we lived in a bubble…But we don't.

So we need to know how to get in touch with the world outside of our template…

EnvDTE.DTE dte = (EnvDTE.DTE) ((IServiceProvider) this.Host)
                   .GetService(typeof(EnvDTE.DTE));

Src: http://msdn.microsoft.com/en-us/library/bb126474.aspx

A perfectly good example of why we need to ask questions outside of our template is to inquire as to the project's default namespace:

<#
// Get the project's default namespace:
IServiceProvider serviceProvider = (IServiceProvider)Host;
DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));

ProjectItem templateItem = dte.Solution.FindProjectItem(Host.TemplateFile);
#>
namespace <#= templateItem.ContainingProject.Properties.Item("DefaultNamespace").Value #>.SomeSubNSOfYours {
//...
}

 

Are there any other methods we can use, other than what was available in the TextTransformation class?

Sure…just include the standard template that is used by all of the EF4 templates:

<#@ include file="EF.Utility.CS.ttinclude"#>

It's a single file that contains several classes of ready helper methods.

See here for the full list of methods you may find useful: [[IT/AD/T4/Syntax/ttinclude/]]
  • /home/skysigal/public_html/data/pages/it/ad/t4/howto/create_a_code_file.txt
  • Last modified: 2023/11/04 01:59
  • by 127.0.0.1