Last active
August 29, 2015 14:27
-
-
Save sstorie/7ac04dd30a823257fb9f to your computer and use it in GitHub Desktop.
A simple class to expose the compiled attributes created by GitVersionTask easier to consume in .NET code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace GitVersionInfo | |
/// <summary> | |
/// A simple wrapper to make the compiled GitVersionTask variables easily accessible. | |
/// | |
/// Here is an example set of values | |
/// { | |
/// "Major":1, | |
/// "Minor":3, | |
/// "Patch":0, | |
/// "PreReleaseTag":"unstable.7", | |
/// "PreReleaseTagWithDash":"-unstable.7", | |
/// "BuildMetaData":7, | |
/// "FullBuildMetaData":"7.Branch.develop.Sha.52efa3f2fcd6fac8d4eade31992ea770380ac1d6", | |
/// "MajorMinorPatch":"1.3.0", | |
/// "SemVer":"1.3.0-unstable.7", | |
/// "LegacySemVer":"1.3.0-unstable7", | |
/// "LegacySemVerPadded":"1.3.0-unstable0007", | |
/// "AssemblySemVer":"1.3.0.0", | |
/// "FullSemVer":"1.3.0-unstable.7", | |
/// "InformationalVersion":"1.3.0-unstable.7+Branch.develop.Sha.52efa3f2fcd6fac8d4eade31992ea770380ac1d6", | |
/// "BranchName":"develop", | |
/// "Sha":"52efa3f2fcd6fac8d4eade31992ea770380ac1d6", | |
/// "NuGetVersionV2":"1.3.0-unstable0007", | |
/// "NuGetVersion":"1.3.0-unstable0007", | |
/// "CommitDate":"2015-08-18" | |
///} | |
/// </summary> | |
public class GitVersionInfo | |
{ | |
/// <summary> | |
/// The Major version | |
/// </summary> | |
public string Major { get; set; } | |
/// <summary> | |
/// The Minor version | |
/// </summary> | |
public string Minor { get; set; } | |
/// <summary> | |
/// The Patch version | |
/// </summary> | |
public string Patch { get; set; } | |
/// <summary> | |
/// The pre-release tag | |
/// </summary> | |
public string PreReleaseTag { get; set; } | |
/// <summary> | |
/// The pre-release tag with a dash | |
/// </summary> | |
public string PreReleaseTagWithDash { get; set; } | |
/// <summary> | |
/// The build meta data value (should be the number of commits ahead of the branch creation) | |
/// </summary> | |
public string BuildMetaData { get; set; } | |
/// <summary> | |
/// The full build meta data string (includes the Sha) | |
/// </summary> | |
public string FullBuildMetaData { get; set; } | |
/// <summary> | |
/// The Major.Minor.Patch value | |
/// </summary> | |
public string MajorMinorPatch { get; set; } | |
/// <summary> | |
/// The semver value | |
/// </summary> | |
public string SemVer { get; set; } | |
/// <summary> | |
/// The legacy semver value | |
/// </summary> | |
public string LegacySemVer { get; set; } | |
/// <summary> | |
/// The legacy semver value with padded zeros before the metadata value | |
/// </summary> | |
public string LegacySemVerPadded { get; set; } | |
/// <summary> | |
/// The Assembly semver value | |
/// </summary> | |
public string AssemblySemVer { get; set; } | |
/// <summary> | |
/// The full semver value | |
/// </summary> | |
public string FullSemVer { get; set; } | |
/// <summary> | |
/// The full length version that includes all available metadata | |
/// </summary> | |
public string InformationalVersion { get; set; } | |
/// <summary> | |
/// The name of the branch the version was generated from | |
/// </summary> | |
public string BranchName { get; set; } | |
/// <summary> | |
/// The full length Sha | |
/// </summary> | |
public string Sha { get; set; } | |
/// <summary> | |
/// The Nuget version designed to meet the needs of NuGet v2 | |
/// </summary> | |
public string NuGetVersionV2 { get; set; } | |
/// <summary> | |
/// The nuget version designed to meet the needs of NuGet v3 | |
/// </summary> | |
public string NuGetVersion { get; set; } | |
/// <summary> | |
/// The date the last committ occurred | |
/// </summary> | |
public string CommitDate { get; set; } | |
/// <summary> | |
/// Returns the full semver with the short git hash appended. For example: 1.3.0-unstable.7+12.52efa3f | |
/// </summary> | |
public string FullSemVerWithShortHash | |
{ | |
get | |
{ | |
return string.Format("{0}.{1}", FullSemVer, Sha.Substring(0, 7)); | |
} | |
} | |
} | |
/// <summary> | |
/// A collection of extensions to help populating a GitVersionInfo instance | |
/// </summary> | |
public static class GitVersionInfoExtensions | |
{ | |
/// <summary> | |
/// Populate all fields from the currently executing assembly. | |
/// </summary> | |
public static GitVersionInfo PopulateFromExecutingAssembly(this GitVersionInfo instance) | |
{ | |
// This approach has been taken from the GitVersion docs for their | |
// NuGet package | |
// https://gitversion.readthedocs.org/en/latest/usage/#nuget-library | |
// | |
var assembly = Assembly.GetExecutingAssembly(); | |
return instance.PopulateFromAssembly(assembly); | |
} | |
/// <summary> | |
/// Populate all fields from an existing assembly. Note, this assembly | |
/// must have been built using GitVersionTask! | |
/// </summary> | |
/// <param name="instance"></param> | |
/// <param name="assembly"></param> | |
public static GitVersionInfo PopulateFromAssembly(this GitVersionInfo instance, Assembly assembly) | |
{ | |
// This approach has been taken from the GitVersion docs for their | |
// NuGet package | |
// https://gitversion.readthedocs.org/en/latest/usage/#nuget-library | |
// | |
var assemblyName = assembly.GetName().Name; | |
var gitVersionInformationType = assembly.GetType(assemblyName + ".GitVersionInformation"); | |
instance.Major = (string)gitVersionInformationType.GetField("Major").GetValue(null); | |
instance.Minor = (string)gitVersionInformationType.GetField("Minor").GetValue(null); | |
instance.Patch = (string)gitVersionInformationType.GetField("Patch").GetValue(null); | |
instance.PreReleaseTag = (string)gitVersionInformationType.GetField("PreReleaseTag").GetValue(null); | |
instance.PreReleaseTagWithDash = (string)gitVersionInformationType.GetField("PreReleaseTagWithDash").GetValue(null); | |
instance.BuildMetaData = (string)gitVersionInformationType.GetField("BuildMetaData").GetValue(null); | |
instance.FullBuildMetaData = (string)gitVersionInformationType.GetField("FullBuildMetaData").GetValue(null); | |
instance.MajorMinorPatch = (string)gitVersionInformationType.GetField("MajorMinorPatch").GetValue(null); | |
instance.SemVer = (string)gitVersionInformationType.GetField("SemVer").GetValue(null); | |
instance.LegacySemVer = (string)gitVersionInformationType.GetField("LegacySemVer").GetValue(null); | |
instance.LegacySemVerPadded = (string)gitVersionInformationType.GetField("LegacySemVerPadded").GetValue(null); | |
instance.AssemblySemVer = (string)gitVersionInformationType.GetField("AssemblySemVer").GetValue(null); | |
instance.FullSemVer = (string)gitVersionInformationType.GetField("FullSemVer").GetValue(null); | |
instance.InformationalVersion = (string)gitVersionInformationType.GetField("InformationalVersion").GetValue(null); | |
instance.BranchName = (string)gitVersionInformationType.GetField("BranchName").GetValue(null); | |
instance.Sha = (string)gitVersionInformationType.GetField("Sha").GetValue(null); | |
instance.NuGetVersionV2 = (string)gitVersionInformationType.GetField("NuGetVersionV2").GetValue(null); | |
instance.NuGetVersion = (string)gitVersionInformationType.GetField("NuGetVersion").GetValue(null); | |
instance.CommitDate = (string)gitVersionInformationType.GetField("CommitDate").GetValue(null); | |
return instance; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment