Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > Use T4 when unfolding solution
 

Use T4 when unfolding solution

Hi

I have a recipe which isunfolding a solution. When unfolding the solution I amunfolding a number of projects. I want to generate a class inside one of the projects as the project template unfolds.

One solution is toinclude theclass I wish to generate in the same location as the project vstemplate and add a ProjectItem entry in the project vstemplate e.g.

<

ProjectItem ReplaceParameters="true" TargetFileName="Test.cs">Test.cs</ProjectItem>

My question is that is it possible to re-use a T4 template I have created earlier instead of. So can I have something like:

<

ProjectItem ReplaceParameters="true" TargetFileName="Test.cs">..\..\..\Text\Test.cs.t4</ProjectItem>

Or can I not use T4 templates in this scenario?

Thanks

KevinH1979  Tuesday, June 09, 2009 3:18 PM
I got a solution to this problem. As the solution unfolds, I can use the GetProjectAction to get a reference to each of the projects that are created (as I know the Project Names,which isare arguments in the wizard).I can then use the output ofthe GetProjectActionactions in the AddItemFromStringAction to transform the T4 template and add it to the project.

Although for generating a large number of projects and where complex transformations aren't required, I suspect standard project item templates are quicker.

Thanks for your help Oleg.
  • Marked As Answer byKevinH1979 Wednesday, July 01, 2009 1:00 PM
  •  
KevinH1979  Wednesday, July 01, 2009 1:00 PM

Guidance Automation Extensions should allow you to use T4 in this scenario.


Oleg
Oleg V. Sych  Tuesday, June 09, 2009 4:01 PM
Hi Oleg

Thanks for the response. Do you know of any working examples I could look at, as I can't get it to work?

If I can use T4 in this scenario, in what scenario would I use a class template i.e.

ProjectItem ReplaceParameters = " true " TargetFileName = " Test.cs " >Test.cs </ ProjectItem >


and not T4?

Thanks
KevinH1979  Sunday, June 14, 2009 8:20 PM

Here is an example I found in Guidance Automation Toolkit documentation ("Defining Item Templates" topic):

<VSTemplate Version="2.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>T4ItemTemplate</Name>
    <Description>An example of a T4 Item template</Description>
    <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4515" />
    <ProjectType>CSharp</ProjectType>
    <SortOrder>10</SortOrder>
    <DefaultName>T4ItemTemplate.cs</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true">ItemT4.t4</ProjectItem>
  </TemplateContent>
  <WizardExtension>
    <Assembly>Microsoft.Practices.RecipeFramework.VisualStudio, Version=1.0.60429.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
<FullClassName>Microsoft.Practices.RecipeFramework.VisualStudio.Templates.UnfoldTemplate</FullClassName>
  </WizardExtension>
  <WizardData>
    <Template 
        xmlns="http://schemas.microsoft.com/pag/gax-template" 
        SchemaVersion="1.0"
        Recipe="CreateT4Item">
    </Template>
  </WizardData>
</VSTemplate>



You would probably want to start by using a standard project item template, which doesn't require GAX. Standard templates support only simple string replace logic. If this is not enough for your templates, GAX and T4 may be the way to go.
Oleg
Oleg V. Sych  Monday, June 15, 2009 11:44 AM
Hi Oleg

Again thanks for your response. I did see this in GAT documentation but didn't think it would help me. I also read your article which was very helpful in further understanding T4.

My problem is that the project does not exist at the point where I am trying to use the T4 template. In your article, you talk about selecting templates from the "Add New Item" dialog box for an open / new project.
Using GAT I can create a recipe which will perform actions to transform the T4 template and use the generated code to create a class file, when a user selects a "template" from the "Add New Item" dialog box for an open / new project.

However, in my scenario, I have a recipe which is unfolding a solution and the related project is part of the solution. As that project unfolds, I want to use T4 to generate a class file (in the given project) based on the arguments available in my recipe. I assumed I would be able to simply include a reference to the T4 template in my project vstemplate e.g.

<VSTemplate Version="2.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>Sample Project</Name>
<Description>Sample project</Description>
<Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4547" />
<ProjectType>CSharp</ProjectType>
<SortOrder>20</SortOrder>
<CreateNewFolder>false</CreateNewFolder>
<DefaultName>SampleClass</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
</TemplateData>
<TemplateContent>
<Project File="SampleProject.csproj" ReplaceParameters="true" TargetFileName="SampleProject.csproj">
<ProjectItem ReplaceParameters="true" TargetFileName="$TargetFileName$">SampleClass.cs.t4</ProjectItem>
</Project>
</TemplateContent>

where SampleClass.cs.t4 is a T4 template that resides in the same directory as the vstemplate. However, I believe I'm missing something in my understanding:

Either:
a) This is simply not possible and I should use standard project item templates
or
b) I need some way of calling action(s) prior to the project being created to transform the T4 template and create the class to be generated, which can then be included as the project unfolds.

In your example above from the GAT documentation, it is possible to call a recipe from an Item Template, however in my scenario as the arguments I want to use are already available to the vstemplate file (as the recipe has already been invoked), I don't want to actually do this.
  <
WizardData
>

< Template
xmlns = " http://schemas.microsoft.com/pag/gax-template "
SchemaVersion = " 1.0 "
Recipe = " CreateT4Item " >
</ Template >
</ WizardData >

Any ideas / thoughts would be appreciated ... Thanks

KevinH1979  Monday, June 15, 2009 5:12 PM
I got a solution to this problem. As the solution unfolds, I can use the GetProjectAction to get a reference to each of the projects that are created (as I know the Project Names,which isare arguments in the wizard).I can then use the output ofthe GetProjectActionactions in the AddItemFromStringAction to transform the T4 template and add it to the project.

Although for generating a large number of projects and where complex transformations aren't required, I suspect standard project item templates are quicker.

Thanks for your help Oleg.
  • Marked As Answer byKevinH1979 Wednesday, July 01, 2009 1:00 PM
  •  
KevinH1979  Wednesday, July 01, 2009 1:00 PM

You can use google to search for other answers

Custom Search

More Threads

• Adding drop down menu using editor
• BizTalk Project Type
• conditionally adding a binary reference to a project
• Creating projects in a subdirectory
• Smart Client Development - problem whit context menu i VS2005
• Error In GAT
• Programatically Executing a recipe while changing an input argument
• MSI Installer Report
• Some items in HoL don't match GAT
• How can i add a project in existed Solutionfolder??