Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 code generation during build, custom assembly reference

Tags:

.net

msbuild

t4

I have a T4 template that processes several .tt files in my project. I also have some custom classes I've defined to help with the code transformation process.

<#@ template language="C#"  hostspecific="True" debug="True" #>
<#@ output extension="cs" #>
<#@ assembly name="System.Core.dll" #>
<#@ assembly name="$(TargetDir)\MyDependency.dll" #> 

This works in Visual Studio, I have a VS Macro which defines $(TargetDir) correctly.

Now, I want to perform the code generation process during my build process. I added:

<TransformOnBuild>true</TransformOnBuild>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
<IncludeDslT4Settings>true</IncludeDslT4Settings> <ItemGroup>
<T4ReferencePath Include="$(OutputPath)" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets" /> 

My build runs, but I get:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\TextTemplating\v10.0\Microsoft.TextTemplating.targets (407): The host threw an exception while trying to resolve the assembly reference '$(TargetDir)\MyDependency.dll'. The transformation will not be run. The following Exception was thrown: System.IO.FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) at System.Reflection.AssemblyName.nInit() at Microsoft.VisualStudio.TextTemplating.GlobalAssemblyCacheHelper.GetLocation(String strongName) at Microsoft.VisualStudio.TextTemplating.Sdk.Host.GenericTextTemplatingHost.ResolveAssemblyReference(String assemblyReference) at Microsoft.VisualStudio.TextTemplating.Engine.ResolveAssemblyReferences(ITextTemplatingEngineHost host, TemplateProcessingSession session). Line=-1, Column=-1

Obviously it won't resolve $(TargetDir) in Team build context.

I tried adding TargetDir to my build configuration's property group, to no avail. This value doens't pass through to the context of the T4 Code Generator.

I don't want to use an environment variable.

How can I set $(TargetDir) correctly in Team Build context?

like image 489
Gavin Stevens Avatar asked Oct 30 '12 18:10

Gavin Stevens


People also ask

What is T4 code generation?

In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic.

What is T4 template in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.


1 Answers

I don't see where $(OutputPath) is defined by visual studio. did you try $(TargetDir) in your item w/in your project? Then you can remove the $(TargetDir)\ from your Assembly declaration in the T4 file.

I just worked through a slew of possibilities to get something similar to work, will be doing a blog post today or tomorrow.

So:

<ItemGroup>
    <T4ReferencePath Include="$(TargetDir)" />
</ItemGroup>

in your csproj, and

<#@ assembly name="MyDependency.dll" #>

in your T4 file

like image 132
bc3tech Avatar answered Sep 20 '22 15:09

bc3tech