5

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?

Community
  • 1
  • 1
Arthis
  • 2,283
  • 21
  • 32

1 Answers1

2

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">
Sergey Rybalkin
  • 3,004
  • 22
  • 26
  • That could work with include but what about assembly? I used $(SolutionDir) to locate them, I have same kind of error If I try the relative path. The given assembly name or codebase was invalid. Thx for your help. – Arthis Oct 09 '12 at 14:34
  • I finally used absolute path because anything else was working properly, and I needed to move on. – Arthis Oct 11 '12 at 11:57
  • I'm not sure what was the problem with the relative path, but you can try `Host.ResolvePath` method – Sergey Rybalkin Oct 11 '12 at 12:44
  • I solved this by defining an environment variable and using that in my path (e.g. `%EnvVar%\xxx\yyy\zzz\mytemplate.tt`). – hypercode Jan 29 '13 at 05:32