How to programmatically determine the active solution build configuration using MSBuild libraries in Powershell

霸气de小男生 提交于 2021-02-08 11:25:27

问题


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.

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.

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.

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?


回答1:


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

script pseudo-code is as follows:

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


来源:https://stackoverflow.com/questions/41747700/how-to-programmatically-determine-the-active-solution-build-configuration-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!