Change only Revision number in AssemblyInfo.cs with MSBuild FileUpdate task

后端 未结 2 1308
陌清茗
陌清茗 2021-02-07 21:56

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

相关标签:
2条回答
  • 2021-02-07 22:34

    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.

    0 讨论(0)
  • 2021-02-07 22:39

    How about this:

    <FileUpdate Files="Properties/AssemblyInfo.cs"
       Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
       ReplacementText="$1.$2.$3.$(Revision)" />
    
    0 讨论(0)
提交回复
热议问题