Skip to content

Commit

Permalink
Optimises the update script
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerfulBacon committed Aug 22, 2024
1 parent 3e95c64 commit b03b384
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions tools/SnakeCaseUpdate/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ static string ToSnakeCase(string name)
return Regex.Replace(name, "([a-z])([A-Z])", "$1_$2").ToLower();
}

static void UpdateFile(string filePath, string oldName, string newName)
static bool UpdateFile(string filePath, string oldName, string newName)
{
var text = File.ReadAllText(filePath);
var updated = Regex.Replace(text, $@"\b{Regex.Escape(oldName)}\(", $"{newName}(", RegexOptions.Multiline);
if (updated == text)
return;
Console.WriteLine($"Updated {oldName} to {newName} in {filePath}");

return false;
File.WriteAllText(filePath, updated);
return true;
}

string basePath = "../../../../../code";
Expand All @@ -23,6 +22,13 @@ static void UpdateFile(string filePath, string oldName, string newName)

var files = Directory.EnumerateFiles(basePath, "*.dm", SearchOption.AllDirectories);

Dictionary<string, string> updatedFunctionNames = new Dictionary<string, string>();

List<string> skippedFunctionNames = new List<string>();


Console.ForegroundColor = ConsoleColor.Yellow;

foreach (var file in files)
{
// Skip files containing the ignore string
Expand All @@ -43,22 +49,32 @@ static void UpdateFile(string filePath, string oldName, string newName)
if (!string.IsNullOrEmpty(funcName) && funcName.Length >= minFunctionNameLength)
{
string snakeCaseName = ToSnakeCase(funcName);

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Updating function {funcName} to {snakeCaseName} in {file}");
Console.ResetColor();

// Update function references in the entire codebase
foreach (var refFile in files)
{
UpdateFile(refFile, funcName, snakeCaseName);
}
updatedFunctionNames.TryAdd(funcName, snakeCaseName);
Console.WriteLine($"Renaming {funcName} to {snakeCaseName}");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Skipped {funcName}");
Console.ResetColor();
skippedFunctionNames.Add(funcName);
}
}
}

Console.ForegroundColor = ConsoleColor.Green;
Parallel.ForEach(files, (file, _, _) => {
int updates = 0;
foreach (var update in updatedFunctionNames)
{
updates += UpdateFile(file, update.Key, update.Value) ? 1 : 0;
}
if (updates > 0)
Console.WriteLine($"Updating {file}, applied {updates} updates...");
});
Console.ResetColor();

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Skipped the following function names due to being too short to reliably rename:");
foreach (var skipped in skippedFunctionNames)
{
Console.WriteLine(skipped);
}
Console.ResetColor();

0 comments on commit b03b384

Please sign in to comment.