-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModuleBuilder.cs
More file actions
340 lines (295 loc) · 11.9 KB
/
ModuleBuilder.cs
File metadata and controls
340 lines (295 loc) · 11.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.Collections.Generic;
using Ultz.Extensions.Commands.Attributes.Checks;
using Ultz.Extensions.Commands.Built;
using Ultz.Extensions.Commands.Parsing.ArgumentParsers;
namespace Ultz.Extensions.Commands.Builders
{
/// <summary>
/// Allows for building <see cref="Module" />s using the <see cref="CommandService" />.
/// </summary>
public sealed class ModuleBuilder
{
private RunMode? _runMode;
internal ModuleBuilder(ModuleBuilder parent)
{
Parent = parent;
Aliases = new HashSet<string>();
Checks = new List<CheckAttribute>();
Attributes = new List<Attribute>();
Commands = new List<CommandBuilder>();
Submodules = new List<ModuleBuilder>();
}
internal ModuleBuilder(Type type, ModuleBuilder parent) : this(parent)
{
Type = type;
}
/// <summary>
/// Gets or sets the name of the <see cref="Module" />.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the description of the <see cref="Module" />.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the remarks of the <see cref="Module" />.
/// </summary>
public string Remarks { get; set; }
/// <summary>
/// Gets or sets the <see cref="Extensions.Commands.RunMode" /> of the <see cref="Module" />.
/// </summary>
public RunMode? RunMode
{
get => _runMode;
set
{
if (value != null && !Enum.IsDefined(typeof(RunMode), value.Value))
{
throw new ArgumentOutOfRangeException(nameof(value), "Invalid run mode.");
}
_runMode = value;
}
}
/// <summary>
/// Gets or sets whether the <see cref="Command" />s in the <see cref="Module" /> should ignore extra arguments or not.
/// </summary>
public bool? IgnoresExtraArguments { get; set; }
/// <summary>
/// Gets or sets the <see cref="Type" /> of a custom <see cref="IArgumentParser" />.
/// </summary>
public Type CustomArgumentParserType { get; set; }
/// <summary>
/// Gets the aliases of the <see cref="Module" />.
/// </summary>
public ISet<string> Aliases { get; }
/// <summary>
/// Gets the checks of the <see cref="Module" />.
/// </summary>
public List<CheckAttribute> Checks { get; }
/// <summary>
/// Gets the attributes of the <see cref="Module" />.
/// </summary>
public List<Attribute> Attributes { get; }
/// <summary>
/// Gets the commands of the <see cref="Module" />.
/// </summary>
public List<CommandBuilder> Commands { get; }
/// <summary>
/// Gets the submodules of the <see cref="Module" />.
/// </summary>
public List<ModuleBuilder> Submodules { get; }
/// <summary>
/// Gets or sets whether the <see cref="Module" /> will be enabled or not.
/// Defaults to <see langword="true" />.
/// </summary>
public bool IsEnabled { get; set; } = true;
/// <summary>
/// Gets the parent module of the <see cref="Module" />.
/// </summary>
public ModuleBuilder Parent { get; }
/// <summary>
/// Gets the <see cref="System.Type" /> this <see cref="ModuleBuilder" /> was created from.
/// <see langword="null" /> if this module was created using, for example,
/// <see cref="CommandService.AddModule(Type, Action{ModuleBuilder})" />.
/// </summary>
public Type Type { get; }
/// <summary>
/// Sets the <see cref="Name" />.
/// </summary>
public ModuleBuilder WithName(string name)
{
Name = name;
return this;
}
/// <summary>
/// Sets the <see cref="Description" />.
/// </summary>
public ModuleBuilder WithDescription(string description)
{
Description = description;
return this;
}
/// <summary>
/// Sets the <see cref="Remarks" />.
/// </summary>
public ModuleBuilder WithRemarks(string remarks)
{
Remarks = remarks;
return this;
}
/// <summary>
/// Sets the <see cref="RunMode" />.
/// </summary>
public ModuleBuilder WithRunMode(RunMode? runMode)
{
RunMode = runMode;
return this;
}
/// <summary>
/// Sets the <see cref="IgnoresExtraArguments" />.
/// </summary>
public ModuleBuilder WithIgnoresExtraArguments(bool? ignoresExtraArguments)
{
IgnoresExtraArguments = ignoresExtraArguments;
return this;
}
/// <summary>
/// Sets the <see cref="CustomArgumentParserType" />.
/// </summary>
public ModuleBuilder WithCustomArgumentParserType(Type customArgumentParserType)
{
CustomArgumentParserType = customArgumentParserType;
return this;
}
/// <summary>
/// Adds an alias to <see cref="Aliases" />.
/// </summary>
/// <exception cref="ArgumentException">
/// This alias has already been added.
/// </exception>
public ModuleBuilder AddAlias(string alias)
{
if (!Aliases.Add(alias))
{
throw new ArgumentException($"This alias has already been added ({alias}).", nameof(alias));
}
return this;
}
/// <summary>
/// Adds a check to <see cref="Checks" />.
/// </summary>
public ModuleBuilder AddCheck(CheckAttribute check)
{
Checks.Add(check);
return this;
}
/// <summary>
/// Adds an attribute to <see cref="Attributes" />.
/// </summary>
public ModuleBuilder AddAttribute(Attribute attribute)
{
Attributes.Add(attribute);
return this;
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="CommandBuilder" /> to <see cref="Commands" />.
/// </summary>
/// <param name="callback"> The callback of the <see cref="Command" />. </param>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddCommand(VoidCommandCallbackDelegate callback, Action<CommandBuilder> builderAction)
{
return AddCommandInternal(callback, builderAction);
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="CommandBuilder" /> to <see cref="Commands" />.
/// </summary>
/// <param name="callback"> The callback of the <see cref="Command" />. </param>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddCommand(ResultCommandCallbackDelegate callback, Action<CommandBuilder> builderAction)
{
return AddCommandInternal(callback, builderAction);
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="CommandBuilder" /> to <see cref="Commands" />.
/// </summary>
/// <param name="callback"> The callback of the <see cref="Command" />. </param>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddCommand(TaskCommandCallbackDelegate callback, Action<CommandBuilder> builderAction)
{
return AddCommandInternal(callback, builderAction);
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="CommandBuilder" /> to <see cref="Commands" />.
/// </summary>
/// <param name="callback"> The callback of the <see cref="Command" />. </param>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddCommand(TaskResultCommandCallbackDelegate callback,
Action<CommandBuilder> builderAction)
{
return AddCommandInternal(callback, builderAction);
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="CommandBuilder" /> to <see cref="Commands" />.
/// </summary>
/// <param name="callback"> The callback of the <see cref="Command" />. </param>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddCommand(ValueTaskCommandCallbackDelegate callback, Action<CommandBuilder> builderAction)
{
return AddCommandInternal(callback, builderAction);
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="CommandBuilder" /> to <see cref="Commands" />.
/// </summary>
/// <param name="callback"> The callback of the <see cref="Command" />. </param>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddCommand(ValueTaskResultCommandCallbackDelegate callback,
Action<CommandBuilder> builderAction)
{
return AddCommandInternal(callback, builderAction);
}
private ModuleBuilder AddCommandInternal(object callback, Action<CommandBuilder> builderAction)
{
if (callback == null)
{
throw new ArgumentNullException(nameof(callback));
}
if (builderAction == null)
{
throw new ArgumentNullException(nameof(builderAction));
}
var builder = new CommandBuilder(this, callback);
builderAction(builder);
Commands.Add(builder);
return this;
}
/// <summary>
/// Attempts to instantiate, modify, and add a <see cref="ModuleBuilder" /> to <see cref="Submodules" />.
/// </summary>
/// <param name="builderAction"> The action to perform on the builder. </param>
public ModuleBuilder AddSubmodule(Action<ModuleBuilder> builderAction)
{
if (builderAction == null)
{
throw new ArgumentNullException(nameof(builderAction));
}
var builder = new ModuleBuilder(this);
builderAction(builder);
Submodules.Add(builder);
return this;
}
/// <summary>
/// Sets the <see cref="IsEnabled" />.
/// </summary>
public ModuleBuilder WithIsEnabled(bool isEnabled)
{
IsEnabled = isEnabled;
return this;
}
internal Module Build(CommandService service, Module parent)
{
if (CustomArgumentParserType != null &&
!Utilities.IsValidArgumentParserDefinition(CustomArgumentParserType))
{
throw new ModuleBuildingException(this,
$"{CustomArgumentParserType} is not a valid argument parser type.");
}
foreach (var alias in Aliases)
{
if (alias == null)
{
throw new ModuleBuildingException(this, "Module's group aliases must not contain null entries.");
}
if (alias.IndexOf(' ') != -1)
{
throw new ModuleBuildingException(this, "Module's group aliases must not contain whitespace.");
}
if (alias.IndexOf(service.Separator, service.StringComparison) != -1)
{
throw new ModuleBuildingException(this, "Module's group aliases must not contain the separator.");
}
}
return new Module(service, this, parent);
}
}
}