forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleLoader.cs
More file actions
97 lines (85 loc) · 3.56 KB
/
ModuleLoader.cs
File metadata and controls
97 lines (85 loc) · 3.56 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;
using ScriptCs.Contracts;
namespace ScriptCs
{
public class ModuleLoader : IModuleLoader
{
private readonly IAssemblyResolver _resolver;
private readonly ILog _logger;
private readonly Action<string, AggregateCatalog> _addToCatalog;
private readonly Func<CompositionContainer, IEnumerable<Lazy<IModule, IModuleMetadata>>> _getModules;
[ImportingConstructor]
public ModuleLoader(IAssemblyResolver resolver, ILog logger) :
this(resolver, logger, null, null)
{
}
public ModuleLoader(IAssemblyResolver resolver, ILog logger, Action<string, AggregateCatalog> addToCatalog, Func<CompositionContainer, IEnumerable<Lazy<IModule, IModuleMetadata>>> getModules)
{
_resolver = resolver;
_logger = logger;
if (addToCatalog == null)
{
addToCatalog = (p, catalog) => catalog.Catalogs.Add(new AssemblyCatalog(p));
}
_addToCatalog = addToCatalog;
if (getModules == null)
{
getModules = (container) =>
{
try
{
return container.GetExports<IModule, IModuleMetadata>();
}
catch (ReflectionTypeLoadException exception)
{
if (exception.LoaderExceptions != null && exception.LoaderExceptions.Any())
{
foreach (var loaderException in exception.LoaderExceptions)
{
logger.Error(string.Format("Module loader exception {0}", loaderException.Message));
}
}
else
{
logger.Error("Module loader threw an exception", exception);
}
return Enumerable.Empty<Lazy<IModule, IModuleMetadata>>();
}
};
}
_getModules = getModules;
}
public void Load(IModuleConfiguration config, string modulePackagesPath, string extension, params string[] moduleNames)
{
_logger.Debug("Loading modules from: " + modulePackagesPath);
var paths = _resolver.GetAssemblyPaths(modulePackagesPath);
var catalog = new AggregateCatalog();
foreach (var path in paths)
{
_addToCatalog(path, catalog);
}
var container = new CompositionContainer(catalog);
var lazyModules = _getModules(container);
var modules = lazyModules
.Where(m => moduleNames.Contains(m.Metadata.Name) ||
(extension != null && m.Metadata.Extensions != null && (m.Metadata.Extensions.Split(',').Contains(extension))))
.Select(m => m.Value);
_logger.Debug("Initializing modules");
foreach (var module in modules)
{
_logger.Debug(string.Format("Initializing module:{0}", module.GetType().FullName));
module.Initialize(config);
}
_logger.Debug("Modules initialized");
}
}
}