views:

712

answers:

2

I need to change only the revision number of an AssemblyInfo.cs file. The version number is in the format Major.Minor.Build.Revision e.g. 1.4.6.0.

Currently I change the version with the FileUpdate task (from the MSBuild Community Tasks Project) and the following regex:

<FileUpdate Files="@(AssemblyResult)"
Regex='(\[\s*assembly:\s*AssemblyVersion\(\s*"[^\.]+\.[^\.]+)\.([^\.]+)(\.)([^\.]+)("\)\s*\])'
ReplacementText='[assembly: AssemblyVersion("$(AssemblyMajorNumber).$(AssemblyMinorNumber).$(AssemblyBuildNumber).$(Revision)")]' />

Now I need to update only the revision number and leave major,minor and build unchanged. So, is there any task to do this? Or can it be done with a regex? What would be the regular expression then?

+4  A: 

How about this:

<FileUpdate Files="AssemblyInfo.cs"
   Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
   ReplacementText="$1.$2.$3.$(Revision)" />
Wim Coenen
hii wcoenen,can we read these numbers(regex or $1.$2.$3.$4) back.??..i need to read the first three ie $1.$2.$3 and assign there value to the properties in MSBUILD..can you help me in this??
Divya mohan Singh
No. You can't use `FileUpdate` to set msbuild properties. You can use the values of captured groups in the replacement text as I have shown, but you cannot use these values to set msbuild properties.
Wim Coenen
A: 

I use the following target to do this:

<Target Name="UpdateAssemblyInfoVersion" DependsOnTargets="GetRevision">
    <CreateItem Include="**\AssemblyInfo.vb">
          <Output TaskParameter="Include" ItemName="AssemblyFiles"/>
    </CreateItem>
<Time>
  <Output TaskParameter="Year" PropertyName="Year" />
</Time>
    <FileUpdate Files="@(AssemblyFiles)"
        Multiline="true"
        Singleline="false"
        Regex="(AssemblyVersion|AssemblyFileVersionAttribute|AssemblyFileVersion)\(&quot;([0-9]+\.[0-9]+\.[0-9]+)(\.[0-9]+)?&quot;\)"
        ReplacementText="$1(&quot;$2.$(Revision)&quot;)" />
    <FileUpdate Files="@(AssemblyFiles)"
        Multiline="true"
        Singleline="false"
        Regex="AssemblyCompany\(&quot;.*&quot;\)"
        ReplacementText="AssemblyCompany(&quot;My Company&quot;)" />
    <FileUpdate Files="@(AssemblyFiles)"
        Multiline="true"
        Singleline="false"
        Regex="AssemblyCopyright\(&quot;.*&quot;\)"
        ReplacementText="AssemblyCopyright(&quot;Copyright &#169; 2009-$(Year) My Company&quot;)" />
</Target>

This replaces the revision (4th number) in any of the AssemblyInfo files (in multiple projects). It looks at the AssemblyVersion AssemblyFileVersionAttribute and AssemblyFileVersion tags, and uses the $(Revision) MSBuild property for the number (I have another target called GetRevision that gets this from SVN and sets the property, so this one depends on that target). The regex replacement handles version numbers that have either 3 or 4 digits (I had a bunch with 3 only, for whatever reason).

It also sets/overwrites the Company and Copyright information, and sets it to "My Company". For copyright, I was lazy and made it so it always uses the current year so I don't have to remember to update it every year (so it says eg "Copyright (c) 2009-2010 My Company").

This target requires the MSBuild Community tasks extension.


As a matter of policy, everything checked into SVN has .0 as the last number, and only the CI server changes this value when it's doing a build. This lets us quickly tell the difference between developer-created builds (which are never allowed to go to customers) and "official" builds created by the CI server.

gregmac