visual-studio 让 Visual Studio 在每次构建时运行 T4 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1646580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:10:26  来源:igfitidea点击:

Get Visual Studio to run a T4 Template on every build

visual-studiotfsmsbuildt4

提问by JoelFan

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.

如何获取 T4 模板以在每次构建时生成其输出?就像现在一样,它只有在我对模板进行更改时才会重新生成它。

I have found other questions similar to this:

我发现了其他类似的问题:

T4 transformation and build order in Visual Studio(unanswered)

Visual Studio 中的 T4 转换和构建顺序(未答复)

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)

如何让t4文件在visual studio中构建?(答案不够详细[虽然仍然很复杂],甚至没有完全的意义)

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

必须有一种更简单的方法来做到这一点!

采纳答案by Seth Reno

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.

我用 JoelFan 的回答提出了这个问题。我更喜欢它,因为每次向项目添加新的 .tt 文件时,您都不必记住修改预构建事件。

  • add TextTransform.exe to your %PATH%
  • created a batch file named transform_all.bat (see below)
  • create a pre-build event "transform_all ..\.."
  • 将 TextTransform.exe 添加到您的 %PATH%
  • 创建了一个名为 transform_all.bat 的批处理文件(见下文)
  • 创建预构建事件“ transform_all ..\..

transform_all.bat

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

回答by Cheburek

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:

我同意 GarethJ - 在 VS2010 中,在每次构建时重新生成 tt 模板要容易得多。Oleg Sych 的博客描述了如何做到这一点。简而言之:

  1. Install Visual Studio SDK
  2. Install Visual Studio 2010 Modeling and Visualization SDK
  3. Open in text editor project file and add to the end of file but before </Project>
  1. 安装Visual Studio SDK
  2. 安装Visual Studio 2010 建模和可视化 SDK
  3. 在文本编辑器项目文件中打开并添加到文件末尾但之前 </Project>

That's it. Open your project. On each build all *.tt templates will be reprocessed

而已。打开您的项目。在每次构建时,所有 *.tt 模板都将被重新处理

<!-- 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)\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets" />

回答by GavKilbride

There is a great NuGet package that does just this:

有一个很棒的 NuGet 包可以做到这一点:

PM> Install-Package Clarius.TransformOnBuild

Details about the package can be found here

可以在此处找到有关该包的详细信息

回答by JoelFan

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

我使用了 MarkGr 的答案并开发了这个解决方案。首先,在主解决方案文件夹上方的单独工具文件夹中创建一个名为RunTemplate.bat的批处理文件。批处理文件只有一行:

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

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

该批处理文件采用 2 个参数... %1是不带 .tt 扩展名的 .tt 文件的路径。 %2是模板中程序集指令引用的任何 DLL 的路径。

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

接下来,进入包含 T4 模板的项目的项目属性。进入构建事件并添加以下预构建事件命令行

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

replacing MyTemplatewith 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

用不带 .tt 扩展名的 .tt 文件(即 MyTemplate.tt)的文件名替换MyTemplate。这将产生在构建项目之前扩展模板以生成 MyTemplate.cs 的结果。然后实际构建将编译 MyTemplate.cs

回答by Mark Melville

Recently found this great VS plugin, Chirpy.

最近发现了这个很棒的 VS 插件Chirpy

Not only does it generate your T4 on a build, but it allows T4-based approach to minification of javascript, CSS, and even lets you use LESS syntax for your CSS!

它不仅在构建时生成您的 T4,而且允许基于 T4 的方法来缩小 javascript、CSS,甚至允许您为 CSS 使用 LESS 语法!

回答by Saul

Probably the simplest way is to install a Visual Studio extension called AutoT4.

可能最简单的方法是安装一个名为AutoT4的 Visual Studio 扩展。

It runs all T4 templates on build automagically.

它在构建时自动运行所有 T4 模板。

回答by Peter Taylor

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.2\TextTransform.exe\" @file"

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

这会转换.tt项目中的所有文件并将它们列出到构建输出中。

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.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.

当然,如果您愿意,您可以将其提取到一个批处理文件中,您可以将项目目录路径传递到该文件中。

NBThe 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 TextTemplatingand TextTransform.exeis different.

注意路径可能需要一些调整。上面的路径是VS 2008在我机器上安装的地方;但是您可能会发现TextTemplating和之间的版本号TextTransform.exe不同。

回答by MarkGr

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.

查看 C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating 那里有一个命令行转换 exe。或者,使用自定义主机编写 MSBuild 任务并自己进行转换。

回答by Alex Essilfie

Expanding on Seth Renoand JoelFan'sanswers, 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.

扩展Seth RenoJoelFan 的答案,我想出了这个。使用此解决方案,您无需记住每次向项目添加新的 .tt 文件时都修改预构建事件。

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 的批处理文件(见下文)
  • transform_all.bat "$(ProjectDir)" $(ProjectExt)使用要构建的 .tt 为每个项目创建预构建事件

transform_all.bat

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.2\
) else (
  echo 64-bit OS detected
  set ttPath=%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating.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. .TTfiles 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.batbatch file.
    For example, I placed it in my solution directory so the pre-build event was as follows "$(SolutionDir)transform_all.bat" "$(ProjectDir)" $(ProjectExt)

  1. 文本转换假定 T4 模板中的代码与您的项目类型使用相同的语言。如果这种情况不适用于您,那么您必须将$(ProjectExt)参数替换为您希望代码生成的文件的扩展名。

  2. .TT文件必须在项目目录中,否则它们不会构建。您可以通过指定不同的路径作为第一个参数(替换"$(ProjectDir)"为包含 TT 文件的路径),在项目目录外构建TT 文件。

  3. 还要记住设置transform_all.bat批处理文件的正确路径。
    比如我把它放在我的解决方案目录中,所以预构建事件如下"$(SolutionDir)transform_all.bat" "$(ProjectDir)" $(ProjectExt)

回答by GarethJ

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

如果您使用的是 Visual Studio 2010,则可以使用 Visual Studio 建模和可视化 SDK:http: //code.msdn.microsoft.com/vsvmsdk

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

这包含用于在构建时执行 T4 模板的 msbuild 任务。

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

查看 Oleg 的博客以获取更多解释:http: //www.olegsych.com/2010/04/understanding-t4-msbuild-integration