T4 模板的自定义输出文件路径 [英] Custom Output File Path For T4 Template

查看:22
本文介绍了T4 模板的自定义输出文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现使用具有非标准目录结构的 t4 模板真的很困难.我在我的 csproj 文件中使用了链接,这似乎是问题的根源.

I am finding it really difficult to use t4 templates with a non-standard directory structure. I am using links inside my csproj file, which seems to be at the root of the problem.

我可以正常工作,但是,VS 会自动做出破坏性的更改.

I have it working, however, VS automatically makes a change which breaks things.

我有以下目录结构:

/source
  + MyLib.cs 
/generate
    /MyLib
      + MyLib.tt
      + MyLib.A.t4 // included by MyLib.tt
      + MyLib.B.t4 // included by MyLib.tt
  + MyLib.C.t4 // included by MyLib.tt
/build_examples
   /vs
     + MyLib.csproj
     + MyLib.sln

MyLib.csproj 看起来像这样:

MyLib.csproj looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
  </ItemGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <ItemGroup>
    <None Include="..\..\generate\MyLib\MyLib.tt">
      <Link>MyLib\MyLib.tt</Link>
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>..\..\source\MyLib.cs</LastGenOutput>
    </None>
    <None Include="..\..\generate\MyLib\MyLib.A.t4">
      <Link>MyLib\MyLib.A.t4</Link></None>
    <None Include="..\..\generate\MyLib\MyLib.B.t4">
      <Link>MyLib\MyLib.A.t4</Link></None>
    <None Include="..\..\generate\MyLib\MyLib.C.t4">
      <Link>MyLib\MyLib.A.t4</Link></None>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="..\..\source\MyLib.cs">
      <Link>MyLib\MyLib.cs</Link>
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>MyLib.tt</DependentUpon>
    </Compile>
  </ItemGroup>
</Project>

所以我的项目有一个指向 t4 模板的链接,我希望该 t4 模板在项目之外生成一个输出文件,项目链接到该文件并进行编译.

So my project has a link to a t4 template, and I want that t4 template to generate an output file, outside of the project, which the project links to and compiles.

我上面的东西有效.像这样设置一个项目,打开它,VS 正确链接和嵌套 LINKED tt 文件和 cs 文件.现在重建.所有的工作.t4 引擎正确重建存在于项目目录之外的文件.

What I have above works. Set up a project like this, open it up and VS properly links to and nests the LINKED tt file and cs file. Now rebuild. All working. The t4 engine properly rebuilds the file that exists outside of the project directory.

但是再试一次,BOOM!

But try again, and BOOM!

进行构建后,VS 会自动从 .csproj 文件中删除以下行:

After doing a build, VS automatically removes the following line from the .csproj file:

      <LastGenOutput>..\..\source\MyLib.cs</LastGenOutput>

我不知道为什么要这样做,一旦触发重建时线路消失,而不是更改 t4 引擎:

I'm not sure why it does it, and once the line is gone when a rebuild is triggered, instead of the t4 engine changing:

/source/MyLib.cs

它决定它需要从 tt 文件自动生成一个新的输出并创建:

It decides that it needs to automatically generates a new output from the tt file and it creates:

/generate/MyLib/MyLib1.cs

任何帮助将不胜感激.

干杯

推荐答案

尝试将 OutputFilePath 添加到您的项目文件中:

Try adding OutputFilePath to your project file:

<None Include="..\..\generate\MyLib\MyLib.tt">
  <Link>MyLib\MyLib.tt</Link>
  <Generator>TextTemplatingFileGenerator</Generator>
  <OutputFilePath>..\..\source\</OutputFilePath>
  <LastGenOutput>..\..\source\MyLib.cs</LastGenOutput>
</None>

请参阅 http://msdn.microsoft.com/en-us/library/ee847423.aspx 有关在构建过程中运行 T4 的详细信息.

See http://msdn.microsoft.com/en-us/library/ee847423.aspx for good details on running T4 in the build process.

这篇关于T4 模板的自定义输出文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆