Search code examples
c#visual-studio-2012t4template-enginesolution

T4: Output file names differing only by extension


We have a .cs file and a .vb file that are each linked into dozens of projects in their respective languages to provide common assembly information. I'd like to set the common aspects between them in T4 by moving each to a template.

AssemblyInfo.cs and AssemblyInfo.vb should be the output file names. However, I know that normally, a T4 template for either of these would be named AssemblyInfo.tt.

Since I obviously can't have two T4 templates with the exact same name, how can I output two files that are different only by extension?

I can't rename the files to be written without un-linking them from many different projects and then having to re-link them again, so that's out of the question.

Is there some sort of directive that could be used to manually specify the output file name so that the templates could have different names?


Solution

  • By default it is not possible to have multiple output files for a single T4 template. But out there are some implementations that offer you this possibility. For example, if you are using tangible's T4 Editor they provide a free template gallery containing a includable template named "TemplateFileManagerV2.1". Referencing this in your T4 template allows you to generate multiple output files, even in different projects of your solution.

    Here's how that could work for you:

        <#@ template language="C#" hostSpecific="true" #>
        <#@ include file="TemplateFileManagerV2.1.ttinclude #>
    
        <#
          var manager = TemplateFileManager.Create(this);
          manager.StartNewFile("output.cs");
        #>
          // content here will be rendered into
          // output.cs file
    
        <#
          manager.StartNewFile("output.vb");
        #>
    
          // content here will be rendered into
          // output.vb file
        <#
          manager.Process();
        #>
    

    You might have a tough time tough, since you're using two different output languages in the same T4 template. Thus the syntax highlighting will be messed up...

    Hope that Helps