Get Visual Studio to run a T4 Template on every bu

2018-12-31 10: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 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!

19条回答
几人难应
2楼-- · 2018-12-31 10:49

Check out C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating there is a command line transformation exe in there. Alternatively write a MSBuild task with a custom host and do the transform yourself.

查看更多
萌妹纸的霸气范
3楼-- · 2018-12-31 10:50

There is a great NuGet package that does just this:

PM> Install-Package Clarius.TransformOnBuild

Details about the package can be found here

查看更多
与风俱净
4楼-- · 2018-12-31 10:51

The pre-build can be reduced to a single line:

forfiles /p "$(ProjectDir)." /m "*.tt" /s /c "cmd /c echo Transforming @path && \"%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe\" @file"

This transforms all .tt files in the project and lists them to the build output.

If you don't want the build output then you have to work around some "interesting behaviour":

forfiles /p "$(ProjectDir)." /m "*.tt" /s /c "cmd /c @\"%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe\" @file"

Of course, you can pull this out into a batch file to which you pass the project directory path if you wish.

NB The path may require some tweaking. The path above is where VS 2008 installed it on my machine; but you might find that the version number between TextTemplating and TextTransform.exe is different.

查看更多
若你有天会懂
5楼-- · 2018-12-31 10:53

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
查看更多
一个人的天荒地老
6楼-- · 2018-12-31 10:58

Please see mhutch's answer https://stackoverflow.com/a/1395377/9587

IMHO, this is the best build server and dev environment friendly option.

查看更多
皆成旧梦
7楼-- · 2018-12-31 10:58

Expanding on Seth Reno and JoelFan's answers, I came up with this. With this solution don't need to remember to modify the pre-build event every time you add a new .tt file to the project.

Implementation Procedure

  • Create a batch file named transform_all.bat (see below)
  • Create a pre-build event transform_all.bat "$(ProjectDir)" $(ProjectExt) for each project with a .tt you want to build

transform_all.bat

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:: set the correct path to the the app
if not defined ProgramFiles(x86). (
  echo 32-bit OS detected
  set ttPath=%CommonProgramFiles%\Microsoft Shared\TextTemplating\1.2\
) else (
  echo 64-bit OS detected
  set ttPath=%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\
)

:: set the working dir (default to current dir)
if not (%1)==() pushd %~dp1

:: set the file extension (default to vb)
set ext=%2
if /i %ext:~1%==vbproj (
  set ext=vb
) else if /i %ext:~1%==csproj (
  set ext=cs
) else if /i [%ext%]==[] (
  set ext=vb
)

:: create a list of all the T4 templates in the working dir
echo Running TextTransform from %cd%
dir *.tt /b /s | findstr /vi obj > t4list.txt

:: transform all the templates
set blank=.
for /f "delims=" %%d in (t4list.txt) do (
  set file_name=%%d
  set file_name=!file_name:~0,-3!.%ext%
  echo:  \--^> !!file_name:%cd%=%blank%!
  "%ttPath%TextTransform.exe" -out "!file_name!" "%%d"
)

:: delete T4 list and return to previous directory
del t4list.txt
popd

echo T4 transformation complete


NOTES

  1. The text transformation assumes the code in the T4 template is the same language as your project type. If this case does not apply to you, then you will have to replace the $(ProjectExt) argument with the extension of the files you want the code generate.

  2. .TT files must be in the project directory else they won't build. You can build TT files outside the project directory by specifying a different path as the first argument (i.e. replace "$(ProjectDir)" with the path containing the TT files.)

  3. Remember also to set the correct path to the transform_all.bat batch file.
    For example, I placed it in my solution directory so the pre-build event was as follows "$(SolutionDir)transform_all.bat" "$(ProjectDir)" $(ProjectExt)

查看更多
登录 后发表回答