.NET Format Style Fixes
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
@@ -4,16 +4,19 @@ namespace JsonExtensionDataGenerator;
|
||||
|
||||
public class JsonExtensionDataGeneratorLauncher
|
||||
{
|
||||
private static readonly Regex _recordAndClassRegex = new("^(public record |public class )", RegexOptions.Multiline);
|
||||
private static readonly Regex _recordAndClassRegex = new(
|
||||
"^(public record |public class )",
|
||||
RegexOptions.Multiline
|
||||
);
|
||||
private static readonly Regex _endRecordClassRegex = new("^}", RegexOptions.Multiline);
|
||||
private static readonly Regex _startRecordClassRegex = new("^{", RegexOptions.Multiline);
|
||||
private const int StartRecordClassOffset = 3;
|
||||
|
||||
private static readonly Regex _extensionFinding =
|
||||
new(
|
||||
// https://regexr.com/8f5gf
|
||||
"^(public){0,1} (record|class) (\\w+(<(\\w+(,){0,1})+>){0,1})(\\(.*\\)){0,1}[\r\n ]*:[\r\n ]*(\\w+(<(\\w+(,){0,1})+>){0,1}([\r\n ]*,[\r\n ]*)*)+"
|
||||
, RegexOptions.Multiline);
|
||||
private static readonly Regex _extensionFinding = new(
|
||||
// https://regexr.com/8f5gf
|
||||
"^(public){0,1} (record|class) (\\w+(<(\\w+(,){0,1})+>){0,1})(\\(.*\\)){0,1}[\r\n ]*:[\r\n ]*(\\w+(<(\\w+(,){0,1})+>){0,1}([\r\n ]*,[\r\n ]*)*)+",
|
||||
RegexOptions.Multiline
|
||||
);
|
||||
|
||||
private static readonly Regex _extensionCleanup = new(",.*");
|
||||
|
||||
@@ -22,7 +25,6 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
|
||||
private const string Using = "using System.Text.Json.Serialization;\r\n";
|
||||
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var modelFiles = LoadModelFiles();
|
||||
@@ -39,13 +41,17 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
var content = File.ReadAllText(modelFile);
|
||||
if (!content.Contains("public record ") && !content.Contains("public class "))
|
||||
{
|
||||
Console.WriteLine($"File {fileName} doesn't contain any records or classes, skipping...");
|
||||
Console.WriteLine(
|
||||
$"File {fileName} doesn't contain any records or classes, skipping..."
|
||||
);
|
||||
// Probably an enum or interface
|
||||
return;
|
||||
}
|
||||
|
||||
var classesAndRecordsToProcessCount = _recordAndClassRegex.Matches(content).Count;
|
||||
Console.WriteLine($"Found {classesAndRecordsToProcessCount} records or classes for {fileName}");
|
||||
Console.WriteLine(
|
||||
$"Found {classesAndRecordsToProcessCount} records or classes for {fileName}"
|
||||
);
|
||||
var firstTimeFlag = false;
|
||||
var currentIndex = 0;
|
||||
try
|
||||
@@ -56,9 +62,15 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
var endIndex = FindEndClassIndex(content, startIndex);
|
||||
currentIndex = endIndex;
|
||||
// Check if this class already has the tag anywhere
|
||||
if (content.Substring(startIndex, endIndex - startIndex).Contains("[JsonExtensionData]"))
|
||||
if (
|
||||
content
|
||||
.Substring(startIndex, endIndex - startIndex)
|
||||
.Contains("[JsonExtensionData]")
|
||||
)
|
||||
{
|
||||
Console.WriteLine($"Class index {i} for {fileName} already contains [JsonExtensionData], skipping class...");
|
||||
Console.WriteLine(
|
||||
$"Class index {i} for {fileName} already contains [JsonExtensionData], skipping class..."
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -66,7 +78,9 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
{
|
||||
if (extensions.Any(e => !e.StartsWith("I")))
|
||||
{
|
||||
Console.WriteLine($"Class index {i} for {fileName} extends a parent class, skipping...");
|
||||
Console.WriteLine(
|
||||
$"Class index {i} for {fileName} extends a parent class, skipping..."
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -76,7 +90,9 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
{
|
||||
if (!content.Contains("using System.Text.Json.Serialization;"))
|
||||
{
|
||||
Console.WriteLine($"Class index {i} for {fileName} doesn't contain using for Json.Serialization. Adding.");
|
||||
Console.WriteLine(
|
||||
$"Class index {i} for {fileName} doesn't contain using for Json.Serialization. Adding."
|
||||
);
|
||||
// insert the using and adjust the indexes
|
||||
content = Using + content;
|
||||
startIndex += Using.Length;
|
||||
@@ -88,8 +104,9 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
}
|
||||
|
||||
// We need to add StartRecordClassOffset to offset the EOL
|
||||
var insertionIndex = _startRecordClassRegex.Match(content, startIndex, endIndex - startIndex).Index +
|
||||
StartRecordClassOffset;
|
||||
var insertionIndex =
|
||||
_startRecordClassRegex.Match(content, startIndex, endIndex - startIndex).Index
|
||||
+ StartRecordClassOffset;
|
||||
content = content.Insert(insertionIndex, Insertion);
|
||||
Console.WriteLine($"Class index {i} for {fileName} processed.");
|
||||
currentIndex += Insertion.Length;
|
||||
@@ -117,7 +134,9 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
if (match.Success)
|
||||
{
|
||||
var extensionsGroup = match.Groups[8];
|
||||
extensions = extensionsGroup.Captures.Select(c => _extensionCleanup.Replace(c.Value, ""));
|
||||
extensions = extensionsGroup.Captures.Select(c =>
|
||||
_extensionCleanup.Replace(c.Value, "")
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -138,7 +157,12 @@ public class JsonExtensionDataGeneratorLauncher
|
||||
private static IEnumerable<string> LoadModelFiles()
|
||||
{
|
||||
var projectDir = Directory.GetParent("./").Parent.Parent.Parent.Parent.Parent;
|
||||
var modelsDir = Path.Combine(projectDir.FullName, "Libraries", "SPTarkov.Server.Core", "Models");
|
||||
var modelsDir = Path.Combine(
|
||||
projectDir.FullName,
|
||||
"Libraries",
|
||||
"SPTarkov.Server.Core",
|
||||
"Models"
|
||||
);
|
||||
return Directory.GetFiles(modelsDir, "*.cs", SearchOption.AllDirectories);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user