From 2aa9c0f018b67190fbb46eed23f6ab6527ac982a Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Tue, 23 Jul 2024 13:20:35 -0700 Subject: [PATCH] Push 07-18 --- .gitignore | 1 + .vscode/launch.json | 1 - .vscode/mklink.md | 4 ++ Day/2024-Q1/Helper-2024-01-06.cs | 17 +-------- Day/2024-Q2/Helper-2024-06-23.cs | 46 ++++++++++++++++++++--- Day/2024-Q3/Helper-2024-07-18.cs | 63 ++++++++++++-------------------- Helpers/HelperMarkdown.cs | 4 ++ Models/Host.cs | 20 ++++++++++ Program.cs | 2 +- 9 files changed, 95 insertions(+), 63 deletions(-) create mode 100644 Models/Host.cs diff --git a/.gitignore b/.gitignore index 7a8378f..d13d98d 100644 --- a/.gitignore +++ b/.gitignore @@ -335,3 +335,4 @@ ASALocalRun/ .extensions-vscode .extensions-vscode-oss .extensions-vscode-insiders +.vscode/.UserSecrets/secrets.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 6cbc81b..6780bf2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,6 @@ "L:/Git/Notes-Network/.Network/.vscode/helper", "Day-Helper-2024-07-18", "hosts.jsonl", - "192.168.0.", "Network", "Wired", ".md", diff --git a/.vscode/mklink.md b/.vscode/mklink.md index 5aac8e9..6c869e5 100644 --- a/.vscode/mklink.md +++ b/.vscode/mklink.md @@ -22,3 +22,7 @@ mklink /J "L:\DevOps\Mesa_FI\File-Folder-Helper\.extensions-vscode" "C:\Users\ph mklink /J "L:\DevOps\Mesa_FI\File-Folder-Helper\.extensions-vscode-oss" "C:\Users\phares\.vscode-oss\extensions\ifx.type-script-helper-1.6.2" mklink /J "L:\DevOps\Mesa_FI\File-Folder-Helper\.extensions-vscode-insiders" "C:\Users\phares\.vscode-insiders\extensions\ifx.type-script-helper-1.6.2" ``` + +```bash Thu Jul 18 2024 13:47:40 GMT-0700 (Mountain Standard Time) +mklink /J "L:\DevOps\Mesa_FI\File-Folder-Helper\.vscode\.UserSecrets" "L:\Git\Notes-User-Secrets\.UserSecrets\8da397d4-13ec-4576-9722-3c79cad25563" +``` diff --git a/Day/2024-Q1/Helper-2024-01-06.cs b/Day/2024-Q1/Helper-2024-01-06.cs index b2a34ff..2ddf02d 100644 --- a/Day/2024-Q1/Helper-2024-01-06.cs +++ b/Day/2024-Q1/Helper-2024-01-06.cs @@ -1,3 +1,4 @@ +using File_Folder_Helper.Models; using Microsoft.Extensions.Logging; using System.Collections.ObjectModel; using System.Text; @@ -9,22 +10,6 @@ namespace File_Folder_Helper.Day; internal static partial class Helper20240106 { - private record Host([property: JsonPropertyName("a")] string? Id, - [property: JsonPropertyName("b")] string? Colon, - [property: JsonPropertyName("c")] string? Hyphen, - [property: JsonPropertyName("d")] string? Line, - [property: JsonPropertyName("e")] string? Count, - [property: JsonPropertyName("f")] string? Segments, - [property: JsonPropertyName("g")] string? Type, - [property: JsonPropertyName("h")] string? Device, - [property: JsonPropertyName("i")] string? Name); - - [JsonSourceGenerationOptions(WriteIndented = true, AllowTrailingCommas = true)] - [JsonSerializable(typeof(Host[]))] - private partial class HostSourceGenerationContext : JsonSerializerContext - { - } - private record Record(string Key, Dictionary KeyValuePairs); [JsonSourceGenerationOptions(WriteIndented = true)] diff --git a/Day/2024-Q2/Helper-2024-06-23.cs b/Day/2024-Q2/Helper-2024-06-23.cs index c2783d3..8323499 100644 --- a/Day/2024-Q2/Helper-2024-06-23.cs +++ b/Day/2024-Q2/Helper-2024-06-23.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using System.Collections.ObjectModel; namespace File_Folder_Helper.Day; @@ -11,9 +12,9 @@ internal static partial class Helper20240623 { List results = []; int? stopLine; + string[] lines; int? subTasksLine; int? codeInsidersLine; - string[] lines; string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories); foreach (string file in files) { @@ -52,13 +53,41 @@ internal static partial class Helper20240623 return results; } + private static ReadOnlyCollection GetSubTasks(string subTasks, string[] tasks, bool? foundDone, string fallbackLine, string checkFile) + { + List results = []; + string? h1 = null; + bool foundSubTasks = false; + int tasksZeroLength = tasks[0].Length; + string[] lines = File.ReadAllLines(checkFile); + foreach (string line in lines) + { + if (line.StartsWith("# ")) + h1 = line[2..]; + if (!foundSubTasks && line == subTasks) + foundSubTasks = true; + if (!foundSubTasks) + continue; + if (line.Length <= tasksZeroLength || !line.StartsWith(tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']') + continue; + results.Add($" {line}"); + } + if (h1 is null) + results.Insert(0, fallbackLine); + else + results.Insert(0, foundDone is null || !foundDone.Value ? $"- [ ] {h1}" : $"- [x] {h1}"); + return new(results); + } + internal static void UpdateSubTasksInMarkdownFiles(ILogger logger, List args) { int lineCheck; bool? foundDone; + string checkFile; string[] segments; List lines; string[] indexLines; + string fallbackLine; string checkDirectory; string done = args[7]; List indexFiles; @@ -68,6 +97,7 @@ internal static partial class Helper20240623 string indexFile = args[5]; string searchPattern = args[2]; string directoryFilter = args[8]; + ReadOnlyCollection useLines; string[] tasks = args[6].Split(','); string codeInsiders = $"{args[4]} \""; string sourceDirectory = Path.GetFullPath(args[0]); @@ -109,6 +139,7 @@ internal static partial class Helper20240623 newLines.Clear(); oldLines.Clear(); indexLines = File.ReadAllLines(indexFiles[0]); + checkDirectory = Path.GetDirectoryName(indexFiles[0]) ?? throw new Exception(); for (int i = 0; i < indexLines.Length; i++) { if (indexLines[i] == done) @@ -116,10 +147,15 @@ internal static partial class Helper20240623 segments = indexLines[i].Split(tasks[1]); if (segments.Length > 2 || !segments[0].StartsWith(tasks[0])) continue; - if (foundDone is null || !foundDone.Value) - newLines.Add($"- [ ] {segments[0][tasks[0].Length..]}"); - else - newLines.Add($"- [x] {segments[0][tasks[0].Length..]}"); + fallbackLine = foundDone is null || !foundDone.Value ? $"- [ ] {segments[0][tasks[0].Length..]}" : $"- [x] {segments[0][tasks[0].Length..]}"; + checkFile = Path.GetFullPath(Path.Combine(checkDirectory, segments[1][..^1])); + if (!File.Exists(checkFile)) + { + newLines.Add(fallbackLine); + continue; + } + useLines = GetSubTasks(subTasks, tasks, foundDone, fallbackLine, checkFile); + newLines.AddRange(useLines); } if (newLines.Count == 0) continue; diff --git a/Day/2024-Q3/Helper-2024-07-18.cs b/Day/2024-Q3/Helper-2024-07-18.cs index 416307a..85e6b6f 100644 --- a/Day/2024-Q3/Helper-2024-07-18.cs +++ b/Day/2024-Q3/Helper-2024-07-18.cs @@ -1,53 +1,27 @@ +using File_Folder_Helper.Models; using Microsoft.Extensions.Logging; using System.Collections.ObjectModel; using System.Text.Json; -using System.Text.Json.Serialization; namespace File_Folder_Helper.Day; internal static partial class Helper20240718 { - private record Host([property: JsonPropertyName("a")] string? Id, - [property: JsonPropertyName("b")] string? Colon, - [property: JsonPropertyName("c")] string? Hyphen, - [property: JsonPropertyName("d")] string? Line, - [property: JsonPropertyName("e")] string? Count, - [property: JsonPropertyName("f")] string? Segments, - [property: JsonPropertyName("g")] string? Type, - [property: JsonPropertyName("h")] string? Device, - [property: JsonPropertyName("i")] string? Name); - - [JsonSourceGenerationOptions(WriteIndented = true, AllowTrailingCommas = true)] - [JsonSerializable(typeof(Host[]))] - private partial class HostBSourceGenerationContext : JsonSerializerContext - { - } - - private record Record(string Key, Dictionary KeyValuePairs); - - [JsonSourceGenerationOptions(WriteIndented = true)] - [JsonSerializable(typeof(Dictionary>))] - private partial class DictionaryDictionaryBSourceGenerationContext : JsonSerializerContext - { - } - private static Host[] GetHosts(ILogger logger, string file) { Host[] results; string lines = File.ReadAllText(file); string json = $"[{lines.Replace("\r\n", ",")}]"; logger.LogDebug(lines); - JsonSerializerOptions jsonSerializerOptions = new() { AllowTrailingCommas = true }; - results = JsonSerializer.Deserialize(json, jsonSerializerOptions) ?? throw new NullReferenceException(); + results = JsonSerializer.Deserialize(json, HostSourceGenerationContext.Default.HostArray) ?? throw new NullReferenceException(); return results; } - private static ReadOnlyCollection GetLines(Host[] hosts, string argsIP, string title, string wired) + private static ReadOnlyCollection GetLines(Host[] hosts, string title, string wired) { - List results = ["flowchart LR", $" subgraph {title}"]; + List results = [$"# {title}", string.Empty, "```mermaid", "flowchart TB", $" subgraph {title}"]; int id; - string line; string check; List distinct = []; string newLine = $"{Environment.NewLine} "; @@ -60,32 +34,41 @@ internal static partial class Helper20240718 if (distinct.Contains(id)) throw new NotSupportedException($"{id} is not distinct!"); distinct.Add(id); - line = $"{id}(fa:{host.Type}{newLine}{host.Name}{newLine}{host.Colon}{newLine}{host.Hyphen}{newLine}{host.Device}{newLine}https://{host.Name}/)"; - results.Add(line); + results.Add($" {id}(fa:{host.Type}{newLine}{host.Colon}{newLine}{host.Hyphen}{newLine}{host.Device}{newLine}https://{host.Name}/)"); } results.Add(" end"); results.Add($" subgraph {title}"); - foreach (Host host in hosts) + foreach (Host host in from l in hosts orderby l.Location, l.Type, l.Line select l) { if (host.Id is null || host.Hyphen is null || host.Device is null || host.Name is null || host.Hyphen.Length != 17) continue; if (!int.TryParse(host.Id, out id)) throw new NotSupportedException($"{host.Id} is not a number"); check = host.Type == wired ? "-->" : "-.->"; - line = $"{id} {check} {argsIP}{host.Line}"; - results.Add(line); + results.Add($" {id} {check} |{id}| {host.Location}{host.Type}{host.Line}"); } results.Add(" end"); + results.Add($" subgraph {title}"); + foreach (Host host in from l in hosts orderby l.Line, l.Location, l.Type select l) + { + if (host.Id is null || host.Hyphen is null || host.Device is null || host.Name is null || host.Line is null || host.Hyphen.Length != 17) + continue; + if (!int.TryParse(host.Id, out id)) + throw new NotSupportedException($"{host.Id} is not a number"); + check = host.Type == wired ? "-->" : "-.->"; + results.Add($" {host.Location}{host.Type}{host.Line} {check} Line{host.Line}"); + } + results.Add(" end"); + results.Add("```"); return results.AsReadOnly(); } internal static void JsonToMarkdown(ILogger logger, List args) { Host[] hosts; - string title = args[4]; - string wired = args[5]; - string argsIP = args[3]; - string extension = args[6]; + string title = args[3]; + string wired = args[4]; + string extension = args[5]; string searchPattern = args[2]; ReadOnlyCollection lines; string sourceDirectory = Path.GetFullPath(args[0]); @@ -93,7 +76,7 @@ internal static partial class Helper20240718 foreach (string file in files) { hosts = GetHosts(logger, file); - lines = GetLines(hosts, argsIP, title, wired); + lines = GetLines(hosts, title, wired); File.WriteAllText($"{file}{extension}", string.Join(Environment.NewLine, lines)); } } diff --git a/Helpers/HelperMarkdown.cs b/Helpers/HelperMarkdown.cs index 444681f..bd00899 100644 --- a/Helpers/HelperMarkdown.cs +++ b/Helpers/HelperMarkdown.cs @@ -369,7 +369,9 @@ internal static partial class HelperMarkdown private static ReadOnlyDictionary GetFromMatterYaml(List lines, LineNumber lineNumber) { Dictionary results = []; +#pragma warning disable IL3050 IDeserializer deserializer = new DeserializerBuilder().Build(); +#pragma warning restore IL3050 ReadOnlyCollection frontMatterYamlLines = GetFromMatterYamlLines(lines, lineNumber); string frontMatterYaml = string.Join(Environment.NewLine, frontMatterYamlLines); Dictionary? keyValuePairs = deserializer.Deserialize>(frontMatterYaml); @@ -693,7 +695,9 @@ internal static partial class HelperMarkdown string frontMatterYaml; MarkdownFile markdownFile; string[] frontMatterYamlLines; +#pragma warning disable IL3050 ISerializer serializer = new SerializerBuilder().WithIndentedSequences().Build(); +#pragma warning restore IL3050 foreach (KeyValuePair relativeTo in relativeToCollection) { results.Clear(); diff --git a/Models/Host.cs b/Models/Host.cs new file mode 100644 index 0000000..b09ee29 --- /dev/null +++ b/Models/Host.cs @@ -0,0 +1,20 @@ +using System.Text.Json.Serialization; + +namespace File_Folder_Helper.Models; + +public record Host([property: JsonPropertyName("a")] string? Id, + [property: JsonPropertyName("b")] string? Colon, + [property: JsonPropertyName("c")] string? Hyphen, + [property: JsonPropertyName("d")] string? Line, + [property: JsonPropertyName("e")] string? Count, + [property: JsonPropertyName("f")] string? Segments, + [property: JsonPropertyName("g")] string? Type, + [property: JsonPropertyName("h")] string? Device, + [property: JsonPropertyName("i")] string? Name, + [property: JsonPropertyName("j")] string? Location); + +[JsonSourceGenerationOptions(WriteIndented = true, AllowTrailingCommas = true)] +[JsonSerializable(typeof(Host[]))] +public partial class HostSourceGenerationContext : JsonSerializerContext +{ +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 67b2c9e..e5689be 100644 --- a/Program.cs +++ b/Program.cs @@ -12,7 +12,7 @@ internal class Program public static void Main(string[] args) { #pragma warning disable IL3050 - HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); + HostApplicationBuilder builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(args); #pragma warning restore IL3050 _ = builder.Configuration.AddEnvironmentVariables(); _ = builder.Configuration.AddUserSecrets();