Anthony Anthony - 1 year ago 113
C# Question

Extract branch name in MSBuild prebuild step

My ultimate goal here is to extract the branch name (which is part of $SolutionDirectory) in an MSBuild pre-build event and then inject it into my app.config appSettings.

I've been doing lots of digging and haven't really found an approach to this problem.

I would like to be able to do something like this during pre-build:

<XmlPoke XmlInputPath="App.config"
Value="$(Branch)" />

Has anyone done something like this before?

Answer Source

Just want to thank everyone for your help. While the answers didn't quite get me what I wanted, it pushed me in the right direction for my needs. Here is the solution that I ended up going with:

<Target Name="AfterBuild">
  <XmlPoke Condition=" '$(TeamBuildOutDir)' == '' " XmlInputPath="$(ProjectDir)bin\$(ConfigurationName)\$(TargetName).dll.config" Query="/configuration/appSettings/add[@key='BuildInfo']/@value" Value="$(SolutionDir)" />
  <XmlPoke Condition=" '$(TeamBuildOutDir)' != '' " XmlInputPath="$(TeamBuildOutDir)\$(SolutionName)\$(ProjectName)\$(TargetName).dll.config" Query="/configuration/appSettings/add[@key='BuildInfo']/@value" Value="$(SolutionDir)" />

It turns out we were actually doing some funky stuff on our build machines that was structuring the directories much differently than locally. We were making use of the $(TeamBuildOutDir) build property to dump all our binaries to. After obtaining that knowledge, I was able to create the above solution for our needs. This ends up creating a fairly consistent string that I can append to my BuildInfo property and process later on.

Hopefully this helps someone else!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download