Skip to content

Commit

Permalink
Add PluginLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
timia2109 committed Jun 18, 2022
1 parent bd0eedf commit 59e3502
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 7 deletions.
16 changes: 16 additions & 0 deletions DayFlags.Core/EntryTypes/AEntryTypeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DayFlags.Core.Models;

namespace DayFlags.Core.EntryTypes;

/// <summary>
/// Provides <see cref="EntryType"/>s
/// </summary>
public abstract class AEntryTypeProvider
{
/// <summary>
/// Provides all EntryTypes that this Provider knows
/// </summary>
/// <returns>List of EntryTypes</returns>
public abstract Task<List<EntryType>> GetEntryTypesAsync();

}
23 changes: 23 additions & 0 deletions DayFlags.Server/EntryTypes/DbEntryTypesProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using DayFlags.Core.EntryTypes;
using DayFlags.Core.Models;
using Microsoft.EntityFrameworkCore;

namespace DayFlags.Server.EntryTypes;

/// <summary>
/// A <see cref="AEntryTypeProvider"/> that fetches entries from the database
/// </summary>
public class DbEntryTypesProvider : AEntryTypeProvider
{
private readonly DayFlagsDb _db;

public DbEntryTypesProvider(DayFlagsDb db)
{
_db = db;
}

public override Task<List<EntryType>> GetEntryTypesAsync()
{
return _db.EntryTypes.ToListAsync();
}
}
74 changes: 74 additions & 0 deletions DayFlags.Server/Plugins/PluginsLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using DayFlags.Core.Plugins;
using McMaster.NETCore.Plugins;
using Microsoft.Extensions.Logging.Console;

namespace DayFlags.Server.Plugins;

/// <summary>
/// Responsible to load Plugins
/// </summary>
public class PluginsLoader
{

private readonly static Type[] SharedTypes = new Type[]{
typeof(IServiceCollection), typeof(IServiceProvider), typeof(ILogger),
typeof(IDayFlagPlugin)
};

private readonly IConfiguration _config;
private List<IDayFlagPlugin> _plugins = new();
private readonly LoggerFactory _loggerFactory;
private readonly ILogger _logger;

public PluginsLoader(IConfiguration config)
{
_config = config;
_loggerFactory = new LoggerFactory();
_loggerFactory.AddProvider(new ConsoleLoggerProvider(null));
_logger = _loggerFactory.CreateLogger("PluginLoader");
}

private void LoadPlugin(string assemblyPath)
{
_logger.LogInformation($"Loading Plugin {assemblyPath}");
var plugin = PluginLoader.CreateFromAssemblyFile(
assemblyPath, true, SharedTypes
);

if (plugin != null)
{
var pluginEntries = plugin.LoadDefaultAssembly()
.GetTypes()
.Where(e => e.IsAssignableFrom(typeof(IDayFlagPlugin))
&& !e.IsAbstract);

foreach (var pluginEntry in pluginEntries)
{
var instance = (IDayFlagPlugin?)Activator
.CreateInstance(pluginEntry);

if (instance != null)
{
_plugins.Add(instance);
}
}
}
}

public void ConfigureServices(IServiceCollection serviceDescriptors)
{
foreach (var plugin in _plugins)
{
serviceDescriptors.AddSingleton(typeof(IDayFlagPlugin), plugin);
plugin.ConfigureServices(serviceDescriptors);
}
}

public void ConfigureApp(WebApplicationBuilder webApplicationBuilder)
{
foreach (var plugin in _plugins)
{
plugin.ConfigureApp(webApplicationBuilder);
}
}
}
11 changes: 5 additions & 6 deletions DayFlags.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using DayFlags;
using DayFlags.Core.EntryTypes;
using DayFlags.Core.MatchProvider;
using DayFlags.Server.Converters;
using DayFlags.Server.EntryTypes;
using DayFlags.Server.MatchProvider;
using DayFlags.Server.Middlewares;
using DayFlags.Server.Services;
Expand All @@ -13,6 +15,7 @@
builder.Services.AddScoped<DayEntriesService>();
builder.Services.AddScoped<MatchingService>();
builder.Services.AddScoped<IMatchProvider, DatabaseMatchProvider>();
builder.Services.AddScoped<AEntryTypeProvider, DbEntryTypesProvider>();

builder.Services.AddDbContext<DayFlagsDb>(options =>
{
Expand All @@ -31,12 +34,8 @@

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseSwagger();
app.UseSwaggerUI();

app.UseHttpsRedirection();

Expand Down
23 changes: 22 additions & 1 deletion DayFlags.Server/Services/EntryTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
using DayFlags.Core.Models;
using DayFlags.Core.Enums;
using Microsoft.EntityFrameworkCore;
using DayFlags.Core.EntryTypes;

namespace DayFlags.Server.Services;

public class EntryTypeService
{

private readonly DayFlagsDb _db;
private readonly IEnumerable<AEntryTypeProvider> _entryTypesProviders;

public EntryTypeService(DayFlagsDb db)
public EntryTypeService(DayFlagsDb db,
IEnumerable<AEntryTypeProvider> entryTypesProviders)
{
_db = db;
_entryTypesProviders = entryTypesProviders;
}

/// <summary>
Expand Down Expand Up @@ -67,4 +71,21 @@ public Task<EntryType> CreateByIdAsync(string entryTypeId)
EntryTypeRequirement.OnceADay);
return AddAsync(entry);
}

/// <summary>
/// Fetches all EntryTypes including non-usercreated (like from plugins)
/// </summary>
/// <returns>List of all known EntryTypes</returns>
public async Task<List<EntryType>> GetAllEntryTypesAsync()
{
var entryTypes = new List<EntryType>();

foreach (var provider in _entryTypesProviders)
{
var results = await provider.GetEntryTypesAsync();
entryTypes.AddRange(results);
}

return entryTypes;
}
}

0 comments on commit 59e3502

Please sign in to comment.