From e20518692f65b4a136965110f9bd42756fd70835 Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 17 Dec 2024 17:32:40 +0900 Subject: [PATCH] change metadatareference check condition --- ReadMe.md | 2 +- .../GeneratorSandbox/GeneratorSandbox.csproj | 6 +++-- sandbox/GeneratorSandbox/Program.cs | 5 ++++ .../ConsoleAppGenerator.cs | 27 +++++++------------ src/ConsoleAppFramework/Emitter.cs | 15 +---------- .../SourceGeneratorContexts.cs | 2 +- 6 files changed, 22 insertions(+), 35 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 3160584..3a4e11f 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1016,7 +1016,7 @@ public class PositionOptions } ``` -When `Microsoft.Extensions.Configuration.Abstractions` is imported, `ConfigureEmptyConfiguration` becomes available to call. Additionally, when `Microsoft.Extensions.Configuration.Json` is imported, `ConfigureDefaultConfiguration` becomes available to call. In DefaultConfiguration, `SetBasePath(System.IO.Directory.GetCurrentDirectory())` and `AddJsonFile("appsettings.json", optional: true)` are executed before calling `Action configure`. +When `Microsoft.Extensions.Configuration` is imported, `ConfigureEmptyConfiguration` becomes available to call. Additionally, when `Microsoft.Extensions.Configuration.Json` is imported, `ConfigureDefaultConfiguration` becomes available to call. In DefaultConfiguration, `SetBasePath(System.IO.Directory.GetCurrentDirectory())` and `AddJsonFile("appsettings.json", optional: true)` are executed before calling `Action configure`. Furthermore, overloads of `Action configure` and `Action configure` are added to `ConfigureServices` and `ConfigureLogging`, allowing you to retrieve the Configuration when executing the delegate. diff --git a/sandbox/GeneratorSandbox/GeneratorSandbox.csproj b/sandbox/GeneratorSandbox/GeneratorSandbox.csproj index b8b17e0..89c2ed5 100644 --- a/sandbox/GeneratorSandbox/GeneratorSandbox.csproj +++ b/sandbox/GeneratorSandbox/GeneratorSandbox.csproj @@ -16,8 +16,10 @@ + --> + + + diff --git a/sandbox/GeneratorSandbox/Program.cs b/sandbox/GeneratorSandbox/Program.cs index a370a53..2150d59 100644 --- a/sandbox/GeneratorSandbox/Program.cs +++ b/sandbox/GeneratorSandbox/Program.cs @@ -1,6 +1,7 @@ #nullable enable using ConsoleAppFramework; +using Microsoft.Extensions.DependencyInjection; //using Microsoft.Extensions.Configuration; //using Microsoft.Extensions.DependencyInjection; //// using Microsoft.Extensions.Hosting; @@ -27,6 +28,10 @@ //app.Add(); //app.Run(args); +// sc.BuildServiceProvider() + +//IServiceProvider ser; +//ser.CreateScope() ConsoleApp.Run(args, () => { }); diff --git a/src/ConsoleAppFramework/ConsoleAppGenerator.cs b/src/ConsoleAppFramework/ConsoleAppGenerator.cs index 99b95a3..dedef6e 100644 --- a/src/ConsoleAppFramework/ConsoleAppGenerator.cs +++ b/src/ConsoleAppFramework/ConsoleAppGenerator.cs @@ -16,7 +16,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterPostInitializationOutput(EmitConsoleAppTemplateSource); // Emti ConfigureConfiguration/Logging/Services and Host.AsConsoleApp - var hasDependencyInjection = context.MetadataReferencesProvider + var hasReferences = context.MetadataReferencesProvider .Collect() .Select((xs, _) => { @@ -24,7 +24,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var hasLogging = false; var hasConfiguration = false; var hasJsonConfiguration = false; - var hasHostAbstraction = false; var hasHost = false; foreach (var x in xs) @@ -32,47 +31,41 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var name = x.Display; if (name == null) continue; - if (!hasDependencyInjection && name.EndsWith("Microsoft.Extensions.DependencyInjection.Abstractions.dll")) + if (!hasDependencyInjection && name.EndsWith("Microsoft.Extensions.DependencyInjection.dll")) // BuildServiceProvider { hasDependencyInjection = true; continue; } - if (!hasLogging && name.EndsWith("Microsoft.Extensions.Logging.Abstractions.dll")) + if (!hasLogging && name.EndsWith("Microsoft.Extensions.Logging.dll")) // AddLogging { hasLogging = true; continue; } - if (!hasConfiguration && name.EndsWith("Microsoft.Extensions.Configuration.Abstractions.dll")) + if (!hasConfiguration && name.EndsWith("Microsoft.Extensions.Configuration.dll")) // needs ConfigurationBuilder { hasConfiguration = true; continue; } - if (!hasJsonConfiguration && name.EndsWith("Microsoft.Extensions.Configuration.Json.dll")) + if (!hasJsonConfiguration && name.EndsWith("Microsoft.Extensions.Configuration.Json.dll")) // AddJson { hasJsonConfiguration = true; continue; } - if (!hasHostAbstraction && name.EndsWith("Microsoft.Extensions.Hosting.Abstractions.dll")) - { - hasHostAbstraction = true; - continue; - } - - if (!hasHost && name.EndsWith("Microsoft.Extensions.Hosting.dll")) + if (!hasHost && name.EndsWith("Microsoft.Extensions.Hosting.dll")) // IHostBuilder, ApplicationHostBuilder { hasHost = true; continue; } } - return new DllReference(hasDependencyInjection, hasLogging, hasConfiguration, hasJsonConfiguration, hasHostAbstraction, hasHost); + return new DllReference(hasDependencyInjection, hasLogging, hasConfiguration, hasJsonConfiguration, hasHost); }); - context.RegisterSourceOutput(hasDependencyInjection, EmitConsoleAppConfigure); + context.RegisterSourceOutput(hasReferences, EmitConsoleAppConfigure); // get Options for Combine var generatorOptions = context.CompilationProvider.Select((compilation, token) => @@ -291,7 +284,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex static void EmitConsoleAppConfigure(SourceProductionContext sourceProductionContext, DllReference dllReference) { - if (!dllReference.HasDependencyInjection && !dllReference.HasLogging && !dllReference.HasConfiguration && !dllReference.HasHost && !dllReference.HasHostAbstraction) + if (!dllReference.HasDependencyInjection && !dllReference.HasLogging && !dllReference.HasConfiguration && !dllReference.HasHost) { return; } @@ -312,7 +305,7 @@ static void EmitConsoleAppConfigure(SourceProductionContext sourceProductionCont sb.AppendLine("using Microsoft.Extensions.Configuration;"); } - if (dllReference.HasHost || dllReference.HasHostAbstraction) + if (dllReference.HasHost) { var sb2 = sb.Clone(); sb2.AppendLine("using Microsoft.Extensions.Hosting;"); diff --git a/src/ConsoleAppFramework/Emitter.cs b/src/ConsoleAppFramework/Emitter.cs index 45e9bde..fc4e38e 100644 --- a/src/ConsoleAppFramework/Emitter.cs +++ b/src/ConsoleAppFramework/Emitter.cs @@ -839,11 +839,6 @@ public void Dispose() } } -"""); - - if (dllReference.HasHostAbstraction) - { - sb.AppendLine(""" internal static ConsoleApp.ConsoleAppBuilder ToConsoleAppBuilder(this IHostBuilder hostBuilder) { var host = hostBuilder.Build(); @@ -855,12 +850,6 @@ internal static ConsoleApp.ConsoleAppBuilder ToConsoleAppBuilder(this IHostBuild return ConsoleApp.Create(); } -"""); - } - - if (dllReference.HasHost) - { - sb.AppendLine(""" internal static ConsoleApp.ConsoleAppBuilder ToConsoleAppBuilder(this HostApplicationBuilder hostBuilder) { var host = hostBuilder.Build(); @@ -871,10 +860,8 @@ internal static ConsoleApp.ConsoleAppBuilder ToConsoleAppBuilder(this HostApplic return ConsoleApp.Create(); } +} """); - - sb.AppendLine("}"); - } } internal record CommandWithId(string? FieldType, Command Command, int Id) diff --git a/src/ConsoleAppFramework/SourceGeneratorContexts.cs b/src/ConsoleAppFramework/SourceGeneratorContexts.cs index abbe03d..c1cbba0 100644 --- a/src/ConsoleAppFramework/SourceGeneratorContexts.cs +++ b/src/ConsoleAppFramework/SourceGeneratorContexts.cs @@ -2,4 +2,4 @@ readonly record struct ConsoleAppFrameworkGeneratorOptions(bool DisableNamingConversion); -readonly record struct DllReference(bool HasDependencyInjection, bool HasLogging, bool HasConfiguration, bool HasJsonConfiguration, bool HasHostAbstraction, bool HasHost); \ No newline at end of file +readonly record struct DllReference(bool HasDependencyInjection, bool HasLogging, bool HasConfiguration, bool HasJsonConfiguration, bool HasHost); \ No newline at end of file