diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_Major.approved.txt b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_Major.approved.txt index c1047738fc..3f1eccb9ac 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_Major.approved.txt +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_Major.approved.txt @@ -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; } @@ -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"; } } } } diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinor.approved.txt b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinor.approved.txt index 4bd9fc75a1..24aa7e48fd 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinor.approved.txt +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinor.approved.txt @@ -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; } @@ -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"; } } } } diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatch.approved.txt b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatch.approved.txt index cbd123be6d..cf12fed4c2 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatch.approved.txt +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatch.approved.txt @@ -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; } @@ -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"; } } } } diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatchTag.approved.txt b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatchTag.approved.txt index cf4392da2d..4e3625433d 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatchTag.approved.txt +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyAssemblyVersion_MajorMinorPatchTag.approved.txt @@ -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; } @@ -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"; } } } } diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyCreatedCode.approved.txt b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyCreatedCode.approved.txt index f0c6488df1..08e0314438 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyCreatedCode.approved.txt +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.VerifyCreatedCode.approved.txt @@ -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; } @@ -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"; } } } } diff --git a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs index 7f9dc7969d..8b985cc287 100644 --- a/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs +++ b/src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.CompilerServices; using ApprovalTests; using GitVersion; @@ -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] @@ -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); + } + } } \ No newline at end of file diff --git a/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs b/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs index f6e5919ea9..6b36578f72 100644 --- a/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs +++ b/src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs @@ -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; }} @@ -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> vars) + static string GenerateStaticVariableMembers(IList> 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> vars) + { + return GenerateMembers(vars, " public string {0} {{ get {{ return \"{1}\"; }} }}"); + } + + static string GenerateMembers(IList> vars, string memberFormat) + { + return string.Join(Environment.NewLine, vars.Select(v => string.Format(memberFormat, v.Key, v.Value))); } } \ No newline at end of file