0

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?

str8ball
  • 111
  • 1
  • 12
  • Please note that this script will eventually be run in TFS as part of a build verification process. Also wondering if EnvDTE might be another option. – str8ball Jan 19 '17 at 17:27
  • I think volatile settings like platform/config for VS are stored in the solution's .suo files. Those are normally not in source control as they are user/machine specific so I wonder how you're going to deal with that? Anyway, those files are binary and not really related to msbuild since they're used only by VS. EnvDTE will likely be the way to go. – stijn Jan 19 '17 at 19:31
  • 1
    @stign, you make an excellent point. What I'm trying to do will ultimately run via TFS post build, and the build server therefore provides the configuration parms. That's me being dense! Thanks for the head check. – str8ball Jan 19 '17 at 20:09

1 Answers1

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
str8ball
  • 111
  • 1
  • 12