Skip to content

Commit

Permalink
cleanup: further code deduplication on new hosting APIs (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster authored Jan 9, 2021
1 parent 643f9c3 commit f5938c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 58 deletions.
69 changes: 19 additions & 50 deletions src/Hosting.CommandLine/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
CancellationToken cancellationToken = default)
where TApp : class
{
return await RunCommandLineApplicationAsync<TApp>(hostBuilder, args, app => { }, cancellationToken);
return await RunCommandLineApplicationAsync<TApp>(hostBuilder, args, null, cancellationToken);
}

/// <summary>
Expand All @@ -56,18 +56,7 @@ public static async Task<int> RunCommandLineApplicationAsync<TApp>(
CancellationToken cancellationToken = default)
where TApp : class
{
var state = new CommandLineState(args);
hostBuilder.Properties[typeof(CommandLineState)] = state;
hostBuilder.ConfigureServices(
(context, services)
=>
{
services.AddCommonServices(state);
services.AddSingleton<ICommandLineService, CommandLineService<TApp>>();
services.AddSingleton(configure);
});

using var host = hostBuilder.Build();
using var host = hostBuilder.UseCommandLineApplication<TApp>(args, configure).Build();
return await host.RunCommandLineApplicationAsync(cancellationToken);
}

Expand All @@ -88,16 +77,14 @@ public static async Task<int> RunCommandLineApplicationAsync(
Action<CommandLineApplication> configure,
CancellationToken cancellationToken = default)
{
configure ??= _ => { };
var state = new CommandLineState(args);
hostBuilder.Properties[typeof(CommandLineState)] = state;
hostBuilder.ConfigureServices(
(context, services)
=>
{
services.AddCommonServices(state);
services.AddSingleton<ICommandLineService, CommandLineService>();
services.AddSingleton(configure);
});
hostBuilder.ConfigureServices((context, services) =>
services
.AddCommonServices(state)
.AddSingleton<ICommandLineService, CommandLineService>()
.AddSingleton(configure));

using var host = hostBuilder.Build();
return await host.RunCommandLineApplicationAsync(cancellationToken);
Expand Down Expand Up @@ -131,50 +118,32 @@ public static IHostBuilder UseCommandLineApplication<TApp>(
Action<CommandLineApplication<TApp>> configure)
where TApp : class
{
configure ??= app => { };
configure ??= _ => { };
var state = new CommandLineState(args);
hostBuilder.Properties[typeof(CommandLineState)] = state;
hostBuilder.ConfigureServices(
(context, services) =>
{
services
.TryAddSingleton<StoreExceptionHandler>();
services
.TryAddSingleton<IUnhandledExceptionHandler>(provider => provider.GetRequiredService<StoreExceptionHandler>());
services
.AddSingleton<IHostLifetime, CommandLineLifetime>()
.TryAddSingleton(PhysicalConsole.Singleton);
services
.AddSingleton(provider =>
{
state.SetConsole(provider.GetService<IConsole>());
return state;
})
.AddSingleton<CommandLineContext>(state)
.AddSingleton<ICommandLineService, CommandLineService<TApp>>();
services
.AddSingleton(configure);
});
hostBuilder.ConfigureServices((context, services) =>
services
.AddCommonServices(state)
.AddSingleton<ICommandLineService, CommandLineService<TApp>>()
.AddSingleton(configure));

return hostBuilder;
}

private static void AddCommonServices(this IServiceCollection services, CommandLineState state)
private static IServiceCollection AddCommonServices(this IServiceCollection services, CommandLineState state)
{
services
.TryAddSingleton<StoreExceptionHandler>();
services
.TryAddSingleton<IUnhandledExceptionHandler>(provider => provider.GetRequiredService<StoreExceptionHandler>());
services.TryAddSingleton<StoreExceptionHandler>();
services.TryAddSingleton<IUnhandledExceptionHandler>(provider => provider.GetRequiredService<StoreExceptionHandler>());
services.TryAddSingleton(PhysicalConsole.Singleton);
services
.AddSingleton<IHostLifetime, CommandLineLifetime>()
.TryAddSingleton(PhysicalConsole.Singleton);
services
.AddSingleton(provider =>
{
state.SetConsole(provider.GetService<IConsole>());
return state;
})
.AddSingleton<CommandLineContext>(state);
return services;
}
}
}
16 changes: 8 additions & 8 deletions src/Hosting.CommandLine/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.Hosting.CommandLine.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Hosting
{
using System;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.Hosting.CommandLine.Internal;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for <see cref="IHost" /> support.
/// </summary>
Expand Down

0 comments on commit f5938c8

Please sign in to comment.