Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > t4 help
 

t4 help

I do not know anything about T4 templates. I want to try a simple test to help me figure out how they work so I can develop a class template. What is the simplest way to set up my environmentto do this. Ideally I would like to use a T4 template to generate classes based on tables.
lcj  Friday, May 04, 2007 11:56 AM
You should have installed GAT if so, in the default Guidance Package you'll find two samples of how you can use a T4 template. You can create a default GP doing New Project and in the Guidance Packages item select "Guidance Package Development".

There you'll find a Templates\Text folder which have two T4 templates, also in the main manifest YourGuidancePackage.xml you'll find two recipe which are using those templates.
Jose Escrich  Friday, May 04, 2007 2:50 PM
You should have installed GAT if so, in the default Guidance Package you'll find two samples of how you can use a T4 template. You can create a default GP doing New Project and in the Guidance Packages item select "Guidance Package Development".

There you'll find a Templates\Text folder which have two T4 templates, also in the main manifest YourGuidancePackage.xml you'll find two recipe which are using those templates.
Jose Escrich  Friday, May 04, 2007 2:50 PM
How do you create multiple .cs files? I am trying to adapt the example to create a file per iteration.
lcj  Friday, May 04, 2007 7:39 PM

Looks like that is the next action. Does anybody know where:

Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers

Can be found?

lcj  Friday, May 04, 2007 8:18 PM
Hi
The example how to create multiple files using T4 templates you can found in Data Access Guidance Package (part of Service Factory). For example you can take a look at Templates\DAC\EntityFactory.t4 which generates one string for all business entities, but each class is separated by:
Code Snippet

// begin "filename"
here is the content
// end "filename"



This template is rub by CreateDataRepositoryFromDCProject or CreateDataRepositoryFromBEProject recipes (located in Recipes folder in files with the same name). The action which makes the "magic" is action called SplitAndAddItmesFromStringAction located in Actions\VisualStudio. Please take a look at splitRegex which is responsible for splitting generate string into separate classes.
Here is some code:
Regex:

Code Snippet

static readonly Regex splitRegex = new Regex(@"^// name ""(?<name>[^""]+)""\n^// begin ""(?<filename>[^""]+)"".?$\n(?<content>.*)^// end ""\k<filename>"".?$", RegexOptions.Multiline | RegexOptions.Singleline);


Here is code of a "Execute" method which splits generated string into separate files:

Code Snippet

const string FilenameGroups = "filename";
const string ContentGroups = "content";
const string FolderNameGroups = "name";

public override void Execute()
{
DTE vs = GetService<DTE>(true);
projectItems = new List<EnvDTE.ProjectItem>();
for(Match match = splitRegex.Match(content); match.Success; match = match.NextMatch())
{
string relativeFileName = match.Groups[FilenameGroups].Value;
string fileName = Path.GetFileName(relativeFileName);
string relativeTargetPath = Path.GetDirectoryName(relativeFileName);
... bottom is code which adds a file to a given project
}
}


Hope this clarifies your question. If not please let me know I will try to help.
Kind regards,
Lukasz
faf38  Tuesday, May 15, 2007 7:09 AM
Hi,
This namespace is located in Microsoft.Practices.RecipeFramework.Extensions assembly which is part of a Data Access Guidance Package.
Regards,
Lukasz

faf38  Tuesday, May 15, 2007 7:15 AM

You can use google to search for other answers

Custom Search

More Threads

• Create Typed Dataset with EnvDTE
• Download not available
• The file cannot be opened with the selected editor
• Using EntLib with Gax/Gat
• GAT on Visual Studio Release Candidate
• Combining GAT and CAB
• Use AddEntity recipe form HoL on existing porjects
• GAT: unexpected error loading template...
• autosize appplication
• Install of February 2008 GAX Fails