1

All I want to do is assign a project property with a value parsed from an external file when building from Visual Studio 2010. Specifically, I want to populate the HelpFileVersion in a Sandcastle Help File Builder project (see my thread here for specifics).

When building from VS, properties are only evaluated when the project is loaded, thus I thought I could just re-evaluate this property at build time in an MSBuild task, like this (in my shfbproj):

<Target Name="BeforeBuildHelp">
  <PropertyGroup>
    <HelpFileVersion>{@Major}.{@Minor}.{@Build}.{@Revision}</HelpFileVersion>
    <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\..\SolutionInfo.cs'))</In>
    <Pattern>^\s*\[assembly: AssemblyVersion\(\D*(\d+)\.(\d+)\.(\d+).(\d+)</Pattern>
    <Major>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</Major>
    <Minor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[2].Value)</Minor>
    <Build>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[3].Value)</Build>
    <Revision>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[4].Value)</Revision>
  </PropertyGroup>
  <Message Importance="High" Text="HelpFileVersion: $(Major).$(Minor).$(Build).$(Revision)" />
</Target>

This works just fine when building from MSBuild or command line directly, but if I update the external file in VS, the updated version (as set in the MSBuild task above) does not make it back to the IDE's instance of the HelpFileVersion property until I reload the project (which has not been working out very well for me to remember to do). I know my Major, Minor, Build, and Revision properties are updating correctly (seen by the message task). In my other thread listed above, the author of the SHFB tool mentioned that:

"As far as I know Visual Studio only edits project properties found at the project's root level and won't see any within a separate build task such as BeforeBuildHelp."

So, my question is, do any of you know a way, building from Visual Studio 2010, for me to update an IDE's instance of a property at build time from an MSBuild task or otherwise? I have tried setting the property several ways (CreateProperty, PropertyGroup, using DependsOn attribute, using CallTarget, etc), and every time I can get the correct value within the task, but the value is always stale in the IDE's property (seen by echoing it from a PostBuildEvent).

Community
  • 1
  • 1
miesch1
  • 165
  • 2
  • 8

1 Answers1

0

See:

Visual Studio actually keeps the files that make up your solution in memory to improve its performance. MsBuild will actually detect that it's running inside Visual Studio and will grab the files from the in-memory cache instead of grabbing them from disk.

The problem is that any changes made to files in the solution during the build aren't picked up by MsBuild until the next time you build your project. The inner workings are explained in this post in the MSDN forums.

See:

In our case we decided to make MsBuild use the on-disk files by setting the "UseHostCompilerIfAvailable" property to 'false' in the first propertygroup of the project file we were trying to patch on the fly.

See:

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Thanks for the suggestion. Unfortunately, adding 'FALSE' to the first PropertyGroup of my shfbproj project file does not enable the HelpFileVersion property to be updated. I tried setting my HelpFileVersion property in the same PropertyGroup as the UseHostCompilerIfAvailable, as well as in the "BeforeBuildHelp" MSBuild task. I wonder if the reason is that you are modifying your file, where I am just trying to modify my property? I just can't get my project's instance of HelpFileVersion to be updated after my project is loaded in VS. – miesch1 Jul 05 '14 at 06:01
  • Seems I misunderstood exactly how you're doing this, I reread your question and I suppose that it's not the issue you're running into. But since the IDE reads it from the project files, I suppose you'll need to actually update the project file. – jessehouwing Jul 07 '14 at 11:36
  • Thanks again for taking the time to look into this. At one point, I was trying to update the project file at build time from an MSBuild task. It seems VS didn't want to reload a project file during a build, but I should look into it further. – miesch1 Jul 08 '14 at 02:54