23

I have encountered a small problem with my T4 code generation.

I have broken my T4 templates up into separate files and placed them in various directories, I have done this so parts of my code generation may be re-used in multiple projects, e.g. model generation, repository generation and service generation all include a core EntityGeneration.tt file.

Unfortunately, when TextTemplating resolves my nested includes, it builds up a long #line pre-processor directive in its generated .cs file, combining all of the relative paths to the lowest level included file.

Unfortunately, as this path is built up with relative paths, it ends up needlessly long, so long in fact that it exceeds the maximum path length (Windows 7).

Here is the line at fault from the generated code in case you are interested:

#line 3 "C:\VS2010\AlbatrossTravelGroup\ASC\AlbatrossTravelGroup.ASC.BusinessRules\Services\Contracts\..\..\..\..\AlbatrossTravelGroup.BusinessRules\Services\Contracts\..\..\..\AlbatrossTravelGroup.Models\Repositories\Contracts\..\..\../AlbatrossTravelGroup.Common/CodeGeneration.tt"

My question is this, how can I disable these directives being written to the generated code file? Failing that, how can I avoid this problem without changing my file structure?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lukazoid
  • 19,016
  • 3
  • 62
  • 85
  • 2
    Currently, as a temporary solution, I have created a folder, "CodeGeneration", created symbolic links in this folder to each of my T4 files to include. Then instead of each included T4 file including other T4 files through a relative path, they use just the file name. Then at the end location where I wish to use these T4 templates, I use a relative path to the "CodeGeneration" directory. This results in a much shorter path length as it uses a lot less relative paths. If anyone wants a full explanation I'll post an answer if requested :) – Lukazoid Oct 25 '11 at 11:02
  • 1
    My temporary solution above broke, no idea why as visual studio simply started to fail resolving the files. Finally I'm having to use the registry to specify additional tt include directories, less than ideal, but seems like the only thing which works. – Lukazoid Nov 03 '11 at 14:42
  • You might try something like the VS macro solution presented [here](http://stackoverflow.com/questions/3506525/t4-preprocessed-template-debugging-not-working-visual-studio-2010-rtm/7505247#7505247). – BitMask777 Nov 02 '12 at 16:35

2 Answers2

21

Visual Studio 2012 adds the linePragmas="false" template directive:

<#@ template language="C#" linePragmas="false" #>

http://msdn.microsoft.com/en-us/library/gg586945(v=vs.110).aspx

Still not sure how to do this in VS2010 which I'm stuck with at work.

RichardTowers
  • 4,682
  • 1
  • 26
  • 43
  • This works in VS 2019 as well. It also disables _#line hidden_ and _#line default_ directives. – kwitee Aug 28 '20 at 05:49
0

I'm not sure this will fix your problem. I don't have a great deal of experience with T4. That said, I'm using it build a number of files in my project and I have not run into the problem you are running into.

In my case, I have a bunch of individual .tt files that render individual files in my project.

The way I'm doing it is as follows: I have a SaveOutput method (stolen from somewhere on stackoverflow, I believe):

void SaveOutput(string outputFileName)
{
    if (!string.IsNullOrEmpty(outputFileName))
    {
        string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
        string outputFilePath = Path.Combine(templateDirectory, outputFileName);
        string output = this.GenerationEnvironment.ToString();
        File.WriteAllText(outputFilePath, output); 
    }

    this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}

So in my main .tt file, I have <#@ includes... #> for my various .tt files for the individual files. For example: <#@ include file="DataObjectInterface.tt" #>

<#+
    public string GenerateInterfaceDataObject(sqlTable table)
    {
        string ifaceName = "I" + table.ObjectName;
        if (table.GenerateInterfaceObjectAsBase)
        {
            ifaceName += "Base";
        }
#>
using System;

namespace <#= InterfaceNamespace #>
{
    public interface <#= ifaceName #>
    {
<#+
        foreach(sqlColumn col in table.Columns)
        {
#>
        <#= GetColumnCSharpDataType(col) #> <#= col.FieldName #> { get; }
<#+
        }
#>
    }
}
<#+
        return RootPath + @"\AutoGenerated\" + ifaceName + ".cs";
    }
#>

Then, in my main .tt class, I simply make a call like this:

string filePath = GenerateInterfaceDataObject(table);
SaveOutput(filePath);

Not sure if this method of breaking your templates up will work for your needs, but it worked for mine and I haven't run into any problems with it yet.

I should clarify that my main .tt file doesn't actually generate anything from itself. It is not itself a template. It simply loads each of the templates and generates the files from the the other templates.

Pete
  • 6,585
  • 5
  • 43
  • 69