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).$(Revisi开发者_如何学运维on)")]' />
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?
How about this:
<FileUpdate Files="Properties/AssemblyInfo.cs"
Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="$1.$2.$3.$(Revision)" />
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)\("([0-9]+\.[0-9]+\.[0-9]+)(\.[0-9]+)?"\)"
ReplacementText="$1("$2.$(Revision)")" />
<FileUpdate Files="@(AssemblyFiles)"
Multiline="true"
Singleline="false"
Regex="AssemblyCompany\(".*"\)"
ReplacementText="AssemblyCompany("My Company")" />
<FileUpdate Files="@(AssemblyFiles)"
Multiline="true"
Singleline="false"
Regex="AssemblyCopyright\(".*"\)"
ReplacementText="AssemblyCopyright("Copyright © 2009-$(Year) My Company")" />
</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.
精彩评论