Push 07-18

This commit is contained in:
Mike Phares 2024-07-23 13:20:35 -07:00
parent b2ab434ec3
commit 2aa9c0f018
9 changed files with 95 additions and 63 deletions

1
.gitignore vendored
View File

@ -335,3 +335,4 @@ ASALocalRun/
.extensions-vscode .extensions-vscode
.extensions-vscode-oss .extensions-vscode-oss
.extensions-vscode-insiders .extensions-vscode-insiders
.vscode/.UserSecrets/secrets.json

1
.vscode/launch.json vendored
View File

@ -16,7 +16,6 @@
"L:/Git/Notes-Network/.Network/.vscode/helper", "L:/Git/Notes-Network/.Network/.vscode/helper",
"Day-Helper-2024-07-18", "Day-Helper-2024-07-18",
"hosts.jsonl", "hosts.jsonl",
"192.168.0.",
"Network", "Network",
"Wired", "Wired",
".md", ".md",

4
.vscode/mklink.md vendored
View File

@ -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-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" 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"
```

View File

@ -1,3 +1,4 @@
using File_Folder_Helper.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Text; using System.Text;
@ -9,22 +10,6 @@ namespace File_Folder_Helper.Day;
internal static partial class Helper20240106 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<string, string> KeyValuePairs); private record Record(string Key, Dictionary<string, string> KeyValuePairs);
[JsonSourceGenerationOptions(WriteIndented = true)] [JsonSourceGenerationOptions(WriteIndented = true)]

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
namespace File_Folder_Helper.Day; namespace File_Folder_Helper.Day;
@ -11,9 +12,9 @@ internal static partial class Helper20240623
{ {
List<Record> results = []; List<Record> results = [];
int? stopLine; int? stopLine;
string[] lines;
int? subTasksLine; int? subTasksLine;
int? codeInsidersLine; int? codeInsidersLine;
string[] lines;
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories); string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
foreach (string file in files) foreach (string file in files)
{ {
@ -52,13 +53,41 @@ internal static partial class Helper20240623
return results; return results;
} }
private static ReadOnlyCollection<string> GetSubTasks(string subTasks, string[] tasks, bool? foundDone, string fallbackLine, string checkFile)
{
List<string> 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<Worker> logger, List<string> args) internal static void UpdateSubTasksInMarkdownFiles(ILogger<Worker> logger, List<string> args)
{ {
int lineCheck; int lineCheck;
bool? foundDone; bool? foundDone;
string checkFile;
string[] segments; string[] segments;
List<string> lines; List<string> lines;
string[] indexLines; string[] indexLines;
string fallbackLine;
string checkDirectory; string checkDirectory;
string done = args[7]; string done = args[7];
List<string> indexFiles; List<string> indexFiles;
@ -68,6 +97,7 @@ internal static partial class Helper20240623
string indexFile = args[5]; string indexFile = args[5];
string searchPattern = args[2]; string searchPattern = args[2];
string directoryFilter = args[8]; string directoryFilter = args[8];
ReadOnlyCollection<string> useLines;
string[] tasks = args[6].Split(','); string[] tasks = args[6].Split(',');
string codeInsiders = $"{args[4]} \""; string codeInsiders = $"{args[4]} \"";
string sourceDirectory = Path.GetFullPath(args[0]); string sourceDirectory = Path.GetFullPath(args[0]);
@ -109,6 +139,7 @@ internal static partial class Helper20240623
newLines.Clear(); newLines.Clear();
oldLines.Clear(); oldLines.Clear();
indexLines = File.ReadAllLines(indexFiles[0]); indexLines = File.ReadAllLines(indexFiles[0]);
checkDirectory = Path.GetDirectoryName(indexFiles[0]) ?? throw new Exception();
for (int i = 0; i < indexLines.Length; i++) for (int i = 0; i < indexLines.Length; i++)
{ {
if (indexLines[i] == done) if (indexLines[i] == done)
@ -116,10 +147,15 @@ internal static partial class Helper20240623
segments = indexLines[i].Split(tasks[1]); segments = indexLines[i].Split(tasks[1]);
if (segments.Length > 2 || !segments[0].StartsWith(tasks[0])) if (segments.Length > 2 || !segments[0].StartsWith(tasks[0]))
continue; continue;
if (foundDone is null || !foundDone.Value) fallbackLine = foundDone is null || !foundDone.Value ? $"- [ ] {segments[0][tasks[0].Length..]}" : $"- [x] {segments[0][tasks[0].Length..]}";
newLines.Add($"- [ ] {segments[0][tasks[0].Length..]}"); checkFile = Path.GetFullPath(Path.Combine(checkDirectory, segments[1][..^1]));
else if (!File.Exists(checkFile))
newLines.Add($"- [x] {segments[0][tasks[0].Length..]}"); {
newLines.Add(fallbackLine);
continue;
}
useLines = GetSubTasks(subTasks, tasks, foundDone, fallbackLine, checkFile);
newLines.AddRange(useLines);
} }
if (newLines.Count == 0) if (newLines.Count == 0)
continue; continue;

View File

@ -1,53 +1,27 @@
using File_Folder_Helper.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
namespace File_Folder_Helper.Day; namespace File_Folder_Helper.Day;
internal static partial class Helper20240718 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<string, string> KeyValuePairs);
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Dictionary<string, Dictionary<string, string>>))]
private partial class DictionaryDictionaryBSourceGenerationContext : JsonSerializerContext
{
}
private static Host[] GetHosts(ILogger<Worker> logger, string file) private static Host[] GetHosts(ILogger<Worker> logger, string file)
{ {
Host[] results; Host[] results;
string lines = File.ReadAllText(file); string lines = File.ReadAllText(file);
string json = $"[{lines.Replace("\r\n", ",")}]"; string json = $"[{lines.Replace("\r\n", ",")}]";
logger.LogDebug(lines); logger.LogDebug(lines);
JsonSerializerOptions jsonSerializerOptions = new() { AllowTrailingCommas = true }; results = JsonSerializer.Deserialize(json, HostSourceGenerationContext.Default.HostArray) ?? throw new NullReferenceException();
results = JsonSerializer.Deserialize<Host[]>(json, jsonSerializerOptions) ?? throw new NullReferenceException();
return results; return results;
} }
private static ReadOnlyCollection<string> GetLines(Host[] hosts, string argsIP, string title, string wired) private static ReadOnlyCollection<string> GetLines(Host[] hosts, string title, string wired)
{ {
List<string> results = ["flowchart LR", $" subgraph {title}"]; List<string> results = [$"# {title}", string.Empty, "```mermaid", "flowchart TB", $" subgraph {title}"];
int id; int id;
string line;
string check; string check;
List<int> distinct = []; List<int> distinct = [];
string newLine = $"{Environment.NewLine} "; string newLine = $"{Environment.NewLine} ";
@ -60,32 +34,41 @@ internal static partial class Helper20240718
if (distinct.Contains(id)) if (distinct.Contains(id))
throw new NotSupportedException($"{id} is not distinct!"); throw new NotSupportedException($"{id} is not distinct!");
distinct.Add(id); 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($" {id}(fa:{host.Type}{newLine}{host.Colon}{newLine}{host.Hyphen}{newLine}{host.Device}{newLine}https://{host.Name}/)");
results.Add(line);
} }
results.Add(" end"); results.Add(" end");
results.Add($" subgraph {title}"); 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) if (host.Id is null || host.Hyphen is null || host.Device is null || host.Name is null || host.Hyphen.Length != 17)
continue; continue;
if (!int.TryParse(host.Id, out id)) if (!int.TryParse(host.Id, out id))
throw new NotSupportedException($"{host.Id} is not a number"); throw new NotSupportedException($"{host.Id} is not a number");
check = host.Type == wired ? "-->" : "-.->"; check = host.Type == wired ? "-->" : "-.->";
line = $"{id} {check} {argsIP}{host.Line}"; results.Add($" {id} {check} |{id}| {host.Location}{host.Type}{host.Line}");
results.Add(line);
} }
results.Add(" end"); 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(); return results.AsReadOnly();
} }
internal static void JsonToMarkdown(ILogger<Worker> logger, List<string> args) internal static void JsonToMarkdown(ILogger<Worker> logger, List<string> args)
{ {
Host[] hosts; Host[] hosts;
string title = args[4]; string title = args[3];
string wired = args[5]; string wired = args[4];
string argsIP = args[3]; string extension = args[5];
string extension = args[6];
string searchPattern = args[2]; string searchPattern = args[2];
ReadOnlyCollection<string> lines; ReadOnlyCollection<string> lines;
string sourceDirectory = Path.GetFullPath(args[0]); string sourceDirectory = Path.GetFullPath(args[0]);
@ -93,7 +76,7 @@ internal static partial class Helper20240718
foreach (string file in files) foreach (string file in files)
{ {
hosts = GetHosts(logger, file); hosts = GetHosts(logger, file);
lines = GetLines(hosts, argsIP, title, wired); lines = GetLines(hosts, title, wired);
File.WriteAllText($"{file}{extension}", string.Join(Environment.NewLine, lines)); File.WriteAllText($"{file}{extension}", string.Join(Environment.NewLine, lines));
} }
} }

View File

@ -369,7 +369,9 @@ internal static partial class HelperMarkdown
private static ReadOnlyDictionary<string, object> GetFromMatterYaml(List<string> lines, LineNumber lineNumber) private static ReadOnlyDictionary<string, object> GetFromMatterYaml(List<string> lines, LineNumber lineNumber)
{ {
Dictionary<string, object> results = []; Dictionary<string, object> results = [];
#pragma warning disable IL3050
IDeserializer deserializer = new DeserializerBuilder().Build(); IDeserializer deserializer = new DeserializerBuilder().Build();
#pragma warning restore IL3050
ReadOnlyCollection<string> frontMatterYamlLines = GetFromMatterYamlLines(lines, lineNumber); ReadOnlyCollection<string> frontMatterYamlLines = GetFromMatterYamlLines(lines, lineNumber);
string frontMatterYaml = string.Join(Environment.NewLine, frontMatterYamlLines); string frontMatterYaml = string.Join(Environment.NewLine, frontMatterYamlLines);
Dictionary<string, object>? keyValuePairs = deserializer.Deserialize<Dictionary<string, object>>(frontMatterYaml); Dictionary<string, object>? keyValuePairs = deserializer.Deserialize<Dictionary<string, object>>(frontMatterYaml);
@ -693,7 +695,9 @@ internal static partial class HelperMarkdown
string frontMatterYaml; string frontMatterYaml;
MarkdownFile markdownFile; MarkdownFile markdownFile;
string[] frontMatterYamlLines; string[] frontMatterYamlLines;
#pragma warning disable IL3050
ISerializer serializer = new SerializerBuilder().WithIndentedSequences().Build(); ISerializer serializer = new SerializerBuilder().WithIndentedSequences().Build();
#pragma warning restore IL3050
foreach (KeyValuePair<string, MarkdownFileAndLines> relativeTo in relativeToCollection) foreach (KeyValuePair<string, MarkdownFileAndLines> relativeTo in relativeToCollection)
{ {
results.Clear(); results.Clear();

20
Models/Host.cs Normal file
View File

@ -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
{
}

View File

@ -12,7 +12,7 @@ internal class Program
public static void Main(string[] args) public static void Main(string[] args)
{ {
#pragma warning disable IL3050 #pragma warning disable IL3050
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); HostApplicationBuilder builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(args);
#pragma warning restore IL3050 #pragma warning restore IL3050
_ = builder.Configuration.AddEnvironmentVariables(); _ = builder.Configuration.AddEnvironmentVariables();
_ = builder.Configuration.AddUserSecrets<Program>(); _ = builder.Configuration.AddUserSecrets<Program>();