如何使用Powershell中的MSBuild库以编程方式确定活动的解决方案构建配置

时间:2017-01-19 17:08:40

标签: c# visual-studio-2015 msbuild

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

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

下一个愿望是只关注那些属于解决方案Active Build / Platform配置的项目 - 那些在Visual Studio Configuration Manager中检查了Build位的项目。

虽然我能够收集项目构建配置属性列表,但我很难找到解决方案文件如何存储Active Solution Build和Active Platform配置所选值。

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

任何人都可以了解我应该如何确定解决方案或项目的Active Build / Platform配置?

1 个答案:

答案 0 :(得分:1)

来自@stign的反馈使我的思想朝着正确的方向前进。有点冗长,但它做了我需要的事情

脚本伪代码如下:

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