T4 in item template and C# generation
Asked Answered
M

2

18

I want to have a T4 template in a Visual Studio item template, but I don't want the "tt" file to appear in the project after the item has been added. In short, I want to generate a .cs file and then discard the .tt file completely. The code generation is a one time only occurrence when the item is added to the project.

I managed to get the .tt file added to the item template but I cannot see how to discard it after code generation.

Thanks Laurent

Malar answered 27/4, 2014 at 14:22 Comment(6)
If it truly is a 1 time process, why not add the tt file to generate your cs file, then copy off the generated file, delete the tt file, then re-add the copied vs file to the project? The tt will create the actual csv file on disk.May
it is a one time occurrence when the item is added to the project, meaning "every time that a new item is added to the project". I need this step to happen automatically.Malar
Understood, are you able to elaborate on the reason for this? The tt file is ignored by the compiler so this request seems to be purely cosmetic. What able a utility project that you can use to just process the files via a tt?May
This is for MVVM Light, an open source project I created. It is widely use in the xaml/c# community, including many developers who are not always experts. Adding a .tt file to the projects will bring some confusion, which I want to avoid. I don't need additional support request emails asking me what to do with this file.Malar
Understood, I love and use that library in my WP apps. I would suggest creating a project for just doing the transforms for you that you do not include in your open source repository as it seems like a personal utility that you want to use but not share (for readability). And you can think of it like a tool similar to a Macro in a text editor.May
If your software comes with a Visual Studio extension, so if you ship code (or plan to) and not only templates, you could use the IWizard interface: msdn.microsoft.com/en-us/library/ms185301.aspxCryolite
K
0

It's possible with tangible's tool shown here. It's not ideal, but with some finessing you can generate a file from one project containing the template into another. This then leaves the destination project free from templates.

Another approach is to generate a pre-processed template and then amend the generated file to create the output to a customised destination

Keratogenous answered 25/5, 2018 at 15:6 Comment(0)
E
0

If you add the *.tt file for the project it builds automatically. But when you make a right-click and call properties... than you can disable the compile.

enter image description here

Make a build action to none, and after the corresponding csproj file, disable the AutoGen

<ItemGroup>
   <None Update="TextTemplate1.tt">
     <Generator>TextTemplatingFileGenerator</Generator>
     <LastGenOutput>TextTemplate1.txt</LastGenOutput>
   </None>
   <None Update="TextTemplate1.txt">
     <DesignTime>True</DesignTime>
     <AutoGen>False</AutoGen>
     <DependentUpon>TextTemplate1.tt</DependentUpon>
   </None>
Ezarra answered 16/8, 2024 at 11:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.