Skip to content

Commit

Permalink
Restructure registrations using modules and build separate container …
Browse files Browse the repository at this point in the history
…to resolve config
  • Loading branch information
Elscrux committed Nov 13, 2024
1 parent 41ac930 commit 75bbcf7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 81 deletions.
38 changes: 38 additions & 0 deletions Mutagen.Bethesda.Analyzers.Cli/Modules/AnalyzerCommandModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Autofac;
using Mutagen.Bethesda.Analyzers.Cli.Args;
using Mutagen.Bethesda.Analyzers.Cli.Overrides;
using Mutagen.Bethesda.Analyzers.Reporting.Handlers;
using Mutagen.Bethesda.Environments.DI;
using Mutagen.Bethesda.Plugins.Order.DI;
using Noggog.WorkEngine;

namespace Mutagen.Bethesda.Analyzers.Cli.Modules;

public class AnalyzerCommandModule(RunAnalyzersCommand command) : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterInstance(command).AsImplementedInterfaces();
builder.RegisterInstance(new GameReleaseInjection(command.GameRelease)).AsImplementedInterfaces();
builder.RegisterInstance(new NumWorkThreadsConstant(command.NumThreads)).AsImplementedInterfaces();

if (command.OutputFilePath is not null)
{
builder.RegisterType<CsvReportHandler>().AsImplementedInterfaces().SingleInstance();
builder.RegisterInstance(new CsvInputs(command.OutputFilePath)).AsSelf().AsImplementedInterfaces();
}

if (command.CustomDataFolder is not null)
{
var dataDirectoryProvider = new DataDirectoryInjection(command.CustomDataFolder);
builder.RegisterInstance(dataDirectoryProvider).As<IDataDirectoryProvider>();
}

if (command.UseDataFolderForLoadOrder)
{
builder.RegisterType<DataDirectoryEnabledPluginListingsProvider>().As<IEnabledPluginListingsProvider>();
builder.RegisterType<NullPluginListingsPathProvider>().As<IPluginListingsPathProvider>();
builder.RegisterType<NullCreationClubListingsPathProvider>().As<ICreationClubListingsPathProvider>();
}
}
}
64 changes: 64 additions & 0 deletions Mutagen.Bethesda.Analyzers.Cli/Modules/AnalyzerConfigModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.IO.Abstractions;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Mutagen.Bethesda.Analyzers.Autofac;
using Mutagen.Bethesda.Analyzers.Cli.Overrides;
using Mutagen.Bethesda.Analyzers.Config.Analyzer;
using Mutagen.Bethesda.Analyzers.Reporting.Handlers;
using Mutagen.Bethesda.Environments.DI;
using Mutagen.Bethesda.Plugins.Order.DI;

namespace Mutagen.Bethesda.Analyzers.Cli.Modules;

public class AnalyzerConfigModule(GameRelease gameRelease) : Module
{
protected override void Load(ContainerBuilder builder)
{
var analyzerConfig = GetAnalyzerConfig();

if (analyzerConfig.DataDirectoryPath.HasValue)
{
var dataDirectoryProvider = new DataDirectoryInjection(analyzerConfig.DataDirectoryPath.Value);
builder.RegisterInstance(dataDirectoryProvider).As<IDataDirectoryProvider>();
}

if (analyzerConfig.LoadOrderSetByDataDirectory)
{
builder.RegisterType<DataDirectoryEnabledPluginListingsProvider>().As<IEnabledPluginListingsProvider>();
builder.RegisterType<NullPluginListingsPathProvider>().As<IPluginListingsPathProvider>();
builder.RegisterType<NullCreationClubListingsPathProvider>().As<ICreationClubListingsPathProvider>();
}
else if (analyzerConfig.LoadOrderSetToMods is not null)
{
builder.RegisterInstance(new InjectedEnabledPluginListingsProvider(analyzerConfig.LoadOrderSetToMods)).As<IEnabledPluginListingsProvider>();
builder.RegisterType<NullPluginListingsPathProvider>().As<IPluginListingsPathProvider>();
builder.RegisterType<NullCreationClubListingsPathProvider>().As<ICreationClubListingsPathProvider>();
}

if (analyzerConfig.OutputFilePath is not null)
{
builder.RegisterType<CsvReportHandler>().AsImplementedInterfaces().SingleInstance();
builder.RegisterInstance(new CsvInputs(analyzerConfig.OutputFilePath)).AsSelf().AsImplementedInterfaces();
}
}

private IAnalyzerConfigLookup GetAnalyzerConfig()
{
var services = new ServiceCollection();
services.AddLogging(x => x.AddConsole());

var builder = new ContainerBuilder();
builder.Populate(services);
builder.RegisterInstance(new FileSystem()).As<IFileSystem>();
builder.RegisterModule<MainModule>();
builder.RegisterInstance(new GameReleaseInjection(gameRelease))
.AsImplementedInterfaces();

var container = builder.Build();

var analyzerConfigProvider = container.Resolve<AnalyzerConfigProvider>();
return analyzerConfigProvider.Config;
}
}
13 changes: 3 additions & 10 deletions Mutagen.Bethesda.Analyzers.Cli/Modules/RunAnalyzerModule.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
using Autofac;
using Mutagen.Bethesda.Analyzers.Autofac;
using Mutagen.Bethesda.Analyzers.Cli.Args;
using Mutagen.Bethesda.Analyzers.Reporting.Drops;
using Mutagen.Bethesda.Analyzers.Reporting.Handlers;
using Mutagen.Bethesda.Analyzers.SDK.Drops;

namespace Mutagen.Bethesda.Analyzers.Cli.Modules;

public class RunAnalyzerModule(RunAnalyzersCommand? command) : Module
public class RunAnalyzerModule : Module
{
public RunAnalyzerModule() : this(null) {}

protected override void Load(ContainerBuilder builder)
{
if (command?.OutputFilePath is not null)
{
builder.RegisterType<CsvReportHandler>().AsImplementedInterfaces();
builder.RegisterInstance(new CsvInputs(command.OutputFilePath)).AsSelf().AsImplementedInterfaces();
}

// Last registered runs first
builder.RegisterType<PassToHandlerReportDropbox>().AsImplementedInterfaces();
builder.RegisterDecorator<EditorIdEnricher, IReportDropbox>();
builder.RegisterDecorator<MinimumSeverityFilter, IReportDropbox>();
builder.RegisterDecorator<SeverityAdjuster, IReportDropbox>();
builder.RegisterDecorator<DisallowedParametersChecker, IReportDropbox>();

builder.RegisterType<ConsoleReportHandler>().AsImplementedInterfaces();

builder.RegisterModule<MainModule>();
}
}
80 changes: 9 additions & 71 deletions Mutagen.Bethesda.Analyzers.Cli/RunAnalyzers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
using Microsoft.Extensions.Logging;
using Mutagen.Bethesda.Analyzers.Cli.Args;
using Mutagen.Bethesda.Analyzers.Cli.Modules;
using Mutagen.Bethesda.Analyzers.Cli.Overrides;
using Mutagen.Bethesda.Analyzers.Config.Analyzer;
using Mutagen.Bethesda.Analyzers.Engines;
using Mutagen.Bethesda.Analyzers.Reporting.Handlers;
using Mutagen.Bethesda.Analyzers.SDK.Topics;
using Mutagen.Bethesda.Analyzers.Skyrim;
using Mutagen.Bethesda.Environments.DI;
using Mutagen.Bethesda.Plugins.Order.DI;
using Noggog;
using Noggog.StructuredStrings;
using Noggog.WorkEngine;
Expand All @@ -23,10 +18,10 @@ public static class RunAnalyzers
{
public static async Task<int> Run(RunAnalyzersCommand command)
{
var lifetimeScope = GetContainer(command);
var container = GetContainer(command);

var engine = lifetimeScope.Resolve<ContextualEngine>();
var consumer = lifetimeScope.Resolve<IWorkConsumer>();
var engine = container.Resolve<ContextualEngine>();
var consumer = container.Resolve<IWorkConsumer>();

PrintTopics(command, engine);

Expand Down Expand Up @@ -59,78 +54,21 @@ private static void PrintTopics(RunAnalyzersCommand command, ContextualEngine en
Console.WriteLine();
}

private static ILifetimeScope GetContainer(RunAnalyzersCommand command)
private static IContainer GetContainer(RunAnalyzersCommand command)
{
var services = new ServiceCollection();
services.AddLogging(x => x.AddConsole());

var builder = new ContainerBuilder();
builder.Populate(services);
builder.RegisterInstance(new FileSystem())
.As<IFileSystem>();
builder.RegisterModule(new RunAnalyzerModule(command));
builder.RegisterInstance(new GameReleaseInjection(command.GameRelease))
.AsImplementedInterfaces();
builder.RegisterType<ConsoleReportHandler>().AsImplementedInterfaces();
builder.RegisterInstance(command).AsImplementedInterfaces();
builder.RegisterInstance(new NumWorkThreadsConstant(command.NumThreads)).AsImplementedInterfaces();
builder.RegisterInstance(new FileSystem()).As<IFileSystem>();

if (command.CustomDataFolder is not null)
{
var dataDirectoryProvider = new DataDirectoryInjection(command.CustomDataFolder);
builder.RegisterInstance(dataDirectoryProvider).As<IDataDirectoryProvider>();
}

if (command.UseDataFolderForLoadOrder)
{
builder.RegisterType<DataDirectoryEnabledPluginListingsProvider>().As<IEnabledPluginListingsProvider>();
builder.RegisterType<NullPluginListingsPathProvider>().As<IPluginListingsPathProvider>();
builder.RegisterType<NullCreationClubListingsPathProvider>().As<ICreationClubListingsPathProvider>();
}
builder.RegisterModule<RunAnalyzerModule>();
builder.RegisterModule(new AnalyzerCommandModule(command));
builder.RegisterModule(new AnalyzerConfigModule(command.GameRelease));

builder.RegisterModule<SkyrimAnalyzerModule>();

var container = builder.Build();

return container.BeginLifetimeScope(b =>
{
var analyzerConfigProvider = container.Resolve<AnalyzerConfigProvider>();
var analyzerConfig = analyzerConfigProvider.Config;
if (analyzerConfig.DataDirectoryPath.HasValue)
{
var dataDirectoryProvider = new DataDirectoryInjection(analyzerConfig.DataDirectoryPath.Value);
b.RegisterInstance(dataDirectoryProvider).As<IDataDirectoryProvider>();
}
if (analyzerConfig.LoadOrderSetByDataDirectory)
{
b.RegisterType<DataDirectoryEnabledPluginListingsProvider>().As<IEnabledPluginListingsProvider>();
b.RegisterType<NullPluginListingsPathProvider>().As<IPluginListingsPathProvider>();
b.RegisterType<NullCreationClubListingsPathProvider>().As<ICreationClubListingsPathProvider>();
}
else if (analyzerConfig.LoadOrderSetToMods is not null)
{
b.RegisterInstance(new InjectedEnabledPluginListingsProvider(analyzerConfig.LoadOrderSetToMods)).As<IEnabledPluginListingsProvider>();
b.RegisterType<NullPluginListingsPathProvider>().As<IPluginListingsPathProvider>();
b.RegisterType<NullCreationClubListingsPathProvider>().As<ICreationClubListingsPathProvider>();
}
if (analyzerConfig.GameRelease.HasValue)
{
var gameReleaseInjection = new GameReleaseInjection(analyzerConfig.GameRelease.Value);
b.RegisterInstance(gameReleaseInjection).AsImplementedInterfaces();
}
if (analyzerConfig.OutputFilePath is not null)
{
b.RegisterType<CsvReportHandler>().AsImplementedInterfaces();
b.RegisterInstance(new CsvInputs(analyzerConfig.OutputFilePath)).AsSelf().AsImplementedInterfaces();
}
b.RegisterType<ContextualEngine>()
.AsSelf()
.AsImplementedInterfaces();
});
return builder.Build();
}
}

0 comments on commit 75bbcf7

Please sign in to comment.