Dynamic Versioning
Asked Answered
E

1

8

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.

Equilibrate answered 7/1, 2013 at 20:24 Comment(7)
autobuildversion.codeplex.comHabitat
@RobertHarvey so no way to intercept the Version information in Pre-Build? This sucks as most of the auto-increment for VS2012 assumes wild-card.Equilibrate
Are you sure the Autobuild plugin doesn't do what you want?Habitat
@RobertHarvey I am building the app, but it will be maintained by the client. So i am trying to keep the tool add-ins to a minimum, if possibleEquilibrate
Here are two previous answers. Do these help? [can-i-automatically-increment-the-file-build-version-when-using-visual-studio][1] [programmatically-change-the-assemblyversion-and-assemblyfileversion-attributes][2] [1]: #357043 [2]: #1550749Rapturous
@DonnyMcCoy Ill review them further tomorrow. Wish they allowed more pattern designed Versioning than the Increment(1, 1) styled versioning.Equilibrate
You could use a T4 file to regenerate the the file containing the buildnumber on every build.Retrace
C
3

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.

Cordey answered 14/1, 2013 at 13:3 Comment(5)
I would assume that this applies to VB as well? I flip between CS and VB, just want to make sure i can use the same DevEnv build standard between the two languages.Equilibrate
Yes, it works fine with VB. You just have to adjust the 'AssemblyInfo' task parameters (CodeLanguage="VB") and adjust the properties My-PropertiesDir and My-AssemblyInfo accordingly.Cordey
ill have to get this installed on my dev-top....as client computer is locked down to where i cant install anything without requests ;)Equilibrate
Good luck on your task. MSBuild Community Tasks is like a swiss knife for building .Net projects. I'm pretty sure that you will like it. ^^Cordey
always like new tools, especially those that allow for user customization for what they need, instead of what MS thinks they need ;)Equilibrate

© 2022 - 2024 — McMap. All rights reserved.