Skip to content

Commit baf58eb

Browse files
committed
Add ConsoleAPp.Create(configure) if MS.E.DI is referenced
1 parent 9d5a10c commit baf58eb

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

sandbox/GeneratorSandbox/Filters.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
using ConsoleAppFramework;
3-
using Microsoft.Extensions.DependencyInjection;
3+
// using Microsoft.Extensions.DependencyInjection;
44
using System.Diagnostics;
55
using System.Reflection;
66

@@ -107,12 +107,12 @@ public override async Task InvokeAsync(ConsoleAppContext context, CancellationTo
107107
}
108108

109109

110-
internal class ServiceProviderScopeFilter(IServiceProvider serviceProvider, ConsoleAppFilter next) : ConsoleAppFilter(next)
111-
{
112-
public override async Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
113-
{
114-
// create Microsoft.Extensions.DependencyInjection scope
115-
await using var scope = serviceProvider.CreateAsyncScope();
116-
await Next.InvokeAsync(context, cancellationToken);
117-
}
118-
}
110+
//internal class ServiceProviderScopeFilter(IServiceProvider serviceProvider, ConsoleAppFilter next) : ConsoleAppFilter(next)
111+
//{
112+
// public override async Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
113+
// {
114+
// // create Microsoft.Extensions.DependencyInjection scope
115+
// await using var scope = serviceProvider.CreateAsyncScope();
116+
// await Next.InvokeAsync(context, cancellationToken);
117+
// }
118+
//}

sandbox/GeneratorSandbox/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
using ConsoleAppFramework;
22
using FilterShareProject;
3+
using Microsoft.Extensions.DependencyInjection;
34

45

56
args = ["Output"];
67

78
var app = ConsoleApp.Create();
89

10+
// ConsoleApp.ServiceProvider
11+
// ConsoleApp.Create(
12+
13+
ConsoleApp.Create(sc =>
14+
{
15+
16+
});
17+
918
app.Add<MyCommands>();
1019

1120
app.Run(args);
@@ -38,4 +47,24 @@ public void Output(string msg = @"\\")
3847
{
3948
Console.WriteLine(msg); // 「\」
4049
}
50+
}
51+
52+
namespace ConsoleAppFramework
53+
{
54+
internal static partial class ConsoleApp
55+
{
56+
//public static ConsoleAppBuilder Create(IServiceProvider serviceProvider)
57+
//{
58+
// ConsoleApp.ServiceProvider = serviceProvider;
59+
// return ConsoleApp.Create();
60+
//}
61+
62+
//public static ConsoleAppBuilder Create(Action<IServiceCollection> configure)
63+
//{
64+
// var services = new ServiceCollection();
65+
// configure(services);
66+
// ConsoleApp.ServiceProvider = services.BuildServiceProvider();
67+
// return ConsoleApp.Create();
68+
//}
69+
}
4170
}

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ public static Task RunAsync(string[] args)
199199
200200
public static ConsoleAppBuilder Create() => new ConsoleAppBuilder();
201201
202+
public static ConsoleAppBuilder Create(IServiceProvider serviceProvider)
203+
{
204+
ConsoleApp.ServiceProvider = serviceProvider;
205+
return ConsoleApp.Create();
206+
}
207+
202208
static void ThrowArgumentParseFailed(string argumentName, string value)
203209
{
204210
throw new ArgumentParseFailedException($"Argument '{argumentName}' failed to parse, provided value: {value}");
@@ -667,6 +673,26 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
667673
emitter.EmitHelp(help, commandIds!);
668674
}
669675
sourceProductionContext.AddSource("ConsoleApp.Builder.Help.g.cs", help.ToString());
676+
677+
// emit Create(IServiceCollection) if exists Microsoft.Extensions.DependencyInjection.ServiceCollection
678+
if (collectBuilderContext.HasDependencyInjectionReference)
679+
{
680+
var runBuilder = new SourceBuilder(0);
681+
runBuilder.AppendLine(GeneratedCodeHeader);
682+
runBuilder.AppendLine("using Microsoft.Extensions.DependencyInjection");
683+
runBuilder.AppendLine();
684+
using (runBuilder.BeginBlock("internal static partial class ConsoleApp"))
685+
{
686+
using (runBuilder.BeginBlock("public static ConsoleAppBuilder Create(Action<IServiceCollection> configure)"))
687+
{
688+
runBuilder.AppendLine("var services = new ServiceCollection();");
689+
runBuilder.AppendLine("configure(services);");
690+
runBuilder.AppendLine("ConsoleApp.ServiceProvider = services.BuildServiceProvider();");
691+
runBuilder.AppendLine("return ConsoleApp.Create();");
692+
}
693+
}
694+
sourceProductionContext.AddSource("ConsoleApp.Builder.Run.g.cs", runBuilder.ToString());
695+
}
670696
}
671697

672698
class CommanContext(Command? command, bool isAsync, DiagnosticReporter diagnosticReporter, InvocationExpressionSyntax node) : IEquatable<CommanContext>
@@ -694,6 +720,7 @@ class CollectBuilderContext : IEquatable<CollectBuilderContext>
694720
public CancellationToken CancellationToken { get; }
695721
public bool HasRun { get; }
696722
public bool HasRunAsync { get; }
723+
public bool HasDependencyInjectionReference { get; }
697724

698725
public CollectBuilderContext(ImmutableArray<BuilderContext> contexts, CancellationToken cancellationToken)
699726
{
@@ -805,13 +832,18 @@ public CollectBuilderContext(ImmutableArray<BuilderContext> contexts, Cancellati
805832
this.Commands = commands1.Concat(commands2!).ToArray()!; // not null if no diagnostics
806833
this.HasRun = methodGroup["Run"].Any();
807834
this.HasRunAsync = methodGroup["RunAsync"].Any();
835+
836+
var model = contexts[0].Model;
837+
var serviceCollectionSymbol = model.Compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.ServiceCollection");
838+
this.HasDependencyInjectionReference = serviceCollectionSymbol != null;
808839
}
809840

810841
public bool Equals(CollectBuilderContext other)
811842
{
812843
if (DiagnosticReporter.HasDiagnostics || other.DiagnosticReporter.HasDiagnostics) return false;
813844
if (HasRun != other.HasRun) return false;
814845
if (HasRunAsync != other.HasRunAsync) return false;
846+
if (HasDependencyInjectionReference != other.HasDependencyInjectionReference) return false;
815847

816848
return Commands.AsSpan().SequenceEqual(other.Commands);
817849
}

0 commit comments

Comments
 (0)