Skip to content

Commit fcb7366

Browse files
committed
Merge pull request #354 from AArnott/fix346
Fix nano framework targeting
2 parents b613d84 + 024d067 commit fcb7366

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ namespace AssemblyInfo
3434
[<assembly: System.Reflection.AssemblyFileVersionAttribute(""1.3.1.0"")>]
3535
[<assembly: System.Reflection.AssemblyInformationalVersionAttribute("""")>]
3636
do()
37+
#if NETSTANDARD || NETFRAMEWORK || NETCOREAPP
3738
[<System.CodeDom.Compiler.GeneratedCode(""" + AssemblyVersionInfo.GeneratorName + @""",""" + AssemblyVersionInfo.GeneratorVersion + @""")>]
39+
#endif
3840
type internal ThisAssembly() =
3941
static member internal AssemblyVersion = ""1.3.0.0""
4042
static member internal AssemblyFileVersion = ""1.3.1.0""

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ public async Task AssemblyInfo(bool isVB, bool includeNonVersionAttributes, bool
799799
if (gitRepo)
800800
{
801801
Assert.True(long.TryParse(result.GitCommitDateTicks, out _), $"Invalid value for GitCommitDateTicks: '{result.GitCommitDateTicks}'");
802-
DateTimeOffset gitCommitDate = new DateTimeOffset(long.Parse(result.GitCommitDateTicks), TimeSpan.Zero);
802+
var gitCommitDate = new DateTime(long.Parse(result.GitCommitDateTicks), DateTimeKind.Utc);
803803
Assert.Equal(gitCommitDate, thisAssemblyClass.GetProperty("GitCommitDate", fieldFlags)?.GetValue(null) ?? thisAssemblyClass.GetField("GitCommitDate", fieldFlags)?.GetValue(null) ?? string.Empty);
804804
}
805805
else

src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
public class AssemblyVersionInfo : Task
1515
{
16+
/// <summary>
17+
/// The #if expression that surrounds a <see cref="GeneratedCodeAttribute"/> to avoid a compilation failure when targeting the nano framework.
18+
/// </summary>
19+
/// <remarks>
20+
/// See https://github.com/AArnott/Nerdbank.GitVersioning/issues/346
21+
/// </remarks>
22+
private const string CompilerDefinesAroundGeneratedCodeAttribute = "NETSTANDARD || NETFRAMEWORK || NETCOREAPP";
23+
1624
public static readonly string GeneratorName = ThisAssembly.AssemblyName;
1725
public static readonly string GeneratorVersion = ThisAssembly.AssemblyVersion;
1826
#if NET461
@@ -225,22 +233,22 @@ private static CodeMemberField CreateField(string name, string value)
225233

226234
private static IEnumerable<CodeTypeMember> CreateCommitDateProperty(long ticks)
227235
{
228-
// internal static System.DateTimeOffset GitCommitDate {{ get; }} = new System.DateTimeOffset({ticks}, System.TimeSpan.Zero);");
229-
yield return new CodeMemberField(typeof(DateTimeOffset), "gitCommitDate")
236+
// internal static System.DateTime GitCommitDate {{ get; }} = new System.DateTime({ticks}, System.DateTimeKind.Utc);");
237+
yield return new CodeMemberField(typeof(DateTime), "gitCommitDate")
230238
{
231239
Attributes = MemberAttributes.Private,
232240
InitExpression = new CodeObjectCreateExpression(
233-
typeof(DateTimeOffset),
241+
typeof(DateTime),
234242
new CodePrimitiveExpression(ticks),
235243
new CodePropertyReferenceExpression(
236-
new CodeTypeReferenceExpression(typeof(TimeSpan)),
237-
nameof(TimeSpan.Zero)))
244+
new CodeTypeReferenceExpression(typeof(DateTimeKind)),
245+
nameof(DateTimeKind.Utc)))
238246
};
239247

240248
var property = new CodeMemberProperty()
241249
{
242250
Attributes = MemberAttributes.Assembly,
243-
Type = new CodeTypeReference(typeof(DateTimeOffset)),
251+
Type = new CodeTypeReference(typeof(DateTime)),
244252
Name = "GitCommitDate",
245253
HasGet = true,
246254
HasSet = false,
@@ -463,7 +471,7 @@ internal override void DeclareAttribute(Type type, string arg)
463471

464472
internal override void AddCommitDateProperty(long ticks)
465473
{
466-
this.codeBuilder.AppendLine($" static member internal GitCommitDate = new System.DateTimeOffset({ticks}L, System.TimeSpan.Zero)");
474+
this.codeBuilder.AppendLine($" static member internal GitCommitDate = new System.DateTime({ticks}L, System.DateTimeKind.Utc)");
467475
}
468476

469477
internal override void EndThisAssemblyClass()
@@ -474,7 +482,9 @@ internal override void EndThisAssemblyClass()
474482
internal override void StartThisAssemblyClass()
475483
{
476484
this.codeBuilder.AppendLine("do()");
485+
this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}");
477486
this.codeBuilder.AppendLine($"[<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>]");
487+
this.codeBuilder.AppendLine("#endif");
478488
this.codeBuilder.AppendLine("type internal ThisAssembly() =");
479489
}
480490
}
@@ -493,7 +503,9 @@ internal override void DeclareAttribute(Type type, string arg)
493503

494504
internal override void StartThisAssemblyClass()
495505
{
506+
this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}");
496507
this.codeBuilder.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")]");
508+
this.codeBuilder.AppendLine("#endif");
497509
this.codeBuilder.AppendLine("internal static partial class ThisAssembly {");
498510
}
499511

@@ -504,7 +516,7 @@ internal override void AddThisAssemblyMember(string name, string value)
504516

505517
internal override void AddCommitDateProperty(long ticks)
506518
{
507-
this.codeBuilder.AppendLine($" internal static readonly System.DateTimeOffset GitCommitDate = new System.DateTimeOffset({ticks}, System.TimeSpan.Zero);");
519+
this.codeBuilder.AppendLine($" internal static readonly System.DateTime GitCommitDate = new System.DateTime({ticks}L, System.DateTimeKind.Utc);");
508520
}
509521

510522
internal override void EndThisAssemblyClass()
@@ -527,7 +539,9 @@ internal override void DeclareAttribute(Type type, string arg)
527539

528540
internal override void StartThisAssemblyClass()
529541
{
542+
this.codeBuilder.AppendLine($"#If {CompilerDefinesAroundGeneratedCodeAttribute.Replace("||", " Or ")} Then");
530543
this.codeBuilder.AppendLine($"<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>");
544+
this.codeBuilder.AppendLine("#End If");
531545
this.codeBuilder.AppendLine("Partial Friend NotInheritable Class ThisAssembly");
532546
}
533547

@@ -538,7 +552,7 @@ internal override void AddThisAssemblyMember(string name, string value)
538552

539553
internal override void AddCommitDateProperty(long ticks)
540554
{
541-
this.codeBuilder.AppendLine($" Friend Shared ReadOnly GitCommitDate As System.DateTimeOffset = New System.DateTimeOffset({ticks}, System.TimeSpan.Zero)");
555+
this.codeBuilder.AppendLine($" Friend Shared ReadOnly GitCommitDate As System.DateTime = New System.DateTime({ticks}L, System.DateTimeKind.Utc)");
542556
}
543557

544558
internal override void EndThisAssemblyClass()

0 commit comments

Comments
 (0)