87 lines
3.1 KiB
C#
87 lines
3.1 KiB
C#
namespace InspectionUI
|
|
{
|
|
using InspectionUI.Abstraction;
|
|
using InspectionUI.Abstraction.Interfaces;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public class PluginsManager
|
|
{
|
|
private readonly IDictionary<string, Type> entryPoints
|
|
= new Dictionary<string, Type>();
|
|
|
|
internal bool IsActivated { get; private set; }
|
|
|
|
internal ITestBatch Activate(string plugin, ITestBench testBench)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(plugin) && this.entryPoints.ContainsKey(plugin))
|
|
{
|
|
var entryPoint = this.entryPoints[plugin];
|
|
var instance = Activator.CreateInstance(entryPoint, new object[] { testBench, App.Appsettings });
|
|
|
|
if (instance is ITestBatch testBatch)
|
|
{
|
|
return testBatch;
|
|
}
|
|
}
|
|
|
|
return default(ITestBatch);
|
|
}
|
|
|
|
internal IEnumerable<string> AvailablePlugins()
|
|
{
|
|
var localDirectory = Path.GetDirectoryName(typeof(PluginsManager).Assembly.Location);
|
|
|
|
CopyDirectories(App.Appsettings.PluginsDirectory, localDirectory);
|
|
|
|
var pluginFiles = Directory.GetFiles(localDirectory, "*Plugins*.dll", SearchOption.AllDirectories);
|
|
|
|
foreach (var pluginFile in pluginFiles)
|
|
{
|
|
var assembly = Assembly.LoadFrom(pluginFile);
|
|
var entryPoint = assembly
|
|
.GetTypes()
|
|
.FirstOrDefault(x => !x.IsAbstract && typeof(TestBatchBase).IsAssignableFrom(x));
|
|
var name = (entryPoint
|
|
.GetCustomAttributes(true)
|
|
?.FirstOrDefault(x => x is TestBatchAttribute _)
|
|
as TestBatchAttribute)
|
|
?.DisplayName ?? entryPoint.Name;
|
|
|
|
this.entryPoints[name] = entryPoint;
|
|
|
|
yield return name;
|
|
}
|
|
}
|
|
|
|
private static void CopyDirectories(string sourceDirectory, string destinationDirectory)
|
|
{
|
|
CopyFiles(sourceDirectory, destinationDirectory);
|
|
|
|
foreach (var subDirectory in Directory.GetDirectories(sourceDirectory))
|
|
{
|
|
var subDirectoryName = new DirectoryInfo(subDirectory).Name;
|
|
var destinationSubDirectoryPath = Path.Combine(destinationDirectory, subDirectoryName);
|
|
var destinationSubDirectory = Directory.CreateDirectory(destinationSubDirectoryPath).FullName;
|
|
|
|
CopyDirectories(subDirectory, destinationSubDirectory);
|
|
}
|
|
}
|
|
|
|
private static void CopyFiles(string sourceDirectory, string destinationDirectory)
|
|
{
|
|
foreach (var sourceFile in Directory.GetFiles(sourceDirectory))
|
|
{
|
|
var sourceFileName = Path.GetFileName(sourceFile);
|
|
var destinationFile = Path.Combine(destinationDirectory, sourceFileName);
|
|
|
|
File.Copy(sourceFile, destinationFile, true);
|
|
}
|
|
}
|
|
}
|
|
}
|