Skip to content

Commit

Permalink
Merge pull request #354 from AArnott/fix346
Browse files Browse the repository at this point in the history
Fix nano framework targeting
  • Loading branch information
AArnott committed Jun 11, 2019
2 parents b613d84 + 024d067 commit fcb7366
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/NerdBank.GitVersioning.Tests/AssemblyInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ namespace AssemblyInfo
[<assembly: System.Reflection.AssemblyFileVersionAttribute(""1.3.1.0"")>]
[<assembly: System.Reflection.AssemblyInformationalVersionAttribute("""")>]
do()
#if NETSTANDARD || NETFRAMEWORK || NETCOREAPP
[<System.CodeDom.Compiler.GeneratedCode(""" + AssemblyVersionInfo.GeneratorName + @""",""" + AssemblyVersionInfo.GeneratorVersion + @""")>]
#endif
type internal ThisAssembly() =
static member internal AssemblyVersion = ""1.3.0.0""
static member internal AssemblyFileVersion = ""1.3.1.0""
Expand Down
2 changes: 1 addition & 1 deletion src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 23 additions & 9 deletions src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

public class AssemblyVersionInfo : Task
{
/// <summary>
/// The #if expression that surrounds a <see cref="GeneratedCodeAttribute"/> to avoid a compilation failure when targeting the nano framework.
/// </summary>
/// <remarks>
/// See https://github.com/AArnott/Nerdbank.GitVersioning/issues/346
/// </remarks>
private const string CompilerDefinesAroundGeneratedCodeAttribute = "NETSTANDARD || NETFRAMEWORK || NETCOREAPP";

public static readonly string GeneratorName = ThisAssembly.AssemblyName;
public static readonly string GeneratorVersion = ThisAssembly.AssemblyVersion;
#if NET461
Expand Down Expand Up @@ -225,22 +233,22 @@ private static CodeMemberField CreateField(string name, string value)

private static IEnumerable<CodeTypeMember> 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,
Expand Down Expand Up @@ -463,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()
Expand All @@ -474,7 +482,9 @@ internal override void EndThisAssemblyClass()
internal override void StartThisAssemblyClass()
{
this.codeBuilder.AppendLine("do()");
this.codeBuilder.AppendLine($"#if {CompilerDefinesAroundGeneratedCodeAttribute}");
this.codeBuilder.AppendLine($"[<System.CodeDom.Compiler.GeneratedCode(\"{GeneratorName}\",\"{GeneratorVersion}\")>]");
this.codeBuilder.AppendLine("#endif");
this.codeBuilder.AppendLine("type internal ThisAssembly() =");
}
}
Expand All @@ -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 {");
}

Expand All @@ -504,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()
Expand All @@ -527,7 +539,9 @@ internal override void DeclareAttribute(Type type, string arg)

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

Expand All @@ -538,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()
Expand Down

0 comments on commit fcb7366

Please sign in to comment.