T4 code generation during build, custom assembly reference

9

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?

.net
msbuild
t4
asked on Stack Overflow Oct 30, 2012 by Gavin Stevens • edited Oct 31, 2012 by Just another metaprogrammer

1 Answer

4

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

answered on Stack Overflow Jun 12, 2013 by bc3tech

User contributions licensed under CC BY-SA 3.0