< Summary

Information
Class: NexusLabs.Needlr.Hosting.CreateHostOptionsExtensions
Assembly: NexusLabs.Needlr.Hosting
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Hosting/CreateHostOptionsExtensions.cs
Line coverage
100%
Covered lines: 102
Uncovered lines: 0
Coverable lines: 102
Total lines: 275
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Hosting/CreateHostOptionsExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.Hosting;
 3using Microsoft.Extensions.Logging;
 4
 5namespace NexusLabs.Needlr.Hosting;
 6
 7/// <summary>
 8/// Provides extension methods for configuring <see cref="CreateHostOptions"/>.
 9/// </summary>
 10public 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    {
 126        ArgumentNullException.ThrowIfNull(options);
 127        ArgumentException.ThrowIfNullOrWhiteSpace(name);
 28
 129        using var loggerFactory = LoggerFactory
 230            .Create(builder => builder
 231            .AddConsole()
 232            .SetMinimumLevel(level));
 133        var logger = loggerFactory.CreateLogger(name);
 34
 135        var newOptions = options with
 136        {
 137            Logger = logger
 138        };
 39
 140        return newOptions;
 141    }
 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    {
 154        ArgumentNullException.ThrowIfNull(options);
 155        ArgumentNullException.ThrowIfNull(args);
 56
 157        var newOptions = options with
 158        {
 159            Settings = new HostApplicationBuilderSettings
 160            {
 161                Args = args,
 162                ApplicationName = options.Settings.ApplicationName,
 163                ContentRootPath = options.Settings.ContentRootPath,
 164                EnvironmentName = options.Settings.EnvironmentName,
 165                Configuration = options.Settings.Configuration,
 166                DisableDefaults = options.Settings.DisableDefaults
 167            }
 168        };
 69
 170        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    {
 384        ArgumentNullException.ThrowIfNull(options);
 385        ArgumentNullException.ThrowIfNull(applicationName);
 86
 387        var newOptions = options with
 388        {
 389            Settings = new HostApplicationBuilderSettings
 390            {
 391                Args = options.Settings.Args,
 392                ApplicationName = applicationName,
 393                ContentRootPath = options.Settings.ContentRootPath,
 394                EnvironmentName = options.Settings.EnvironmentName,
 395                Configuration = options.Settings.Configuration,
 396                DisableDefaults = options.Settings.DisableDefaults
 397            }
 398        };
 99
 3100        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    {
 3114        ArgumentNullException.ThrowIfNull(options);
 3115        ArgumentNullException.ThrowIfNull(environmentName);
 116
 3117        var newOptions = options with
 3118        {
 3119            Settings = new HostApplicationBuilderSettings
 3120            {
 3121                Args = options.Settings.Args,
 3122                ApplicationName = options.Settings.ApplicationName,
 3123                ContentRootPath = options.Settings.ContentRootPath,
 3124                EnvironmentName = environmentName,
 3125                Configuration = options.Settings.Configuration,
 3126                DisableDefaults = options.Settings.DisableDefaults
 3127            }
 3128        };
 129
 3130        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    {
 1144        ArgumentNullException.ThrowIfNull(options);
 1145        ArgumentNullException.ThrowIfNull(contentRootPath);
 146
 1147        var newOptions = options with
 1148        {
 1149            Settings = new HostApplicationBuilderSettings
 1150            {
 1151                Args = options.Settings.Args,
 1152                ApplicationName = options.Settings.ApplicationName,
 1153                ContentRootPath = contentRootPath,
 1154                EnvironmentName = options.Settings.EnvironmentName,
 1155                Configuration = options.Settings.Configuration,
 1156                DisableDefaults = options.Settings.DisableDefaults
 1157            }
 1158        };
 159
 1160        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    {
 2174        ArgumentNullException.ThrowIfNull(options);
 2175        ArgumentNullException.ThrowIfNull(callback);
 2176        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    {
 3190        ArgumentNullException.ThrowIfNull(options);
 3191        ArgumentNullException.ThrowIfNull(callbacks);
 3192        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    {
 3206        ArgumentNullException.ThrowIfNull(options);
 3207        ArgumentNullException.ThrowIfNull(callbacks);
 208
 3209        var allCallbacks = new List<Action<IServiceCollection>>(options.PrePluginRegistrationCallbacks);
 3210        allCallbacks.AddRange(callbacks);
 211
 3212        var newOptions = options with
 3213        {
 3214            PrePluginRegistrationCallbacks = allCallbacks
 3215        };
 216
 3217        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    {
 7231        ArgumentNullException.ThrowIfNull(callback);
 6232        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    {
 9246        ArgumentNullException.ThrowIfNull(options);
 9247        ArgumentNullException.ThrowIfNull(callbacks);
 8248        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    {
 10262        ArgumentNullException.ThrowIfNull(options);
 10263        ArgumentNullException.ThrowIfNull(callbacks);
 264
 9265        var allCallbacks = new List<Action<IServiceCollection>>(options.PostPluginRegistrationCallbacks);
 9266        allCallbacks.AddRange(callbacks);
 267
 9268        var newOptions = options with
 9269        {
 9270            PostPluginRegistrationCallbacks = allCallbacks
 9271        };
 272
 9273        return newOptions;
 274    }
 275}