Question

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

No correct solution

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top