Generating T4 on Build using VS 2012

自作多情 提交于 2019-12-10 14:38:34

问题


I am trying to generate code on every build of my project using VS2012.

I have 3 projects in my solution :

  • project 1 has some classes
  • project 2 has the generic template
  • project 3 has the template that read a json file and then call the generic template form project 2 to generate its file.

When I am clicking on Build/Transform All T4 Templates, there is no problem, the generation goes well.

But I am trying to configure my build to include this step automatically on every build.

I have added this code to my csproj :

<Import Project="$MsBuildToolsPath)\Microsoft.CSharp.Targets" />
<PropertyGroup>
   <TransformOnBuild>true</TransformOnBuild>
   <OverWriteReadOnlyOutputFiles>true</OverWriteReadOnlyOutputFiles>
</PropertyGroup>
<Import Project="$(MSBuildExtensionPath32)\Microsoft\VisualStudio\v11.0\TextTemplating\Microsoft.TextTemplating.targets"/>

I have made up myself the path "\Microsoft\VisualStudio\v11.0\TextTemplating\Microsoft.TextTemplating.targets" from what I found on my pc. The example I took it from was :get-visual-studio-to-run-a-t4-template-on-every-build

The problem comes from this line I am using : <#@ include file="$(SolutionDir)\xxx\yyy\zzz\mytemplate.tt">

and I receive the error :

Failed to resolve include text for file : D:\Projects\pppp\qqq\eeee\$(SolutionDir)\xxx\yyy\zzz\mytemplate.tt

As the template works well when it is generated "by hand" (Build/Transform All T4 Templates), I wonder what might be the problem for generating it at build time.

Any idea?


回答1:


The problem is that when you are running your template during the build process it is being executed under different host and $(SolutionDir) macro does not exist. Try using relative path instead e.g.

<#@ include file="..\xxx\yyy\zzz\mytemplate.tt">


来源:https://stackoverflow.com/questions/12801927/generating-t4-on-build-using-vs-2012

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!