-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRazorComponentClientModuleExtensions.cs
189 lines (175 loc) · 7.28 KB
/
RazorComponentClientModuleExtensions.cs
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
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Modulight.Modules.Client.RazorComponents;
using Modulight.Modules.Client.RazorComponents.UI;
using Modulight.Modules.Hosting;
using System.Reflection;
namespace Modulight.Modules.Hosting
{
/// <summary>
/// Extension methods for aspnet modules.
/// </summary>
public static class RazorComponentClientModuleExtensions
{
/// <summary>
/// Use building plugin for razor component modules.
/// </summary>
/// <param name="modules"></param>
/// <returns></returns>
public static IModuleHostBuilder UseRazorComponentClientModules(this IModuleHostBuilder modules)
{
return modules.ConfigureBuilderServices(services =>
{
services.TryAddTransient<IRazorComponentClientModuleManifestBuilder, DefaultRazorComponentClientModuleManifestBuilder>();
}).UsePlugin<RazorComponentClientModulePlugin>().AddModule<RazorComponentClientCoreModule>();
}
/// <summary>
/// Get razor component module host from service provider.
/// </summary>
/// <param name="host"></param>
/// <returns></returns>
public static IRazorComponentClientModuleCollection GetRazorComponentClientModuleCollection(this IModuleHost host) => host.Services.GetRequiredService<IRazorComponentClientModuleCollection>();
/// <summary>
/// Test a type is a page provider type.
/// </summary>
/// <param name="type"></param>
/// <param name="providerType"></param>
/// <returns></returns>
public static bool IsPageProvider(this Type type, Type? providerType = null) => type.IsAssignableTo(providerType ?? typeof(IPageProvider));
/// <summary>
/// Test a type is a specified page provider type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsPageProvider<T>(this Type type) where T : IPageProvider => type.IsPageProvider(typeof(T));
/// <summary>
/// Ensure a type is a page provider type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static void EnsurePageProvider(this Type type) => type.EnsurePageProvider<IPageProvider>();
/// <summary>
/// Ensure a type is a specified page provider type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
public static void EnsurePageProvider<T>(this Type type) where T : IPageProvider
{
if (!type.IsPageProvider<T>())
throw new IncompatibleTypeException(type, typeof(T));
}
/// <summary>
/// Get page provider.
/// </summary>
/// <param name="module"></param>
/// <param name="host"></param>
/// <returns></returns>
public static IPageProvider? GetPageProvider(this IRazorComponentClientModule module, IModuleHost host)
{
var collection = host.GetRazorComponentClientModuleCollection();
var manifest = collection.GetManifest(module.GetType());
if (manifest.PageProvider is not null)
{
return (IPageProvider)host.Services.GetRequiredService(manifest.PageProvider);
}
return null;
}
}
}
namespace Modulight.Modules
{
/// <summary>
/// Extension methods for razor component modules.
/// </summary>
public static class RazorComponentClientModuleExtensions
{
/// <summary>
/// Add resource to manifest.
/// </summary>
/// <param name="builder"></param>
/// <param name="resource"></param>
/// <returns></returns>
public static IRazorComponentClientModuleManifestBuilder WithResource(this IRazorComponentClientModuleManifestBuilder builder, UIResource resource)
{
builder.Resources.Add(resource);
return builder;
}
/// <summary>
/// Add global component to manifest.
/// </summary>
/// <param name="type"></param>
/// <param name="builder"></param>
/// <returns></returns>
public static IRazorComponentClientModuleManifestBuilder WithGlobalComponent(this IRazorComponentClientModuleManifestBuilder builder, Type type)
{
if (type.IsAssignableTo(typeof(IComponent)))
{
builder.GlobalComponents.Add(type);
}
else
{
throw new IncompatibleTypeException(type, typeof(IComponent));
}
return builder;
}
/// <summary>
/// Add page provider to manifest.
/// </summary>
/// <param name="type"></param>
/// <param name="builder"></param>
/// <returns></returns>
public static IRazorComponentClientModuleManifestBuilder WithPageProvider(this IRazorComponentClientModuleManifestBuilder builder, Type type)
{
type.EnsurePageProvider();
builder.PageProvider = type;
return builder;
}
/// <summary>
/// Add page provider to manifest.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static IRazorComponentClientModuleManifestBuilder WithPageProvider<T>(this IRazorComponentClientModuleManifestBuilder builder) where T : IPageProvider => builder.WithPageProvider(typeof(T));
/// <summary>
/// Add global component to manifest.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static IRazorComponentClientModuleManifestBuilder WithGlobalComponent<T>(this IRazorComponentClientModuleManifestBuilder builder) where T : IComponent => builder.WithGlobalComponent(typeof(T));
/// <summary>
/// Configure the builder by default from attributes.
/// </summary>
/// <param name="builder"></param>
/// <param name="type"></param>
/// <returns></returns>
public static IRazorComponentClientModuleManifestBuilder WithDefaultsFromModuleType(this IRazorComponentClientModuleManifestBuilder builder, Type type)
{
{
var attrs = type.GetCustomAttributes<ModuleUIResourceAttribute>();
foreach (var attr in attrs)
{
builder.WithResource(new UIResource(attr.Type, attr.Path) { Attributes = attr.Attributes });
}
}
{
var attrs = type.GetCustomAttributes<ModuleUIGlobalComponentAttribute>();
foreach (var attr in attrs)
{
builder.WithGlobalComponent(attr.Type);
}
}
{
var attr = type.GetCustomAttribute<ModulePageProviderAttribute>();
if (attr is not null)
{
builder.WithPageProvider(attr.Type);
}
}
return builder;
}
}
}