-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.cs
275 lines (236 loc) · 9.91 KB
/
Menu.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
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
using System.Text.RegularExpressions;
namespace NopPluginTemplater;
public static partial class Menu
{
public static void MakeSelections()
{
SetDevName();
SetNopVersion();
SetPluginType();
SetWidgetPreference();
SetAdminMenuPerference();
SetPluginName();
}
private static void SetDevName()
{
Console.Write("Enter your developer name: ");
var devName = Console.ReadLine();
if (string.IsNullOrWhiteSpace(devName))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You can't be a nameless developer.");
Console.ForegroundColor = ConsoleColor.White;
SetDevName();
}
else if (devName.EqualsReservedWords(out var matchedWords))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Your dev name equals reserved word(s) {matchedWords}.");
Console.WriteLine("This will cause problems and must be changed.");
Console.ForegroundColor = ConsoleColor.White;
SetDevName();
}
else if (!NamespaceRegex().IsMatch(devName))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Your name contains Illegal characters or formatting for a namespace.");
Console.WriteLine("This is will likely cause errors and/or warnings.");
Console.WriteLine("Would you like to change your name? (y/n)");
Console.ForegroundColor = ConsoleColor.White;
if (YesOrNo())
SetDevName();
else
PluginSettings.DevName = devName;
}
else if (devName.ContainsReservedWords(out matchedWords))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Your dev name contains reserved word(s) {matchedWords}.");
Console.WriteLine("This is will likely cause errors and/or warnings.");
Console.WriteLine("Would you like to change your name? (y/n)");
Console.ForegroundColor = ConsoleColor.White;
if (YesOrNo())
SetDevName();
else
PluginSettings.DevName = devName;
}
else
{
PluginSettings.DevName = devName;
}
}
private static void SetNopVersion()
{
Console.Write("What version of nop are you targeting?");
Console.WriteLine();
foreach (var val in Enum.GetValues<NopVersions>())
Console.WriteLine($"{(int)val}) {nopRawVersion(val)}");
PluginSettings.NopVersion = getNopVersion();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"nop {nopRawVersion(PluginSettings.NopVersion)} selected.");
Console.ForegroundColor = ConsoleColor.White;
static NopVersions getNopVersion()
{
var enumVals = Enum.GetValues<NopVersions>();
var key = Console.ReadKey(true);
if (int.TryParse(key.KeyChar.ToString(), out var keyInt) &&
keyInt < enumVals.Length)
{
return (NopVersions)keyInt;
}
return getNopVersion();
}
static string nopRawVersion(NopVersions nopVersion)
{
return nopVersion switch
{
NopVersions.nop450 => "4.50",
NopVersions.nop460 => "4.60",
NopVersions.nop470 => "4.70",
NopVersions.nop480 => "4.80",
NopVersions.nop490 => "4.90",
_ => string.Empty
};
}
}
private static void SetPluginType()
{
Console.Write("What type of plugin do want to make?");
Console.WriteLine();
foreach (var val in Enum.GetValues<PluginType>())
Console.WriteLine($"{(int)val}) {pluginHumanReadable(val)}");
PluginSettings.PType = getPluginType();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{pluginHumanReadable(PluginSettings.PType)} selected.");
Console.ForegroundColor = ConsoleColor.White;
static PluginType getPluginType()
{
var enumVals = Enum.GetValues<PluginType>();
var key = Console.ReadKey(true);
if (int.TryParse(key.KeyChar.ToString(), out var keyInt) &&
keyInt < enumVals.Length)
{
return (PluginType)keyInt;
}
return getPluginType();
}
static string pluginHumanReadable(PluginType pType)
{
return pType switch
{
PluginType.Misc => "Misc",
PluginType.Payment => "Payment",
PluginType.DiscountRule => "Discount Rule",
PluginType.ShippingRateComputationMethod => "Shipping Method",
PluginType.PickupProvider => "Pickup Provider",
PluginType.TaxProvider => "Tax Provider",
PluginType.ExternalAuthenticationMethod => "External Authentication Method",
PluginType.MultiFactorAuthentication => "Multi-Factor Authentication Method",
_ => string.Empty
};
}
}
private static void SetWidgetPreference()
{
Console.WriteLine();
Console.WriteLine("Will this plugin contain a widget? (y/n)");
PluginSettings.ContainsWidget = YesOrNo();
}
private static void SetAdminMenuPerference()
{
Console.WriteLine();
Console.WriteLine("Should this plugin be visible on the admin menu? (y/n)");
PluginSettings.IsAdminMenu = YesOrNo();
}
private static void SetPluginName()
{
Console.WriteLine();
Console.WriteLine($"Current namespace: {PluginSettings.FullPluginNamespace}{{PluginName}}");
Console.WriteLine();
Console.Write("Name your plugin: ");
var pluginName = Console.ReadLine();
if (string.IsNullOrEmpty(pluginName))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Plugin must be named.");
Console.ForegroundColor = ConsoleColor.White;
SetPluginName();
}
else if (pluginName.EqualsReservedWords(out var matchedWords))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Your plugin name equals reserved word(s) {matchedWords}.");
Console.WriteLine("This will cause problems and must be changed.");
Console.ForegroundColor = ConsoleColor.White;
SetPluginName();
}
else if (!NamespaceRegex().IsMatch(pluginName))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Your plugin name contains Illegal characters or formatting for a namespace.");
Console.WriteLine("This is will likely cause errors and/or warnings.");
Console.WriteLine("Would you like to change the plugin name? (y/n)");
Console.ForegroundColor = ConsoleColor.White;
if (YesOrNo())
SetPluginName();
else
PluginSettings.Name = pluginName;
}
else if (pluginName.ContainsReservedWords(out matchedWords))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Your pluing name contains reserved word(s) {matchedWords}.");
Console.WriteLine("This is will likely cause errors and/or warnings.");
Console.WriteLine("Would you like to change the plugin name? (y/n)");
Console.ForegroundColor = ConsoleColor.White;
if (YesOrNo())
SetPluginName();
else
PluginSettings.Name = pluginName;
}
else
{
PluginSettings.Name = pluginName;
}
}
private static bool YesOrNo()
{
bool? result = null;
while (!result.HasValue)
{
var entry = Console.ReadKey(true);
if (entry.Key == ConsoleKey.Y)
{
result = true;
}
else if (entry.Key == ConsoleKey.N)
{
result = false;
}
}
return result.Value;
}
[GeneratedRegex("(?:[A-Z][a-zA-Z0-9_]+)+[a-z0-9_]")]
private static partial Regex NamespaceRegex();
private static readonly string[] ReservedWords = ["abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while"];
private static bool ContainsReservedWords(this string checkString, out string match)
{
match = string.Empty;
if (!ReservedWords.Any(checkString.Contains))
{
match = string.Join(", ", ReservedWords.Where(x => checkString.Contains(x, StringComparison.InvariantCultureIgnoreCase)));
return false;
}
return true;
}
private static bool EqualsReservedWords(this string checkString, out string match)
{
match = string.Empty;
if (ReservedWords.Any(x => x.Equals(checkString, StringComparison.CurrentCultureIgnoreCase)))
{
match = string.Join(", ", ReservedWords.Where(x => x.Equals(checkString, StringComparison.InvariantCultureIgnoreCase)));
return true;
}
return false;
}
}