-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for HostApplicationBuilder
- Loading branch information
1 parent
28257ec
commit bec4e44
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...st.HostApplicationBuilder/CoconaSample.Advanced.GenericHost.HostApplicationBuilder.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Cocona\Cocona.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
14 changes: 14 additions & 0 deletions
14
samples/CoconaSample.Advanced.GenericHost.HostApplicationBuilder/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.Extensions.Hosting; | ||
|
||
var builder = Host.CreateApplicationBuilder(); | ||
|
||
builder.ConfigureCocona(args, new[] { typeof(Commands) }); | ||
|
||
var host = builder.Build(); | ||
|
||
await host.RunAsync(); | ||
|
||
public class Commands | ||
{ | ||
public void Hello() => Console.WriteLine($"Hello Konnichiwa!"); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Cocona/Hosting/CoconaHostApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Cocona.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Cocona.Builder.Internal; | ||
using Cocona; | ||
using Cocona.Builder; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting; | ||
|
||
public static class CoconaHostApplicationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds and configures a Cocona application. | ||
/// </summary> | ||
/// <param name="hostBuilder"></param> | ||
/// <param name="args"></param> | ||
/// <param name="types"></param> | ||
/// <param name="methods"></param> | ||
/// <returns></returns> | ||
public static HostApplicationBuilder ConfigureCocona( | ||
this HostApplicationBuilder hostBuilder, | ||
string[]? args, | ||
IEnumerable<Type> types, | ||
IEnumerable<Delegate>? methods = null) | ||
=> hostBuilder.ConfigureCocona(args, (app) => | ||
{ | ||
app.AddCommands(types); | ||
|
||
foreach (var method in methods ?? []) | ||
{ | ||
app.AddCommand(method); | ||
} | ||
}); | ||
|
||
/// <summary> | ||
/// Adds and configures a Cocona application. | ||
/// </summary> | ||
/// <param name="hostBuilder"></param> | ||
/// <param name="args"></param> | ||
/// <param name="configureApplication"></param> | ||
/// <returns></returns> | ||
public static HostApplicationBuilder ConfigureCocona( | ||
this HostApplicationBuilder hostBuilder, | ||
string[]? args, | ||
Action<ICoconaCommandsBuilder>? configureApplication = null) | ||
{ | ||
hostBuilder.Services.AddCoconaCore(args); | ||
hostBuilder.Services.AddCoconaShellCompletion(); | ||
|
||
hostBuilder.Services.AddHostedService<CoconaHostedService>(); | ||
|
||
hostBuilder.Services.Configure<CoconaAppHostOptions>(options => | ||
{ | ||
options.ConfigureApplication = app => | ||
{ | ||
configureApplication?.Invoke(app); | ||
}; | ||
}); | ||
return hostBuilder; | ||
} | ||
|
||
} |