| | | 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 command line arguments. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="options">The options to configure.</param> |
| | | 47 | | /// <param name="args">The command line arguments to use.</param> |
| | | 48 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the command line arguments configured.</returns> |
| | | 49 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="args"/> is nul |
| | | 50 | | public static CreateHostOptions UsingArgs( |
| | | 51 | | this CreateHostOptions options, |
| | | 52 | | string[] args) |
| | | 53 | | { |
| | 1 | 54 | | ArgumentNullException.ThrowIfNull(options); |
| | 1 | 55 | | ArgumentNullException.ThrowIfNull(args); |
| | | 56 | | |
| | 1 | 57 | | var newOptions = options with |
| | 1 | 58 | | { |
| | 1 | 59 | | Settings = new HostApplicationBuilderSettings |
| | 1 | 60 | | { |
| | 1 | 61 | | Args = args, |
| | 1 | 62 | | ApplicationName = options.Settings.ApplicationName, |
| | 1 | 63 | | ContentRootPath = options.Settings.ContentRootPath, |
| | 1 | 64 | | EnvironmentName = options.Settings.EnvironmentName, |
| | 1 | 65 | | Configuration = options.Settings.Configuration, |
| | 1 | 66 | | DisableDefaults = options.Settings.DisableDefaults |
| | 1 | 67 | | } |
| | 1 | 68 | | }; |
| | | 69 | | |
| | 1 | 70 | | return newOptions; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Configures the options to use the specified application name. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="options">The options to configure.</param> |
| | | 77 | | /// <param name="applicationName">The application name to use.</param> |
| | | 78 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the application name configured.</returns> |
| | | 79 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="applicationNam |
| | | 80 | | public static CreateHostOptions UsingApplicationName( |
| | | 81 | | this CreateHostOptions options, |
| | | 82 | | string applicationName) |
| | | 83 | | { |
| | 3 | 84 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 85 | | ArgumentNullException.ThrowIfNull(applicationName); |
| | | 86 | | |
| | 3 | 87 | | var newOptions = options with |
| | 3 | 88 | | { |
| | 3 | 89 | | Settings = new HostApplicationBuilderSettings |
| | 3 | 90 | | { |
| | 3 | 91 | | Args = options.Settings.Args, |
| | 3 | 92 | | ApplicationName = applicationName, |
| | 3 | 93 | | ContentRootPath = options.Settings.ContentRootPath, |
| | 3 | 94 | | EnvironmentName = options.Settings.EnvironmentName, |
| | 3 | 95 | | Configuration = options.Settings.Configuration, |
| | 3 | 96 | | DisableDefaults = options.Settings.DisableDefaults |
| | 3 | 97 | | } |
| | 3 | 98 | | }; |
| | | 99 | | |
| | 3 | 100 | | return newOptions; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Configures the options to use the specified environment name. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="options">The options to configure.</param> |
| | | 107 | | /// <param name="environmentName">The environment name to use.</param> |
| | | 108 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the environment name configured.</returns> |
| | | 109 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="environmentNam |
| | | 110 | | public static CreateHostOptions UsingEnvironmentName( |
| | | 111 | | this CreateHostOptions options, |
| | | 112 | | string environmentName) |
| | | 113 | | { |
| | 3 | 114 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 115 | | ArgumentNullException.ThrowIfNull(environmentName); |
| | | 116 | | |
| | 3 | 117 | | var newOptions = options with |
| | 3 | 118 | | { |
| | 3 | 119 | | Settings = new HostApplicationBuilderSettings |
| | 3 | 120 | | { |
| | 3 | 121 | | Args = options.Settings.Args, |
| | 3 | 122 | | ApplicationName = options.Settings.ApplicationName, |
| | 3 | 123 | | ContentRootPath = options.Settings.ContentRootPath, |
| | 3 | 124 | | EnvironmentName = environmentName, |
| | 3 | 125 | | Configuration = options.Settings.Configuration, |
| | 3 | 126 | | DisableDefaults = options.Settings.DisableDefaults |
| | 3 | 127 | | } |
| | 3 | 128 | | }; |
| | | 129 | | |
| | 3 | 130 | | return newOptions; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Configures the options to use the specified content root path. |
| | | 135 | | /// </summary> |
| | | 136 | | /// <param name="options">The options to configure.</param> |
| | | 137 | | /// <param name="contentRootPath">The content root path to use.</param> |
| | | 138 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the content root path configured.</returns> |
| | | 139 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="contentRootPat |
| | | 140 | | public static CreateHostOptions UsingContentRootPath( |
| | | 141 | | this CreateHostOptions options, |
| | | 142 | | string contentRootPath) |
| | | 143 | | { |
| | 1 | 144 | | ArgumentNullException.ThrowIfNull(options); |
| | 1 | 145 | | ArgumentNullException.ThrowIfNull(contentRootPath); |
| | | 146 | | |
| | 1 | 147 | | var newOptions = options with |
| | 1 | 148 | | { |
| | 1 | 149 | | Settings = new HostApplicationBuilderSettings |
| | 1 | 150 | | { |
| | 1 | 151 | | Args = options.Settings.Args, |
| | 1 | 152 | | ApplicationName = options.Settings.ApplicationName, |
| | 1 | 153 | | ContentRootPath = contentRootPath, |
| | 1 | 154 | | EnvironmentName = options.Settings.EnvironmentName, |
| | 1 | 155 | | Configuration = options.Settings.Configuration, |
| | 1 | 156 | | DisableDefaults = options.Settings.DisableDefaults |
| | 1 | 157 | | } |
| | 1 | 158 | | }; |
| | | 159 | | |
| | 1 | 160 | | return newOptions; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Adds a pre-plugin registration callback to the options. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="options">The options to configure.</param> |
| | | 167 | | /// <param name="callback">The callback to add for pre-plugin registration.</param> |
| | | 168 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callback added.</returns> |
| | | 169 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callback"/> is |
| | | 170 | | public static CreateHostOptions UsingPrePluginRegistrationCallback( |
| | | 171 | | this CreateHostOptions options, |
| | | 172 | | Action<IServiceCollection> callback) |
| | | 173 | | { |
| | 2 | 174 | | ArgumentNullException.ThrowIfNull(options); |
| | 2 | 175 | | ArgumentNullException.ThrowIfNull(callback); |
| | 2 | 176 | | return options.UsingPrePluginRegistrationCallbacks(callback); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Adds multiple pre-plugin registration callbacks to the options. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <param name="options">The options to configure.</param> |
| | | 183 | | /// <param name="callbacks">The callbacks to add for pre-plugin registration.</param> |
| | | 184 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 185 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 186 | | public static CreateHostOptions UsingPrePluginRegistrationCallbacks( |
| | | 187 | | this CreateHostOptions options, |
| | | 188 | | params Action<IServiceCollection>[] callbacks) |
| | | 189 | | { |
| | 3 | 190 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 191 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | 3 | 192 | | return options.UsingPrePluginRegistrationCallbacks(callbacks.AsEnumerable()); |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Adds multiple pre-plugin registration callbacks to the options. |
| | | 197 | | /// </summary> |
| | | 198 | | /// <param name="options">The options to configure.</param> |
| | | 199 | | /// <param name="callbacks">The callbacks to add for pre-plugin registration.</param> |
| | | 200 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 201 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 202 | | public static CreateHostOptions UsingPrePluginRegistrationCallbacks( |
| | | 203 | | this CreateHostOptions options, |
| | | 204 | | IEnumerable<Action<IServiceCollection>> callbacks) |
| | | 205 | | { |
| | 3 | 206 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 207 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | | 208 | | |
| | 3 | 209 | | var allCallbacks = new List<Action<IServiceCollection>>(options.PrePluginRegistrationCallbacks); |
| | 3 | 210 | | allCallbacks.AddRange(callbacks); |
| | | 211 | | |
| | 3 | 212 | | var newOptions = options with |
| | 3 | 213 | | { |
| | 3 | 214 | | PrePluginRegistrationCallbacks = allCallbacks |
| | 3 | 215 | | }; |
| | | 216 | | |
| | 3 | 217 | | return newOptions; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Adds a post-plugin registration callback to the options. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <param name="options">The options to configure.</param> |
| | | 224 | | /// <param name="callback">The callback to add for post-plugin registration.</param> |
| | | 225 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callback added.</returns> |
| | | 226 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callback"/> is |
| | | 227 | | public static CreateHostOptions UsingPostPluginRegistrationCallback( |
| | | 228 | | this CreateHostOptions options, |
| | | 229 | | Action<IServiceCollection> callback) |
| | | 230 | | { |
| | 7 | 231 | | ArgumentNullException.ThrowIfNull(callback); |
| | 6 | 232 | | return options.UsingPostPluginRegistrationCallbacks(callback); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Adds multiple post-plugin registration callbacks to the options. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="options">The options to configure.</param> |
| | | 239 | | /// <param name="callbacks">The callbacks to add for post-plugin registration.</param> |
| | | 240 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 241 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 242 | | public static CreateHostOptions UsingPostPluginRegistrationCallbacks( |
| | | 243 | | this CreateHostOptions options, |
| | | 244 | | params Action<IServiceCollection>[] callbacks) |
| | | 245 | | { |
| | 9 | 246 | | ArgumentNullException.ThrowIfNull(options); |
| | 9 | 247 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | 8 | 248 | | return options.UsingPostPluginRegistrationCallbacks(callbacks.AsEnumerable()); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Adds multiple post-plugin registration callbacks to the options. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <param name="options">The options to configure.</param> |
| | | 255 | | /// <param name="callbacks">The callbacks to add for post-plugin registration.</param> |
| | | 256 | | /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns> |
| | | 257 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i |
| | | 258 | | public static CreateHostOptions UsingPostPluginRegistrationCallbacks( |
| | | 259 | | this CreateHostOptions options, |
| | | 260 | | IEnumerable<Action<IServiceCollection>> callbacks) |
| | | 261 | | { |
| | 10 | 262 | | ArgumentNullException.ThrowIfNull(options); |
| | 10 | 263 | | ArgumentNullException.ThrowIfNull(callbacks); |
| | | 264 | | |
| | 9 | 265 | | var allCallbacks = new List<Action<IServiceCollection>>(options.PostPluginRegistrationCallbacks); |
| | 9 | 266 | | allCallbacks.AddRange(callbacks); |
| | | 267 | | |
| | 9 | 268 | | var newOptions = options with |
| | 9 | 269 | | { |
| | 9 | 270 | | PostPluginRegistrationCallbacks = allCallbacks |
| | 9 | 271 | | }; |
| | | 272 | | |
| | 9 | 273 | | return newOptions; |
| | | 274 | | } |
| | | 275 | | } |