177 lines
6.5 KiB
C#
177 lines
6.5 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
namespace File_Folder_Helper.Day.Q42024;
|
|
|
|
internal static partial class Helper20241002
|
|
{
|
|
|
|
public record Record(string? Calculation,
|
|
string Chart,
|
|
string Group,
|
|
string GroupId,
|
|
long Id,
|
|
string? RawCalculation,
|
|
string Test,
|
|
string TestId)
|
|
{
|
|
|
|
public static Record Get(Record record, string? calculation) =>
|
|
new(calculation,
|
|
record.Chart,
|
|
record.Group,
|
|
record.GroupId,
|
|
record.Id,
|
|
record.RawCalculation,
|
|
record.Test,
|
|
record.TestId);
|
|
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(ReadOnlyCollection<Record>))]
|
|
internal partial class RecordCollectionSourceGenerationContext : JsonSerializerContext
|
|
{
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(ReadOnlyDictionary<string, Record>))]
|
|
internal partial class RecordDictionarySourceGenerationContext : JsonSerializerContext
|
|
{
|
|
}
|
|
|
|
private static string? GetCalculation(string searchPatternC, string[] lines, int i, string id, long idValue)
|
|
{
|
|
string? result = null;
|
|
string line;
|
|
long check;
|
|
string[] segments;
|
|
string[] segmentsB;
|
|
for (int j = i + 1; j < lines.Length; j++)
|
|
{
|
|
line = lines[j];
|
|
if (!line.Contains(id))
|
|
break;
|
|
segments = line.Split(searchPatternC);
|
|
if (segments.Length < 2)
|
|
continue;
|
|
segmentsB = segments[1].Split('=');
|
|
if (segmentsB.Length < 2)
|
|
continue;
|
|
if (!long.TryParse(segmentsB[0], out check))
|
|
continue;
|
|
if (check != idValue)
|
|
break;
|
|
result = segmentsB[1];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static ReadOnlyCollection<Record> GetRecords(string sourceDirectory, string searchPattern, string searchPatternB, string searchPatternC)
|
|
{
|
|
List<Record> results = [];
|
|
string id;
|
|
string line;
|
|
long idValue;
|
|
string[] lines;
|
|
string[] segments;
|
|
string[] segmentsB;
|
|
string[] segmentsC;
|
|
string? calculation;
|
|
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
|
|
foreach (string file in files)
|
|
{
|
|
lines = File.ReadAllLines(file);
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
line = lines[i];
|
|
segments = line.Split(searchPatternB);
|
|
if (segments.Length < 2)
|
|
continue;
|
|
segmentsB = segments[1].Split('=');
|
|
if (segmentsB.Length < 2)
|
|
continue;
|
|
if (!long.TryParse(segmentsB[0], out idValue))
|
|
continue;
|
|
id = segmentsB[0];
|
|
segmentsC = segments[1].Split(',');
|
|
if (segmentsC.Length < 4)
|
|
continue;
|
|
calculation = GetCalculation(searchPatternC, lines, i, id, idValue);
|
|
results.Add(new(null,
|
|
Path.GetFileName(file),
|
|
segmentsC[2].Trim('"'),
|
|
segmentsC[1],
|
|
idValue,
|
|
calculation,
|
|
segmentsC[4].Trim('"'),
|
|
segmentsC[3]));
|
|
}
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static string GetKey(Record record) =>
|
|
$"ch({record.Id + 1})";
|
|
|
|
private static ReadOnlyDictionary<string, Record> GetKeyValuePairs(ReadOnlyCollection<Record> records)
|
|
{
|
|
Dictionary<string, Record> results = [];
|
|
string key;
|
|
string? last = null;
|
|
foreach (Record record in records)
|
|
{
|
|
if (last is not null && record.Chart != last)
|
|
continue;
|
|
last = record.Chart;
|
|
key = GetKey(record);
|
|
if (results.ContainsKey(key))
|
|
continue;
|
|
results.Add(key, record);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static ReadOnlyDictionary<string, Record> GetKeyValuePairs(ReadOnlyDictionary<string, Record> keyValuePairs)
|
|
{
|
|
Dictionary<string, Record> results = [];
|
|
Record result;
|
|
Record record;
|
|
string? calculation;
|
|
foreach (KeyValuePair<string, Record> keyValuePair in keyValuePairs)
|
|
{
|
|
record = keyValuePair.Value;
|
|
calculation = record.RawCalculation;
|
|
if (calculation is not null)
|
|
{
|
|
foreach (KeyValuePair<string, Record> kVP in keyValuePairs)
|
|
calculation = calculation.Replace(kVP.Key, $"%DCS(Value, {kVP.Value.Test})");
|
|
}
|
|
result = Record.Get(record, calculation);
|
|
results.Add(keyValuePair.Key, result);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
internal static void ConvertInfinityQSProjectFiles(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string searchPattern = args[2];
|
|
string searchPatternB = args[3];
|
|
string searchPatternC = args[4];
|
|
string destinationDirectory = args[5];
|
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
|
if (!Directory.Exists(destinationDirectory))
|
|
_ = Directory.CreateDirectory(destinationDirectory);
|
|
ReadOnlyCollection<Record> records = GetRecords(sourceDirectory, searchPattern, searchPatternB, searchPatternC);
|
|
logger.LogInformation("Found {records} records(s)", records.Count);
|
|
ReadOnlyDictionary<string, Record> collection = GetKeyValuePairs(records);
|
|
logger.LogInformation("Found {collection} collection(s)", collection.Count);
|
|
ReadOnlyDictionary<string, Record> keyValuePairs = GetKeyValuePairs(collection);
|
|
logger.LogInformation("Found {keyValuePairs}", keyValuePairs.Count);
|
|
string json = JsonSerializer.Serialize(keyValuePairs, RecordDictionarySourceGenerationContext.Default.ReadOnlyDictionaryStringRecord);
|
|
string fileName = Path.Combine(destinationDirectory, ".json");
|
|
File.WriteAllText(fileName, json);
|
|
}
|
|
|
|
} |