Thursday, July 11, 2013

Dump MSBuild properties and items for a project

Sometimes when you're reading a .csproj file you encounter properties like $(TargetDir) and wonder what directory does this property evaluate to when the project is compiled. Of course you can use heavy machinery like the MSBuild debugger, but sometimes a simpler tool can suffice. Here's what I wrote in literally 5 minutes:

using System;
using System.IO;
using Microsoft.Build.Evaluation;
 
class MSBuildDumper
{
    static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.WriteLine("Usage: MSBuildDumper <path-to-project.csproj>");
            return;
        }
 
        var projectFilePath = Path.GetFullPath(args[0]);
        var project = new Project(projectFilePath); // add reference to Microsoft.Build.dll to compile
 
        foreach (var property in project.AllEvaluatedProperties)
        {
            Console.WriteLine(" {0}={1}", property.Name, property.EvaluatedValue);
        }
 
        foreach (var item in project.AllEvaluatedItems)
        {
            Console.WriteLine(" {0}: {1}", item.ItemType, item.EvaluatedInclude);
        }
    }
}

Read more: Kirill Osenkov 
QR: Inline image 1