| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Hosting; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for configuring <see cref="CreateHostOptions"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class CreateHostOptionsExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Configures the options to use a console logger for startup logging. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="options">The options to configure.</param> |
| | | 16 | | /// <param name="name">The name of the logger. Defaults to "Startup".</param> |
| | | 17 | | /// <param name="level">The minimum log level. Defaults to <see cref="LogLevel.Debug"/>.</param> |
| | | 18 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the console logger configured.</returns> |
| | | 19 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> is null.</exception> |
| | | 20 | | /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is null or whitespace.</exception> |
| | | 21 | | public static CreateHostOptions UsingStartupConsoleLogger( |
| | | 22 | | this CreateHostOptions options, |
| | | 23 | | string name = "Startup", |
| | | 24 | | LogLevel level = LogLevel.Debug) |
| | | 25 | | { |
| | 1 | 26 | | ArgumentNullException.ThrowIfNull(options); |
| | 1 | 27 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 28 | | |
| | 1 | 29 | | using var loggerFactory = LoggerFactory |
| | 2 | 30 | | .Create(builder => builder |
| | 2 | 31 | | .AddConsole() |
| | 2 | 32 | | .SetMinimumLevel(level)); |
| | 1 | 33 | | var logger = loggerFactory.CreateLogger(name); |
| | | 34 | | |
| | 1 | 35 | | var newOptions = options with |
| | 1 | 36 | | { |
| | 1 | 37 | | Logger = logger |
| | 1 | 38 | | }; |
| | | 39 | | |
| | 1 | 40 | | return newOptions; |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Configures the options to use the specified logger for startup diagnostics. |
| | | 45 | | /// Use this when you already have an <see cref="ILogger"/> instance and want to |
| | | 46 | | /// pass it directly instead of having Needlr create a console logger internally. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="options">The options to configure.</param> |
| | | 49 | | /// <param name="logger">The logger to use during startup.</param> |
| | | 50 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the logger configured.</returns> |
| | | 51 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="logger"/> is n |
| | | 52 | | /// <example> |
| | | 53 | | /// <code> |
| | | 54 | | /// var host = new Syringe() |
| | | 55 | | /// .ForHost() |
| | | 56 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 57 | | /// .UsingLogger(myLogger)) |
| | | 58 | | /// .BuildHost(); |
| | | 59 | | /// </code> |
| | | 60 | | /// </example> |
| | | 61 | | public static CreateHostOptions UsingLogger( |
| | | 62 | | this CreateHostOptions options, |
| | | 63 | | ILogger logger) |
| | | 64 | | { |
| | 5 | 65 | | ArgumentNullException.ThrowIfNull(options); |
| | 4 | 66 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 67 | | |
| | 3 | 68 | | return options with { Logger = logger }; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Configures the options to use the current process's command line arguments, |
| | | 73 | | /// automatically stripping the executable path that appears at index 0 of |
| | | 74 | | /// <see cref="Environment.GetCommandLineArgs"/>. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="options">The options to configure.</param> |
| | | 77 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the current process's command line arguments.</r |
| | | 78 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> is null.</exception> |
| | | 79 | | /// <example> |
| | | 80 | | /// <code> |
| | | 81 | | /// var host = new Syringe() |
| | | 82 | | /// .ForHost() |
| | | 83 | | /// .UsingOptions(() => CreateHostOptions.Default |
| | | 84 | | /// .UsingCurrentProcessArgs()) |
| | | 85 | | /// .BuildHost(); |
| | | 86 | | /// </code> |
| | | 87 | | /// </example> |
| | | 88 | | public static CreateHostOptions UsingCurrentProcessArgs( |
| | | 89 | | this CreateHostOptions options) |
| | | 90 | | { |
| | 5 | 91 | | ArgumentNullException.ThrowIfNull(options); |
| | | 92 | | |
| | 4 | 93 | | var args = Environment.GetCommandLineArgs().Skip(1).ToArray(); |
| | 4 | 94 | | return options.UsingArgs(args); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Configures the options to use the specified command line arguments. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="options">The options to configure.</param> |
| | | 101 | | /// <param name="args">The command line arguments to use.</param> |
| | | 102 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the command line arguments configured.</returns> |
| | | 103 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="args"/> is nul |
| | | 104 | | public static CreateHostOptions UsingArgs( |
| | | 105 | | this CreateHostOptions options, |
| | | 106 | | string[] args) |
| | | 107 | | { |
| | 5 | 108 | | ArgumentNullException.ThrowIfNull(options); |
| | 5 | 109 | | ArgumentNullException.ThrowIfNull(args); |
| | | 110 | | |
| | 5 | 111 | | var newOptions = options with |
| | 5 | 112 | | { |
| | 5 | 113 | | Settings = new HostApplicationBuilderSettings |
| | 5 | 114 | | { |
| | 5 | 115 | | Args = args, |
| | 5 | 116 | | ApplicationName = options.Settings.ApplicationName, |
| | 5 | 117 | | ContentRootPath = options.Settings.ContentRootPath, |
| | 5 | 118 | | EnvironmentName = options.Settings.EnvironmentName, |
| | 5 | 119 | | Configuration = options.Settings.Configuration, |
| | 5 | 120 | | DisableDefaults = options.Settings.DisableDefaults |
| | 5 | 121 | | } |
| | 5 | 122 | | }; |
| | | 123 | | |
| | 5 | 124 | | return newOptions; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Configures the options to use the specified application name. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="options">The options to configure.</param> |
| | | 131 | | /// <param name="applicationName">The application name to use.</param> |
| | | 132 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the application name configured.</returns> |
| | | 133 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="applicationNam |
| | | 134 | | public static CreateHostOptions UsingApplicationName( |
| | | 135 | | this CreateHostOptions options, |
| | | 136 | | string applicationName) |
| | | 137 | | { |
| | 5 | 138 | | ArgumentNullException.ThrowIfNull(options); |
| | 5 | 139 | | ArgumentNullException.ThrowIfNull(applicationName); |
| | | 140 | | |
| | 5 | 141 | | var newOptions = options with |
| | 5 | 142 | | { |
| | 5 | 143 | | Settings = new HostApplicationBuilderSettings |
| | 5 | 144 | | { |
| | 5 | 145 | | Args = options.Settings.Args, |
| | 5 | 146 | | ApplicationName = applicationName, |
| | 5 | 147 | | ContentRootPath = options.Settings.ContentRootPath, |
| | 5 | 148 | | EnvironmentName = options.Settings.EnvironmentName, |
| | 5 | 149 | | Configuration = options.Settings.Configuration, |
| | 5 | 150 | | DisableDefaults = options.Settings.DisableDefaults |
| | 5 | 151 | | } |
| | 5 | 152 | | }; |
| | | 153 | | |
| | 5 | 154 | | return newOptions; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Configures the options to use the specified environment name. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="options">The options to configure.</param> |
| | | 161 | | /// <param name="environmentName">The environment name to use.</param> |
| | | 162 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the environment name configured.</returns> |
| | | 163 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="environmentNam |
| | | 164 | | public static CreateHostOptions UsingEnvironmentName( |
| | | 165 | | this CreateHostOptions options, |
| | | 166 | | string environmentName) |
| | | 167 | | { |
| | 3 | 168 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 169 | | ArgumentNullException.ThrowIfNull(environmentName); |
| | | 170 | | |
| | 3 | 171 | | var newOptions = options with |
| | 3 | 172 | | { |
| | 3 | 173 | | Settings = new HostApplicationBuilderSettings |
| | 3 | 174 | | { |
| | 3 | 175 | | Args = options.Settings.Args, |
| | 3 | 176 | | ApplicationName = options.Settings.ApplicationName, |
| | 3 | 177 | | ContentRootPath = options.Settings.ContentRootPath, |
| | 3 | 178 | | EnvironmentName = environmentName, |
| | 3 | 179 | | Configuration = options.Settings.Configuration, |
| | 3 | 180 | | DisableDefaults = options.Settings.DisableDefaults |
| | 3 | 181 | | } |
| | 3 | 182 | | }; |
| | | 183 | | |
| | 3 | 184 | | return newOptions; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Configures the options to use the specified content root path. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <param name="options">The options to configure.</param> |
| | | 191 | | /// <param name="contentRootPath">The content root path to use.</param> |
| | | 192 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the content root path configured.</returns> |
| | | 193 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="contentRootPat |
| | | 194 | | public static CreateHostOptions UsingContentRootPath( |
| | | 195 | | this CreateHostOptions options, |
| | | 196 | | string contentRootPath) |
| | | 197 | | { |
| | 1 | 198 | | ArgumentNullException.ThrowIfNull(options); |
| | 1 | 199 | | ArgumentNullException.ThrowIfNull(contentRootPath); |
| | | 200 | | |
| | 1 | 201 | | var newOptions = options with |
| | 1 | 202 | | { |
| | 1 | 203 | | Settings = new HostApplicationBuilderSettings |
| | 1 | 204 | | { |
| | 1 | 205 | | Args = options.Settings.Args, |
| | 1 | 206 | | ApplicationName = options.Settings.ApplicationName, |
| | 1 | 207 | | ContentRootPath = contentRootPath, |
| | 1 | 208 | | EnvironmentName = options.Settings.EnvironmentName, |
| | 1 | 209 | | Configuration = options.Settings.Configuration, |
| | 1 | 210 | | DisableDefaults = options.Settings.DisableDefaults |
| | 1 | 211 | | } |
| | 1 | 212 | | }; |
| | | 213 | | |
| | 1 | 214 | | return newOptions; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Adds a pre-plugin registration callback to the options. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <param name="options">The options to configure.</param> |
| | | 221 | | /// <param name="callback">The callback to add for pre-plugin registration.</param> |
| | | 222 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callback added.</returns> |
| | | 223 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callback"/> is |
| | | 224 | | public static CreateHostOptions UsingPrePluginRegistrationCallback( |
| | | 225 | | this CreateHostOptions options, |
| | | 226 | | Action<IServiceCollection> callback) |
| | | 227 | | { |
| | 2 | 228 | | ArgumentNullException.ThrowIfNull(options); |
| | 2 | 229 | | ArgumentNullException.ThrowIfNull(callback); |
| | 2 | 230 | | return options.UsingPrePluginRegistrationCallbacks(callback); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Adds multiple pre-plugin registration callbacks to the options. |
| | | 235 | | /// </summary> |
| | | 236 | | /// <param name="options">The options to configure.</param> |
| | | 237 | | /// <param name="callbacks">The callbacks to add for pre-plugin registration.</param> |
| | | 238 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 239 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 240 | | public static CreateHostOptions UsingPrePluginRegistrationCallbacks( |
| | | 241 | | this CreateHostOptions options, |
| | | 242 | | params Action<IServiceCollection>[] callbacks) |
| | | 243 | | { |
| | 3 | 244 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 245 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | 3 | 246 | | return options.UsingPrePluginRegistrationCallbacks(callbacks.AsEnumerable()); |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Adds multiple pre-plugin registration callbacks to the options. |
| | | 251 | | /// </summary> |
| | | 252 | | /// <param name="options">The options to configure.</param> |
| | | 253 | | /// <param name="callbacks">The callbacks to add for pre-plugin registration.</param> |
| | | 254 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 255 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 256 | | public static CreateHostOptions UsingPrePluginRegistrationCallbacks( |
| | | 257 | | this CreateHostOptions options, |
| | | 258 | | IEnumerable<Action<IServiceCollection>> callbacks) |
| | | 259 | | { |
| | 3 | 260 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 261 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | | 262 | | |
| | 3 | 263 | | var allCallbacks = new List<Action<IServiceCollection>>(options.PrePluginRegistrationCallbacks); |
| | 3 | 264 | | allCallbacks.AddRange(callbacks); |
| | | 265 | | |
| | 3 | 266 | | var newOptions = options with |
| | 3 | 267 | | { |
| | 3 | 268 | | PrePluginRegistrationCallbacks = allCallbacks |
| | 3 | 269 | | }; |
| | | 270 | | |
| | 3 | 271 | | return newOptions; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Adds a post-plugin registration callback to the options. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name="options">The options to configure.</param> |
| | | 278 | | /// <param name="callback">The callback to add for post-plugin registration.</param> |
| | | 279 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callback added.</returns> |
| | | 280 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callback"/> is |
| | | 281 | | public static CreateHostOptions UsingPostPluginRegistrationCallback( |
| | | 282 | | this CreateHostOptions options, |
| | | 283 | | Action<IServiceCollection> callback) |
| | | 284 | | { |
| | 7 | 285 | | ArgumentNullException.ThrowIfNull(callback); |
| | 6 | 286 | | return options.UsingPostPluginRegistrationCallbacks(callback); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Adds multiple post-plugin registration callbacks to the options. |
| | | 291 | | /// </summary> |
| | | 292 | | /// <param name="options">The options to configure.</param> |
| | | 293 | | /// <param name="callbacks">The callbacks to add for post-plugin registration.</param> |
| | | 294 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 295 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 296 | | public static CreateHostOptions UsingPostPluginRegistrationCallbacks( |
| | | 297 | | this CreateHostOptions options, |
| | | 298 | | params Action<IServiceCollection>[] callbacks) |
| | | 299 | | { |
| | 9 | 300 | | ArgumentNullException.ThrowIfNull(options); |
| | 9 | 301 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | 8 | 302 | | return options.UsingPostPluginRegistrationCallbacks(callbacks.AsEnumerable()); |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Adds multiple post-plugin registration callbacks to the options. |
| | | 307 | | /// </summary> |
| | | 308 | | /// <param name="options">The options to configure.</param> |
| | | 309 | | /// <param name="callbacks">The callbacks to add for post-plugin registration.</param> |
| | | 310 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 311 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 312 | | public static CreateHostOptions UsingPostPluginRegistrationCallbacks( |
| | | 313 | | this CreateHostOptions options, |
| | | 314 | | IEnumerable<Action<IServiceCollection>> callbacks) |
| | | 315 | | { |
| | 10 | 316 | | ArgumentNullException.ThrowIfNull(options); |
| | 10 | 317 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | | 318 | | |
| | 9 | 319 | | var allCallbacks = new List<Action<IServiceCollection>>(options.PostPluginRegistrationCallbacks); |
| | 9 | 320 | | allCallbacks.AddRange(callbacks); |
| | | 321 | | |
| | 9 | 322 | | var newOptions = options with |
| | 9 | 323 | | { |
| | 9 | 324 | | PostPluginRegistrationCallbacks = allCallbacks |
| | 9 | 325 | | }; |
| | | 326 | | |
| | 9 | 327 | | return newOptions; |
| | | 328 | | } |
| | | 329 | | } |