From cfd68aeac9fca2a50d5524c0e1d6cfce6b35653c Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Tue, 4 Feb 2025 22:10:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B5-=D0=B3=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=80=D0=B8=D1=80=D1=83=D0=B5=D0=BC=D0=BE=D0=B3=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SampleIncrementalSourceGenerator.cs | 47 ++++++++++++++++++- .../mROA.Codegen.AssemblyInfo.cs | 2 +- .../obj/mROA.Codegen.csproj.nuget.g.props | 6 +-- mROA.Example/TestController.cs | 2 + .../Debug/net9.0/mROA.Example.AssemblyInfo.cs | 2 +- .../obj/mROA.Example.csproj.nuget.g.props | 4 +- .../Debug/net9.0/mROA.Test.AssemblyInfo.cs | 2 +- mROA.Test/obj/mROA.Test.csproj.nuget.g.props | 6 +-- mROA/obj/Debug/net9.0/mROA.AssemblyInfo.cs | 2 +- ....GeneratedMSBuildEditorConfig.editorconfig | 2 +- mROA/obj/mROA.csproj.nuget.g.props | 4 +- 11 files changed, 62 insertions(+), 17 deletions(-) diff --git a/mROA.Codegen/SampleIncrementalSourceGenerator.cs b/mROA.Codegen/SampleIncrementalSourceGenerator.cs index 31f9dd6..5d231a8 100644 --- a/mROA.Codegen/SampleIncrementalSourceGenerator.cs +++ b/mROA.Codegen/SampleIncrementalSourceGenerator.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Collections.Immutable; +using System.ComponentModel; using System.Linq; using System.Text; using Microsoft.CodeAnalysis; @@ -31,7 +33,6 @@ namespace {Namespace} public void Initialize(IncrementalGeneratorInitializationContext context) { - // Filter classes annotated with the [Report] attribute. Only filtered Syntax Nodes can trigger code generation. var provider = context.SyntaxProvider .CreateSyntaxProvider( @@ -82,12 +83,15 @@ namespace {Namespace} private void GenerateCode(SourceProductionContext context, Compilation compilation, ImmutableArray classDeclarations) { + var methods = new List<(string, IMethodSymbol)>(); + // Go through all filtered class declarations. foreach (var classDeclarationSyntax in classDeclarations) { // We need to get semantic model of the class to retrieve metadata. var semanticModel = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree); + // Symbols allow us to get the compile-time information. if (semanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol) continue; @@ -100,7 +104,7 @@ namespace {Namespace} // Go through all class members with a particular type (property) to generate method lines. var methodBody = classSymbol.GetMembers() .OfType(); - Console.WriteLine(methodBody.Select(i => i.ToDisplayString())); + methods.AddRange(methodBody.Select(i => (namespaceName + "." + className, i))); var originalName = className; // Build up the source code @@ -119,8 +123,47 @@ partial class {className} (int id, ISerialisationModule.IFrontendSerialisationMo }} "; + // Add the source code to the compilation. // context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8)); } + + + var methodsStringed = methods.Select(i => + $"typeof({i.Item1}).GetMethod(\"{i.Item2.Name}\", [{string.Join(", ", i.Item2.Parameters.Select(p => $"typeof({p.Type.ToDisplayString()})"))}])") + .ToList(); + + var coCodegenRepoCode = @$"// +using System.Collections.Generic; +using System.Reflection; + +namespace mROA.Codegen; + +public class CoCodegenMethodRepository :IMethodRepository +{{ + private readonly List _methods = [ + {string.Join(", \r\n\t\t", methodsStringed)} + ]; + public MethodInfo GetMethod(int id) + {{ + if (_methods.Count <= id) + return null; + + return _methods[id]; + }} + + public int RegisterMethod(MethodInfo method) + {{ + _methods.Add(method); + return _methods.Count - 1; + }} + + public IEnumerable GetMethods() + {{ + return _methods; + }} +}} +"; + context.AddSource($"CoCodegenMethodRepository.g.cs", SourceText.From(coCodegenRepoCode, Encoding.UTF8)); } } \ No newline at end of file diff --git a/mROA.Codegen/obj/Debug/netstandard2.0/mROA.Codegen.AssemblyInfo.cs b/mROA.Codegen/obj/Debug/netstandard2.0/mROA.Codegen.AssemblyInfo.cs index 01dbb28..e06c2f4 100644 --- a/mROA.Codegen/obj/Debug/netstandard2.0/mROA.Codegen.AssemblyInfo.cs +++ b/mROA.Codegen/obj/Debug/netstandard2.0/mROA.Codegen.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Codegen")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d732510a9fde122ff874ca08ed8c0f85b5fe6c02")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f0f8460632a9a837735d6bbfcc5c59708a0c44ad")] [assembly: System.Reflection.AssemblyProductAttribute("mROA.Codegen")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA.Codegen")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/mROA.Codegen/obj/mROA.Codegen.csproj.nuget.g.props b/mROA.Codegen/obj/mROA.Codegen.csproj.nuget.g.props index 353ed1c..6f36b67 100644 --- a/mROA.Codegen/obj/mROA.Codegen.csproj.nuget.g.props +++ b/mROA.Codegen/obj/mROA.Codegen.csproj.nuget.g.props @@ -5,18 +5,18 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference 6.12.0 - + - C:\Users\Mikhail\.nuget\packages\microsoft.codeanalysis.analyzers\3.3.4 + C:\Users\Mimm\.nuget\packages\microsoft.codeanalysis.analyzers\3.3.4 \ No newline at end of file diff --git a/mROA.Example/TestController.cs b/mROA.Example/TestController.cs index a7c0456..09adf59 100644 --- a/mROA.Example/TestController.cs +++ b/mROA.Example/TestController.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.SymbolStore; +using System.Reflection; using mROA.Implementation; namespace mROA.Example; diff --git a/mROA.Example/obj/Debug/net9.0/mROA.Example.AssemblyInfo.cs b/mROA.Example/obj/Debug/net9.0/mROA.Example.AssemblyInfo.cs index f58ad61..f56fbcd 100644 --- a/mROA.Example/obj/Debug/net9.0/mROA.Example.AssemblyInfo.cs +++ b/mROA.Example/obj/Debug/net9.0/mROA.Example.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Example")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d732510a9fde122ff874ca08ed8c0f85b5fe6c02")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f0f8460632a9a837735d6bbfcc5c59708a0c44ad")] [assembly: System.Reflection.AssemblyProductAttribute("mROA.Example")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA.Example")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/mROA.Example/obj/mROA.Example.csproj.nuget.g.props b/mROA.Example/obj/mROA.Example.csproj.nuget.g.props index 721d876..7f9c9db 100644 --- a/mROA.Example/obj/mROA.Example.csproj.nuget.g.props +++ b/mROA.Example/obj/mROA.Example.csproj.nuget.g.props @@ -5,12 +5,12 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference 6.12.0 - + \ No newline at end of file diff --git a/mROA.Test/obj/Debug/net9.0/mROA.Test.AssemblyInfo.cs b/mROA.Test/obj/Debug/net9.0/mROA.Test.AssemblyInfo.cs index a68ed5a..f2a60f6 100644 --- a/mROA.Test/obj/Debug/net9.0/mROA.Test.AssemblyInfo.cs +++ b/mROA.Test/obj/Debug/net9.0/mROA.Test.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("mROA.Test")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d732510a9fde122ff874ca08ed8c0f85b5fe6c02")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f0f8460632a9a837735d6bbfcc5c59708a0c44ad")] [assembly: System.Reflection.AssemblyProductAttribute("mROA.Test")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA.Test")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/mROA.Test/obj/mROA.Test.csproj.nuget.g.props b/mROA.Test/obj/mROA.Test.csproj.nuget.g.props index 92a05d6..f0b8464 100644 --- a/mROA.Test/obj/mROA.Test.csproj.nuget.g.props +++ b/mROA.Test/obj/mROA.Test.csproj.nuget.g.props @@ -5,12 +5,12 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference 6.12.0 - + @@ -21,6 +21,6 @@ - C:\Users\Mikhail\.nuget\packages\nunit.analyzers\4.6.0 + C:\Users\Mimm\.nuget\packages\nunit.analyzers\4.6.0 \ No newline at end of file diff --git a/mROA/obj/Debug/net9.0/mROA.AssemblyInfo.cs b/mROA/obj/Debug/net9.0/mROA.AssemblyInfo.cs index 497e2c7..e60d704 100644 --- a/mROA/obj/Debug/net9.0/mROA.AssemblyInfo.cs +++ b/mROA/obj/Debug/net9.0/mROA.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("mROA")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d732510a9fde122ff874ca08ed8c0f85b5fe6c02")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f0f8460632a9a837735d6bbfcc5c59708a0c44ad")] [assembly: System.Reflection.AssemblyProductAttribute("mROA")] [assembly: System.Reflection.AssemblyTitleAttribute("mROA")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/mROA/obj/Debug/net9.0/mROA.GeneratedMSBuildEditorConfig.editorconfig b/mROA/obj/Debug/net9.0/mROA.GeneratedMSBuildEditorConfig.editorconfig index 6ec64ed..c1caa79 100644 --- a/mROA/obj/Debug/net9.0/mROA.GeneratedMSBuildEditorConfig.editorconfig +++ b/mROA/obj/Debug/net9.0/mROA.GeneratedMSBuildEditorConfig.editorconfig @@ -8,7 +8,7 @@ build_property.PlatformNeutralAssembly = build_property.EnforceExtendedAnalyzerRules = build_property._SupportedPlatformList = Linux,macOS,Windows build_property.RootNamespace = mROA -build_property.ProjectDir = C:\Users\Mikhail\Projects\VisualStudioProjects\mROA\mROA\ +build_property.ProjectDir = C:\Users\Mimm\Projects\VisualStudioProjects\mROA\mROA\ build_property.EnableComHosting = build_property.EnableGeneratedComInterfaceComImportInterop = build_property.EffectiveAnalysisLevelStyle = 9.0 diff --git a/mROA/obj/mROA.csproj.nuget.g.props b/mROA/obj/mROA.csproj.nuget.g.props index 721d876..7f9c9db 100644 --- a/mROA/obj/mROA.csproj.nuget.g.props +++ b/mROA/obj/mROA.csproj.nuget.g.props @@ -5,12 +5,12 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference 6.12.0 - + \ No newline at end of file