c# - 预提交钩子(Hook)修改AssemblyInfo

标签 c# .net git bash pre-commit-hook

我有一个预提交钩子(Hook),它使用 ruby​​ gem semver2 定义内部版本号。 gem 基本上只是创建一个名为 .semver 的文件,用于存储包的版本信息。

该 Hook 根据某些日期/提交参数生成内部版本号,然后使用此信息更改 AssemblyInfo.cs,然后在提交之前添加更改的文件。

我有几个问题:

  1. 就 .NET 而言,使用 Hook 修改我的 AssemblyInfo 文件是否存在危险?

  2. 这应该使用预提交 Hook 还是其他 Hook 来完成?

  3. 如何告诉此钩子(Hook)在 --amendmergerebase 提交上表现不同?

  4. 我如何告诉这个钩子(Hook)在每个分支上都有不同的行为?

  5. 您是否有其他解决方案来自动生成版本号?

钩子(Hook):

#!/bin/sh
#
# Append build number to semver version 
#

# check semver has been initiated
if [ -f .semver ]; then
    echo `semver`
else
    echo `semver init`
    echo `semver inc minor`
    echo `semver pre 'alpha.1'`
    echo `semver`
fi

# grab date string
date_str=`date +%y%m.%d.`

# grab commit count +1
build_num=$(git rev-list --since="00:00:00" HEAD --count)
let "build_num += 1"

# generate build & apply to semver
build=$date_str$build_num
semver meta $build

# define version strings
semver_str=$(semver)
ver_full=${semver_str#"v"}
cut_meta=$(cut -d "+" -f 1 <<<"$ver_full")
ver_small=$(cut -d "-" -f 1 <<<"$cut_meta")

# find AssemblyInfo & line number for AssemblyVersion
line=$(grep -n "AssemblyVersion(" "Properties/AssemblyInfo.cs")
line_num=$(cut -d ":" -f 1 <<<"$line")

# edit AssemblyVersion
new_line='[assembly: AssemblyVersion("'$ver_small'")]'
sed -i "${line_num}s/.*/$new_line/" "Properties/AssemblyInfo.cs"

# find line number for Semver
line=$(grep -n "Semver(" "Properties/AssemblyInfo.cs")
line_num=$(cut -d ":" -f 1 <<<"$line")

# edit Semver
new_line='[assembly: Semver("'$ver_full'")]'
sed -i "${line_num}s/.*/$new_line/" "Properties/AssemblyInfo.cs"

# add files
git add .semver
git add "Properties/AssemblyInfo.cs"

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Authenticator.Properties;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b6f9caad-fbfc-455a-8d69-f795fb9812ad")]

// This assembly uses the Semantic Versioning v2.0.0
// For more information on Semantic Versioning please see http://semver.org/
[assembly: AssemblyVersion("0.1.0")]
[assembly: Semver("0.1.0-alpha.1.0.0+1406.04.15")]

最佳答案

我的看法。

  1. 不,通常的做法是在编译之前的构建过程中编辑 AssemblyInfo 文件
  2. 提交路径技术很有趣,但有一些缺点:并非所有 Git 实现都能保证 Hook 工作(想想 Visual Studio Online)以及您的脚本工作。
  3. 参见#5。
  4. 参见#5。
  5. 如果您在构建期间编辑 AssemblyInfo 文件,那么无论如何它都会起作用。您的脚本需要查询 Git 的当前状态(哪个分支和提交)以选择正确的 SemVer 值。

I blogged an example它展示了如何挂接到 MSBuild 并更改 AssemblyInfo 文件,从中您可以找到许多其他示例、工具和引用。

关于c# - 预提交钩子(Hook)修改AssemblyInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24067939/

相关文章:

c# - 通过线程池内的定时器执行方法

.net - ASP.NET 工作进程是否仍以 31kb 的 block 返回数据

python - Python 项目的预提交 Hook

c# - 在 C# 中处理非常大的整数

c# - 比较两个列表并找到这两个列表之间的增量的最有效模式/算法是什么?

c# - .net 6 最小 api docker 问题 : error CS5001: Program does not contain a static 'Main' method suitable for an entry point

git - 无法使用 crontab 运行 git 命令

c# - 需要 PipeDirection.InOut 的 NamedPipeServerStream 与 NamedPipeServerClient 上的示例

c# - 在加载表单后立即调用方法的最佳方式是什么?

.net - 如何将 .NET 标准库打包/发布到私有(private) VSTS Nuget 提要?