Determine solution configuration (debug/release) when running a T4 template to include assembly in the solution

StackOverflow https://stackoverflow.com/questions/21342687

Question

I'm trying to get my T4 template to reference my assembly in my solution in a practical way so that when I change configurations between debug and release, it will include the correct assembly from my solution.

This is what I have tried:

<#
    // Figured out how to get the current Configuration
    var serviceProvider = Host as IServiceProvider;
    var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
    string ConfigName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;

    // I have verified that I am getting the strings "Debug", and "Release"
    if (configName == "Debug") { 
#>
    <#@ include file="template.Debug.tt" #>
<# } else { #>
    <#@ include file="template.Release.tt" #>
<# } #>

The template.Debug.tt file looks like this:

<#@ Assembly 
name="$(SolutionDir)TestProject.Core/bin/Debug/TestProject.Core.dll"#>

And the template.Release.tt file looks like this:

<#@ Assembly 
name="$(SolutionDir)TestProject.Core/bin/Release/TestProject.Core.dll"#>

I am getting this error when I try to run the main T4:

Error 1 Compiling transformation: An assembly with the same simple name 'TestProject.Core', Version=1.0.0.0, Culture=neutral, PublicKeyToken=null has already been imported. Try removing one of the references or sign them to enable side-by-side.

I'm guessing the preprocessor handles the assembly include line because it is happening along both execution paths which results in both assemblies being referenced.

Tried this too:

<#@ Assembly 
name="$(SolutionDir)TestProject.Core/bin/$(Configuration)/TestProject.Core.dll"#>

Is there a way to add $(Configuration)? That seems like the most logical choice

Everyone else just seems to be hard coding Debug in their paths.

Referencing the wrong DLL and not knowing it is very annoying when debugging and it doesn't help things go smoothly when publishing your code to production.

Was it helpful?

Solution

One idea is that your T4 support dll can be considered to be part of build environment and thus Debug/Relase doesn't apply.

For the support dlls change build path for all configs to goto for instance $(SolutionDir)\BuildArtifacts and reference that location in your T4 file.

Hope this helps

PS. What I do (although I know I am in minority) is avoiding T4 support dlls for partly these reasons and just write everything as T4 "scripts".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top