T4 in item template and C# generation - visual-studio

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

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

Related

Is it possible to configure a csproj file to allow any project item type?

I'm working on a unique project in that I would like to be able to have CSHTML Razor Views, Windows Forms, and various other "non project type crossing" project item templates in one single project in visual studio.
Is there a way I can override the project type so that it compiles as a Windows Form's project but still allows me to add CSHTML files and other template types to it?
When you edit the .csproj using notepad or an xml editor you'll find that <ProjectTypeGuids> element.
You can then edit it to have an entry like below
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: these are Guids from a portable class library project. You need to find correct ProjectTypeGuids for your requirements and add it.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d9d05cdc-96a1-4044-95d8-a4f8885a660a/what-is-the-significance-of-projecttypeguids-tag-in-the-visual-studio-project-file?forum=vsx

Finding T4 text template class code

T4 text templates can be used to generate not only code but also any kind of text with visual studio.
I've read blogs and tutorials about T4 and as far as I can understand, visual studio dynamically builds a class in the background, compiles and runs the code in that class to build the text output.
Is it possible to see the source code of that class?
Yes, the easiest way is to change the Custom Tool in the properties window when the template file is selected in Solution Explorer.
By default, it will be 'TextTemplatingFileGenerator'.
If you change the custom tool to 'TextTemplatingFilePreprocessor' you'll get the underlying template class instead of the template output generated into your project.
To be precise, this code won't be exactly the same as that which is run under the covers, but it will be very close.
If you need the absolute exact code, you should leave the custom tool alone, but set the debug="true" flag on your <## template #> directive. This will then leave the generated code sitting around in a random named '.cs' or 'vb' file in your %TEMP% directory. Just sort the directory by time and it should be up at the top.

How do I disconnect code generated by a T4 template from that template?

When I 'run' a T4 template, the class files it generates for me all appear under the template. I cannot remove the template from the project without the files going away too. I also can't copy the files out from 'under' the template into the project root because the are already in the project root. How can I emancipate these files from the template that created them?
Get creative:
Right click and open containing folder.
Drag-and-drop the generated file to another location in the solution.
Delete the original template and file from Visual Studio.
Presto.
If you want to do one-time code-generation with T4 then I would suggest using T4 scaffolding. This tool was originally created to support the generation of views, controllers etc. in MVC3 but you can use it on pretty much anything.

How to automatically run custom tool for T4 file when another file is saved

Is there a way to automatically run the T4 transform when another file is saved?
The AutoRunCustomTool Visual Studio extension was created to address this exact scenario.
It seems like T4 Toolbox is not available for VS2012. However there is now a feature in tangible T4 Editor that allows you to simply add a auto-transformed template to any exisitng file in your solution so that when the parent file is saved the dependend t4 file is transformed. See here:
http://t4-editor.tangible-engineering.com/blog/dependent-t4-templates.html
You can do that with T4 Toolbox. Select the file in Solution Explorer and set the Custom Tool Template in the Properties window.
I get this answer form How can set the custom tool property of an xml file to a T4 file?

Xcode - change project templates

So I can add new templates for single files, but I want Xcode to uses these files when setting up the new project (most noticeably the Controller and Delegate .h and .m files). How do I do that?
In addition to the templates for individual classes that you found, there are also templates for the various project types. If you just want to replace a single class file that is already being added in the projects you can just replace the file in each of the project template files (as long as the names are the same). You will need to copy the template files into each project template that you want to change it in. The other option is to make your own project templates with whatever default files you want.

Resources