Skip to content

Commit

Permalink
feature: add version to generated source (#1186)
Browse files Browse the repository at this point in the history
* feature: add version to generated source

* Automatically linting code

---------

Co-authored-by: Rocket Understudy <[email protected]>
  • Loading branch information
RLittlesII and rsg-bot authored Apr 1, 2024
1 parent 7bd3107 commit 6f364ca
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
15 changes: 8 additions & 7 deletions src/Testing.AutoFixtures/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ namespace Rocket.Surgery.Extensions.Testing.AutoFixtures;

internal static class Attribute
{
public const string Source = @"#nullable enable
public static readonly string source = @$"#nullable enable
using System;
using System.Diagnostics;
namespace Rocket.Surgery.Extensions.Testing.AutoFixtures
{
{{
{AutoFixtures.CodeGenerationAttribute}
[AttributeUsage(AttributeTargets.Class)]
[Conditional(""CODEGEN"")]
internal class AutoFixtureAttribute : Attribute
{
public AutoFixtureAttribute() : this(null) {}
{{
public AutoFixtureAttribute() : this(null) {{}}
public AutoFixtureAttribute(Type? type) => Type = type;
public Type? Type { get; }
}
}";
public Type? Type {{ get; }}
}}
}}";
}
12 changes: 7 additions & 5 deletions src/Testing.AutoFixtures/AutoFixtureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterPostInitializationOutput(
initializationContext =>
{
initializationContext.AddSource("AutoFixtureAttribute.g.cs", Attribute.Source);
initializationContext.AddSource("AutoFixtureAttribute.g.cs", Attribute.source);
initializationContext.AddSource($"{nameof(AutoFixtureBase)}.g.cs", AutoFixtureBase.Source);
}
);
Expand Down Expand Up @@ -108,10 +108,12 @@ void generateFixtureBuilder(
usingDirectives.Add(substituteMetadata.ContainingNamespace.ToDisplayString());
}

var usingDirectiveSyntax = usingDirectives
.OrderBy(usingDirective => usingDirective, NamespaceComparer.Default)
.Select(x => UsingDirective(ParseName(x)))
.ToArray();
var usingDirectiveSyntax =
usingDirectives
.OrderBy(usingDirective => usingDirective, NamespaceComparer.Default)
.Select(x => UsingDirective(ParseName(x)))
.ToArray();

var unit =
CompilationUnit()
.AddUsings(usingDirectiveSyntax)
Expand Down
9 changes: 9 additions & 0 deletions src/Testing.AutoFixtures/AutoFixtures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Diagnostics;

namespace Rocket.Surgery.Extensions.Testing.AutoFixtures;

internal static class AutoFixtures
{
public static string Version { get; } = FileVersionInfo.GetVersionInfo(typeof(AutoFixtureGenerator).Assembly.Location).ProductVersion;
public static string CodeGenerationAttribute { get; } = $@"[System.CodeDom.Compiler.GeneratedCode(""AutoFixtures"", ""{Version}"")]";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task GivenAutoFixture_WhenGenerate_ThenShouldGenerateAutoFixtureAtt
var result = await generatorInstance.GenerateAsync();

// Then
await Verify(result);
await Verify(result).ScrubLines(text => text.Contains("System.CodeDom.Compiler.GeneratedCode"));
}

[Fact]
Expand Down

0 comments on commit 6f364ca

Please sign in to comment.