From 065592942ae5a2265d02e82dce3f6e3d79d24b72 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Tue, 11 Jun 2019 01:24:05 -0600 Subject: [PATCH 1/2] Avoid emitting GeneratedCodeAttribute on nano framework --- .../AssemblyVersionInfo.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs b/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs index 193b25e6..99d729d8 100644 --- a/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs +++ b/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs @@ -13,6 +13,14 @@ public class AssemblyVersionInfo : Task { + /// + /// The #if expression that surrounds a to avoid a compilation failure when targeting the nano framework. + /// + /// + /// See https://github.com/AArnott/Nerdbank.GitVersioning/issues/346 + /// + private const string CompilerDefinesAroundGeneratedCodeAttribute = "NETSTANDARD || NETFRAMEWORK || NETCOREAPP"; + public static readonly string GeneratorName = ThisAssembly.AssemblyName; public static readonly string GeneratorVersion = ThisAssembly.AssemblyVersion; #if NET461 @@ -474,7 +482,9 @@ internal override void EndThisAssemblyClass() internal override void StartThisAssemblyClass() { this.codeBuilder.AppendLine("do()"); + this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}"); this.codeBuilder.AppendLine($"[]"); + this.codeBuilder.AppendLine("#endif"); this.codeBuilder.AppendLine("type internal ThisAssembly() ="); } } @@ -493,7 +503,9 @@ internal override void DeclareAttribute(Type type, string arg) internal override void StartThisAssemblyClass() { + this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}"); this.codeBuilder.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")]"); + this.codeBuilder.AppendLine("#endif"); this.codeBuilder.AppendLine("internal static partial class ThisAssembly {"); } @@ -527,7 +539,9 @@ internal override void DeclareAttribute(Type type, string arg) internal override void StartThisAssemblyClass() { + this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}"); this.codeBuilder.AppendLine($""); + this.codeBuilder.AppendLine("#endif"); this.codeBuilder.AppendLine("Partial Friend NotInheritable Class ThisAssembly"); } From 024d067ec14185acd44f143bae22dd8b4464256a Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Tue, 11 Jun 2019 01:29:05 -0600 Subject: [PATCH 2/2] Use DateTime instead of DateTimeOffset in ThisAssembly This avoids a compile break when targeting nano framework. --- .../AssemblyInfoTest.cs | 2 ++ .../BuildIntegrationTests.cs | 2 +- .../AssemblyVersionInfo.cs | 22 +++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs b/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs index 52204c58..a531638e 100644 --- a/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs +++ b/src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs @@ -34,7 +34,9 @@ namespace AssemblyInfo [] [] do() +#if NETSTANDARD || NETFRAMEWORK || NETCOREAPP [] +#endif type internal ThisAssembly() = static member internal AssemblyVersion = ""1.3.0.0"" static member internal AssemblyFileVersion = ""1.3.1.0"" diff --git a/src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs b/src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs index 34fba7a9..d7f08e01 100644 --- a/src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs +++ b/src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs @@ -799,7 +799,7 @@ public async Task AssemblyInfo(bool isVB, bool includeNonVersionAttributes, bool if (gitRepo) { Assert.True(long.TryParse(result.GitCommitDateTicks, out _), $"Invalid value for GitCommitDateTicks: '{result.GitCommitDateTicks}'"); - DateTimeOffset gitCommitDate = new DateTimeOffset(long.Parse(result.GitCommitDateTicks), TimeSpan.Zero); + var gitCommitDate = new DateTime(long.Parse(result.GitCommitDateTicks), DateTimeKind.Utc); Assert.Equal(gitCommitDate, thisAssemblyClass.GetProperty("GitCommitDate", fieldFlags)?.GetValue(null) ?? thisAssemblyClass.GetField("GitCommitDate", fieldFlags)?.GetValue(null) ?? string.Empty); } else diff --git a/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs b/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs index 99d729d8..c72733b4 100644 --- a/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs +++ b/src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs @@ -233,22 +233,22 @@ private static CodeMemberField CreateField(string name, string value) private static IEnumerable CreateCommitDateProperty(long ticks) { - // internal static System.DateTimeOffset GitCommitDate {{ get; }} = new System.DateTimeOffset({ticks}, System.TimeSpan.Zero);"); - yield return new CodeMemberField(typeof(DateTimeOffset), "gitCommitDate") + // internal static System.DateTime GitCommitDate {{ get; }} = new System.DateTime({ticks}, System.DateTimeKind.Utc);"); + yield return new CodeMemberField(typeof(DateTime), "gitCommitDate") { Attributes = MemberAttributes.Private, InitExpression = new CodeObjectCreateExpression( - typeof(DateTimeOffset), + typeof(DateTime), new CodePrimitiveExpression(ticks), new CodePropertyReferenceExpression( - new CodeTypeReferenceExpression(typeof(TimeSpan)), - nameof(TimeSpan.Zero))) + new CodeTypeReferenceExpression(typeof(DateTimeKind)), + nameof(DateTimeKind.Utc))) }; var property = new CodeMemberProperty() { Attributes = MemberAttributes.Assembly, - Type = new CodeTypeReference(typeof(DateTimeOffset)), + Type = new CodeTypeReference(typeof(DateTime)), Name = "GitCommitDate", HasGet = true, HasSet = false, @@ -471,7 +471,7 @@ internal override void DeclareAttribute(Type type, string arg) internal override void AddCommitDateProperty(long ticks) { - this.codeBuilder.AppendLine($" static member internal GitCommitDate = new System.DateTimeOffset({ticks}L, System.TimeSpan.Zero)"); + this.codeBuilder.AppendLine($" static member internal GitCommitDate = new System.DateTime({ticks}L, System.DateTimeKind.Utc)"); } internal override void EndThisAssemblyClass() @@ -516,7 +516,7 @@ internal override void AddThisAssemblyMember(string name, string value) internal override void AddCommitDateProperty(long ticks) { - this.codeBuilder.AppendLine($" internal static readonly System.DateTimeOffset GitCommitDate = new System.DateTimeOffset({ticks}, System.TimeSpan.Zero);"); + this.codeBuilder.AppendLine($" internal static readonly System.DateTime GitCommitDate = new System.DateTime({ticks}L, System.DateTimeKind.Utc);"); } internal override void EndThisAssemblyClass() @@ -539,9 +539,9 @@ internal override void DeclareAttribute(Type type, string arg) internal override void StartThisAssemblyClass() { - this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}"); + this.codeBuilder.AppendLine($"#If {CompilerDefinesAroundGeneratedCodeAttribute.Replace("||", " Or ")} Then"); this.codeBuilder.AppendLine($""); - this.codeBuilder.AppendLine("#endif"); + this.codeBuilder.AppendLine("#End If"); this.codeBuilder.AppendLine("Partial Friend NotInheritable Class ThisAssembly"); } @@ -552,7 +552,7 @@ internal override void AddThisAssemblyMember(string name, string value) internal override void AddCommitDateProperty(long ticks) { - this.codeBuilder.AppendLine($" Friend Shared ReadOnly GitCommitDate As System.DateTimeOffset = New System.DateTimeOffset({ticks}, System.TimeSpan.Zero)"); + this.codeBuilder.AppendLine($" Friend Shared ReadOnly GitCommitDate As System.DateTime = New System.DateTime({ticks}L, System.DateTimeKind.Utc)"); } internal override void EndThisAssemblyClass()