4

I am finding it really difficult to use t4 templates with a non-standard directory structure. I am using links inside my csproj file, which seems to be at the root of the problem.

I have it working, however, VS automatically makes a change which breaks things.

I've have the following directory structure:

/source
  + MyLib.cs 
/generate
    /MyLib
      + MyLib.tt
      + MyLib.A.t4 // included by MyLib.tt
      + MyLib.B.t4 // included by MyLib.tt
  + MyLib.C.t4 // included by MyLib.tt
/build_examples
   /vs
     + MyLib.csproj
     + MyLib.sln

MyLib.csproj looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <ItemGroup>
    <None Include="..\..\generate\MyLib\MyLib.tt">
      <Link>MyLib\MyLib.tt</Link>
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>..\..\source\MyLib.cs</LastGenOutput>
    </None>
    <None Include="..\..\generate\MyLib\MyLib.A.t4">
      <Link>MyLib\MyLib.A.t4</Link></None>
    <None Include="..\..\generate\MyLib\MyLib.B.t4">
      <Link>MyLib\MyLib.A.t4</Link></None>
    <None Include="..\..\generate\MyLib\MyLib.C.t4">
      <Link>MyLib\MyLib.A.t4</Link></None>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="..\..\source\MyLib.cs">
      <Link>MyLib\MyLib.cs</Link>
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>MyLib.tt</DependentUpon>
    </Compile>
  </ItemGroup>
</Project>

So my project has a link to a t4 template, and I want that t4 template to generate an output file, outside of the project, which the project links to and compiles.

What I have above works. Set up a project like this, open it up and VS properly links to and nests the LINKED tt file and cs file. Now rebuild. All working. The t4 engine properly rebuilds the file that exists outside of the project directory.

But try again, and BOOM!

After doing a build, VS automatically removes the following line from the .csproj file:

      <LastGenOutput>..\..\source\MyLib.cs</LastGenOutput>

I'm not sure why it does it, and once the line is gone when a rebuild is triggered, instead of the t4 engine changing:

/source/MyLib.cs

It decides that it needs to automatically generates a new output from the tt file and it creates:

/generate/MyLib/MyLib1.cs

Any help would be appreciated.

Cheers

sungiant
  • 3,152
  • 5
  • 32
  • 49
  • You might want to check the T4ToolBox. It does this, and you can see how by looking at the source code. – Federico Berasategui Jan 25 '14 at 15:35
  • 1
    Perhaps not possible due to coding guidelines or personal taste but I see T4 as source code and thus would put the tt file in the /source folder. I personally prefix the generated files so that they are easily identifiable. – Just another metaprogrammer Jan 25 '14 at 20:30
  • Consider using [Runtime T4](http://msdn.microsoft.com/en-us/library/ee844259.aspx) and save yourself a lot of headache with reference and path issues. Granted, it is a different way of using T4. – Phillip Scott Givens Jan 25 '14 at 21:19

1 Answers1

4

Try adding OutputFilePath to your project file:

<None Include="..\..\generate\MyLib\MyLib.tt">
  <Link>MyLib\MyLib.tt</Link>
  <Generator>TextTemplatingFileGenerator</Generator>
  <OutputFilePath>..\..\source\</OutputFilePath>
  <LastGenOutput>..\..\source\MyLib.cs</LastGenOutput>
</None>

See http://msdn.microsoft.com/en-us/library/ee847423.aspx for good details on running T4 in the build process.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Trying any variation were the folder lives in a subfolder of the project folder and the .tt file disappears from the solution explorer – Peter Nimmo Oct 16 '19 at 10:12
  • Also it just doesn't work at all, the file is generated according to the None Include path, the OutputFilePath has no affect, and the moment you run the tool Visual Studio adds a new "Content Link" variation underneath and the file link is identical to the one that we wrote ourselves for "None Include" – Peter Nimmo Oct 16 '19 at 11:22