Convert InfinityQS Project Files

This commit is contained in:
Mike Phares 2024-10-03 11:18:41 -07:00
parent 03ada95fbb
commit b8ebd7cfe1
3 changed files with 184 additions and 5 deletions

10
.vscode/launch.json vendored
View File

@ -13,12 +13,12 @@
"args": [
"s",
"X",
"\\\\messa04ec.infineon.com\\EC_SPC_Si\\SPC\\Projects\\Active",
"Day-Helper-2024-09-25",
"*.ipj",
"=TEST,",
"D:/EC_Shares/EC_SPC_Si/SPC/Projects/Active",
"Day-Helper-2024-10-02",
"14 point thickness.BAK_241001115400",
"DE_TS_",
"DE_TS_EQU",
"L:/DevOps/Mesa_FI/File-Folder-Helper/.vscode/helper/iqs",
"555",
"666",
"777",
"888",

View File

@ -111,6 +111,8 @@ internal static class HelperDay
Day.Q32024.Helper20240916.DebugProxyPass(logger, args);
else if (args[1] == "Day-Helper-2024-09-25")
Day.Q32024.Helper20240925.DistinctTests(logger, args);
else if (args[1] == "Day-Helper-2024-10-02")
Day.Q42024.Helper20241002.ConvertInfinityQSProjectFiles(logger, args);
else
throw new Exception(appSettings.Company);
}

View File

@ -0,0 +1,177 @@
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);
}
}