【问题标题】:How can I get the GitVersion version into my binary?如何将 GitVersion 版本放入我的二进制文件中?
【发布时间】:2022-11-18 23:20:01
【问题描述】:

我正在使用 GitVersion 对我的 C#.NET 应用程序进行版本控制。我的应用程序还有一个 -V 选项,用于显示二进制文件的当前版本。

我怎样才能从GitVersion获取数据到我的应用程序中,以便在我每次构建时更新它?

【问题讨论】:

  • 我删除了我的答案,因为我不确定 GitInfo 是否适合您 - 虽然它与 GitVersion 的关系为零 - 有些人可能认为这是冗余。
  • This nuget 包看起来很有前途。

标签: c# .net gitversion


【解决方案1】:

我结合使用 PowerShell 脚本和预构建事件获得了它:

脚本如下(在工程目录下保存为gitversion.ps1

$gitVersionJson = dotnet gitversion /output json
$By = [System.Text.Encoding]::Unicode.GetBytes($gitVersionJson)
$output =[Convert]::ToBase64String($By)

"using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

class GitVersion {
    private static Dictionary<string, object> _values;
    private static Dictionary<string, object> Values {
        get {
            if (_values == null) {              
                byte[] data = Convert.FromBase64String(""$output"");
                string decodedString = Encoding.Unicode.GetString(data);
                _values = JsonSerializer.Deserialize<Dictionary<string, object>>(decodedString);
            }

            return _values;
        }
    }

    public static object MajorMinorPatch {
        get {
            return Values[""MajorMinorPatch""];
        }
    }

}
" | Out-File GitVersion.cs

"Generated GitVersion.cs" | Write-Output

然后作为预构建事件,我在构建设置中添加了它:

powershell -ExecutionPolicy Bypass -NoProfile -NonInteractive -File "$(ProjectDir)gitversion.ps1" 

或者myproject.csproj

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
   <Exec Command="powershell -ExecutionPolicy Bypass -NoProfile -NonInteractive -File &quot;$(ProjectDir)gitversion.ps1&quot; " />
</Target>

这将创建一个 GitVersion 类,您可以在您的代码中使用:

Console.WriteLine(GitVersion.MajorMinorPatch);

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 2017-03-20
    • 2016-05-06
    • 2014-11-13
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多