Get Visual Studio to run a T4 Template on every build

How do I get a T4 template to generate its output on every build? As it is now, it only regenerates it when I make a change to the template.

I have found other questions similar to this:

T4 transformation and build order in Visual Studio (unanswered)

How to get t4 files to build in visual studio? (answers are not detailed enough [while still being plenty complicated] and don't even make total sense)

There has got to be a simpler way to do this!


I used JoelFan's answer to come up w/ this. I like it better because you don't have to remember to modify the pre-build event every time you add a new .tt file to the project.

  • add TextTransform.exe to your %PATH%
  • created a batch file named transform_all.bat (see below)
  • create a pre-build event " transform_all .... "
  • transform_all.bat

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    :: set the working dir (default to current dir)
    set wdir=%cd%
    if not (%1)==() set wdir=%1
    
    :: set the file extension (default to vb)
    set extension=vb
    if not (%2)==() set extension=%2
    
    echo executing transform_all from %wdir%
    :: create a list of all the T4 templates in the working dir
    dir %wdir%*.tt /b /s > t4list.txt
    
    echo the following T4 templates will be transformed:
    type t4list.txt
    
    :: transform all the templates
    for /f %%d in (t4list.txt) do (
    set file_name=%%d
    set file_name=!file_name:~0,-3!.%extension%
    echo:  --^> !file_name!    
    TextTransform.exe -out !file_name! %%d
    )
    
    echo transformation complete
    

    I agree with GarethJ - in VS2010 it is much easier to regenerate tt templates on each build. Oleg Sych's blog describes how to do it. In short:

  • Install Visual Studio SDK
  • Install Visual Studio 2010 Modeling and Visualization SDK
  • Open in text editor project file and add to the end of file but before </Project>
  • That's it. Open your project. On each build all *.tt templates will be reprocessed

    <!-- This line could already present in file. If it is so just skip it  -->
    <Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
    <!-- process *.tt templates on each build  -->
    <PropertyGroup>
        <TransformOnBuild>true</TransformOnBuild>
    </PropertyGroup>
    <Import Project="$(MSBuildExtensionsPath)MicrosoftVisualStudioTextTemplatingv10.0Microsoft.TextTemplating.targets" />
    

    There is a great NuGet package that does just this:

    PM> Install-Package Clarius.TransformOnBuild
    

    Details about the package can be found here

    链接地址: http://www.djcxy.com/p/92428.html

    上一篇: 具有增量构建的MSBuild时间戳问题

    下一篇: 让Visual Studio在每个版本上运行T4模板