-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineModulePlugin.cs
More file actions
66 lines (54 loc) · 2.43 KB
/
CommandLineModulePlugin.cs
File metadata and controls
66 lines (54 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Modulight.Modules.Hosting;
namespace Modulight.Modules.CommandLine
{
internal sealed class CommandLineModulePlugin : ModuleHostBuilderPlugin
{
public CommandLineModulePlugin(IServiceProvider builderServices, IOptions<CommandLineModuleBuilderOptions> options)
{
BuilderServices = builderServices;
Options = options.Value;
}
IServiceProvider BuilderServices { get; }
public CommandLineModuleBuilderOptions Options { get; }
public override void AfterBuild(ModuleDefinition[] modules, IServiceCollection services)
{
services.AddHostedService<CommandLineWorker>();
if (Options.SuppressStatusMessages is true)
{
services.AddLogging(builder =>
{
builder.AddFilter("Modulight.Modules.Hosting", LogLevel.Warning);
});
}
// From HostBuilder.UseConsoleLifetime
services.AddSingleton<IHostLifetime, ConsoleLifetime>();
services.Configure<ConsoleLifetimeOptions>(o => o.SuppressStatusMessages = Options.SuppressStatusMessages);
base.AfterBuild(modules, services);
}
public override void AfterModule(ModuleDefinition module, IServiceCollection services)
{
if (module.Type.IsModule<ICommandLineModule>())
{
var manifestBuilder = BuilderServices.GetRequiredService<ICommandLineModuleManifestBuilder>();
manifestBuilder.WithDefaultsFromModuleType(module.Type);
{
if (module.Startup is ICommandLineModuleStartup startup)
{
startup.ConfigureCommandLineModuleManifest(manifestBuilder);
}
}
var manifest = manifestBuilder.Build();
services.RegisterModuleManifest(new ModuleManifestServiceEntry<ICommandLineModule, CommandLineModuleManifest>(module.Type, manifest));
foreach (var cmd in manifest.Commands)
services.TryAddScoped(cmd);
}
base.AfterModule(module, services);
}
}
}