Wednesday, September 12, 2012

Auto increment build number msbuild 4



If you just need to increment that number in the AssemblyVersion, it’s amazingly easy:
[assembly: AssemblyVersion("1.0.*")]


However the above  will increment only the AssemblyVersion (as a note: the third number represent the number of days since 1/1/2000 and the third HHmm of your local time) if  you rather want to increment the AssemblyFileVersion things complicate a bit… as someone suggest on stackoverflow you can omit the AssemblyFileVersion in your AssemblyInfo.cs and you’ll get the operating system displaying as FileVersion the value of AssemblyVersion.


I, however, was looking for something different. I wanted the FileVersion to auto increment at each build accordingly to my day’s fantasies.


I’ve discovered from the msbuild master Sayed Ibrahim that in msbuild 4.0 ther’s a new feature called Inline Task. It’s just like a regular msbuild custom task, but it’s defined inline within the project file. And that's a great news.


So I created an inline task like this


<UsingTask
    TaskName="SetVersion"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup>
      <FileName ParameterType="System.String" Required="true" />
      <MajorVersion ParameterType="System.Int32" Required="true" />
      <MinorVersion ParameterType="System.Int32" Required="true" />
    </ParameterGroup>
    <Task>
      <!--<Reference Include=""/>-->
      <Using Namespace="System"/>
      <Using Namespace="System.IO"/>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
            string dayCount = Math.Floor(DateTime.Now.Subtract(new DateTime(1979,5,2)).TotalDays).ToString();
            string text = String.Format("[assembly:System.Reflection.AssemblyFileVersion(\"{0}.{1}.{2}.{3}\")]", MajorVersion, MinorVersion, dayCount, DateTime.Now.ToString("HHmm") );
            File.WriteAllText(FileName, text);
            return true;
  ]]></Code>
    </Task>
  </UsingTask>


and called it in “BeforeBuild” Target


<Target Name="BeforeBuild">
    <SetVersion FileName="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs" MajorVersion="1" MinorVersion="3" />
</Target>


The file “Properties\VersionInfo.cs” is an empty file included in my project, that my InlineTask code fills.


Of course there are other ways to do the same thing with AutoBuildVersion being one of the easiest, but this one is customizable by code and  don’t have dependencies so it compile fine everywhere .net 4 is installed.


Marco Casamento

No comments: