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:59

If you're using Visual Studio 2010, you can use the Visual Studio Modeling and Visualization SDK: http://code.msdn.microsoft.com/vsvmsdk

This contains msbuild tasks for executing T4 templates at build time.

Have a look at Oleg's blog for more explanation: http://www.olegsych.com/2010/04/understanding-t4-msbuild-integration

查看更多
冷夜・残月
3楼-- · 2018-12-31 11:00

Another good article on this: Code Generation in a Build Process

2012 Modeling and Visualization SDK download link :

https://www.microsoft.com/en-us/download/details.aspx?id=30680

查看更多
公子世无双
4楼-- · 2018-12-31 11:00

Hey, my script can also parse output extension

for /r %1 %%f in (*.tt) do (
 for /f "tokens=3,4 delims==, " %%a in (%%f) do (
  if %%~a==extension "%CommonProgramFiles%\Microsoft Shared\TextTemplating\1.2\texttransform.exe" -out %%~pnf.%%~b -P %%~pf -P "%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.5" %%f
 )
)
echo Exit Code = %ERRORLEVEL%

Just create transform_all.bat $(SolutionDir) pre-build event, and all *.tt files in your solution will be transformed automaticaly.

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 11:03

You just need to add this command to the pre-build event of the project:

if $(ConfigurationName) == Debug $(MSBuildToolsPath)\Msbuild.exe  /p:CustomBeforeMicrosoftCSharpTargets="$(ProgramFiles)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Microsoft.TextTemplating.targets"  $(ProjectPath) /t:TransformAll 

The check on configuration = debug, makes sure that you don't regenerate the code in the release mode, when you do the build on the TFS build server for instance.

查看更多
无色无味的生活
6楼-- · 2018-12-31 11:03

In visual studio 2013, right click the T4 template and set the transform on build property to true.

查看更多
查无此人
7楼-- · 2018-12-31 11:03

I used MarkGr's answer and developed this solution. First, create a batch file called RunTemplate.bat in a separate tools folder above the main solution folder. The batch file just has the line:

"%CommonProgramFiles%\Microsoft Shared\TextTemplating\1.2\texttransform.exe" -out %1.cs -P %2 -P "%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.5" %1.tt

This batch file takes 2 parameters... %1 is the path to the .tt file without the .tt extension. %2 is the path to any DLLs referred to by Assembly directives in the template.

Next, go into the Project Properties of the project containing the T4 template. Go into Build Events and add the following Pre-build event command line:

$(SolutionDir)..\..\tools\RunTemplate.bat $(ProjectDir)MyTemplate $(OutDir)

replacing MyTemplate with filename of your .tt file (i.e. MyTemplate.tt) without the .tt extension. This will have the result of expanding the template to produce MyTemplate.cs before building the project. Then the actual build will compile MyTemplate.cs

查看更多
登录 后发表回答