How to Auto Increment Assembly or Assembly File Version, from MSBuild?

Viewed 1403

Constraints are: Using Visual Studio 2017. Needs to ultimately be called from a powershell script calling MSBuild.

Not sure its relevant, but needs to be able to build the following:

  • asp.net 461
  • asp.net-core 1.1 and 2.0 assemblies

Unsuccessful attempts so far:

Example attempt of "Code Generation in a Build Process" That works on build from VS but not MSBuild:

placed in root of project - handleVersioning.tt:

<#@ template language="C#" #>

using System.Reflection;

[assembly: AssemblyVersion("1.0.0.*")]
[assembly: AssemblyFileVersion("<#= this.Year #>.<#= this.Month #>.<#= this.Day #>.<#= this.Minute #>")]
<#+
    int Year = DateTime.UtcNow.Year;
    int Month = DateTime.UtcNow.Month;
    int Day = DateTime.UtcNow.Day;
    int Minute = unchecked((int)DateTime.UtcNow.TimeOfDay.TotalMinutes);
#>

.csproj:

<Import Project="...hardcoded...\Microsoft.CSharp.targets" />
<!-- This is the important line: -->  
<Import Project="...hardcoded...\TextTemplating\Microsoft.TextTemplating.targets" />

<PropertyGroup>  
    <TransformOnBuild>true</TransformOnBuild>  
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup> 

Called like so: msbuild myProject.csproj /t:TransformAll

1 Answers

U can write simple console application and in Project Properties > Build Events, add a "Pre-build event command line" like this: "D:\SomePath\MyAssemblyInfoPatcher.exe" "$(ProjectDir)Properties\AssemblyInfo.cs"

sample application code (works on VS 2022)

using System;
using System.IO;
using System.Linq;

namespace MyAssemblyInfoPatcher
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                string path = args[0].ToString();
                Console.WriteLine(string.Format("Current App version is set to: {0}", path));
                string now_date = DateTime.Now.ToString("yyyy.MM.dd.HHmm");
                if (File.Exists(path))
                {
                    string _AssemblyVersion = string.Empty;
                    string _AssemblyFileVersion = string.Empty;

                    var lines = File.ReadLines(string.Format(path));
                    for (int i = 0; i < lines.Count(); i++)
                    {
                        if (lines.ElementAt(i).ToString().StartsWith("[assembly: AssemblyVersion"))
                        {
                            _AssemblyVersion = lines.ElementAt(i).ToString();
                        }
                        else if (lines.ElementAt(i).ToString().StartsWith("[assembly: AssemblyFileVersion"))
                        {
                            _AssemblyFileVersion = lines.ElementAt(i).ToString();
                        }
                    }

                    string _replace_assembly = File.ReadAllText(path);

                    if (_AssemblyVersion != string.Empty)
                    {
                        _replace_assembly = _replace_assembly.Replace(_AssemblyVersion, string.Format("[assembly: AssemblyVersion(\"{0}\")]", now_date));
                    }
                    if (_AssemblyFileVersion != string.Empty)
                    {
                        _replace_assembly = _replace_assembly.Replace(_AssemblyFileVersion, string.Format("[assembly: AssemblyFileVersion(\"{0}\")]", now_date));
                    }

                    File.WriteAllText(path, _replace_assembly);
                }
            }   
        }
    }
}
Related