预提交钩子修改AssemblyInfo

时间:2014-06-05 16:16:56

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

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

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

我在这里有几个问题:

  1. 就.NET而言,有一个钩子修改我的AssemblyInfo文件有危险吗?

  2. 是否应该使用预提交挂钩或其他钩子来完成?

  3. 如何判断此挂钩在--amendmergerebase提交时的行为方式不同?

  4. 如何在逐个分支的基础上告诉此钩子的行为不同?

  5. 您是否有自动化内部版本号的不同解决方案?

  6. The 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 个答案:

答案 0 :(得分:5)

我的看法。

  1. 不,通常的做法是在编译之前编译AssemblyInfo文件
  2. 提交路由的技术很有意思但有一些缺点:并非所有Git实现都能保证钩子的工作(想想Visual Studio Online)以及脚本的工作原理。
  3. 见#5。
  4. 见#5。
  5. 如果您在构建期间执行编辑AssemblyInfo文件的工作,则无论如何都可以。您的脚本需要向Git查询当前状态(分支和提交)以选择正确的SemVer值。
  6. I blogged an example显示如何挂钩到MSBuild并更改AssemblyInfo文件,从中可以找到许多其他示例,工具和参考。