Skip to content

Commit

Permalink
change metadatareference check condition
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 17, 2024
1 parent d28d5db commit e205186
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<IConfigurationBuilder> 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<IConfigurationBuilder> configure`.

Furthermore, overloads of `Action<IConfiguration, IServiceCollection> configure` and `Action<IConfiguration, ILoggingBuilder> configure` are added to `ConfigureServices` and `ConfigureLogging`, allowing you to retrieve the Configuration when executing the delegate.

Expand Down
6 changes: 4 additions & 2 deletions sandbox/GeneratorSandbox/GeneratorSandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

<ItemGroup>
<!--<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />
<PackageReference Include="ZLogger" Version="2.5.9" />-->
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />-->
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<!--<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />-->
<!--<PackageReference Include="ZLogger" Version="2.5.9" />-->
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions sandbox/GeneratorSandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using ConsoleAppFramework;
using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Configuration;
//using Microsoft.Extensions.DependencyInjection;
//// using Microsoft.Extensions.Hosting;
Expand All @@ -27,6 +28,10 @@

//app.Add<MyCommand>();
//app.Run(args);
// sc.BuildServiceProvider()

//IServiceProvider ser;
//ser.CreateScope()

ConsoleApp.Run(args, () => { });

Expand Down
27 changes: 10 additions & 17 deletions src/ConsoleAppFramework/ConsoleAppGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,56 @@ 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, _) =>
{
var hasDependencyInjection = false;
var hasLogging = false;
var hasConfiguration = false;
var hasJsonConfiguration = false;
var hasHostAbstraction = false;
var hasHost = false;

foreach (var x in xs)
{
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) =>
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;");
Expand Down
15 changes: 1 addition & 14 deletions src/ConsoleAppFramework/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,6 @@ public void Dispose()
}
}
""");

if (dllReference.HasHostAbstraction)
{
sb.AppendLine("""
internal static ConsoleApp.ConsoleAppBuilder ToConsoleAppBuilder(this IHostBuilder hostBuilder)
{
var host = hostBuilder.Build();
Expand All @@ -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();
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleAppFramework/SourceGeneratorContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
readonly record struct DllReference(bool HasDependencyInjection, bool HasLogging, bool HasConfiguration, bool HasJsonConfiguration, bool HasHost);

0 comments on commit e205186

Please sign in to comment.