Mirror/.github/ModPreprocessorDefine.csx

114 lines
4.2 KiB
Plaintext
Raw Normal View History

2023-11-09 11:16:40 +00:00
using System;
using System.IO;
using System.Text.RegularExpressions;
2023-11-09 13:15:11 +00:00
Console.WriteLine("ModPreprocessorDefine Started");
2023-11-09 14:45:04 +00:00
// Console.Out.Flush();
2023-11-09 13:15:11 +00:00
2023-11-09 13:08:23 +00:00
ModPreprocessorDefine.DoSomething();
2023-11-09 13:20:02 +00:00
Console.WriteLine("ModPreprocessorDefine Finished");
2023-11-09 14:45:04 +00:00
// Console.Out.Flush();
2023-11-09 13:20:02 +00:00
2023-11-09 13:08:23 +00:00
class ModPreprocessorDefine
2023-11-09 10:58:20 +00:00
{
2023-11-09 13:08:23 +00:00
public static void DoSomething()
2023-11-09 12:39:28 +00:00
{
2023-11-09 13:20:02 +00:00
// Define the path to the PreprocessorDefine.cs file
string filePath = "Assets/Mirror/CompilerSymbols/PreprocessorDefine.cs";
2023-11-09 11:50:35 +00:00
2023-11-09 13:20:02 +00:00
// Read the contents of the file
string fileContents = File.ReadAllText(filePath);
Console.WriteLine("ModPreprocessorDefine File read");
2023-11-09 14:45:04 +00:00
// Console.Out.Flush();
2023-11-09 11:50:35 +00:00
2023-11-09 13:20:02 +00:00
// Find and remove the first entry ending with "_OR_NEWER"
fileContents = RemoveFirstOrNewerEntry(fileContents);
Console.WriteLine("ModPreprocessorDefine Old entry removed");
2023-11-09 14:45:04 +00:00
// Console.Out.Flush();
2023-11-09 11:50:35 +00:00
2023-11-09 13:20:02 +00:00
// Find the last entry and capture the version number
string versionNumber = GetLastVersionNumber(fileContents);
Console.WriteLine($"ModPreprocessorDefine current version {versionNumber}");
2023-11-09 14:45:04 +00:00
// Console.Out.Flush();
2023-11-09 10:58:20 +00:00
2023-11-09 13:20:02 +00:00
// Append a new entry with the correct indentation and next version number
fileContents = AppendNewEntry(fileContents, versionNumber);
Console.WriteLine("ModPreprocessorDefine New entry appended");
2023-11-09 14:45:04 +00:00
// Console.Out.Flush();
2023-11-09 13:20:02 +00:00
// Write the modified contents back to the file
File.WriteAllText(filePath, fileContents);
2023-11-09 12:39:28 +00:00
}
2023-11-09 10:58:20 +00:00
2023-11-09 13:15:11 +00:00
static string RemoveFirstOrNewerEntry(string input)
2023-11-09 12:39:28 +00:00
{
// Regex pattern to match the first entry ending with "_OR_NEWER"
string pattern = @"^\s*""[^""]*_OR_NEWER""\s*,\s*$";
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Find the first match
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// If a match is found, remove the entire line
if (match.Success)
{
input = input.Remove(match.Index, match.Length);
}
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
return input;
2023-11-09 10:58:20 +00:00
}
2023-11-09 13:15:11 +00:00
static string GetLastVersionNumber(string input)
2023-11-09 12:39:28 +00:00
{
// Regex pattern to match the last entry and capture the version number
string pattern = @"^\s*""([^""]*)_OR_NEWER""\s*,\s*$";
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Find all matches
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Capture the version number from the last match
string versionNumber = matches.Count > 0 ? matches[matches.Count - 1].Groups[1].Value : "";
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
return versionNumber;
}
2023-11-09 10:58:20 +00:00
2023-11-09 13:15:11 +00:00
static string AppendNewEntry(string input, string versionNumber)
2023-11-09 12:39:28 +00:00
{
// Calculate the next version number (increment by 1)
int nextVersion = int.TryParse(versionNumber, out int currentVersion) ? currentVersion + 1 : 1;
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Get the indentation of the "HashSet<string> defines = new HashSet<string>" line
string indentation = GetHashSetIndentation(input);
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Create the new entry with the correct indentation and next version number
string newEntry = indentation + $" \"MIRROR_{nextVersion}_OR_NEWER\"";
Console.WriteLine($"New entry: {newEntry}");
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Find the position of the "defines" HashSet and insert the new entry into it
int definesStartIndex = input.IndexOf("HashSet<string> defines = new HashSet<string>");
int definesEndIndex = input.IndexOf("};", definesStartIndex) + 1;
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Insert the comma and new entry into the "defines" HashSet
input = input.Remove(definesEndIndex - 2, 2); // Remove the trailing "};"
input = input.Insert(definesEndIndex - 2, $",\n{newEntry}\n{indentation}}};");
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
Console.WriteLine(input);
2023-11-09 11:35:23 +00:00
2023-11-09 12:39:28 +00:00
return input;
}
2023-11-09 10:58:20 +00:00
2023-11-09 13:15:11 +00:00
static string GetHashSetIndentation(string input)
2023-11-09 12:39:28 +00:00
{
// Regex pattern to match the indentation of "HashSet<string> defines = new HashSet<string>"
string pattern = @"^\s*HashSet<string> defines = new HashSet<string>";
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// Find the first match
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
// If a match is found, capture the indentation and add 4 spaces
string indentation = match.Success ? Regex.Match(match.Value, @"^\s*").Value : "";
2023-11-09 10:58:20 +00:00
2023-11-09 12:39:28 +00:00
return indentation;
}
2023-11-09 10:58:20 +00:00
}