I am currently developing a Sitecore module for a client, who needed a versioning based on builddate. They wanted the module to display an message to the user if it was out of date. We therefore needed to update the assembly files versions on each release. To ensure we remember to to update the version with every release, I looked into making this automatic. This can be usefull for Sitecore modules and all other applications.
To achieve this, I used AssemblyInfo for versioning. First we need to retrive the version form the assembly file:
var version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
We use it to check against a webservice, where the client provided the newest version number in XML.
<ApplicationVersions> <SitecoreConnector>2017.05.31.1003</SitecoreConnector> </ApplicationVersions>
There is a lot of modules or plugins which can auto increment the version in assemblyinfo. However we needed it to be based on date and time. Therefore I decided to use T4 TextTemplates, to create a AssemblyVersion.cs file:
<#@ template language="C#" #> <#@ output extension=".cs" #> using System; using System.Reflection; [assembly: AssemblyVersion("<#=DateTime.Now.ToString("yyyy.MM.dd.HHmm")#>")] [assembly: AssemblyFileVersion("<#=DateTime.Now.ToString("yyyy.MM.dd.HHmm")#>")] [assembly: AssemblyCopyright("Copyright © Customer <#=DateTime.Now.Year#>")]
T4 is used by developers as part of an application or tool framework to automate the creation of text files with a variety of parameters. These text files can ultimately be any text format, such as code (for example C#), XML, HTML or XAML. T4 Text transformation (Wiki)
I use C# to get the date and time, and format it to my versioning needs. It can also be used to set the Copyright year dynamically. When this file is saved, Visual Studio automatically runs the template and create a AssemblyVersion.cs file:
using System; using System.Reflection; [assembly: AssemblyVersion("2017.05.31.1003")] [assembly: AssemblyFileVersion("2017.05.31.1003")] [assembly: AssemblyCopyright("Copyright © Customer 2017")]
To automate this creation, I have created a pre-build event for the project. This event uses TextTransform.exe to transform the T4 template before the build.
set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" %textTemplatingPath% "$(ProjectDir)AssemblyVersion.tt"
And thats it! Automatic created AssemblyVersion.cs which runs on every build.