Skip to content

Commit 03bc452

Browse files
committed
GlobalOptions runtime complete
1 parent 14dfecc commit 03bc452

File tree

2 files changed

+76
-28
lines changed

2 files changed

+76
-28
lines changed

sandbox/GeneratorSandbox/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@
9191

9292
var builder = ConsoleApp.Create();
9393

94+
95+
builder.ConfigureDefaultConfiguration()
96+
.ConfigureServices((c, x) =>
97+
{
98+
99+
});
100+
94101
builder.UseFilter<NopFilter1>();
95102
builder.UseFilter<NopFilter2>();
96103

src/ConsoleAppFramework/Emitter.cs

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -768,78 +768,119 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
768768
// DependencyInjection
769769
if (dllReference.HasDependencyInjection)
770770
{
771+
// field
771772
if (dllReference.HasConfiguration)
772773
{
773-
sb.AppendLine("Action<IConfiguration, IServiceCollection>? configureServices;");
774+
sb.AppendLine("Action<ConsoleAppContext, IConfiguration, IServiceCollection>? configureServices;");
774775
}
775776
else
776777
{
777-
sb.AppendLine("Action<IServiceCollection>? configureServices;");
778+
sb.AppendLine("Action<ConsoleAppContext, IServiceCollection>? configureServices;");
778779
}
779780

780-
sb.AppendLine();
781-
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)"))
781+
// methods
782+
if (dllReference.HasConfiguration)
782783
{
783-
if (dllReference.HasConfiguration)
784+
sb.AppendLine();
785+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)"))
784786
{
785-
sb.AppendLine("this.configureServices = (_, services) => configure(services);");
787+
sb.AppendLine("this.configureServices = (_, _, services) => configure(services);");
788+
sb.AppendLine("return this;");
786789
}
787-
else
790+
791+
sb.AppendLine();
792+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IConfiguration, IServiceCollection> configure)"))
793+
{
794+
// for backward-compatiblity, we chooce (IConfiguration, IServiceCollection) for two arguments overload
795+
sb.AppendLine("this.requireConfiguration = true;");
796+
sb.AppendLine("this.configureServices = (_, configuration, services) => configure(configuration, services);");
797+
sb.AppendLine("return this;");
798+
}
799+
800+
sb.AppendLine();
801+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<ConsoleAppContext, IConfiguration, IServiceCollection> configure)"))
788802
{
803+
sb.AppendLine("this.requireConfiguration = true;");
789804
sb.AppendLine("this.configureServices = configure;");
805+
sb.AppendLine("return this;");
790806
}
791-
sb.AppendLine("return this;");
792807
}
793-
794-
if (dllReference.HasConfiguration)
808+
else
795809
{
796810
sb.AppendLine();
797-
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IConfiguration, IServiceCollection> configure)"))
811+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<IServiceCollection> configure)"))
812+
{
813+
sb.AppendLine("this.configureServices = (_, _, services) => configure(services);");
814+
sb.AppendLine("return this;");
815+
}
816+
817+
sb.AppendLine();
818+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureServices(Action<ConsoleAppContext, IServiceCollection> configure)"))
798819
{
799-
sb.AppendLine("this.requireConfiguration = true;");
800820
sb.AppendLine("this.configureServices = configure;");
801821
sb.AppendLine("return this;");
802822
}
803823
}
824+
804825
sb.AppendLine();
805826
}
806827

807828
// Logging
808829
if (dllReference.HasLogging)
809830
{
831+
// field
810832
if (dllReference.HasConfiguration)
811833
{
812-
sb.AppendLine("Action<IConfiguration, ILoggingBuilder>? configureLogging;");
834+
sb.AppendLine("Action<ConsoleAppContext, IConfiguration, ILoggingBuilder>? configureLogging;");
813835
}
814836
else
815837
{
816-
sb.AppendLine("Action<ILoggingBuilder>? configureLogging;");
838+
sb.AppendLine("Action<ConsoleAppContext, ILoggingBuilder>? configureLogging;");
817839
}
818840

819-
sb.AppendLine();
820-
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)"))
841+
// methods
842+
if (dllReference.HasConfiguration)
821843
{
822-
if (dllReference.HasConfiguration)
844+
sb.AppendLine();
845+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)"))
823846
{
824-
sb.AppendLine("this.configureLogging = (_, logging) => configure(logging);");
847+
sb.AppendLine("this.configureLogging = (_, _, logging) => configure(logging);");
848+
sb.AppendLine("return this;");
825849
}
826-
else
850+
851+
sb.AppendLine();
852+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<IConfiguration, ILoggingBuilder> configure)"))
853+
{
854+
sb.AppendLine("this.requireConfiguration = true;");
855+
sb.AppendLine("this.configureLogging = (_, configuration, logging) => configure(configuration, logging);");
856+
sb.AppendLine("return this;");
857+
}
858+
859+
sb.AppendLine();
860+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ConsoleAppContext, IConfiguration, ILoggingBuilder> configure)"))
827861
{
862+
sb.AppendLine("this.requireConfiguration = true;");
828863
sb.AppendLine("this.configureLogging = configure;");
864+
sb.AppendLine("return this;");
829865
}
830-
sb.AppendLine("return this;");
831866
}
832-
833-
if (dllReference.HasConfiguration)
867+
else
834868
{
835869
sb.AppendLine();
836-
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<IConfiguration, ILoggingBuilder> configure)"))
870+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ILoggingBuilder> configure)"))
871+
{
872+
sb.AppendLine("this.configureLogging = (_, logging) => configure(logging);");
873+
sb.AppendLine("return this;");
874+
}
875+
876+
sb.AppendLine();
877+
using (sb.BeginBlock("public ConsoleApp.ConsoleAppBuilder ConfigureLogging(Action<ConsoleAppContext, ILoggingBuilder> configure)"))
837878
{
838-
sb.AppendLine("this.requireConfiguration = true;");
839879
sb.AppendLine("this.configureLogging = configure;");
840880
sb.AppendLine("return this;");
841881
}
842882
}
883+
843884
sb.AppendLine();
844885
}
845886

@@ -869,11 +910,11 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
869910
sb.AppendLine("var services = new ServiceCollection();");
870911
if (dllReference.HasConfiguration)
871912
{
872-
sb.AppendLine("configureServices?.Invoke(configuration!, services);");
913+
sb.AppendLine("configureServices?.Invoke(context, config!, services);");
873914
}
874915
else
875916
{
876-
sb.AppendLine("configureServices?.Invoke(services);");
917+
sb.AppendLine("configureServices?.Invoke(context, services);");
877918
}
878919

879920
if (dllReference.HasLogging)
@@ -885,11 +926,11 @@ public void EmitConfigure(SourceBuilder sb, DllReference dllReference)
885926
{
886927
if (dllReference.HasConfiguration)
887928
{
888-
sb.AppendLine("configure!(config!, logging);");
929+
sb.AppendLine("configure!(context, config!, logging);");
889930
}
890931
else
891932
{
892-
sb.AppendLine("configure!(logging);");
933+
sb.AppendLine("configure!(context, logging);");
893934
}
894935
}
895936
sb.AppendLine("});");

0 commit comments

Comments
 (0)