|
1 | | -using ConsoleAppFramework; |
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using ConsoleAppFramework; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Hosting; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using System.Reflection; |
| 9 | +using ZLogger; |
2 | 10 |
|
3 | 11 | [assembly: ConsoleAppFrameworkGeneratorOptions(DisableNamingConversion = true)] |
4 | 12 |
|
5 | 13 |
|
6 | | -args = ["HelloWorld", "--help"]; |
7 | | -var app = ConsoleApp.Create(); |
8 | | -//app.Add<MyCommands>(); |
9 | | -app.Add<Tacommands>(); |
10 | | -app.Run(args); |
11 | 14 |
|
| 15 | +var app = ConsoleApp.Create() |
| 16 | + ; |
| 17 | + |
| 18 | +app.ConfigureDefaultConfiguration(); |
| 19 | + |
| 20 | +app.ConfigureServices(services => |
| 21 | +{ |
| 22 | + |
| 23 | +}); |
| 24 | + |
| 25 | + // .ConfigureLogging( |
| 26 | + // .ConfigureDefaultConfiguration() |
| 27 | + // ; |
| 28 | + |
| 29 | +app.Add("", () => { }); |
| 30 | + |
| 31 | +app.Run(args); |
12 | 32 |
|
13 | 33 |
|
14 | 34 |
|
@@ -71,22 +91,79 @@ internal static partial class ConsoleApp |
71 | 91 |
|
72 | 92 |
|
73 | 93 |
|
74 | | - //internal partial struct ConsoleAppBuilder |
| 94 | + //internal partial class ConsoleAppBuilder |
75 | 95 | //{ |
76 | | - // /// <summary> |
77 | | - // /// Add all [RegisterCommands] types as ConsoleApp command. |
78 | | - // /// </summary> |
79 | | - // public void RegisterAll() |
| 96 | + // bool requireConfiguration; |
| 97 | + // IConfiguration? configuration; |
| 98 | + // Action<IConfiguration, IServiceCollection>? configureServices; |
| 99 | + // Action<IConfiguration, ILoggingBuilder>? configureLogging; |
| 100 | + |
| 101 | + // /// <summary>Create configuration with SetBasePath(Directory.GetCurrentDirectory()) and AddJsonFile("appsettings.json").</summary> |
| 102 | + // public void ConfigureDefaultConfiguration(Action<IConfigurationBuilder> configure) |
80 | 103 | // { |
| 104 | + // var config = new ConfigurationBuilder(); |
| 105 | + // config.SetBasePath(System.IO.Directory.GetCurrentDirectory()); |
| 106 | + // config.AddJsonFile("appsettings.json", optional: true); |
| 107 | + // configure(config); |
| 108 | + // configuration = config.Build(); |
81 | 109 | // } |
82 | 110 |
|
83 | | - // /// <summary> |
84 | | - // /// Add all [RegisterCommands] types as ConsoleApp command. |
85 | | - // /// </summary> |
86 | | - // public void RegisterAll(string commandPath) |
| 111 | + // public void ConfigureEmptyConfiguration(Action<IConfigurationBuilder> configure) |
87 | 112 | // { |
| 113 | + // var config = new ConfigurationBuilder(); |
| 114 | + // configure(config); |
| 115 | + // configuration = config.Build(); |
88 | 116 | // } |
89 | 117 |
|
| 118 | + // public void ConfigureServices(Action<IServiceCollection> configure) |
| 119 | + // { |
| 120 | + // this.configureServices = (_, services) => configure(services); |
| 121 | + // } |
| 122 | + |
| 123 | + // public void ConfigureServices(Action<IConfiguration, IServiceCollection> configure) |
| 124 | + // { |
| 125 | + // this.requireConfiguration = true; |
| 126 | + // this.configureServices = configure; |
| 127 | + // } |
| 128 | + |
| 129 | + // public void ConfigureLogging(Action<ILoggingBuilder> configure) |
| 130 | + // { |
| 131 | + // this.configureLogging = (_, builder) => configure(builder); |
| 132 | + // } |
| 133 | + |
| 134 | + // public void ConfigureLogging(Action<IConfiguration, ILoggingBuilder> configure) |
| 135 | + // { |
| 136 | + // this.requireConfiguration = true; |
| 137 | + // this.configureLogging = configure; |
| 138 | + // } |
| 139 | + |
| 140 | + // public void BuildAndSetServiceProvider() |
| 141 | + // { |
| 142 | + // if (configureServices == null && configureLogging == null) return; |
| 143 | + |
| 144 | + // if (configureServices != null) |
| 145 | + // { |
| 146 | + // var services = new ServiceCollection(); |
| 147 | + // configureServices?.Invoke(configuration!, services); |
| 148 | + |
| 149 | + // if (configureLogging != null) |
| 150 | + // { |
| 151 | + // var config = configuration; |
| 152 | + // if (requireConfiguration && config == null) |
| 153 | + // { |
| 154 | + // config = new ConfigurationRoot(Array.Empty<IConfigurationProvider>()); |
| 155 | + // } |
| 156 | + |
| 157 | + // var configure = configureLogging; |
| 158 | + // services.AddLogging(logging => |
| 159 | + // { |
| 160 | + // configure!(config!, logging); |
| 161 | + // }); |
| 162 | + // } |
| 163 | + |
| 164 | + // ConsoleApp.ServiceProvider = services.BuildServiceProvider(); |
| 165 | + // } |
| 166 | + // } |
90 | 167 | //} |
91 | 168 | } |
92 | 169 |
|
|
0 commit comments