Get Visual Studio to run a T4 Template on every build

后端 未结 22 2456
余生分开走
余生分开走 2020-11-22 08:29

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 ques

22条回答
  •  遇见更好的自我
    2020-11-22 09:08

    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
    

提交回复
热议问题