Skip to content

Commit

Permalink
Add Generated Code comment and attribute (#215)
Browse files Browse the repository at this point in the history
* Add autogenerated indicators

Adds autogenerated comment to the beginning of generated files
Adds GeneratedCodeAttribute before generated methods

Fixes #214

* Update Directory.Build.props

---------

Co-authored-by: Domn Werner <domn.werner@gmail.com>
  • Loading branch information
hwoodiwiss and domn1995 committed Jan 21, 2024
1 parent 02a5218 commit 7856eb0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public static string GenerateExtensions(UnionDeclaration union)
}

return new StringBuilder()
.AppendAutoGeneratedComment()
.AppendLine("#pragma warning disable 1591")
.AppendUsingStatements(union)
.AppendLine()
.AppendLine($"namespace {union.Namespace};")
.AppendLine()
.AppendGeneratedCodeAttribute()
.AppendExtensionClassDeclaration(union)
.AppendLine("{")
.AppendMatchAsyncMethodForFuncs(union, task)
Expand Down
28 changes: 27 additions & 1 deletion src/Dunet.Generator/UnionGeneration/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Reflection;
using System.Text;

namespace Dunet.Generator.UnionGeneration;

Expand Down Expand Up @@ -41,4 +42,29 @@ public static void AppendFullUnionName(this StringBuilder builder, UnionDeclarat

builder.Append(union.Name);
}

public static StringBuilder AppendAutoGeneratedComment(this StringBuilder builder)
{
builder.AppendLine("// <auto-generated/>");
return builder;
}

/// <summary>
/// Appends the GeneratedCodeAttribute, to better enable the code to be detected as generated.
/// </summary>
/// <param name="builder">The string builder to append to.</param>
public static StringBuilder AppendGeneratedCodeAttribute(this StringBuilder builder)
{
builder.AppendLine($"[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"Dunet.Generator\", \"{GeneratorVersion}\")]");
return builder;
}

private static string? GeneratorVersion { get; } = GetGeneratorVersion();

private static string? GetGeneratorVersion()
{
var assembly = typeof(UnionGenerator).Assembly;
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return attribute?.InformationalVersion;
}
}
3 changes: 3 additions & 0 deletions src/Dunet.Generator/UnionGeneration/UnionSourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static string Build(UnionDeclaration union)
{
var builder = new StringBuilder();

builder.AppendAutoGeneratedComment();

builder.AppendLine("#pragma warning disable 1591");

foreach (var import in union.Imports)
Expand All @@ -28,6 +30,7 @@ public static string Build(UnionDeclaration union)
builder.AppendLine("{");
}

builder.AppendGeneratedCodeAttribute();
builder.Append($"abstract partial record {union.Name}");
builder.AppendTypeParams(union.TypeParameters);
builder.AppendLine();
Expand Down

0 comments on commit 7856eb0

Please sign in to comment.