题
当我构建我的c#解决方案时,.tt文件将不会创建.cs文件输出。但是,如果我在解决方案资源管理器中一次单击一个.tt文件并选择“运行自定义工具”,生成.cs,因此构建工具设置正确。如何才能使整个解决方案构建强制自定义工具在.tt文件上运行?
解决方案
Paul,你也可以在构建时使用 TextTransform.exe 或 Elton Stoneman's MSBuild任务。请记住内置指令的行为,例如汇编当Visual Studio中的T4运行时, include 不同命令行主机。
其他提示
回答我自己的问题,它们应该按照这个讨论在设计时生成:
https://web.archive.org/web/20081227142303/http://www.olegsych.com/2008/02/t4-template-directive/
在Visual Studio 2013中,我可以通过将这些行添加到.csproj文件来获取.tt文件来重新生成目标:
<PropertyGroup>
<!-- Get the Visual Studio version – defaults to 10: -->
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<!-- Keep the next element all on one line: -->
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<!-- To enable build tasks on your development computer, install Modeling SDK for Visual Studio. https://www.microsoft.com/en-us/download/details.aspx?id=40754 -->
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
<!-- Run the Transform task at the start of every build -->
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<!-- Overwrite files that are read-only, for example because they are not checked out -->
<PropertyGroup>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
</PropertyGroup>
<!-- Transform every template every time -->
<PropertyGroup>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
但是,为此,您需要安装建模适用于Visual Studio的SDK 。我在此页面上找到了所有这些信息,以及对可用选项的更完整描述:构建过程中的代码生成。
在Visual Studio 2017(可能也是下一个版本)中,您应该在预构建事件中添加它:
"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)YourTemplate.cs" "$(ProjectDir)YourTemplate.tt"
P.S。唯一对我有用的解决方案。
p.s.s。如果模板不在根项目目录中,请更改模板的路径。
不隶属于 StackOverflow