2014-06-05 39 views
1

我有一個預先提交的鉤子,它使用ruby gem semver2來定義內部版本號。 gem基本上只是創建一個名爲.semver的文件,用於存儲軟件包的版本信息。Pre-commit hook修改AssemblyInfo

掛鉤根據某些日期/提交參數生成內部版本號,然後使用此信息更改AssemblyInfo.cs,然後在提交之前添加已更改的文件。

我有幾個問題在這裏:

  1. 是否有具有鉤修改我的AssemblyInfo文件就.NET而言危險?

  2. 這應該用預提交鉤子還是不同的鉤子來完成?

  3. 如何判斷此掛鉤在--amendmergerebase提交中的行爲不同?

  4. 我怎麼能告訴這個鉤子在分支上的行爲不同?

  5. 您是否有不同的解決方案來自動構建編號?

鉤:

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

回答

5

我服食。

  1. 不,這是常見的做法是編輯集信息生成過程中的文件只是編譯
  2. 提交路由的技術是有趣之前,但也有一些缺點:並非所有的Git實現保證掛鉤工作(認爲的Visual Studio在線)並且您的腳本能夠正常工作。
  3. 參見#5。
  4. 參見#5。
  5. 如果您在編譯期間編輯AssemblyInfo文件的工作,它將無論如何工作。您的腳本需要向當前狀態(哪個分支和提交)查詢Git來選擇合適的SemVer值。

I blogged an example顯示如何掛接到MSBuild並更改AssemblyInfo文件,從中可以找到很多其他示例,工具和引用。