-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
GenerateREADME.cs
342 lines (281 loc) · 13.7 KB
/
GenerateREADME.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
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
341
342
using System.Text;
using System.Text.RegularExpressions;
using static Bloodcraft.Services.ConfigService;
namespace Bloodcraft
{
internal static class GenerateREADME
{
// Paths set by the user or build script
static string CommandsPath { get; set; }
static string ReadMePath { get; set; }
// Regex patterns for parsing commands
static readonly Regex _commandGroupRegex = new(@"\[CommandGroup\(name:\s*""(?<group>[^""]+)"",\s*""(?<short>[^""]+)""\)\]"); // the first and second one here should really just be one but this works and tired so leaving >_>
static readonly Regex _commandGroupAndShortRegex = new(@"\[CommandGroup\(name:\s*""(?<group>[^""]+)""(?:\s*,\s*short:\s*""(?<short>[^""]+)"")?\)\]");
static readonly Regex _commandAttributeRegex = new(@"\[Command\(name:\s*""(?<name>[^""]+)""(?:,\s*shortHand:\s*""(?<shortHand>[^""]+)"")?(?:,\s*adminOnly:\s*(?<adminOnly>\w+))?(?:,\s*usage:\s*""(?<usage>[^""]+)"")?(?:,\s*description:\s*""(?<description>[^""]+)"")?\)\]");
// Constants for README sections
const string COMMANDS_HEADER = "## Commands";
const string CONFIG_HEADER = "## Configuration";
// We'll store all commands in this structure before outputting them
static readonly Dictionary<(string groupName, string groupShort), List<(string name, string shortHand, bool adminOnly, string usage, string description)>> commandsByGroup
= [];
// Entry point for post-build invocation
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}");
}
}
// Main method to generate the README
static void Generate()
{
CollectCommands();
var commandsSection = BuildCommandsSection();
var configSection = BuildConfigSection();
UpdateReadme(commandsSection, configSection);
}
// Collect commands from all files into the dictionary
static void CollectCommands()
{
var files = Directory.GetFiles(CommandsPath, "*.cs")
.Where(file => !Path.GetFileName(file).Equals("TestCommands.cs", StringComparison.OrdinalIgnoreCase));
foreach (var file in files)
{
var fileContent = File.ReadAllText(file);
var commandGroupMatch = _commandGroupRegex.Match(fileContent);
if (!commandGroupMatch.Success) commandGroupMatch = _commandGroupAndShortRegex.Match(fileContent);
string groupName, groupShort;
if (commandGroupMatch.Success)
{
// Extract group name and shorthand from regex match
groupName = commandGroupMatch.Groups["group"].Value;
groupShort = commandGroupMatch.Groups["short"].Value;
}
else
{
groupName = "misc";
groupShort = string.Empty;
}
if (!commandsByGroup.TryGetValue((groupName, groupShort), out var cmdList))
{
cmdList = [];
commandsByGroup[(groupName, groupShort)] = cmdList;
}
// Find all commands within the group
foreach (Match commandMatch in _commandAttributeRegex.Matches(fileContent))
{
string name = commandMatch.Groups["name"].Value;
// If optional attributes are missing, they won't have .Success = true
string shortHand = commandMatch.Groups["shortHand"].Success ? commandMatch.Groups["shortHand"].Value : string.Empty;
bool adminOnly = false;
if (commandMatch.Groups["adminOnly"].Success)
{
bool.TryParse(commandMatch.Groups["adminOnly"].Value, out adminOnly);
}
string usage = commandMatch.Groups["usage"].Success ? commandMatch.Groups["usage"].Value : string.Empty;
string description = commandMatch.Groups["description"].Success ? commandMatch.Groups["description"].Value : string.Empty;
cmdList.Add((name, shortHand, adminOnly, usage, description));
}
}
}
static string BuildCommandsSection()
{
StringBuilder sb = new();
sb.AppendLine("## Commands");
sb.AppendLine();
var orderedGroups = commandsByGroup.Keys.OrderBy(g => g.groupName).ToList();
foreach (var group in orderedGroups)
{
var (groupName, groupShort) = group;
sb.AppendLine($"### {Capitalize(groupName)} Commands");
var cmdList = commandsByGroup[group];
foreach (var (name, shortHand, adminOnly, usage, description) in cmdList)
{
bool hasShorthand = !string.IsNullOrEmpty(shortHand);
bool hasGroupShort = !string.IsNullOrEmpty(groupShort);
// If has parameters and no shorthand replace
string commandUsage = string.IsNullOrEmpty(usage) ? name : usage;
string nameReplacement = commandUsage.EndsWith(name) || !hasShorthand ? name : string.Empty;
// Prebuild command line strings
string adminLock = adminOnly ? " 🔒" : string.Empty;
string commandParameters = string.Empty;
if (hasGroupShort)
{
commandParameters = hasShorthand ? commandUsage.Replace($".{groupShort} {shortHand}", "") : commandUsage.Replace($".{groupShort} {nameReplacement}", "");
}
else
{
commandParameters = hasShorthand ? commandUsage.Replace($".{groupName} {shortHand}", "") : commandUsage.Replace($".{groupName} {nameReplacement}", "");
}
// Build main command line string
var commandLine = $"- `.{groupName} {name}{commandParameters}`{adminLock}";
// Handle misc formatting
if (groupName == "misc")
{
commandLine = commandLine.Replace("misc ", "");
int adjustmentLength = adminOnly ? usage.Length + 4 : usage.Length + 1; // +3 for " 🔒"
commandLine = commandLine[..^adjustmentLength] + (adminOnly ? "` 🔒" : "`");
}
sb.AppendLine(commandLine);
// Description line if available
if (!string.IsNullOrEmpty(description))
{
sb.AppendLine($" - {description}");
}
sb.AppendLine($" - Shortcut: *{commandUsage}*");
}
// Add spacing after each group, except the last one
if (orderedGroups.IndexOf(group) < orderedGroups.Count - 1)
{
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.OrdinalIgnoreCase))
{
// Start of "## Commands"
inCommandsSection = true;
commandsReplaced = true;
newContent.Add(commandsSection); // Add new commands
continue;
}
if (line.Trim().Equals(CONFIG_HEADER, StringComparison.OrdinalIgnoreCase))
{
// Start of "## Configuration"
inConfigSection = true;
configReplaced = true;
newContent.Add(configSection); // Add new configuration
continue;
}
if (inCommandsSection && line.Trim().StartsWith("## ", StringComparison.OrdinalIgnoreCase) &&
!line.Trim().Equals(COMMANDS_HEADER, StringComparison.OrdinalIgnoreCase))
{
// Reached the next section or a new header
inCommandsSection = false;
}
if (inConfigSection && line.Trim().StartsWith("## ", StringComparison.OrdinalIgnoreCase) &&
!line.Trim().Equals(CONFIG_HEADER, StringComparison.OrdinalIgnoreCase))
{
// 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;
}
}
// Helper method to capitalize strings
static string Capitalize(string input) =>
string.IsNullOrEmpty(input) ? input : char.ToUpper(input[0]) + input[1..];
}
}