Auto Versioning in Visual Studio 2017 (.NET Core)

后端 未结 16 1436
一整个雨季
一整个雨季 2020-11-28 18:45

I have spent the better part of a few hours trying to find a way to auto-increment versions in a .NETCoreApp 1.1 (Visual Studio 2017).

I know the the AssemblyInfo.cs

16条回答
  •  渐次进展
    2020-11-28 19:26

    I came up with a solution that worked almost the same as old AssemblyVersion attribute with star (*) - AssemblyVersion("1.0.*")

    Values for AssemblyVersion and AssemblyFileVersion is in MSBuild project .csproj file (not in AssemblyInfo.cs) as property FileVersion (generates AssemblyFileVersionAttribute) and AssemblyVersion (generates AssemblyVersionAttribute). In MSBuild process we use our custom MSBuild task to generate version numbers and then we override values of these FileVersion and AssemblyVersion properties with new values from task.

    So first, we create our custom MSBuild task GetCurrentBuildVersion:

    public class GetCurrentBuildVersion : Task
    {
        [Output]
        public string Version { get; set; }
     
        public string BaseVersion { get; set; }
     
        public override bool Execute()
        {
            var originalVersion = System.Version.Parse(this.BaseVersion ?? "1.0.0");
     
            this.Version = GetCurrentBuildVersionString(originalVersion);
     
            return true;
        }
     
        private static string GetCurrentBuildVersionString(Version baseVersion)
        {
            DateTime d = DateTime.Now;
            return new Version(baseVersion.Major, baseVersion.Minor,
                (DateTime.Today - new DateTime(2000, 1, 1)).Days,
                ((int)new TimeSpan(d.Hour, d.Minute, d.Second).TotalSeconds) / 2).ToString();
        }
    }
    

    Task class inherit from Microsoft.Build.Utilities.Task class from Microsoft.Build.Utilities.Core NuGet package. It takes BaseVersion property (optional) on input and returns generated version in Version output property. The logic to get version numbers is same as .NET automatic versioning (Build number is days count since 1/1/2000 and Revision is half seconds since midnight).

    To build this MSBuild task, we use .NET Standard 1.3 class library project type with this class.

    .csproj file can looks like this:

    
      
        netstandard1.3
        DC.Build.Tasks
        DC.Build.Tasks
        DC.Build.Tasks
        DC.Build.Tasks
      
     
      
        
        
      
    
    

    This task project is also available in my GitHub holajan/DC.Build.Tasks

    Now we setup MSBuild to use this task and set FileVersion and AssemblyVersion properties. In .csproj file, it looks like this:

    
      
     
      
        ...
        1.0.0.0
        1.0.0.0
      
     
      ...
     
      
        
          
        
        
          $(FileVersion)
        
      
     
    
    

    Important things here:

    • Mentioned UsingTask imports GetCurrentBuildVersion task from DC.Build.Tasks.dll. It assumes that this dll file is located on parent directory from your .csproj file.
    • Our BeforeBuildActionsProject1 Target that calls task must have unique name per project in case we have more projects in the solution which calls GetCurrentBuildVersion task.

    The advantage of this solution is that it works not only from builds on build server, but also in manual builds from dotnet build or Visual Studio.

提交回复
热议问题