首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >预提交钩子修改AssemblyInfo

预提交钩子修改AssemblyInfo
EN

Stack Overflow用户
提问于 2014-06-05 16:16:55
回答 1查看 1.5K关注 0票数 2

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

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

我在这里有几个问题:

  1. 就AssemblyInfo而言,让钩子修改我的.NET文件有危险吗?
  2. 这应该用预提交钩子来完成,还是用另一个钩子完成?
  3. 我如何告诉这个钩子在--amendmergerebase提交时的行为不同?
  4. 我怎样才能告诉这个钩子在一个分支的基础上表现不同呢?
  5. 您是否有不同的解决方案来自动化内部版本号?

The Hook:

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
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")]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-20 08:54:47

我的想法。

  1. 不,编译前在构建过程中编辑AssemblyInfo文件是常见的做法。
  2. 提交路由的技术很有趣,但也有一些缺点:并不是所有的Git实现都能保证钩子工作(想想Visual ),而且脚本也能工作。
  3. 见5号。
  4. 见5号。
  5. 如果您在构建过程中编辑AssemblyInfo文件,那么不管如何,它都会工作。您的脚本需要查询Git以获取当前状态(哪个分支和提交)来选择正确的SemVer值。

我写了一个例子,它展示了如何连接到MSBuild和更改AssemblyInfo文件,您可以从中找到许多其他示例、工具和引用。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24067939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档