Download SSL Certificates Sort Subtasks of Markdown files Test BioRad EAF CopyDirectories json to Markdown Sort Day 2024 Q2 GitRemoteRemove Handle directoryInfo.LinkTarget better Remove StartAt Handle directoryInfo.LinkTarget
84 lines
3.6 KiB
C#
84 lines
3.6 KiB
C#
using File_Folder_Helper.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
using System.Text.Json;
|
|
|
|
namespace File_Folder_Helper.Day;
|
|
|
|
internal static partial class Helper20240718
|
|
{
|
|
|
|
private static Host[] GetHosts(ILogger<Worker> logger, string file)
|
|
{
|
|
Host[] results;
|
|
string lines = File.ReadAllText(file);
|
|
string json = $"[{lines.Replace("\r\n", ",")}]";
|
|
logger.LogDebug(lines);
|
|
results = JsonSerializer.Deserialize(json, HostSourceGenerationContext.Default.HostArray) ?? throw new NullReferenceException();
|
|
return results;
|
|
}
|
|
|
|
private static ReadOnlyCollection<string> GetLines(Host[] hosts, string title, string wired)
|
|
{
|
|
List<string> results = [$"# {title}", string.Empty, "```mermaid", "flowchart TB", $" subgraph {title}"];
|
|
int id;
|
|
string check;
|
|
List<int> distinct = [];
|
|
string newLine = $"{Environment.NewLine} ";
|
|
foreach (Host host in hosts)
|
|
{
|
|
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");
|
|
if (distinct.Contains(id))
|
|
throw new NotSupportedException($"{id} is not distinct!");
|
|
distinct.Add(id);
|
|
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 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 ? "-->" : "-.->";
|
|
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<Worker> logger, List<string> args)
|
|
{
|
|
Host[] hosts;
|
|
string title = args[3];
|
|
string wired = args[4];
|
|
string extension = args[5];
|
|
string searchPattern = args[2];
|
|
ReadOnlyCollection<string> lines;
|
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
|
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
|
foreach (string file in files)
|
|
{
|
|
hosts = GetHosts(logger, file);
|
|
lines = GetLines(hosts, title, wired);
|
|
File.WriteAllText($"{file}{extension}", string.Join(Environment.NewLine, lines));
|
|
}
|
|
}
|
|
|
|
} |