Product of build-time T4 transformation is used only in the next build
Asked Answered
F

2

18

I have a VS project that contains:

  1. a pre-build action running TextTransform on a template.tt to generate generated.cs

  2. generated.cs listed as one of the files to compile (i.e. in the list of project files)

When I build the project, the pre-build action is executed, generated.cs is re-created, but VS compiles the previous version of generated.cs (which I guess it loaded in memory when the build process started).

How to make the build use the new version of generated.cs (i.e. the one generated in the pre-build action)? How to force the build order?

Note that the text transformation input is dynamic and hence cannot be done in design time.

Feinstein answered 18/8, 2009 at 11:36 Comment(0)
O
4

I don't think you need a custom pre-build action. Just add the ".tt" file to your project and set its "Custom Tool" property to "TextTemplatingFileGenerator". You might want to make sure that the *.generated.cs files are also added to the project, but I think that VS takes care of that.

Otes answered 5/1, 2010 at 17:34 Comment(2)
This doesn't seem to work for me. I think Zvika was saying they wanted the T4 template to work its magic whenever his project is built. This is what I want also. Though, I'm using T4MVC with ASP.NET MVC 3. For example, if I take a project that builds, add a new file that should get picked up by T4MVC, then build the project again, I don't see the change that should have been done by T4MVC. I have to explicitly trigger an update by right-clicking on the .tt file and choosing Run Custom Tool.Counterpunch
this will only generate the output when you Save the .tt file.Fingernail
C
4

There's now a solution to this problem! Oleg Sych has a post on his blog detailing how to make transform-at-build-time work.

Here's the source: https://web.archive.org/web/20140116193428/http://www.olegsych.com/2010/04/understanding-t4-msbuild-integration/

Basically, you just include the T4 build targets in your project file and set the TransformOnBuild property to true.

Here's the relevant excerpt:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
  </PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets" />

Note that the Microsoft.TextTemplating.targets file has to be included AFTER the Microsoft.CSharp.targets.

Castiron answered 17/2, 2014 at 3:41 Comment(1)
This generates the files but omit their relative namespace. how can i overcome this ?Fingernail

© 2022 - 2024 — McMap. All rights reserved.