在运行 T4 模板时确定解决方案配置(调试/发布) [英] Determine solution configuration (debug/release) when running a T4 template

查看:25
本文介绍了在运行 T4 模板时确定解决方案配置(调试/发布)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 T4 模板,可以根据标志输出优化内容或标准内容.目前我正在根据我的需要手动更改标志.

我想做的是根据 Visual Studio 中解决方案的配置设置标志.如果设置为在调试模式下构建,我将输出标准内容.如果设置为以发布模式构建,我会改为优化内容.我发现了另一个看起来很有希望的 T4 问题:T4 文本模板 - 是否可以从主机获取编译符号?

但是,在我的情况下,我想做以下事情:

<#@ template language="C#" hostspecific="True"compilerOptions="/d:$(ConfigurationName)" #>

因为我可以在汇编指令中使用 $(SolutionDir):

<#@ assembly name="$(SolutionDir)\myreference.dll" #>

我认为/d:$(ConfigurationName) 会带我去我需要去的地方,然后我可以执行以下操作来设置我的标志:

<预><代码><##if 调试优化 = 假;#别的优化 = 真;#万一#>

唉,这似乎行不通.我也尝试使用:

Host.ResolveParameterValue("-", "-", "ConfigurationName");

也无济于事.有什么想法吗?

解决方案

我还没问,却在 这篇 MSDN 文章 让我知道我要去哪里.这里的答案是使用 IServiceProvider 接口来获取 Visual Studio DTE.这是完成它的代码(提前为硬编码的调试"道歉):

 var serviceProvider = Host as IServiceProvider;var dte = serviceProvider.GetService(typeof(DTE)) 作为 DTE;var configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;优化 = (configName != 调试");

更新

此代码将检查活动项目的当前配置是否开启了优化.它仍然有一个硬编码的属性名称,但更改的可能性要小得多.此外,使用项目的优化标志对我的场景很有意义(尝试决定是否应该在我自己的代码中打开优化):

 var serviceProvider = Host as IServiceProvider;var dte = serviceProvider.GetService(typeof(EnvDTE.DTE)) 作为 DTE;config = dte.Solution.FindProjectItem(Host.TemplateFile).包含项目.配置管理器.ActiveConfiguration;foreach(config.Properties 中的属性道具){if (prop.Name == 优化"){优化 = (bool)prop.Value;休息;}}

I have a T4 template that can output either optimized content or standard content based on a flag. Currently I'm manually changing the flag based on my needs.

What I'd love to do is set the flag based on the Configuration of the Solution in Visual Studio. If set to build in Debug mode, I would output standard content. If set to build in Release mode, I would optimize the content instead. I found another T4 question that looks promising: T4 Text Template - Is it possible to get compilation symbols from host?

However, in my case I would want to do something like the following:

<#@ template language="C#" hostspecific="True" 
    compilerOptions="/d:$(ConfigurationName)" #>

Since I can use $(SolutionDir) in an assembly directive:

<#@ assembly name="$(SolutionDir)\myreference.dll" #>

I would think the /d:$(ConfigurationName) would get me where I needed to go, and then I could do the following to set my flag:

<#
#if Debug 
 optimize = false;
#else 
 optimize = true;
#endif 
#>

Alas, this doesn't seem to work. I've also attempted using:

Host.ResolveParameterValue("-", "-", "ConfigurationName");

Also to no avail. Any ideas?

解决方案

No sooner do I ask but I find a snippet at the bottom of this MSDN article that gets me where I need to be. The answer here is to use the IServiceProvider interface to get the Visual Studio DTE. Here's code that is getting it done (apologies in advance for the hard-coded "Debug"):

    var serviceProvider = Host as IServiceProvider;
    var dte = serviceProvider.GetService(typeof(DTE)) as DTE;
    var configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
    optimize = (configName != "Debug"); 

UPDATE

This code will check to see if the active project's current configuration has optimizations turned on. It still has a hard-coded property name, but one that's much less likely to change. Also, using the project's optimization flag makes a lot of sense for my scenario (trying to decide if I should turn on optimizations in my own code):

    var serviceProvider = Host as IServiceProvider;
    var dte = serviceProvider.GetService(typeof(EnvDTE.DTE)) as DTE;
    config = dte.Solution
                .FindProjectItem(Host.TemplateFile)
                .ContainingProject
                .ConfigurationManager
                .ActiveConfiguration;
    foreach(Property prop in config.Properties)
    {
        if (prop.Name == "Optimize")
        {
            optimize = (bool)prop.Value;
            break;
        }
    }

这篇关于在运行 T4 模板时确定解决方案配置(调试/发布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆