Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitVersionInformationAttribute to make reflection easier [rebased] #531

Merged
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

using System;
using System;
using System.Reflection;

[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.3.4.0")]
[assembly: AssemblyInformationalVersion("2.3.4-beta.5+6.Branch.master.Sha.commitSha")]
[assembly: Fake.ReleaseDate("2014-03-06")]
[assembly: Fake.GitVersionInformation()]

namespace Fake
{
[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class ReleaseDateAttribute : System.Attribute
{
public string Date { get; private set; }
Expand Down Expand Up @@ -42,6 +43,30 @@ namespace Fake
public static string NuGetVersionV2 = "2.3.4-beta0005";
public static string NuGetVersion = "2.3.4-beta0005";
public static string CommitDate = "2014-03-06";
}

[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class GitVersionInformationAttribute : System.Attribute
{
public string Major { get { return "2"; } }
public string Minor { get { return "3"; } }
public string Patch { get { return "4"; } }
public string PreReleaseTag { get { return "beta.5"; } }
public string PreReleaseTagWithDash { get { return "-beta.5"; } }
public string BuildMetaData { get { return "6"; } }
public string FullBuildMetaData { get { return "6.Branch.master.Sha.commitSha"; } }
public string MajorMinorPatch { get { return "2.3.4"; } }
public string SemVer { get { return "2.3.4-beta.5"; } }
public string LegacySemVer { get { return "2.3.4-beta5"; } }
public string LegacySemVerPadded { get { return "2.3.4-beta0005"; } }
public string AssemblySemVer { get { return "2.0.0.0"; } }
public string FullSemVer { get { return "2.3.4-beta.5+6"; } }
public string InformationalVersion { get { return "2.3.4-beta.5+6.Branch.master.Sha.commitSha"; } }
public string BranchName { get { return "master"; } }
public string Sha { get { return "commitSha"; } }
public string NuGetVersionV2 { get { return "2.3.4-beta0005"; } }
public string NuGetVersion { get { return "2.3.4-beta0005"; } }
public string CommitDate { get { return "2014-03-06"; } }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

using System;
using System;
using System.Reflection;

[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyFileVersion("2.3.4.0")]
[assembly: AssemblyInformationalVersion("2.3.4-beta.5+6.Branch.master.Sha.commitSha")]
[assembly: Fake.ReleaseDate("2014-03-06")]
[assembly: Fake.GitVersionInformation()]

namespace Fake
{
[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class ReleaseDateAttribute : System.Attribute
{
public string Date { get; private set; }
Expand Down Expand Up @@ -42,6 +43,30 @@ namespace Fake
public static string NuGetVersionV2 = "2.3.4-beta0005";
public static string NuGetVersion = "2.3.4-beta0005";
public static string CommitDate = "2014-03-06";
}

[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class GitVersionInformationAttribute : System.Attribute
{
public string Major { get { return "2"; } }
public string Minor { get { return "3"; } }
public string Patch { get { return "4"; } }
public string PreReleaseTag { get { return "beta.5"; } }
public string PreReleaseTagWithDash { get { return "-beta.5"; } }
public string BuildMetaData { get { return "6"; } }
public string FullBuildMetaData { get { return "6.Branch.master.Sha.commitSha"; } }
public string MajorMinorPatch { get { return "2.3.4"; } }
public string SemVer { get { return "2.3.4-beta.5"; } }
public string LegacySemVer { get { return "2.3.4-beta5"; } }
public string LegacySemVerPadded { get { return "2.3.4-beta0005"; } }
public string AssemblySemVer { get { return "2.3.0.0"; } }
public string FullSemVer { get { return "2.3.4-beta.5+6"; } }
public string InformationalVersion { get { return "2.3.4-beta.5+6.Branch.master.Sha.commitSha"; } }
public string BranchName { get { return "master"; } }
public string Sha { get { return "commitSha"; } }
public string NuGetVersionV2 { get { return "2.3.4-beta0005"; } }
public string NuGetVersion { get { return "2.3.4-beta0005"; } }
public string CommitDate { get { return "2014-03-06"; } }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

using System;
using System;
using System.Reflection;

[assembly: AssemblyVersion("2.3.4.0")]
[assembly: AssemblyFileVersion("2.3.4.0")]
[assembly: AssemblyInformationalVersion("2.3.4-beta.5+6.Branch.master.Sha.commitSha")]
[assembly: Fake.ReleaseDate("2014-03-06")]
[assembly: Fake.GitVersionInformation()]

namespace Fake
{
[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class ReleaseDateAttribute : System.Attribute
{
public string Date { get; private set; }
Expand Down Expand Up @@ -42,6 +43,30 @@ namespace Fake
public static string NuGetVersionV2 = "2.3.4-beta0005";
public static string NuGetVersion = "2.3.4-beta0005";
public static string CommitDate = "2014-03-06";
}

[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class GitVersionInformationAttribute : System.Attribute
{
public string Major { get { return "2"; } }
public string Minor { get { return "3"; } }
public string Patch { get { return "4"; } }
public string PreReleaseTag { get { return "beta.5"; } }
public string PreReleaseTagWithDash { get { return "-beta.5"; } }
public string BuildMetaData { get { return "6"; } }
public string FullBuildMetaData { get { return "6.Branch.master.Sha.commitSha"; } }
public string MajorMinorPatch { get { return "2.3.4"; } }
public string SemVer { get { return "2.3.4-beta.5"; } }
public string LegacySemVer { get { return "2.3.4-beta5"; } }
public string LegacySemVerPadded { get { return "2.3.4-beta0005"; } }
public string AssemblySemVer { get { return "2.3.4.0"; } }
public string FullSemVer { get { return "2.3.4-beta.5+6"; } }
public string InformationalVersion { get { return "2.3.4-beta.5+6.Branch.master.Sha.commitSha"; } }
public string BranchName { get { return "master"; } }
public string Sha { get { return "commitSha"; } }
public string NuGetVersionV2 { get { return "2.3.4-beta0005"; } }
public string NuGetVersion { get { return "2.3.4-beta0005"; } }
public string CommitDate { get { return "2014-03-06"; } }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

using System;
using System;
using System.Reflection;

[assembly: AssemblyVersion("2.3.4.5")]
[assembly: AssemblyFileVersion("2.3.4.0")]
[assembly: AssemblyInformationalVersion("2.3.4-beta.5+6.Branch.master.Sha.commitSha")]
[assembly: Fake.ReleaseDate("2014-03-06")]
[assembly: Fake.GitVersionInformation()]

namespace Fake
{
[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class ReleaseDateAttribute : System.Attribute
{
public string Date { get; private set; }
Expand Down Expand Up @@ -42,6 +43,30 @@ namespace Fake
public static string NuGetVersionV2 = "2.3.4-beta0005";
public static string NuGetVersion = "2.3.4-beta0005";
public static string CommitDate = "2014-03-06";
}

[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class GitVersionInformationAttribute : System.Attribute
{
public string Major { get { return "2"; } }
public string Minor { get { return "3"; } }
public string Patch { get { return "4"; } }
public string PreReleaseTag { get { return "beta.5"; } }
public string PreReleaseTagWithDash { get { return "-beta.5"; } }
public string BuildMetaData { get { return "6"; } }
public string FullBuildMetaData { get { return "6.Branch.master.Sha.commitSha"; } }
public string MajorMinorPatch { get { return "2.3.4"; } }
public string SemVer { get { return "2.3.4-beta.5"; } }
public string LegacySemVer { get { return "2.3.4-beta5"; } }
public string LegacySemVerPadded { get { return "2.3.4-beta0005"; } }
public string AssemblySemVer { get { return "2.3.4.5"; } }
public string FullSemVer { get { return "2.3.4-beta.5+6"; } }
public string InformationalVersion { get { return "2.3.4-beta.5+6.Branch.master.Sha.commitSha"; } }
public string BranchName { get { return "master"; } }
public string Sha { get { return "commitSha"; } }
public string NuGetVersionV2 { get { return "2.3.4-beta0005"; } }
public string NuGetVersion { get { return "2.3.4-beta0005"; } }
public string CommitDate { get { return "2014-03-06"; } }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

using System;
using System;
using System.Reflection;

[assembly: AssemblyVersion("1.2.3.0")]
[assembly: AssemblyFileVersion("1.2.3.0")]
[assembly: AssemblyInformationalVersion("1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha")]
[assembly: Fake.ReleaseDate("2014-03-06")]
[assembly: Fake.GitVersionInformation()]

namespace Fake
{
[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class ReleaseDateAttribute : System.Attribute
{
public string Date { get; private set; }
Expand Down Expand Up @@ -42,6 +43,30 @@ namespace Fake
public static string NuGetVersionV2 = "1.2.3-unstable0004";
public static string NuGetVersion = "1.2.3-unstable0004";
public static string CommitDate = "2014-03-06";
}

[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class GitVersionInformationAttribute : System.Attribute
{
public string Major { get { return "1"; } }
public string Minor { get { return "2"; } }
public string Patch { get { return "3"; } }
public string PreReleaseTag { get { return "unstable.4"; } }
public string PreReleaseTagWithDash { get { return "-unstable.4"; } }
public string BuildMetaData { get { return "5"; } }
public string FullBuildMetaData { get { return "5.Branch.feature1.Sha.commitSha"; } }
public string MajorMinorPatch { get { return "1.2.3"; } }
public string SemVer { get { return "1.2.3-unstable.4"; } }
public string LegacySemVer { get { return "1.2.3-unstable4"; } }
public string LegacySemVerPadded { get { return "1.2.3-unstable0004"; } }
public string AssemblySemVer { get { return "1.2.3.0"; } }
public string FullSemVer { get { return "1.2.3-unstable.4+5"; } }
public string InformationalVersion { get { return "1.2.3-unstable.4+5.Branch.feature1.Sha.commitSha"; } }
public string BranchName { get { return "feature1"; } }
public string Sha { get { return "commitSha"; } }
public string NuGetVersionV2 { get { return "1.2.3-unstable0004"; } }
public string NuGetVersion { get { return "1.2.3-unstable0004"; } }
public string CommitDate { get { return "2014-03-06"; } }
}
}
43 changes: 40 additions & 3 deletions src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using ApprovalTests;
using GitVersion;
Expand Down Expand Up @@ -29,12 +30,20 @@ public void VerifyCreatedCode()
Approvals.Verify(assemblyInfoText);

var compilation = CSharpCompilation.Create("Fake.dll")
.WithOptions(new CSharpCompilationOptions(OutputKind.NetModule))
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(assemblyInfoText));

var emitResult = compilation.Emit(new MemoryStream());
Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Descriptor)));
using (var stream = new MemoryStream())
{
var emitResult = compilation.Emit(stream);

Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Descriptor)));

stream.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(stream.ToArray());
VerifyGitVersionInformationAttribute(assembly, versionVariables);
}
}

[Test]
Expand Down Expand Up @@ -89,4 +98,32 @@ static void VerifyAssemblyVersion(AssemblyVersioningScheme avs)
var emitResult = compilation.Emit(new MemoryStream());
Assert.IsTrue(emitResult.Success, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(x => x.Descriptor)));
}

static void VerifyGitVersionInformationAttribute(Assembly assembly, VersionVariables versionVariables)
{
var gitVersionInformationAttributeData = assembly.CustomAttributes
.FirstOrDefault(a => a.AttributeType.Name == "GitVersionInformationAttribute");

Assert.IsNotNull(gitVersionInformationAttributeData);

var gitVersionInformationAttributeType = gitVersionInformationAttributeData.AttributeType;
var gitVersionInformationAttribute = assembly
.GetCustomAttributes(gitVersionInformationAttributeType)
.FirstOrDefault();

Assert.IsNotNull(gitVersionInformationAttribute);

var properties = gitVersionInformationAttributeType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

foreach (var variable in versionVariables)
{
Assert.IsNotNull(variable.Value);

var property = properties.FirstOrDefault(p => p.Name == variable.Key);
Assert.IsNotNull(property);

var propertyValue = property.GetValue(gitVersionInformationAttribute, null);
Assert.AreEqual(variable.Value, propertyValue, "{0} had an invalid value.", property.Name);
}
}
}
46 changes: 32 additions & 14 deletions src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using GitVersion;

public class AssemblyInfoBuilder
{
public string GetAssemblyInfoText(VersionVariables vars, string assemblyName)
{
var assemblyInfo = string.Format(@"
using System;
var v = vars.ToList();

var assemblyInfo = string.Format(
@"using System;
using System.Reflection;

[assembly: AssemblyVersion(""{0}"")]
[assembly: AssemblyFileVersion(""{1}"")]
[assembly: AssemblyInformationalVersion(""{2}"")]
[assembly: {5}.ReleaseDate(""{3}"")]
[assembly: {6}.ReleaseDate(""{3}"")]
[assembly: {6}.GitVersionInformation()]

namespace {5}
namespace {6}
{{
[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class ReleaseDateAttribute : System.Attribute
{{
public string Date {{ get; private set; }}
Expand All @@ -33,26 +38,39 @@ static class GitVersionInformation
{{
{4}
}}

[System.Runtime.CompilerServices.CompilerGenerated]
[AttributeUsage(AttributeTargets.Assembly)]
sealed class GitVersionInformationAttribute : System.Attribute
{{
{5}
}}
}}
",
vars.AssemblySemVer,
vars.MajorMinorPatch + ".0",
vars.InformationalVersion,
vars.CommitDate,
GenerateVariableMembers(vars),
GenerateStaticVariableMembers(v),
GenerateAttributeVariableMembers(v),
assemblyName);

return assemblyInfo;
}

string GenerateVariableMembers(IEnumerable<KeyValuePair<string, string>> vars)
static string GenerateStaticVariableMembers(IList<KeyValuePair<string, string>> vars)
{
var members = new StringBuilder();
foreach (var variable in vars)
{
members.AppendLine(string.Format(" public static string {0} = \"{1}\";", variable.Key, variable.Value));
}
return GenerateMembers(vars, " public static string {0} = \"{1}\";");
}

return members.ToString();

static string GenerateAttributeVariableMembers(IList<KeyValuePair<string, string>> vars)
{
return GenerateMembers(vars, " public string {0} {{ get {{ return \"{1}\"; }} }}");
}

static string GenerateMembers(IList<KeyValuePair<string, string>> vars, string memberFormat)
{
return string.Join(Environment.NewLine, vars.Select(v => string.Format(memberFormat, v.Key, v.Value)));
}
}