diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-12-28 21:33:29 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-12-28 21:33:29 -0800 |
commit | 1ca88627f679962809d7908a185f4561dd39b61f (patch) | |
tree | b6e7d665b28edb0d6a87928b3f2037de6f111ae3 /src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs | |
parent | 0dae9278469fa1511df1d73ffa6d03d92fad0973 (diff) | |
parent | 3f37385b653be0c753d61819b6ce8e50b8d06ec4 (diff) | |
download | DotNetOpenAuth-1ca88627f679962809d7908a185f4561dd39b61f.zip DotNetOpenAuth-1ca88627f679962809d7908a185f4561dd39b61f.tar.gz DotNetOpenAuth-1ca88627f679962809d7908a185f4561dd39b61f.tar.bz2 |
Merge branch 'master' into mvcProjTemplate
Diffstat (limited to 'src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs')
-rw-r--r-- | src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs b/src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs index e40eb78..48b5d5c 100644 --- a/src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs +++ b/src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs @@ -16,16 +16,29 @@ namespace DotNetOpenAuth.BuildTasks { public string Version { get; private set; } /// <summary> + /// Gets the Git revision control commit id for HEAD (the current source code version). + /// </summary> + [Output] + public string GitCommitId { get; private set; } + + /// <summary> /// The file that contains the version base (Major.Minor.Build) to use. /// </summary> [Required] public string VersionFile { get; set; } + /// <summary> + /// Gets or sets the parent directory of the .git directory. + /// </summary> + public string GitRepoRoot { get; set; } + public override bool Execute() { try { Version typedVersion = ReadVersionFromFile(); typedVersion = new Version(typedVersion.Major, typedVersion.Minor, typedVersion.Build, CalculateJDate(DateTime.Now)); Version = typedVersion.ToString(); + + this.GitCommitId = GetGitHeadCommitId(); } catch (ArgumentOutOfRangeException ex) { Log.LogErrorFromException(ex); return false; @@ -34,6 +47,25 @@ namespace DotNetOpenAuth.BuildTasks { return true; } + private string GetGitHeadCommitId() { + if (string.IsNullOrEmpty(this.GitRepoRoot)) { + return string.Empty; + } + + string headContent = string.Empty; + try { + headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/HEAD")).Trim(); + if (headContent.StartsWith("ref:", StringComparison.Ordinal)) { + string refName = headContent.Substring(5).Trim(); + headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/" + refName)).Trim(); + } + } catch (FileNotFoundException) { + } catch (DirectoryNotFoundException) { + } + + return headContent.Trim(); + } + private Version ReadVersionFromFile() { string[] lines = File.ReadAllLines(VersionFile); string versionLine = lines[0]; @@ -41,7 +73,7 @@ namespace DotNetOpenAuth.BuildTasks { } private int CalculateJDate(DateTime date) { - int yearLastDigit = date.Year % 10; + int yearLastDigit = date.Year - 2000; // can actually be two digits in or after 2010 DateTime firstOfYear = new DateTime(date.Year, 1, 1); int dayOfYear = (date - firstOfYear).Days + 1; int jdate = yearLastDigit * 1000 + dayOfYear; |