views:

322

answers:

2

I have been tasked with automating our build process with msbuild. We have about twenty Visual C++ projects and twenty C# projects making it a pain to hand edit all the version strings. Our version numbers are meat-generated at build time, so we need to pass the full version string as a property on the command line. My initial implementation used the MSBuild Extension Pack's File class to do replacements on the .rc and AssemblyInfo.cs files:

<FileSystem.File TaskAction="Replace" Files="@(AssemblyInfoFiles)" RegexPattern='\[assembly: AssemblyVersion\(".*"\)\]' Replacement='[assembly: AssemblyVersion("$(Version)")]'/>
<FileSystem.File TaskAction="Replace" Files="@(AssemblyInfoFiles)" RegexPattern='\[assembly: AssemblyFileVersion\(".*"\)\]' Replacement='[assembly: AssemblyFileVersion("$(Version)")]'/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern="FILEVERSION\s+\S+" Replacement="FILEVERSION $(Version)"/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern="PRODUCTVERSION\s+\S+" Replacement="PRODUCTVERSION $(Version)"/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern='VALUE\s+"FileVersion",\s*".*"' Replacement='VALUE "FileVersion", "$(Version)"'/>
<FileSystem.File TaskAction="Replace" Files="@(RCFiles)" RegexPattern='VALUE\s+"ProductVersion",\s*".*"' Replacement='VALUE "ProductVersion", "$(Version)"'/>

This is working great on the AssemblyInfo.cs files, but when I compile any of the C++ projects I get errors like this:

.\MyProject.rc(23): error RC2135: file not found: 0x09
.\MyProject.rc(71): error RC2135: file not found: 1
.\MyProject.rc(72): error RC2135: file not found: 6
.\MyProject.rc(73): error RC2135: file not found: 6
.\MyProject.rc(74): error RC2135: file not found: 0x3fL
.\MyProject.rc(80): error RC2135: file not found: FILEOS
.\MyProject.rc(81): error RC2135: file not found: 0x2L
.\MyProject.rc(84): error RC2164: unexpected value in RCDATA
.\MyProject.rc(86): error RC2135: file not found: BLOCK
.\MyProject.rc(88): error RC2135: file not found: VALUE

The first error is encountered on line 23, but when I diff MyProject.rc against source control everything looks OK. Only the version numbers are changed, those changes begin on line 72, and the string "0x09" does not appear anywhere in the file. What in the world is going on here? Lines 21 - 25 look like this:

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

I'm also open to any suggestions on other methods to accomplish the same goal.

+2  A: 

I would separate out the version information from the AssemblyInfo.cs and *.rc files. Create an AssemblyVersion.cs and Version.rc file, which contain the (common) versioning information for all your assemblies. You'd generate these at the start of your build. Because they only contain the versioning information you don't have to use regular expressions, you can overwrite the entire file each time.

Gordon Broom
"Anything can be solved by one more layer of indirection"
Gordon Broom
A decent suggestion. Hand editing all those files once is certainly better than doing it every time we build, however we're still adding new projects to the build and it would be nice for those additions to be seamless. Plus, my inherent programmer laziness makes me hold out hope for a solution that allows me to avoid any hand editing.
Jared
A: 

The Replace TaskAction was inserting Unicode characters while the rc files are using ASCII encoding. Adding TextEncoding="ASCII" to all the rc replacement tasks results in compilable files.

Jared