如何在 MSBuild 中编译 TypeLite T4 模板
How to compile TypeLite T4 template in MSBuild
我想在构建期间自动使用 TypeLite 为我的所有 C# DTO 创建 .d.ts
文件。我的 Contract.tt 是安装 TypeLite Nuget 包的原始文件略微缩小:
<#@ template debug="true" hostspecific="True" language="C#" #>
<#@ assembly name="TypeLite.dll" #>
<#@ assembly name="TypeLite.Net4.dll" #>
<#@ assembly name="My.Contract.dll" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".d.ts"#>
<#@include file="Manager.ttinclude"#>
<#
var manager = Manager.Create(Host, GenerationEnvironment);
var ts = TypeScript.Definitions()
.WithReference("Enums.ts")
.ForLoadedAssemblies();
#>
<#= ts.Generate(TsGeneratorOutput.Properties) #>
<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>
在 MS Guide and this article 之后,我已将以下代码添加到我的 MSbuild 程序中:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<ItemGroup>
<T4ReferencePath Include="$(SolutionDir)\My.Contract\bin$(Configuration)">
<InProject>False</InProject>
</T4ReferencePath>
<None Include="$(SolutionDir)\Scripts\Contract.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<OutputFilePath>$(SolutionDir)\Scripts</OutputFilePath>
</None>
</ItemGroup>
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
但不幸的是我收到了这个错误:
Error 14 Running transformation: System.ArgumentNullException: Value cannot be null.
Parameter name: Could not obtain DTE from host
at Microsoft.VisualStudio.TextTemplating<cut>.GeneratedTextTransformation.Manager.VSManager..ctor
看起来 T4 模板预计只能在 VisualStudio 中执行。有没有办法 运行 使用 MSBuild 进行 TypeLite 编译?
问题出在Manager.ttinclude, which is responsible for splitting T4 output in multiple files and automatically adding them to the project. This requires DTE which is not part of MSBuild target in Modeling SDK for Visual Studio.
所以我刚刚删除了 manager
并创建了 2 个单独的模板。而不是 Contract.tt
我得到了:
Enums.tt:
<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="TypeLite.dll" #>
<#@ assembly name="TypeLite.Net4.dll" #>
<#@ assembly name="CC.Business.Contract.dll" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".ts"#>
<#
var ts = TypeScript.Definitions()
.ForLoadedAssemblies();
#>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
和Models.tts:
<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="TypeLite.dll" #>
<#@ assembly name="TypeLite.Net4.dll" #>
<#@ assembly name="CC.Business.Contract.dll" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".d.ts"#>
<#
var ts = TypeScript.Definitions()
.WithReference("Enums.ts")
.ForLoadedAssemblies();
#>
<#= ts.Generate(TsGeneratorOutput.Properties) #>
我想在构建期间自动使用 TypeLite 为我的所有 C# DTO 创建 .d.ts
文件。我的 Contract.tt 是安装 TypeLite Nuget 包的原始文件略微缩小:
<#@ template debug="true" hostspecific="True" language="C#" #>
<#@ assembly name="TypeLite.dll" #>
<#@ assembly name="TypeLite.Net4.dll" #>
<#@ assembly name="My.Contract.dll" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".d.ts"#>
<#@include file="Manager.ttinclude"#>
<#
var manager = Manager.Create(Host, GenerationEnvironment);
var ts = TypeScript.Definitions()
.WithReference("Enums.ts")
.ForLoadedAssemblies();
#>
<#= ts.Generate(TsGeneratorOutput.Properties) #>
<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>
在 MS Guide and this article 之后,我已将以下代码添加到我的 MSbuild 程序中:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<ItemGroup>
<T4ReferencePath Include="$(SolutionDir)\My.Contract\bin$(Configuration)">
<InProject>False</InProject>
</T4ReferencePath>
<None Include="$(SolutionDir)\Scripts\Contract.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<OutputFilePath>$(SolutionDir)\Scripts</OutputFilePath>
</None>
</ItemGroup>
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
但不幸的是我收到了这个错误:
Error 14 Running transformation: System.ArgumentNullException: Value cannot be null.
Parameter name: Could not obtain DTE from host
at Microsoft.VisualStudio.TextTemplating<cut>.GeneratedTextTransformation.Manager.VSManager..ctor
看起来 T4 模板预计只能在 VisualStudio 中执行。有没有办法 运行 使用 MSBuild 进行 TypeLite 编译?
问题出在Manager.ttinclude, which is responsible for splitting T4 output in multiple files and automatically adding them to the project. This requires DTE which is not part of MSBuild target in Modeling SDK for Visual Studio.
所以我刚刚删除了 manager
并创建了 2 个单独的模板。而不是 Contract.tt
我得到了:
Enums.tt:
<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="TypeLite.dll" #>
<#@ assembly name="TypeLite.Net4.dll" #>
<#@ assembly name="CC.Business.Contract.dll" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".ts"#>
<#
var ts = TypeScript.Definitions()
.ForLoadedAssemblies();
#>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
和Models.tts:
<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="TypeLite.dll" #>
<#@ assembly name="TypeLite.Net4.dll" #>
<#@ assembly name="CC.Business.Contract.dll" #>
<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".d.ts"#>
<#
var ts = TypeScript.Definitions()
.WithReference("Enums.ts")
.ForLoadedAssemblies();
#>
<#= ts.Generate(TsGeneratorOutput.Properties) #>