如何使用 Powershell 中的 MSBuild 库以编程方式确定活动解决方案构建配置 [英] How to programmatically determine the active solution build configuration using MSBuild libraries in Powershell

查看:59
本文介绍了如何使用 Powershell 中的 MSBuild 库以编程方式确定活动解决方案构建配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Powershell 脚本,该脚本从特定 C# 解决方案中的所有 exe/dll 项目中检索自定义元数据.

I'm in the process of writing a Powershell script that retrieves custom metadata from all the exe/dll projects in a particular C# solution.

我的工作集中在使用 Microsoft.MSBuild 程序集(构造和评估命名空间)来解析解决方案文件并迭代项目以检索元数据.这一切都很好.

My efforts have centered around using the Microsoft.MSBuild assembly (Construction and Evaluation namespaces) to parse the solution file and iterate over the projects to retrieve the metadata. This all works well.

下一个愿望是只关注那些属于解决方案 Active Build/Platform 配置一部分的项目——那些在 Visual Studio 配置管理器中检查了 Build 位的项目.

The next desire is to only concentrate on those projects that are part of the solution Active Build/Platform configuration - those with the Build bit checked in Visual Studio Configuration manager.

虽然我能够收集项目构建配置属性列表,但我正在努力寻找解决方案文件如何存储活动解决方案构建和活动平台配置选择的值.

While I am able to collect the list of project build configuration properties, I am struggling to find the how the solution file stores the Active Solution Build and Active Platform configuration selected values.

此外,我还查看了 MSBuild 库的源代码,它似乎在使用字符串的项目和解决方案配置之间使用表达式匹配,例如:.Debug - Build|Any CPU.ActiveCfg = Debug|Any中央处理器.

In addition, I have also reviewed the source code for the MSBuild library and it appears to use an expression match between the project and solution configurations using the string for example: .Debug - Build|Any CPU.ActiveCfg = Debug|Any CPU.

任何人都可以说明我应该如何确定解决方案或项目的活动构建/平台配置吗?

Can anyone shed any light as to how I should determine the Active Build/Platform configuration for the solution or projects?

推荐答案

@stign 的反馈让我的思绪朝着正确的方向发展.有点冗长,但它可以满足我的需求

Feedback from @stign got my mind moving in the right direction. A bit verbose, but it does what I need

脚本伪代码如下:

Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\MSBuild\v14.0\Microsoft.Build.dll"

$slnFile = [Microsoft.Build.Construction.SolutionFile]::Parse("<path_to_solution>")

$slnFile.ProjectsInOrder | Where-Object { $_.ProjectType -eq "KnownToBeMSBuildFormat" } | % {

$outValue = $null
$found = $_.ProjectConfigurations.TryGetValue("Debug|Any CPU", [ref]$outValue)

    if($found)
    {
        if($outValue.IncludeInBuild) # This bit is set by the MS code when parsing the project configurations with ...Build.0
        {
          #Do stuff here
        }
    }
 } #End collection iterate

这篇关于如何使用 Powershell 中的 MSBuild 库以编程方式确定活动解决方案构建配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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