Skip to content

Commit

Permalink
Merge pull request #52 from crwsolutions/master
Browse files Browse the repository at this point in the history
Also generate the IgnoreAutomaticInterfaceAttribute
  • Loading branch information
ChristianSauer authored Aug 10, 2024
2 parents a66b969 + 61e0742 commit e15d657
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace AutomaticInterface;

Expand All @@ -16,31 +15,8 @@ public class AutomaticInterfaceGenerator : IIncrementalGenerator

public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(static postInitializationContext =>
{
postInitializationContext.AddSource(
$"{DefaultAttributeName}.Attribute.g.cs",
SourceText.From(
$$$"""
// <auto-generated />
using System;
namespace AutomaticInterface
{
/// <summary>
/// Use source generator to automatically create a Interface from this class
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
internal sealed class {{{DefaultAttributeName}}}Attribute : Attribute
{
internal {{{DefaultAttributeName}}}Attribute(string namespaceName = "") { }
}
}
""",
Encoding.UTF8
)
);
});
context.RegisterDefaultAttribute();
context.RegisterIgnoreAttribute();

var classes = context
.SyntaxProvider.CreateSyntaxProvider(CouldBeClassWithInterfaceAttribute, Transform)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace AutomaticInterface;

internal static class RegisterAttributesExtensions
{
public static IncrementalGeneratorInitializationContext RegisterDefaultAttribute(
this IncrementalGeneratorInitializationContext context
)
{
context.RegisterPostInitializationOutput(static postInitializationContext =>
{
postInitializationContext.AddSource(
$"{AutomaticInterfaceGenerator.DefaultAttributeName}.Attribute.g.cs",
SourceText.From(
$$$"""
// <auto-generated />
using System;
namespace AutomaticInterface
{
/// <summary>
/// Use source generator to automatically create a Interface from this class
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
internal sealed class {{{AutomaticInterfaceGenerator.DefaultAttributeName}}}Attribute : Attribute
{
internal {{{AutomaticInterfaceGenerator.DefaultAttributeName}}}Attribute(string namespaceName = "") { }
}
}
""",
Encoding.UTF8
)
);
});
return context;
}

public static IncrementalGeneratorInitializationContext RegisterIgnoreAttribute(
this IncrementalGeneratorInitializationContext context
)
{
context.RegisterPostInitializationOutput(static postInitializationContext =>
{
postInitializationContext.AddSource(
$"{AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName}.Attribute.g.cs",
SourceText.From(
$$$"""
// <auto-generated />
using System;
namespace AutomaticInterface
{
/// <summary>
/// Ignore this member in a generated Interface from this class
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
internal sealed class {{{AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName}}}Attribute : Attribute
{
internal {{{AutomaticInterfaceGenerator.IgnoreAutomaticInterfaceAttributeName}}}Attribute() { }
}
}
""",
Encoding.UTF8
)
);
});
return context;
}
}

This file was deleted.

6 changes: 6 additions & 0 deletions AutomaticInterface/AutomaticInterfaceExample/DemoClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public string CMethod<T, T1, T2, T3, T4>(string? x, string y) // included
return "Ok";
}

[IgnoreAutomaticInterface]
public string IgnoreMethod(string x, string y) // // ignored because of attribute
{
return BMethod(x, y);
}

public Task<string?> ASync(string x, string y)
{
return Task.FromResult("");

Check warning on line 57 in AutomaticInterface/AutomaticInterfaceExample/DemoClass.cs

View workflow job for this annotation

GitHub Actions / test

Nullability of reference types in value of type 'Task<string>' doesn't match target type 'Task<string?>'.

Check warning on line 57 in AutomaticInterface/AutomaticInterfaceExample/DemoClass.cs

View workflow job for this annotation

GitHub Actions / publish

Nullability of reference types in value of type 'Task<string>' doesn't match target type 'Task<string?>'.
Expand Down
47 changes: 0 additions & 47 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,53 +760,6 @@ public partial interface IDemoClass
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void IgnoresMembersAttributedWithSkip()
{
const string code = """

using AutomaticInterfaceAttribute;
using System.IO;

namespace AutomaticInterfaceExample
{

[GenerateAutomaticInterface]
class DemoClass
{
[IgnoreAutomaticInterface] public string Hello1(string x, int y, double z){return x;}
[IgnoreAutomaticInterface] public string Hello2 { get; set; }
[IgnoreAutomaticInterface] public event EventHandler Hello3;
}
}

""";

const string expected = """
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
using System.IO;

namespace AutomaticInterfaceExample
{
[GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
}
}

""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void AddsDescriptionFromMethodToInterface()
{
Expand Down

0 comments on commit e15d657

Please sign in to comment.