Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
* リポジトリ名を Hosting.ConsoleHandler にリネーム。
* ICommand を IConsoleHandler にリネーム。
  • Loading branch information
in-async committed Feb 5, 2020
1 parent d165469 commit 59bec06
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Hosting.Command.sln → Hosting.ConsoleHandler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inasync.Hosting.Command", "Inasync.Hosting.Command\Inasync.Hosting.Command.csproj", "{42F6D4FF-4408-4267-A246-43CF3E211736}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inasync.Hosting.ConsoleHandler", "Inasync.Hosting.ConsoleHandler\Inasync.Hosting.ConsoleHandler.csproj", "{42F6D4FF-4408-4267-A246-43CF3E211736}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inasync.Hosting.Command.Tests", "Inasync.Hosting.Command.Tests\Inasync.Hosting.Command.Tests.csproj", "{7C1C9CBC-B95D-4F2B-947E-8FB18C5D9879}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inasync.Hosting.ConsoleHandler.Tests", "Inasync.Hosting.ConsoleHandler.Tests\Inasync.Hosting.ConsoleHandler.Tests.csproj", "{7C1C9CBC-B95D-4F2B-947E-8FB18C5D9879}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{58246883-9B70-4EE6-8A3F-B1CFF6468A40}"
ProjectSection(SolutionItems) = preProject
Expand Down
3 changes: 0 additions & 3 deletions Inasync.Hosting.Command/InternalsVisibleTo.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Inasync.Hosting.Command\Inasync.Hosting.Command.csproj" />
<ProjectReference Include="..\Inasync.Hosting.ConsoleHandler\Inasync.Hosting.ConsoleHandler.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class UsageTests {
[TestMethod]
[DataRow(new[] { "foo", "bar" })]
public Task Usage1(string[] args) {
// (Disposable な) ICommand を呼ぶシナリオ
return Host.CreateDefaultBuilder(args).InvokeAsync<DisposableCommand>();
// (Disposable な) IConsoleHandler を呼ぶシナリオ
return Host.CreateDefaultBuilder(args).InvokeAsync<DisposableHandler>();
}

[TestMethod]
Expand All @@ -25,8 +25,8 @@ public Task Usage2(string[] args) {
return Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddSingleton<DisposableUseCase>())
.InvokeAsync(provider => {
var command = provider.GetRequiredService<DisposableUseCase>();
return cancellationToken => command.ExecuteAsync(args, cancellationToken);
var handler = provider.GetRequiredService<DisposableUseCase>();
return cancellationToken => handler.ExecuteAsync(args, cancellationToken);
});
}

Expand All @@ -49,15 +49,15 @@ public Task Usage3(string[] args) {
public void Usage4(string[] args) {
// 例外を抑制するシナリオ
TestAA
.Act(() => Host.CreateDefaultBuilder(args).InvokeAsync<ThrowExceptionCommand>())
.Act(() => Host.CreateDefaultBuilder(args).InvokeAsync<ThrowExceptionHandler>())
.Assert();

// 例外を返すシナリオ
TestAA
.Act(() => Host
.CreateDefaultBuilder(args)
.ConfigureServices(services => services.Configure<CommandOptions>(x => x.ThrowException = true))
.InvokeAsync<ThrowExceptionCommand>()
.ConfigureServices(services => services.Configure<ConsoleHandlerOptions>(x => x.ThrowException = true))
.InvokeAsync<ThrowExceptionHandler>()
)
.Assert<ApplicationException>();
}
Expand All @@ -71,16 +71,16 @@ public void Usage5(string[] args) {
TestAA
.Act(() => Host
.CreateDefaultBuilder(args)
.ConfigureServices(services => services.Configure<CommandOptions>(x => x.ThrowException = true))
.InvokeAsync<DisposableCommand>(cts.Token)
.ConfigureServices(services => services.Configure<ConsoleHandlerOptions>(x => x.ThrowException = true))
.InvokeAsync<DisposableHandler>(cts.Token)
)
.Assert<OperationCanceledException>();
}

private sealed class DisposableCommand : ICommand, IDisposable {
private readonly ILogger<DisposableCommand> _logger;
private sealed class DisposableHandler : IConsoleHandler, IDisposable {
private readonly ILogger<DisposableHandler> _logger;

public DisposableCommand(ILogger<DisposableCommand> logger) {
public DisposableHandler(ILogger<DisposableHandler> logger) {
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

Expand Down Expand Up @@ -118,10 +118,10 @@ public async Task ExecuteAsync(string[] args, CancellationToken cancellationToke
}
}

private sealed class ThrowExceptionCommand : ICommand {
private readonly ILogger<ThrowExceptionCommand> _logger;
private sealed class ThrowExceptionHandler : IConsoleHandler {
private readonly ILogger<ThrowExceptionHandler> _logger;

public ThrowExceptionCommand(ILogger<ThrowExceptionCommand> logger) {
public ThrowExceptionHandler(ILogger<ThrowExceptionHandler> logger) {
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Inasync.Hosting {

public class CommandOptions {
public class ConsoleHandlerOptions {
public bool ThrowException { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Inasync.Hosting {

internal static class HostApplicationLifetimeExtensions {

public static async Task InvokeAsync(this IHostApplicationLifetime applicationLifetime, Func<CancellationToken, Task> command, CancellationToken cancellationToken = default) {
public static async Task InvokeAsync(this IHostApplicationLifetime applicationLifetime, Func<CancellationToken, Task> handler, CancellationToken cancellationToken = default) {
Debug.Assert(applicationLifetime != null);
Debug.Assert(command != null);
Debug.Assert(handler != null);

using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, applicationLifetime.ApplicationStopping)) {
var stoppingToken = linkedCts.Token;

await command(stoppingToken).ConfigureAwait(false);
await handler(stoppingToken).ConfigureAwait(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ namespace Inasync.Hosting {

public static class HostBuilderExtensions {

public static Task InvokeAsync<TCommand>(this IHostBuilder hostBuilder, CancellationToken cancellationToken = default) where TCommand : ICommand {
public static Task InvokeAsync<THandler>(this IHostBuilder hostBuilder, CancellationToken cancellationToken = default) where THandler : IConsoleHandler {
return hostBuilder
.ConfigureServices(services => services.AddSingleton(typeof(TCommand)))
.ConfigureServices(services => services.AddSingleton(typeof(THandler)))
.InvokeAsync(provider => {
var command = provider.GetRequiredService<TCommand>();
return command.InvokeAsync;
var handler = provider.GetRequiredService<THandler>();
return handler.InvokeAsync;
}, cancellationToken);
}

public static Task InvokeAsync(this IHostBuilder hostBuilder, Func<CancellationToken, Task> command, CancellationToken cancellationToken = default) {
if (command == null) { throw new ArgumentNullException(nameof(command)); }
public static Task InvokeAsync(this IHostBuilder hostBuilder, Func<CancellationToken, Task> handler, CancellationToken cancellationToken = default) {
if (handler == null) { throw new ArgumentNullException(nameof(handler)); }

return InvokeAsync(hostBuilder, _ => command, cancellationToken);
return InvokeAsync(hostBuilder, _ => handler, cancellationToken);
}

public static async Task InvokeAsync(this IHostBuilder hostBuilder, Func<IServiceProvider, Func<CancellationToken, Task>> commandFactory, CancellationToken cancellationToken = default) {
public static async Task InvokeAsync(this IHostBuilder hostBuilder, Func<IServiceProvider, Func<CancellationToken, Task>> handlerFactory, CancellationToken cancellationToken = default) {
if (hostBuilder == null) { throw new ArgumentNullException(nameof(hostBuilder)); }
if (commandFactory == null) { throw new ArgumentNullException(nameof(commandFactory)); }
if (handlerFactory == null) { throw new ArgumentNullException(nameof(handlerFactory)); }

var host = hostBuilder.Build();
var provider = host.Services;
Expand All @@ -36,8 +36,8 @@ public static async Task InvokeAsync(this IHostBuilder hostBuilder, Func<IServic
try {
await host.StartAsync(cancellationToken).ConfigureAwait(false);

var command = commandFactory(provider);
await applicationLifetime.InvokeAsync(command, cancellationToken).ConfigureAwait(false);
var handler = handlerFactory(provider);
await applicationLifetime.InvokeAsync(handler, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException ex) when (applicationLifetime.ApplicationStopping.IsCancellationRequested) {
var logger = provider.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(HostBuilderExtensions));
Expand All @@ -51,7 +51,7 @@ public static async Task InvokeAsync(this IHostBuilder hostBuilder, Func<IServic
var logger = provider.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(HostBuilderExtensions));
logger.LogError(ex, "");

var options = provider.GetRequiredService<IOptions<CommandOptions>>().Value;
var options = provider.GetRequiredService<IOptions<ConsoleHandlerOptions>>().Value;
if (options.ThrowException) { throw; }
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Inasync.Hosting {

public interface ICommand {
public interface IConsoleHandler {

Task InvokeAsync(CancellationToken cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>Inasync.Hosting</RootNamespace>
<Authors>inasync</Authors>
<Description>A helper library for Generic Host that executes a command delegate and automatically stops on exit.</Description>
<PackageProjectUrl>https://github.com/in-async/Hosting.Command</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/in-async/Hosting.Command/blob/master/LICENSE</PackageLicenseUrl>
<PackageTags>library hosting</PackageTags>
<Version>0.2.0</Version>
<Description>A helper library for Generic Host that executes the specified delegate and automatically shut down on exit.</Description>
<PackageProjectUrl>https://github.com/in-async/Hosting.ConsoleHandler</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/in-async/Hosting.ConsoleHandler/blob/master/LICENSE</PackageLicenseUrl>
<PackageTags>library hosting console</PackageTags>
<Version>0.3.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Inasync.Hosting.ConsoleHandler/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Inasync.Hosting.ConsoleHandler.Tests")]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Hosting.Command
[![Build status](https://ci.appveyor.com/api/projects/status/e8aut3n2sxfibo6k/branch/master?svg=true)](https://ci.appveyor.com/project/inasync/hosting-command/branch/master)
[![NuGet](https://img.shields.io/nuget/v/Inasync.Hosting.Command.svg)](https://www.nuget.org/packages/Inasync.Hosting.Command/)
# Hosting.ConsoleHandler
[![Build status](https://ci.appveyor.com/api/projects/status/e8aut3n2sxfibo6k/branch/master?svg=true)](https://ci.appveyor.com/project/inasync/hosting-consolehandler/branch/master)
[![NuGet](https://img.shields.io/nuget/v/Inasync.Hosting.ConsoleHandler.svg)](https://www.nuget.org/packages/Inasync.Hosting.ConsoleHandler/)

***Hosting.Command*** は、コマンド デリゲートを実行して終了時に自動停止する [Generic Host](https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/host/generic-host) のヘルパー ライブラリです。
***Hosting.ConsoleHandler*** は、指定したデリゲートを実行して終了時に自動停止する [Generic Host](https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/host/generic-host) のヘルパー ライブラリです。


## Target Frameworks
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ deploy:
appveyor_repo_tag: true
- provider: NuGet
api_key:
secure: 73/wU1BpGJNgoEhM0baFqjOIWRoo0mMU/doLcUAM7RjpK6OLSTZJsE8Q5Pn9SuK/
secure: sy96q0kIiuIrU6UkWhHdLJr0Y1So2HWRmK/wkmFdkGcxADJJ1nQV15QwZEBm53xJ
on:
appveyor_repo_tag: true

0 comments on commit 59bec06

Please sign in to comment.