简体   繁体   中英

Pre-commit hook modifies AssemblyInfo

I have a pre-commit hook that defines the build number using ruby gem semver2 . The gem basically just creates a file called .semver that stores the version info for the package.

The hook generates a build number based on some date/commit parameters and then alters AssemblyInfo.cs with this information, then adds the altered file before the commit.

I have a few questions here:

  1. Is there danger in having a hook modify my AssemblyInfo file as far as .NET is concerned?

  2. Should this be done with the pre-commit hook, or a different hook?

  3. How can I tell this hook to behave differently on --amend , merge , and rebase commits?

  4. How can I tell this hook to behave differently on a branch by branch basis?

  5. Do you have a different solution to automating the build number?

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")]

My take.

  1. No, it is common practice to edit AssemblyInfo file during build just before compile
  2. The technique of a commit route is interesting but has some disadvantages: not all Git implementations guarantee that hooks work (think of Visual Studio Online) and that your scripts work.
  3. See #5.
  4. See #5.
  5. If you do the work of editing the AssemblyInfo files during build, it will work regardless. Your script need to inquiry Git for current status (which branch and commit) to pick the right SemVer values.

I blogged an example that shows how to hook into MSBuild and change AssemblyInfo files and from that you can find lot of other examples, tools and references.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM