【问题标题】:Increment the build revision number in visual studio在 Visual Studio 中增加构建版本号
【发布时间】:2021-03-01 17:37:03
【问题描述】:

我为 sharedAssemblyInfo.tt 创建了模板,它的更新版本号仅用于第一次构建或重建。 如果我更改一些代码并尝试构建整个项目,它不会反映版本号。

【问题讨论】:

标签: c# visual-studio visual-studio-2010


【解决方案1】:

如果您想在每次构建项目时增加内部版本号,而不管选择的配置如何,您可以按照以下步骤操作,也可以在Application ➤ Assembly Information 中手动修改它

下面的代码将读取现有的AssemblyInfo.cs 文件,并使用正则表达式查找AssemblyVersion 信息,然后根据TextTransform.exe 的输入增加修订和内部版本号。

  1. 删除现有的AssemblyInfo.cs 文件。
  2. 在其位置创建一个AssemblyInfo.tt 文件。 Visual Studio 应该 创建AssemblyInfo.cs并在保存后将其与T4文件分组 T4 文件。

代码:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
    string output = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo.cs"));
    Regex pattern = new Regex("AssemblyVersion\\(\"(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<revision>\\d+)\\.(?<build>\\d+)\"\\)");
    MatchCollection matches = pattern.Matches(output);
    if( matches.Count == 1 )
    {
        major = Convert.ToInt32(matches[0].Groups["major"].Value);
        minor = Convert.ToInt32(matches[0].Groups["minor"].Value);
        build = Convert.ToInt32(matches[0].Groups["build"].Value) + 1;
        revision = Convert.ToInt32(matches[0].Groups["revision"].Value);
        if( this.Host.ResolveParameterValue("-","-","BuildConfiguration") == "Release" )
            revision++;
    }
#>

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;

// General Information
[assembly: AssemblyTitle("Insert title here")]
[assembly: AssemblyDescription("Insert description here")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Insert company here")]
[assembly: AssemblyProduct("Insert product here")]
[assembly: AssemblyCopyright("Insert copyright here")]
[assembly: AssemblyTrademark("Insert trademark here")]
[assembly: AssemblyCulture("")]

// Version informationr(
[assembly: AssemblyVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]

<#+
    int major = 1;
    int minor = 0;
    int revision = 0;
    int build = 0;
#>
  1. 将此添加到您的预构建活动中:
"%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"

如果我经常在我的项目中使用此解决方案,如果某些方面无法使用,请告诉我。

#编辑:

对于许多项目,您可以尝试使用 VS 扩展,这在这种情况下可能会有所帮助。我的同事使用了一个扩展。它可以在 Visual Studio Marketplace、Release 区域和 Visual Studio 内部的“扩展和更新”下找到,称为“Intentional Solution Version Editor”。

【讨论】:

  • 我的解决方案有多个项目,因此我没有为每个项目创建 AssemblyInfo 模板,而是为 sharedAssemblyInfo.tt 创建了模板
  • @Nikhil 我已经更新了我的帖子,不确定是否有帮助,但请检查一下
【解决方案2】:

如果您使用gitlab,您可以使用GitVersionTask (https://www.nuget.org/packages/GitVersionTask)。

在每次提交时,它会自动增加您构建的Version,甚至重命名输出文件。您只需将 nuget 安装到您的项目中即可。


2 提交时分支 test-complex-udt 的示例输出:

HtOPC.Local.1.5.2-test-complex-udt0002

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2012-02-02
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多