forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptServicesRegistration.cs
More file actions
108 lines (88 loc) · 3.87 KB
/
ScriptServicesRegistration.cs
File metadata and controls
108 lines (88 loc) · 3.87 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
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using ScriptCs.Contracts;
using Autofac;
namespace ScriptCs.Hosting
{
public abstract class ScriptServicesRegistration
{
private readonly ILogProvider _logProvider;
private readonly ILog _log;
private readonly IDictionary<Type, object> _overrides;
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
public Common.Logging.ILog Logger { get; private set; }
public ILogProvider LogProvider
{
get { return _logProvider; }
}
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
protected ScriptServicesRegistration(Common.Logging.ILog logger, IDictionary<Type, object> overrides)
:this(new CommonLoggingLogProvider(logger), overrides)
{
}
protected ScriptServicesRegistration(ILogProvider logProvider, IDictionary<Type, object> overrides)
{
Guard.AgainstNullArgument("logProvider", logProvider);
_overrides = overrides ?? new Dictionary<Type, object>();
_logProvider = logProvider;
_log = _logProvider.ForCurrentType();
#pragma warning disable 618
Logger = new ScriptCsLogger(_log);
#pragma warning restore 618
}
protected void RegisterOverrideOrDefault<T>(ContainerBuilder builder, Action<ContainerBuilder> registrationAction)
{
Guard.AgainstNullArgument("registrationAction", registrationAction);
if (_overrides.ContainsKey(typeof(T)))
{
var reg = _overrides[typeof(T)];
_log.Debug(string.Format("Registering override: {0}", reg));
if (reg.GetType().IsSubclassOf(typeof(Type)))
{
builder.RegisterType((Type)reg).As<T>().SingleInstance();
}
else
{
builder.RegisterInstance(reg).As<T>();
}
}
else
{
_log.Debug(string.Format("Registering default: {0}", typeof(T)));
registrationAction(builder);
}
}
protected void RegisterLineProcessors(ContainerBuilder builder)
{
object processors;
this.Overrides.TryGetValue(typeof(ILineProcessor), out processors);
var processorList = (processors as IEnumerable<Type> ?? Enumerable.Empty<Type>()).ToArray();
var loadProcessorType = processorList
.FirstOrDefault(x => typeof(ILoadLineProcessor).IsAssignableFrom(x))
?? typeof(LoadLineProcessor);
var usingProcessorType = processorList
.FirstOrDefault(x => typeof(IUsingLineProcessor).IsAssignableFrom(x))
?? typeof(UsingLineProcessor);
var referenceProcessorType = processorList
.FirstOrDefault(x => typeof(IReferenceLineProcessor).IsAssignableFrom(x))
?? typeof(ReferenceLineProcessor);
var shebangProcessorType = processorList
.FirstOrDefault(x => typeof(IShebangLineProcessor).IsAssignableFrom(x))
?? typeof(ShebangLineProcessor);
var processorArray = new[] { loadProcessorType, usingProcessorType, referenceProcessorType, shebangProcessorType }
.Union(processorList).ToArray();
builder.RegisterTypes(processorArray).As<ILineProcessor>();
}
private IContainer _container;
public IContainer Container
{
get { return _container ?? (_container = CreateContainer()); }
}
protected IDictionary<Type, object> Overrides
{
get { return _overrides; }
}
protected abstract IContainer CreateContainer();
}
}