-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGenerateREADME.cs
More file actions
320 lines (268 loc) · 11.8 KB
/
GenerateREADME.cs
File metadata and controls
320 lines (268 loc) · 11.8 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
using System.Text;
using System.Text.RegularExpressions;
using static Bloodcraft.Services.ConfigService;
namespace Bloodcraft;
internal static class GenerateREADME
{
static string CommandsPath { get; set; }
static string ReadMePath { get; set; }
static readonly Regex _commandGroupRegex =
new("\\[CommandGroup\\((?<args>.*?)\\)\\]",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
static readonly Regex _commandAttributeRegex =
new("\\[Command\\((?<args>.*?)\\)\\]",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
static readonly Regex _argPairRegex =
new("\\b(?<key>\\w+)\\s*:\\s*(?<value>\\\"[^\\\"]*\\\"|[^,\\)\\r\\n]+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
const string COMMANDS_HEADER = "## Chat Commands";
const string CONFIG_HEADER = "## Configuration";
static readonly Dictionary<(string groupName, string groupShort), List<(string name, string shortHand, bool adminOnly, string usage, string description)>> _commandsByGroup
= [];
public static void Main(string[] args)
{
// Check if we're running in a GitHub Actions environment and skip
if (Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true")
{
Console.WriteLine("GenerateREADME skipped during GitHub Actions build.");
return;
}
if (args.Length < 2)
{
Console.WriteLine("Usage: GenerateREADME <CommandsPath> <ReadMePath>");
return;
}
CommandsPath = args[0];
ReadMePath = args[1];
try
{
Generate();
Console.WriteLine("README generated successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error generating README: {ex.Message}");
}
}
static void Generate()
{
CollectCommands();
var commandsSection = BuildCommandsSection();
var configSection = BuildConfigSection();
UpdateReadme(commandsSection, configSection);
}
static void CollectCommands()
{
var files = Directory.GetFiles(CommandsPath, "*.cs")
.Where(file => !Path.GetFileName(file).Equals("DevCommands.cs", StringComparison.CurrentCultureIgnoreCase));
foreach (string file in files)
{
string content = File.ReadAllText(file);
// ——— Group ———
Match grpMatch = _commandGroupRegex.Match(content);
string groupArgs = grpMatch.Success ? grpMatch.Groups["args"].Value : string.Empty;
string groupName = GetStringArg(groupArgs, "name");
string groupShort = GetStringArg(groupArgs, "short");
if (string.IsNullOrEmpty(groupShort))
groupShort = GetStringArg(groupArgs, "shortHand");
if (string.IsNullOrEmpty(groupName)) groupName = "misc";
if (!_commandsByGroup.TryGetValue((groupName, groupShort), out var list))
{
list = [];
_commandsByGroup[(groupName, groupShort)] = list;
}
// ——— Commands ———
foreach (Match cmdMatch in _commandAttributeRegex.Matches(content))
{
string args = cmdMatch.Groups["args"].Value;
string name = GetStringArg(args, "name");
string shortHand = GetStringArg(args, "shortHand");
bool adminOnly = GetBoolArg(args, "adminOnly");
string usage = GetStringArg(args, "usage");
string description = GetStringArg(args, "description");
list.Add((name, shortHand, adminOnly, usage, description));
}
}
// keep each list sorted for deterministic output
foreach (var key in _commandsByGroup.Keys.ToList())
_commandsByGroup[key] = [.._commandsByGroup[key].OrderBy(c => c.name, StringComparer.CurrentCultureIgnoreCase)];
}
static string BuildCommandsSection()
{
var sb = new StringBuilder();
sb.AppendLine(COMMANDS_HEADER).AppendLine();
foreach (var (groupName, groupShort) in _commandsByGroup.Keys.OrderBy(g => g.groupName))
{
sb.AppendLine($"### {Capitalize(groupName)} Commands");
foreach (var (name, shortHand, adminOnly, usage, description) in _commandsByGroup[(groupName, groupShort)])
{
bool hasShortHand = !string.IsNullOrEmpty(shortHand);
// derive a usable shortcut string if the attribute didn't provide one
string effectiveUsage = string.IsNullOrEmpty(usage)
? $".{(string.IsNullOrEmpty(groupShort) ? groupName : groupShort)} {(hasShortHand ? shortHand : name)}"
: usage;
// strip the prefix (dot + two tokens) to get parameters only
string parameters = string.Empty;
var parts = effectiveUsage.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 3) parameters = parts[2];
else if (parts.Length == 2 && parts[1].StartsWith("[")) parameters = parts[1];
string adminLock = adminOnly ? " 🔒" : string.Empty;
string cmdLine = parameters.Length > 0
? $"- `.{groupName} {name} {parameters}`{adminLock}"
: $"- `.{groupName} {name}`{adminLock}";
sb.AppendLine(cmdLine);
if (!string.IsNullOrEmpty(description)) sb.AppendLine($" - {description}");
sb.AppendLine($" - Shortcut: *{effectiveUsage}*");
}
sb.AppendLine();
}
return sb.ToString();
}
static string BuildConfigSection()
{
StringBuilder sb = new();
sb.AppendLine("## Configuration");
sb.AppendLine();
// Group config entries by their section
var groupedConfigEntries = ConfigInitialization.ConfigEntries
.GroupBy(entry => entry.Section)
.OrderBy(group => ConfigInitialization.SectionOrder.IndexOf(group.Key)).ToList();
foreach (var group in groupedConfigEntries)
{
sb.AppendLine($"### {group.Key}");
foreach (var entry in group)
{
string defaultValue = entry.DefaultValue is string strValue ? $"\"{strValue}\"" : entry.DefaultValue.ToString();
string typeName = entry.DefaultValue.GetType().Name.ToLower();
// Adjust type naming for readability
if (typeName == "boolean") typeName = "bool";
else if (typeName == "single") typeName = "float";
else if (typeName == "int32") typeName = "int";
sb.AppendLine($"- **{AddSpacesToCamelCase(entry.Key)}**: `{entry.Key}` ({typeName}, default: {defaultValue})");
if (!string.IsNullOrEmpty(entry.Description))
{
sb.AppendLine($" {entry.Description}");
}
}
// Add spacing after each group, except the last one
if (groupedConfigEntries.IndexOf(group) < groupedConfigEntries.Count - 1)
{
sb.AppendLine();
}
}
return sb.ToString();
}
static string AddSpacesToCamelCase(string input)
{
if (string.IsNullOrEmpty(input))
return input;
StringBuilder sb = new();
for (int i = 0; i < input.Length; i++)
{
char current = input[i];
// Check for capital letters but ignore consecutive ones (e.g., XP)
bool isUpperCase = char.IsUpper(current);
bool isNotFirstChar = i > 0;
bool isPreviousCharLowerCase = isNotFirstChar && char.IsLower(input[i - 1]);
bool isNextCharLowerCase = (i < input.Length - 1) && char.IsLower(input[i + 1]);
if (isNotFirstChar && isUpperCase && (isPreviousCharLowerCase || isNextCharLowerCase))
{
sb.Append(' ');
}
sb.Append(current);
}
return sb.ToString();
}
static void UpdateReadme(string commandsSection, string configSection)
{
bool inCommandsSection = false;
bool inConfigSection = false;
bool commandsReplaced = false;
bool configReplaced = false;
List<string> newContent = [];
try
{
foreach (string line in File.ReadLines(ReadMePath))
{
if (line.Trim().Equals(COMMANDS_HEADER, StringComparison.CurrentCultureIgnoreCase))
{
// Start of "## Commands"
inCommandsSection = true;
commandsReplaced = true;
newContent.Add(commandsSection); // Add new commands
continue;
}
if (line.Trim().Equals(CONFIG_HEADER, StringComparison.CurrentCultureIgnoreCase))
{
// Start of "## Configuration"
inConfigSection = true;
configReplaced = true;
newContent.Add(configSection); // Add new configuration
continue;
}
if (inCommandsSection && line.Trim().StartsWith("## ", StringComparison.CurrentCultureIgnoreCase) &&
!line.Trim().Equals(COMMANDS_HEADER, StringComparison.CurrentCultureIgnoreCase))
{
// Reached the next section or a new header
inCommandsSection = false;
}
if (inConfigSection && line.Trim().StartsWith("## ", StringComparison.CurrentCultureIgnoreCase) &&
!line.Trim().Equals(CONFIG_HEADER, StringComparison.CurrentCultureIgnoreCase))
{
// Reached the next section or a new header
inConfigSection = false;
}
if (!inCommandsSection && !inConfigSection)
{
newContent.Add(line);
}
}
if (inConfigSection)
{
newContent.Add(configSection);
inConfigSection = false;
}
if (!commandsReplaced)
{
// Append new section if "## Commands" not found
newContent.Add(COMMANDS_HEADER);
newContent.Add(commandsSection);
}
if (!configReplaced)
{
// Append new config section if "## Configuration" not found
newContent.Add(CONFIG_HEADER);
newContent.Add(configSection);
}
File.WriteAllLines(ReadMePath, newContent);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error updating the readme: {ex.Message}");
throw;
}
}
static string GetStringArg(string args, string key)
{
foreach (Match m in _argPairRegex.Matches(args))
{
if (m.Groups["key"].Value.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
string raw = m.Groups["value"].Value.Trim();
return raw.Length > 1 && raw[0] == '\"' && raw[^1] == '\"'
? raw[1..^1] // strip quotes
: raw;
}
}
return string.Empty;
}
static bool GetBoolArg(string args, string key)
{
foreach (Match m in _argPairRegex.Matches(args))
if (string.Equals(m.Groups["key"].Value, key, StringComparison.CurrentCultureIgnoreCase))
return bool.TryParse(m.Groups["value"].Value, out bool b) && b;
return false;
}
static string Capitalize(string input) =>
string.IsNullOrEmpty(input) ? input : char.ToUpper(input[0]) + input[1..];
}