我正在尝试将一些代码生成模板连接到我团队的自动构建过程中。我们的 SCM 团队不希望在我们的构建机器上使用 Visual Studio(我很难与之争论)。
有没有办法在没有 Visual Studio 的情况下安装 T4 引擎?
I wrote a cleanly reverse-engineered implementation of a T4 engine for the MonoDevelop IDE. It's open-source, licensed under the permissive MIT/X11 license, so you are free to embed the engine in your app or redistribute it. There's also an implementation of the TextTransform.exe command-line tool available as a dotnet global tool, and some APIs in the Mono.TextTemplating namespace to aid in hosting the engine.
You can get packages from NuGet and the source is on GitHub.
我们实际上并没有生成代码......我们正在生成 Sandcastle 脚本,我们绝对希望在构建服务器上完成。我们采取了一种方法,我们创建了一系列自定义属性,我们用这些属性来装饰我们的代码,这些属性塑造了文档的生成方式。所以我们有一组 .tt 文件来反映这些属性的程序集,并生成 Sandcastle 的输入文件 (.shfb)。我本可以使用自定义工具来完成,但 T4 非常适合。
无论如何...我已经确认您可以在没有 Visual Studio 的情况下运行 TextTransform.exe。您所需要的只是存在的 Microsoft.VisualStudio.TextTemplating.dll。我创建了一个全新的 VM,安装了 .NET 3.5,并将以下内容复制到文件系统:
Test.tt 看起来像这样:
<#@ template language="C#3.5" debug="true" hostspecific="true" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections"#>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Xml" #>
<#@ output extension=".cs" #>
测试:<#=System.DateTime.Now.ToString()#>....
从我执行 Test.tt 像这样的VM:
C:\TextTransform.exe 测试.tt
和 Test.cs 创建包含
测试:2009 年 6 月 10 日下午 5:33:32....
胜利!!!!
最难的部分是找到 Microsoft.VisualStudio.TextTemplating.dll。我不得不启动 FileMon 并在我的带有 Visual Studio 的盒子上执行一个模板。FileMon 然后告诉我 TextTransform.exe 从哪里加载它。这可能存在于几个地方,但我在 GAC 中的 C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating\9.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.dll 中找到了它。为了使用 Windows 资源管理器浏览到该位置,我必须像这样创建一个 subst:
subst X: C:\Windows\程序集
然后我可以浏览到 X:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating\9.0.0.0__b03f5f7f11d50a3a\ 并复制 dll。
无论如何......谢谢你的想法。希望有一天这可以帮助其他一些可怜的灵魂:)
所以这是我最终采取的方法:
我有一台带有 Visual Studio 2010 sp1 的开发机器和一台构建机器(使用 msbuild)。
干草你应该很高兴。
进一步阅读:
我还没有尝试过,但我相信您可以使用命令行工具,将其放在您的构建服务器上,并在构建之前/期间转换您的模板。
我的理解是您仍然需要安装 Visual Studio。为什么要逆流而上?您是否考虑过在设计时而不是在构建时生成代码?
12 只需要复制
C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\12.0