【问题标题】:How to Auto Increment Assembly or Assembly File Version, from MSBuild?如何从 MSBuild 自动增加程序集或程序集文件版本?
【发布时间】:2018-05-05 10:01:47
【问题描述】:

约束是: 使用 Visual Studio 2017。 最终需要从调用 MSBuild 的 powershell 脚本调用。

不确定其相关性,但需要能够构建以下内容:

  • asp.net 461
  • asp.net-core 1.1 和 2.0 程序集

目前尝试失败:

示例尝试 of "Code Generation in a Build Process" 适用于从 VS 构建但不适用于 MSBuild:

放置在项目的根目录 - 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> 

这样调用: msbuild myProject.csproj /t:TransformAll

【问题讨论】:

  • 您是否故意标记“asp.net-core”?如果您使用的是 asp.net 核心,则您已经有一个项目系统,该系统可以根据 msbuild 值生成程序集信息文件,这些值可以在命令行上传递或根据项目文件中的日期和时间计算。
  • 它需要能够构建 asp.net 461 程序集以及 asp.net-core 1.1 和 2.0。我之前从标题中删除了 asp.net-core,也会删除标签。
  • 所以你的asp.net核心项目已经支持-msbuild /p:Version=1.2.3.4将设置程序集版本,我建议基于existing source code为基于sdk的项目为经典项目创建“polyfill目标” .现在没有时间做,但也许在接下来的几天里。然后会表现得像stackoverflow.com/questions/43274254
  • @Martin Ullrich,谢谢。我已经在使用 powershell 来调用 msbuild 所以 msbuild /p:Version=1.2.3.4 工作完美。它解决了我的 实际 问题,但必须使用 t4 模板禁用自动增量解决方案才能工作。知道一种无论您从哪里构建都可以使用的方法仍然会很棒。同样感谢!
  • 您使用的是哪个 MSBuild?位于С:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin ?

标签: msbuild t4


【解决方案1】:

你可以编写简单的控制台应用程序,并在项目属性>构建事件中,添加一个“预构建事件命令行”,如下所示:“D:\SomePath\MyAssemblyInfoPatcher.exe”“$(ProjectDir)Properties\AssemblyInfo.cs "

示例应用程序代码(适用于 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);
                }
            }   
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2018-09-12
    • 2012-08-31
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    相关资源
    最近更新 更多