From 5568a98e0be17f35ac5b21bdd01dde2b578ce808 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 21 May 2024 13:03:43 +0100 Subject: [PATCH] add MembersToIgnore --- .gitignore | 2 + .../ProxyInterfaceConsumer/Address.cs | 1 + .../ProxyInterfaceConsumer/IAddress.cs | 4 +- .../ProxyInterfaceConsumerForPnP/Program.cs | 2 +- .../ProxyInterfaceConsumerViaNuGet/IPerson.cs | 4 +- .../ProxyInterfaceConsumerViaNuGet/Person.cs | 2 +- .../FileGenerators/ExtraFilesGenerator.cs | 8 +- .../AttributeArgumentListParser.cs | 2 +- .../Types/FluentBuilderAttributeArguments.cs | 1 + ...y_Should_GenerateCorrectFiles.verified.txt | 8 +- ...rateFiles_ForClassWithIgnores.verified.txt | 132 ++++++++++++++++++ .../ProxyInterfaceSourceGeneratorTest.cs | 38 +++++ .../ProxyInterfaceSourceGeneratorTests.csproj | 6 + .../Source/Foo2.cs | 31 ++++ .../Source/IFoo2.cs | 6 + 15 files changed, 240 insertions(+), 7 deletions(-) create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithIgnores.verified.txt create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/Foo2.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/IFoo2.cs diff --git a/.gitignore b/.gitignore index 7508c6d..313797d 100644 --- a/.gitignore +++ b/.gitignore @@ -349,3 +349,5 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ /tests/ProxyInterfaceSourceGeneratorTests/Destination/Disposable/*.g.cs + +.idea/.idea.ProxyInterfaceSourceGenerator Solution/.idea/ diff --git a/src-examples/ProxyInterfaceConsumer/Address.cs b/src-examples/ProxyInterfaceConsumer/Address.cs index c9e80b8..71395f3 100644 --- a/src-examples/ProxyInterfaceConsumer/Address.cs +++ b/src-examples/ProxyInterfaceConsumer/Address.cs @@ -7,5 +7,6 @@ namespace ProxyInterfaceConsumer public int HouseNumber { get; set; } public event EventHandler MyEvent; + public int Weird { get; set; } } } \ No newline at end of file diff --git a/src-examples/ProxyInterfaceConsumer/IAddress.cs b/src-examples/ProxyInterfaceConsumer/IAddress.cs index f7e3242..2343753 100644 --- a/src-examples/ProxyInterfaceConsumer/IAddress.cs +++ b/src-examples/ProxyInterfaceConsumer/IAddress.cs @@ -1,6 +1,8 @@ +using ProxyInterfaceGenerator; + namespace ProxyInterfaceConsumer { - [ProxyInterfaceGenerator.Proxy(typeof(Address))] + [ProxyInterfaceGenerator.Proxy(typeof(Address), false, ProxyClassAccessibility.Public, new []{"Weird"})] public partial interface IAddress { } diff --git a/src-examples/ProxyInterfaceConsumerForPnP/Program.cs b/src-examples/ProxyInterfaceConsumerForPnP/Program.cs index 5fbba12..b5ee24a 100644 --- a/src-examples/ProxyInterfaceConsumerForPnP/Program.cs +++ b/src-examples/ProxyInterfaceConsumerForPnP/Program.cs @@ -28,7 +28,7 @@ public class Program IClientContext cp = new ClientContextProxy(clientContext); - cp.Load(cp.Web, w => w.Lists, w => w.Language, w => w.Author); + cp.Load(cp.Web, w => w.Lists, w => w.Language, w => w.Author); await cp.ExecuteQueryRetryAsync(); diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs index 2969bc2..f6f5b19 100644 --- a/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/IPerson.cs @@ -1,6 +1,8 @@ +using ProxyInterfaceGenerator; + namespace ProxyInterfaceConsumer { - [ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.Person), ProxyClassAccessibility.Internal))] + [Proxy(typeof(Person), ProxyClassAccessibility.Internal)] public partial interface IPerson { } diff --git a/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs b/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs index b21a785..b999002 100644 --- a/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs +++ b/src-examples/ProxyInterfaceConsumerViaNuGet/Person.cs @@ -52,7 +52,7 @@ namespace ProxyInterfaceConsumer b = 1; } - public int In_Out_Ref2(in Address a, out Address b, ref Address c) + public int In_Out_Ref2(in IAddress a, out Address b, ref IAddress c) { b = new Address(); return 404; diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ExtraFilesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ExtraFilesGenerator.cs index 32f7a13..eb8fb9b 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ExtraFilesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ExtraFilesGenerator.cs @@ -27,6 +27,7 @@ namespace ProxyInterfaceGenerator public Type Type {{ get; }} public bool ProxyBaseClasses {{ get; }} public ProxyClassAccessibility Accessibility {{ get; }} + public string[] MembersToIgnore {{ get; }} public ProxyAttribute(Type type) : this(type, false, ProxyClassAccessibility.Public) {{ @@ -40,11 +41,16 @@ namespace ProxyInterfaceGenerator {{ }} - public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility) + public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, Array.Empty()) + {{ + }} + + public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility, string[] membersToIgnore) {{ Type = type; ProxyBaseClasses = proxyBaseClasses; Accessibility = accessibility; + MembersToIgnore = membersToIgnore; }} }} diff --git a/src/ProxyInterfaceSourceGenerator/SyntaxReceiver/AttributeArgumentListParser.cs b/src/ProxyInterfaceSourceGenerator/SyntaxReceiver/AttributeArgumentListParser.cs index a8fafcf..91a8e66 100644 --- a/src/ProxyInterfaceSourceGenerator/SyntaxReceiver/AttributeArgumentListParser.cs +++ b/src/ProxyInterfaceSourceGenerator/SyntaxReceiver/AttributeArgumentListParser.cs @@ -12,7 +12,7 @@ internal static class AttributeArgumentListParser { public static ProxyInterfaceGeneratorAttributeArguments ParseAttributeArguments(AttributeArgumentListSyntax? argumentList, SemanticModel semanticModel) { - if (argumentList is null || argumentList.Arguments.Count is < 1 or > 3) + if (argumentList is null || argumentList.Arguments.Count is < 1 or > 4) { throw new ArgumentException("The ProxyAttribute requires 1, 2 or 3 arguments."); } diff --git a/src/ProxyInterfaceSourceGenerator/Types/FluentBuilderAttributeArguments.cs b/src/ProxyInterfaceSourceGenerator/Types/FluentBuilderAttributeArguments.cs index 0e4ae9b..b1af428 100644 --- a/src/ProxyInterfaceSourceGenerator/Types/FluentBuilderAttributeArguments.cs +++ b/src/ProxyInterfaceSourceGenerator/Types/FluentBuilderAttributeArguments.cs @@ -5,4 +5,5 @@ internal record ProxyInterfaceGeneratorAttributeArguments(string FullyQualifiedD public bool ProxyBaseClasses { get; set; } public ProxyClassAccessibility Accessibility { get; set; } + public string[] MembersToIgnore { get; set; } = Array.Empty(); } \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithArray_Should_GenerateCorrectFiles.verified.txt b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithArray_Should_GenerateCorrectFiles.verified.txt index 1f66b57..0c9e0a8 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithArray_Should_GenerateCorrectFiles.verified.txt +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithArray_Should_GenerateCorrectFiles.verified.txt @@ -21,6 +21,7 @@ namespace ProxyInterfaceGenerator public Type Type { get; } public bool ProxyBaseClasses { get; } public ProxyClassAccessibility Accessibility { get; } + public string[] MembersToIgnore { get; } public ProxyAttribute(Type type) : this(type, false, ProxyClassAccessibility.Public) { @@ -34,11 +35,16 @@ namespace ProxyInterfaceGenerator { } - public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility) + public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, Array.Empty()) + { + } + + public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility, string[] membersToIgnore) { Type = type; ProxyBaseClasses = proxyBaseClasses; Accessibility = accessibility; + MembersToIgnore = membersToIgnore; } } diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithIgnores.verified.txt b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithIgnores.verified.txt new file mode 100644 index 0000000..a78d037 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.GenerateFiles_ForClassWithIgnores.verified.txt @@ -0,0 +1,132 @@ +[ + { + HintName: ProxyInterfaceGenerator.Extra.g.cs, + Source: +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +using System; + +namespace ProxyInterfaceGenerator +{ + [AttributeUsage(AttributeTargets.Interface)] + internal sealed class ProxyAttribute : Attribute + { + public Type Type { get; } + public bool ProxyBaseClasses { get; } + public ProxyClassAccessibility Accessibility { get; } + public string[] MembersToIgnore { get; } + + public ProxyAttribute(Type type) : this(type, false, ProxyClassAccessibility.Public) + { + } + + public ProxyAttribute(Type type, bool proxyBaseClasses) : this(type, proxyBaseClasses, ProxyClassAccessibility.Public) + { + } + + public ProxyAttribute(Type type, ProxyClassAccessibility accessibility) : this(type, false, accessibility) + { + } + + public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, Array.Empty()) + { + } + + public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility, string[] membersToIgnore) + { + Type = type; + ProxyBaseClasses = proxyBaseClasses; + Accessibility = accessibility; + MembersToIgnore = membersToIgnore; + } + } + + [Flags] + internal enum ProxyClassAccessibility + { + Public = 0, + + Internal = 1 + } +} + }, + { + HintName: ProxyInterfaceSourceGeneratorTests.Source.IFoo2.g.cs, + Source: +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial interface IFoo2 + { + global::ProxyInterfaceSourceGeneratorTests.Source.Foo2 _Instance { get; } + + global::ProxyInterfaceSourceGeneratorTests.Source.IFoo2[] Foos { get; set; } + + global::ProxyInterfaceSourceGeneratorTests.Source.IFoo2[] DoSomethingAndGetAnArrayOfFoos(); + } +} +#nullable restore + }, + { + HintName: ProxyInterfaceSourceGeneratorTests.Source.Foo2Proxy.g.cs, + Source: +//---------------------------------------------------------------------------------------- +// +// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial class Foo2Proxy : global::ProxyInterfaceSourceGeneratorTests.Source.IFoo2 + { + public global::ProxyInterfaceSourceGeneratorTests.Source.Foo2 _Instance { get; } + + public global::ProxyInterfaceSourceGeneratorTests.Source.IFoo2[] Foos { get => Mapster.TypeAdapter.Adapt(_Instance.Foos); set => _Instance.Foos = Mapster.TypeAdapter.Adapt(value); } + + public global::ProxyInterfaceSourceGeneratorTests.Source.IFoo2[] DoSomethingAndGetAnArrayOfFoos() + { + var result_1603865878 = _Instance.DoSomethingAndGetAnArrayOfFoos(); + return Mapster.TypeAdapter.Adapt(result_1603865878); + } + + + public Foo2Proxy(global::ProxyInterfaceSourceGeneratorTests.Source.Foo2 instance) + { + _Instance = instance; + + + Mapster.TypeAdapterConfig.NewConfig().ConstructUsing(instance1325374861 => new global::ProxyInterfaceSourceGeneratorTests.Source.Foo2Proxy(instance1325374861)); + Mapster.TypeAdapterConfig.NewConfig().MapWith(proxy1047178445 => ((global::ProxyInterfaceSourceGeneratorTests.Source.Foo2Proxy) proxy1047178445)._Instance); + + + } + } +} +#nullable restore + } +] \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs index 1a1f749..758cfe9 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs @@ -86,6 +86,44 @@ public class ProxyInterfaceSourceGeneratorTest return Verify(results); } + + [Fact] + public Task GenerateFiles_ForClassWithIgnores() + { + // Arrange + var fileNames = new[] + { + "ProxyInterfaceSourceGeneratorTests.Source.IFoo2.g.cs", + "ProxyInterfaceSourceGeneratorTests.Source.Foo2Proxy.g.cs" + }; + + var path = "./Source/IFoo2.cs"; + var sourceFile = new SourceFile + { + Path = path, + Text = File.ReadAllText(path), + AttributeToAddToInterface = new ExtraAttribute + { + Name = "ProxyInterfaceGenerator.Proxy", + ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.Foo2)" + } + }; + + // Act + var result = _sut.Execute(new[] + { + sourceFile + }); + + // Assert + result.Valid.Should().BeTrue(); + result.Files.Should().HaveCount(fileNames.Length + 1); + + // Verify + var results = result.GeneratorDriver.GetRunResult().Results.First().GeneratedSources; + return Verify(results); + } + [Fact] public void GenerateFiles_ForGenericType_Should_GenerateCorrectFiles() { diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj index 5acaa96..6266499 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj @@ -49,6 +49,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/Foo2.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/Foo2.cs new file mode 100644 index 0000000..6d316de --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/Foo2.cs @@ -0,0 +1,31 @@ +namespace ProxyInterfaceSourceGeneratorTests.Source; + +public class Foo2 +{ + //public Bar DoSomethingAndGetABar() + //{ + // return new Bar(); + //} + + //public Bar[] DoSomethingAndGetAnArrayOfBars() + //{ + // return new[] { new Bar() }; + //} + + //public Foo DoSomethingAndGetAFoo() + //{ + // return new Foo(); + //} + + public Foo2[] Foos { get; set; } + + public Foo2[] DoSomethingAndGetAnArrayOfFoos() + { + return new[] { new Foo2() }; + } + + //public List DoSomethingAndGetAListOfFoos() + //{ + // return new[] { new Foo() }.ToList(); + //} +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IFoo2.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IFoo2.cs new file mode 100644 index 0000000..c10a3a4 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IFoo2.cs @@ -0,0 +1,6 @@ +namespace ProxyInterfaceSourceGeneratorTests.Source +{ + public partial interface IFoo2 + { + } +} \ No newline at end of file