I would like to know if is possible to run a T4 template when saving a other file, example run my .tt file after/on saving a xml file
I forgot to mention that my IDE is VS2010
Yes, it is possible. Here is how.
Related
I've been searching on the web for this, and maybe I'm just using the wrong keywords or something? I could use some help.
My problem is simple - we have a bunch of reference.cs files in our solution, which were auto-generated by VS2010 when adding services. These files don't add XML comments by default, so when we build the project, I get 800 or so messages in the build list. This doesn't break anything, but it does make the build take (significantly) longer, and mucks up the output screen.
I "fixed" this by adding the appropriate #pragma statments to the beginning and end of each reference.cs file, but if those ever get regenerated, they will have to be re-added by hand. I'd like to streamline that process and just add them to whatever T4 template VS2010 is using in the first place. The problem is, I don't know where that is, or if VS2010 is using something else to build these files?
Can this be done? Is there a better solution? I don't necessarily want to turn off XML comments for the entire project.
Visual Studio does not use T4 templates to generate the service reference proxy classes (Reference.cs). Instead Visual Studio is most likely using the WsdlImporter and ServiceContractGenerator classes to generate this code.
There is a stackoverflow post on using either a custom wsdl exporter or WCFExtras to add xml comments to the generated code. Both of these assume you have access to the code for the services you are referencing.
I would like to automatically execute one of the .tt files when my project is built, but not the others. Is it possible to set the TransformOnBuild property on specific .tt files instead of project wide? Or another method to accomplish this?
I have decided to add a custom pre build step to generate the .tt files needed:
<Exec Command=""%PROGRAMFILES(x86)%\Common Files\microsoft shared\TextTemplating\10.0\TextTransform.exe" template.tt -out template.cs" />
This method also allowed me to easily pass parameters (defined in the project file) to the template with -a; another problem I was facing.
Answering as another method to accomplish this.
You could change your .tt as TextTemplatingFilePreprocessor
(descibed better here:
http://www.olegsych.com/2009/09/t4-preprocessed-text-templates/).
Create small command line tool that executes the preprocessor on pre-build event.
The first link includes this example, but it describes the use of preprocessed class:
http://blogs.clariusconsulting.net/pga/vs10-beta-1-t4-preprocessing-part-2/
How can I specify the output path of a T4 template?
When I add my T4 template using "Add as Link" to my project from $TemplatePath$, it generates the output file in $TemplatePath$, not $ProjectPath$. How can I specify that $ProjectPath is where the generated file is placed?
I got around this by having a master template in one location and then templates that imported that one (but did little else) in each project. Of course, that's not the nicest solution.
We typically use the same system that Jeff Yates mentioned with minimal (often single line) stub templates including the shared template.
You could also use Oleg Sych's T4Toolbox to project output to any directory you please.
However, the standard IVsSingleFileGenerator that T4's custom tool is built upon doesn't have any dials to turn to control it's output location and it's behavior in the case of linked files is as you describe.
I am using T4toolbox to generate a bunch of files, let's say my t4 file name is x.t4, but default it generate a x.txt, which has nothing inside, can I tell t4 engine not to do this?
Found a trick/hack!
<## output extension="/" #>
or
<## output extension="\\" #>
Visual Studio neither outputs the default file nor complains about its inability too much.
If you want to avoid warnings, you can also modify output path through the extension:
<## output extension="xml/../_" #>
The file will still be created and attached to the T4 file within the project hierarchy in Visual Studio, but you can put it into any directory.
P.S. I've tried it with T4MultiFile NuGet package, but it should work with T4Toolbox too, I think.
Right click on x.t4 in Solution Explorer and click Properties. It will say "TextTemplatingFileGenerator" beside Custom Tool. Delete this.
x.t4 will now be part of your project but it will not generate anything. This is useful when the .t4/.tt file is only being used as an include file in other templates.
No. This file is created by Visual Studio and not by T4. The best you can do is generate something useful in it such as actual code or, perhaps, a log of the code generation run.
I wanted to use T4 to generate properties for a partial class. I'm running into a configuration problem where my .TT file is in the same project as the class file I want to extend. So if I have to include the assembly in the .TT file it get's locked. I tried to do a command line in the pre-build but the problem there is that VS always wants to recompile the .TT file with the project.
The only solution I can think of is to rename the .tt files to say .t4 and then use a pre-build command with TextTransform -out to create the .cs file in the project directory.
Can anyone think of a cleaner way to do this?
Assuming that locking is caused by your template using Reflection to read metadata of the partial class you need to extend, you could solve the locking problem if you use CodeModel. This API is provided by Visual Studio and allows you to get the metadata directly from the source file, without the need to compile the partial class or load the compiled DLL. Here is an example of a T4 code generator that uses this approach: http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration