-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleExtensions.cs
More file actions
205 lines (188 loc) · 7.88 KB
/
ModuleExtensions.cs
File metadata and controls
205 lines (188 loc) · 7.88 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Reflection;
namespace Modulight.Modules
{
/// <summary>
/// Extension methods for modules.
/// </summary>
public static class ModuleExtensions
{
/// <summary>
/// Get all modules in given type.
/// </summary>
/// <typeparam name="T">Target module type.</typeparam>
/// <param name="this">Module list.</param>
/// <returns></returns>
public static IEnumerable<T> AllSpecifyModules<T>(this IEnumerable<IModule> @this) => @this.Where(m => m is T).Select(m => (T)m);
/// <summary>
/// Get assembly name from type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetAssemblyName(this Type type) => type.Assembly.GetName().Name!;
/// <summary>
/// Test a type is a module type.
/// </summary>
/// <param name="type"></param>
/// <param name="moduleType"></param>
/// <returns></returns>
public static bool IsModule(this Type type, Type? moduleType = null) => type.IsAssignableTo(moduleType ?? typeof(IModule));
/// <summary>
/// Test a type is a specified module type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsModule<T>(this Type type) where T : IModule => type.IsModule(typeof(T));
/// <summary>
/// Ensure a type is a module type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static void EnsureModule(this Type type) => type.EnsureModule<IModule>();
/// <summary>
/// Ensure a type is a specified module type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
public static void EnsureModule<T>(this Type type) where T : IModule
{
if (!type.IsModule<T>())
throw new IncompatibleTypeException(type, typeof(T));
}
/// <summary>
/// Test a type is a module startup type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsModuleStartup(this Type type) => type.IsModuleStartup<IModuleStartup>();
/// <summary>
/// Test a type is a specified module startup type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsModuleStartup<T>(this Type type) where T : IModuleStartup => type.IsAssignableTo(typeof(T));
/// <summary>
/// Ensure a type is a module startup type.
/// </summary>
/// <param name="type"></param>
public static void EnsureModuleStartup(this Type type) => type.EnsureModuleStartup<IModuleStartup>();
/// <summary>
/// Ensure a type is a specified module startup type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
public static void EnsureModuleStartup<T>(this Type type) where T : IModuleStartup
{
if (!type.IsModuleStartup<T>())
throw new IncompatibleTypeException(type, typeof(T));
}
/// <summary>
/// Add service to manifest.
/// </summary>
/// <param name="builder"></param>
/// <param name="descriptor"></param>
/// <returns></returns>
public static IModuleManifestBuilder WithService(this IModuleManifestBuilder builder, ModuleServiceDescriptor descriptor)
{
builder.Services.Add(descriptor);
return builder;
}
/// <summary>
/// Add option to manifest.
/// </summary>
/// <param name="builder"></param>
/// <param name="type"></param>
/// <returns></returns>
public static IModuleManifestBuilder WithOption(this IModuleManifestBuilder builder, Type type)
{
builder.Options.Add(type);
return builder;
}
/// <summary>
/// Add option to manifest.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static IModuleManifestBuilder WithOption<T>(this IModuleManifestBuilder builder) => builder.WithOption(typeof(T));
/// <summary>
/// Add dependency to manifest.
/// </summary>
/// <param name="builder"></param>
/// <param name="type"></param>
/// <returns></returns>
public static IModuleManifestBuilder WithDependency(this IModuleManifestBuilder builder, Type type)
{
type.EnsureModule();
builder.Dependencies.Add(type);
return builder;
}
/// <summary>
/// Add dependency to manifest.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static IModuleManifestBuilder WithDependency<T>(this IModuleManifestBuilder builder) where T : IModule => builder.WithDependency(typeof(T));
/// <summary>
/// Configure the builder by default from attributes.
/// </summary>
/// <param name="builder"></param>
/// <param name="type"></param>
/// <returns></returns>
public static IModuleManifestBuilder WithDefaultsFromModuleType(this IModuleManifestBuilder builder, Type type)
{
// Split by upper chars
static string[] GenerateName(string typeName)
{
if (typeName.EndsWith("Module"))
typeName = typeName[0..^6];
List<int> splitIndexs;
{
SortedSet<int> indexSet = new(new[] { 0, typeName.Length });
foreach (int i in Enumerable.Range(0, typeName.Length))
{
if (char.IsUpper(typeName[i]))
indexSet.Add(i);
}
splitIndexs = indexSet.ToList();
}
List<string> names = new();
foreach (int i in Enumerable.Range(0, splitIndexs.Count - 1))
{
names.Add(typeName[splitIndexs[i]..splitIndexs[i + 1]]);
}
return names.ToArray();
}
var moduleAttr = type.GetCustomAttribute<ModuleAttribute>(true);
var serviceAttr = type.GetCustomAttributes<ModuleServiceAttribute>(true);
var optionAttr = type.GetCustomAttributes<ModuleOptionAttribute>(true);
var depAttr = type.GetCustomAttributes<ModuleDependencyAttribute>(true);
builder.Name = moduleAttr?.Name ?? string.Concat(GenerateName(type.Name));
builder.DisplayName = moduleAttr?.DisplayName ?? string.Join(' ', GenerateName(type.Name));
builder.Version = moduleAttr?.Version ?? type.Assembly.GetName().Version?.ToString() ?? "0.0.0.0";
builder.Author = moduleAttr?.Author ?? "Anonymous";
builder.Description = moduleAttr?.Description ?? "";
builder.Url = moduleAttr?.Url ?? "";
foreach (var item in serviceAttr)
{
var d = new ModuleServiceDescriptor(
item.ImplementationType,
item.ServiceType ?? item.ImplementationType,
item.Lifetime,
item.RegisterBehavior);
builder.WithService(d);
}
foreach (var item in optionAttr)
{
builder.WithOption(item.OptionType);
}
foreach (var item in depAttr)
{
builder.WithDependency(item.ModuleType);
}
return builder;
}
}
}