【问题标题】:How to compile TypeLite T4 template in MSBuild如何在 MSBuild 中编译 TypeLite T4 模板
【发布时间】:2015-03-11 17:12:19
【问题描述】:

我想在构建期间为我的所有 C# DTO 创建.d.ts 文件,自动使用 TypeLite。我的 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 Guidethis 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 编译?

【问题讨论】:

  • 在构建中运行 T4 模板的唯一方法是安装 VS SDK,然后安装 Modeling SDK,不是吗?我错过了什么吗?
  • 我收到此错误“编译转换:找不到元数据文件'$(TargetDir)TypeLite.dll'。行=0,列=0”。有任何想法吗?我相信我可能将 T4ReferencePath 设置错了?

标签: msbuild typescript code-generation t4 typelite


【解决方案1】:

问题出在Manager.ttinclude,它负责将T4输出拆分成多个文件并自动添加到项目中。这需要不属于 Modeling SDK for Visual Studio 中的 MSBuild 目标的 DTE。

所以我刚刚删除了 manager 并创建了 2 个单独的模板。而不是Contract.tt 我得到了:

枚举.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) #>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多