14

When I build my c# solution the .tt files will not create the .cs file outputs. But if I right click the .tt files one at a time in solution explorer and select "Run Custom Tool" the .cs is generated, so the build tool setting is correct. What do I do to get the overall solution build to force the custom tool to run on the .tt files?

P a u l
  • 7,805
  • 15
  • 59
  • 92
  • http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx – P a u l Jan 01 '09 at 22:39

4 Answers4

9

Paul, you can also generate code at build time with TextTransform.exe or Elton Stoneman's MSBuild task. Just keep in mind that behavior of the built-in directives, like assembly and include is different when T4 runs in Visual Studio vs. the command-line host.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Oleg Sych
  • 598
  • 3
  • 4
5

Answering my own question, they are supposed to be generated at design time as per this discussion:

https://web.archive.org/web/20081227142303/http://www.olegsych.com/2008/02/t4-template-directive/

01F0
  • 1,228
  • 2
  • 19
  • 32
P a u l
  • 7,805
  • 15
  • 59
  • 92
1

In Visual Studio 2017 (probably next versions too), you should add this in Pre-build event:

"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)YourTemplate.cs" "$(ProjectDir)YourTemplate.tt"

p.s. The only solution that worked for me.

p.s.s. Change path to your template if it's located not in root project directory.

random one
  • 140
  • 1
  • 5
0

In Visual Studio 2013, I was able to get the .tt files to regenerate their targets by just adding these lines to the .csproj file:

<PropertyGroup>
  <!-- Get the Visual Studio version – defaults to 10: -->
  <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
  <!-- Keep the next element all on one line: -->
  <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<!-- To enable build tasks on your development computer, install Modeling SDK for Visual Studio. https://www.microsoft.com/en-us/download/details.aspx?id=40754 -->
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
<!-- Run the Transform task at the start of every build -->
<PropertyGroup>
  <TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<!-- Overwrite files that are read-only, for example because they are not checked out -->
<PropertyGroup>
  <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
</PropertyGroup>
<!-- Transform every template every time -->
<PropertyGroup>
  <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

However, for this to work, you'll need to have installed the Modeling SDK for Visual Studio. I found all of this information, along with a more complete description of the options available, on this page: Code Generation in a Build Process.

Gyromite
  • 769
  • 6
  • 16