12
votes

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?

2
This question is not related to wix or windows installer at all, so I have edited accordingly.Wim Coenen
One more thing..can i use these property $1,$2 and $3 further..like to assign there value to some other property in have taken.?? actually i need to extract the major.minor,build number and have to use them somewhere else..so,for that i need them..so ,can you please do this last favor..Divya mohan Singh

2 Answers

16
votes

How about this:

<FileUpdate Files="Properties/AssemblyInfo.cs"
   Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
   ReplacementText="$1.$2.$3.$(Revision)" />
7
votes

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.