Skip to content

Commit

Permalink
Add CreateFromHostBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 1, 2022
1 parent 39dfb8a commit 61357c2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
14 changes: 12 additions & 2 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ This converting behaviour can configure by `ConsoleAppOptions.NameConverter`.

ConsoleApp / ConsoleAppBuilder
---
`ConsoleApp` is an entrypoint of creating ConsoleAppFramework app. It has three APIs, `Create`, `CreateBuilder` and `Run`.
`ConsoleApp` is an entrypoint of creating ConsoleAppFramework app. It has three APIs, `Create`, `CreateBuilder`, `CreateFromHostBuilder` and `Run`.

```csharp
// Create is shorthand of CraeteBuilder(args).Build();
Expand All @@ -387,7 +387,7 @@ ConsoleApp.Run(args, /* lambda expression */);
ConsoleApp.Run<MyCommands>(args);
```

When calling `Create/CreateBuilder`, also configure `ConsoleAppOptions`. Full option details, see [ConsoleAppOptions](#consoleappoptions) section.
When calling `Create/CreateBuilder/CreateFromHostBuilder`, also configure `ConsoleAppOptions`. Full option details, see [ConsoleAppOptions](#consoleappoptions) section.

```csharp
var app = ConsoleApp.Create(args, options =>
Expand All @@ -397,6 +397,16 @@ var app = ConsoleApp.Create(args, options =>
});
```

Advanced API of `ConsoleApp`, `CreateFromHostBuilder` creates ConsoleApp from IHostBuilder.

```csharp
// Setup services outside of ConsoleAppFramework.
var hostBuilder = Host.CreateDefaultBuilder()
.ConfigureServices();

var app = ConsoleApp.CreateFromHostBuilder(hostBuilder);
```

`ConsoleAppBuilder` itself is `IHostBuilder` so you can use any configuration methods like `ConfigureServices`, `ConfigureLogging`, etc. If method chain is not returns `ConsoleAppBuilder`(for example, using external lib's extension methods), can not get `ConsoleApp` directly. In that case, use `BuildAsConsoleApp()` instead of `Build()`.

`ConsoleApp` exposes some utility properties.
Expand Down
21 changes: 18 additions & 3 deletions src/ConsoleAppFramework/ConsoleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,32 @@ public static ConsoleApp Create(string[] args, Action<HostBuilderContext, Consol

public static ConsoleAppBuilder CreateBuilder(string[] args)
{
return new ConsoleAppBuilder(args);
return new ConsoleAppBuilder(args, Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args));
}

public static ConsoleAppBuilder CreateBuilder(string[] args, Action<ConsoleAppOptions> configureOptions)
{
return new ConsoleAppBuilder(args, configureOptions);
return new ConsoleAppBuilder(args, Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args), configureOptions);
}

public static ConsoleAppBuilder CreateBuilder(string[] args, Action<HostBuilderContext, ConsoleAppOptions> configureOptions)
{
return new ConsoleAppBuilder(args, configureOptions);
return new ConsoleAppBuilder(args, Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args), configureOptions);
}

public static ConsoleApp CreateFromHostBuilder(IHostBuilder hostBuilder, string[] args)
{
return new ConsoleAppBuilder(args, hostBuilder).Build();
}

public static ConsoleApp CreateFromHostBuilder(IHostBuilder hostBuilder, string[] args, Action<ConsoleAppOptions> configureOptions)
{
return new ConsoleAppBuilder(args, hostBuilder, configureOptions).Build();
}

public static ConsoleApp CreateFromHostBuilder(IHostBuilder hostBuilder, string[] args, Action<HostBuilderContext, ConsoleAppOptions> configureOptions)
{
return new ConsoleAppBuilder(args, hostBuilder, configureOptions).Build();
}

public static void Run(string[] args, Delegate rootCommand)
Expand Down
22 changes: 6 additions & 16 deletions src/ConsoleAppFramework/ConsoleAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,26 @@ public class ConsoleAppBuilder : IHostBuilder
{
readonly IHostBuilder builder;

internal ConsoleAppBuilder(string[] args)
: this(args, (_, __) => { })
internal ConsoleAppBuilder(string[] args, IHostBuilder hostBuilder)
: this(args, hostBuilder, (_, __) => { })
{
}

internal ConsoleAppBuilder(string[] args, ConsoleAppOptions consoleAppOptions)
internal ConsoleAppBuilder(string[] args, IHostBuilder hostBuilder, ConsoleAppOptions consoleAppOptions)
{
var hostBuilder = new HostBuilder();
hostBuilder.ConfigureDefaults(args);
this.builder = AddConsoleAppFramework(hostBuilder, args, consoleAppOptions, null);
}

internal ConsoleAppBuilder(string[] args, Action<ConsoleAppOptions> configureOptions)
: this(args, (_, options) => configureOptions(options))
internal ConsoleAppBuilder(string[] args, IHostBuilder hostBuilder, Action<ConsoleAppOptions> configureOptions)
: this(args, hostBuilder, (_, options) => configureOptions(options))
{
}

internal ConsoleAppBuilder(string[] args, Action<HostBuilderContext, ConsoleAppOptions> configureOptions)
internal ConsoleAppBuilder(string[] args, IHostBuilder hostBuilder, Action<HostBuilderContext, ConsoleAppOptions> configureOptions)
{
var hostBuilder = new HostBuilder();
hostBuilder.ConfigureDefaults(args);
this.builder = AddConsoleAppFramework(hostBuilder, args, new ConsoleAppOptions(), configureOptions);
}

// internal use for legacy compatible
internal ConsoleAppBuilder(string[] args, IHostBuilder hostBuilder, ConsoleAppOptions options)
{
this.builder = AddConsoleAppFramework(hostBuilder, args, options, (_, __) => { });
}

IHostBuilder AddConsoleAppFramework(IHostBuilder builder, string[] args, ConsoleAppOptions options, Action<HostBuilderContext, ConsoleAppOptions>? configureOptions)
{
return builder
Expand Down

0 comments on commit 61357c2

Please sign in to comment.