Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating T4 on Build using VS 2012

Tags:

c#

.net

t4

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?

like image 609
Arthis Avatar asked Nov 03 '22 14:11

Arthis


1 Answers

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">
like image 87
Sergey Rybalkin Avatar answered Nov 08 '22 17:11

Sergey Rybalkin