Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Versioning

I have a situation where i want the versioning to be dynamic at build time.

Version Pattern: <year>.<month>.<day>.<hhmm>

But i have read where the String value used in the Attribute is reparsed at compile time.

Any advise on how to get this dynamic versioning completed?

Ideal situation:

<Assembly: AssemblyVersion("4.0.0.0")> 
<Assembly: AssemblyFileVersion(Year(Now) & "." & Month(Now()) & "." & Day(Now()) & "." & String.format("hhmm", now()))> 

I know it wont work but should get the point acrossed.

like image 230
GoldBishop Avatar asked Jan 07 '13 20:01

GoldBishop


1 Answers

You can use the MsbuildCommunityTasks to generate the build number and to customize the assembly file version on pre-build time.

  • Download the zip at MsbuildCommunityTasks

  • Unzip to the folder [SolutionFolder]\MsBuildExtensions\MSBuildCommunityTasks

  • Add the sample below on your project (csproj), just after the Microsoft.CSharp.Targets import.

  <PropertyGroup>
  <MSBuildCommunityTasksPath>$(MSBuildThisFileDirectory)..\MsBuildExtensions\MSBuildCommunityTasks</MSBuildCommunityTasksPath>  
    <My-PropertiesDir>Properties</My-PropertiesDir>
  </PropertyGroup>
  <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets"/>
  <Target Name="BeforeBuild">
    <Time Format="yyyy.MM.dd.HHmm">
      <Output TaskParameter="FormattedTime" PropertyName="My-VersionNumber" />
    </Time>
    <Message Text="Building $(My-VersionNumber) ...">
    </Message>
    <ItemGroup>
      <My-AssemblyInfo Include="$(My-PropertiesDir)\AssemblyVersionInfo.cs" />
      <Compile Include="@(My-AssemblyInfo)" />
    </ItemGroup>
    <MakeDir Directories="$(My-PropertiesDir)" />
    <AssemblyInfo OutputFile="@(My-AssemblyInfo)"
          CodeLanguage="CS"
          AssemblyFileVersion="$(My-VersionNumber)"
          AssemblyInformationalVersion="$(My-VersionNumber)"
          Condition="$(My-VersionNumber) != '' "
          />
  </Target>
  <Target Name="AfterBuild">
    <Delete Files="@(My-AssemblyInfo)" />
  </Target>

  • Wipe the AssemblyFileVersion attribute from your AssemblyInfo.cs. It will be generated at build time.

  • You'll see the version number being printed on the console when you build. The generated file is deleted on the AfterBuild target, to keep your source control clean.

    BeforeBuild:

    Building 2013.01.14.1016 ... Created AssemblyInfo file "Properties\AssemblyVersionInfo.cs".

    (...)

    AfterBuild:

    Deleting file "Properties\AssemblyVersionInfo.cs".

  • If you want do this to many projects with less msbuild code, it will be necessary to create a customized build script to wrap up your solution.

like image 195
Marcos Brigante Avatar answered Oct 28 '22 11:10

Marcos Brigante