C# 预提交钩子修改AssemblyInfo

C# 预提交钩子修改AssemblyInfo,c#,.net,git,bash,pre-commit-hook,C#,.net,Git,Bash,Pre Commit Hook,我有一个预提交钩子,它使用RubyGem定义构建编号。gem基本上只创建一个名为.semver的文件,该文件存储包的版本信息 钩子会根据一些日期/提交参数生成一个构建编号,然后使用此信息修改AssemblyInfo.cs,然后在提交之前添加修改过的文件 我这里有几个问题: 就.NET而言,让钩子修改我的AssemblyInfo文件有危险吗 这应该使用pre-commit钩子还是其他钩子来完成 我怎样才能告诉这个钩子在修改、合并和重新设置提交的基础上有不同的行为 我怎样才能让这个钩子在一个分支一个

我有一个预提交钩子,它使用RubyGem定义构建编号。gem基本上只创建一个名为.semver的文件,该文件存储包的版本信息

钩子会根据一些日期/提交参数生成一个构建编号,然后使用此信息修改AssemblyInfo.cs,然后在提交之前添加修改过的文件

我这里有几个问题:

就.NET而言,让钩子修改我的AssemblyInfo文件有危险吗

这应该使用pre-commit钩子还是其他钩子来完成

我怎样才能告诉这个钩子在修改、合并和重新设置提交的基础上有不同的行为

我怎样才能让这个钩子在一个分支一个分支的基础上表现出不同的行为呢

您有不同的解决方案来自动生成版本号吗

钩子:

我的看法

不,通常的做法是在编译之前的构建期间编辑AssemblyInfo文件 提交路由的技术很有趣,但也有一些缺点:并非所有的Git实现都能保证钩子工作,就像VisualStudioOnline一样,脚本也能工作。 见第5条。 见第5条。 如果您在生成过程中编辑AssemblyInfo文件,则不管怎样,它都会起作用。您的脚本需要查询Git的当前状态,以选择正确的SemVer值。 这展示了如何连接到MSBuild并更改AssemblyInfo文件,从中可以找到许多其他示例、工具和引用。

MyTake

不,通常的做法是在编译之前的构建期间编辑AssemblyInfo文件 提交路由的技术很有趣,但也有一些缺点:并非所有的Git实现都能保证钩子工作,就像VisualStudioOnline一样,脚本也能工作。 见第5条。 见第5条。 如果您在生成过程中编辑AssemblyInfo文件,则不管怎样,它都会起作用。您的脚本需要查询Git的当前状态,以选择正确的SemVer值。 这展示了如何连接到MSBuild并更改AssemblyInfo文件,从中可以找到许多其他示例、工具和引用

#!/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"
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")]