Minor changes
Empty file ISO Add date back for just .kanbn Removed HardcodedFileSearchAndSort Sync with 01-23 JsonToTsv System.Text.Json White-List Ready to move to Move Helper Remove Whitelist Force Start At Check for .git directory before ls Optional Allow root for unc path nuget bump PreVerify EnforceCodeStyleInBuild dotnet_analyzer_diagnostic HelperGit searchDelegate Host File AlertIfNewDeviceIsConnected AOT SetFrontMatterAndH1 Match Error Unknown with better logging Undo 04-05 WriteAppendToHostConfFile MonA IsKanbanIndex Dotnet Format Pre-commit NPM CreateWindowsShortcut Working directory Split description Copy tests Ready to test Delete after a couple of days GitConfigCleanUp knb Files
This commit is contained in:
@ -1,538 +0,0 @@
|
||||
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 Helper20231212
|
||||
{
|
||||
|
||||
private record Debugging(
|
||||
[property: JsonPropertyName("Level")] int Level
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Debugging))]
|
||||
private partial class DebuggingSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Distance(
|
||||
[property: JsonPropertyName("Value")] int Value
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Distance))]
|
||||
private partial class DistanceSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Finished(
|
||||
[property: JsonPropertyName("Elapsed")] double Elapsed,
|
||||
[property: JsonPropertyName("Exit")] string Exit,
|
||||
[property: JsonPropertyName("Summary")] string Summary,
|
||||
[property: JsonPropertyName("Time")] int Time,
|
||||
[property: JsonPropertyName("TimeStr")] string TimeStr
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Finished))]
|
||||
private partial class FinishedSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Hop(
|
||||
[property: JsonPropertyName("Host")] string Host,
|
||||
[property: JsonPropertyName("IPAddr")] string IPAddr,
|
||||
[property: JsonPropertyName("RTT")] double RTT,
|
||||
[property: JsonPropertyName("TTL")] int TTL
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Hop))]
|
||||
private partial class HopSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Host(
|
||||
[property: JsonPropertyName("Distance")] Distance Distance,
|
||||
[property: JsonPropertyName("EndTime")] int EndTime,
|
||||
[property: JsonPropertyName("HostAddress")] IReadOnlyList<HostAddress> HostAddresses,
|
||||
[property: JsonPropertyName("HostNames")] HostNames HostNames,
|
||||
[property: JsonPropertyName("IPIDSequence")] IPIDSequence IPIDSequence,
|
||||
[property: JsonPropertyName("OS")] OS OS,
|
||||
[property: JsonPropertyName("Port")] IReadOnlyList<Port> Ports,
|
||||
[property: JsonPropertyName("StartTime")] int StartTime,
|
||||
[property: JsonPropertyName("Status")] Status Status,
|
||||
[property: JsonPropertyName("TCPSequence")] TCPSequence TCPSequence,
|
||||
[property: JsonPropertyName("TCPTSSequence")] TCPTSSequence TCPTSSequence,
|
||||
[property: JsonPropertyName("Trace")] Trace Trace,
|
||||
[property: JsonPropertyName("Uptime")] Uptime Uptime
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Host))]
|
||||
private partial class HostSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record HostAddress(
|
||||
[property: JsonPropertyName("Address")] string Address,
|
||||
[property: JsonPropertyName("AddressType")] string AddressType,
|
||||
[property: JsonPropertyName("Vendor")] string Vendor
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(HostAddress))]
|
||||
private partial class HostAddressSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record HostName(
|
||||
[property: JsonPropertyName("Name")] string Name,
|
||||
[property: JsonPropertyName("Type")] string Type
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(HostName))]
|
||||
private partial class HostNameSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record HostNames(
|
||||
[property: JsonPropertyName("HostName")] IReadOnlyList<HostName> HostName
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(HostNames))]
|
||||
private partial class HostNamesSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Hosts(
|
||||
[property: JsonPropertyName("Down")] int Down,
|
||||
[property: JsonPropertyName("Total")] int Total,
|
||||
[property: JsonPropertyName("Up")] int Up
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Hosts))]
|
||||
private partial class HostsSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record IPIDSequence(
|
||||
[property: JsonPropertyName("Class")] string Class,
|
||||
[property: JsonPropertyName("Values")] string Values
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(IPIDSequence))]
|
||||
private partial class IPIDSequenceSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record OS(
|
||||
[property: JsonPropertyName("OSClass")] object OSClass,
|
||||
[property: JsonPropertyName("OSMatch")] IReadOnlyList<OSMatch> OSMatches,
|
||||
[property: JsonPropertyName("OSPortUsed")] IReadOnlyList<OSPortUsed> OSPortsUsed
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(OS))]
|
||||
private partial class OSSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record OSMatch(
|
||||
[property: JsonPropertyName("Accuracy")] string Accuracy,
|
||||
[property: JsonPropertyName("Line")] string Line,
|
||||
[property: JsonPropertyName("Name")] string Name
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(OSMatch))]
|
||||
private partial class OSMatchSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record OSPortUsed(
|
||||
[property: JsonPropertyName("PortID")] int PortID,
|
||||
[property: JsonPropertyName("Protocol")] string Protocol,
|
||||
[property: JsonPropertyName("State")] string State
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(OSPortUsed))]
|
||||
private partial class OSPortUsedSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Port(
|
||||
[property: JsonPropertyName("PortID")] int PortID,
|
||||
[property: JsonPropertyName("Protocol")] string Protocol,
|
||||
[property: JsonPropertyName("Script")] IReadOnlyList<Script> Scripts,
|
||||
[property: JsonPropertyName("Service")] Service Service,
|
||||
[property: JsonPropertyName("State")] State State
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Port))]
|
||||
private partial class PortSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Record(
|
||||
[property: JsonPropertyName("Args")] string Args,
|
||||
[property: JsonPropertyName("Debugging")] Debugging Debugging,
|
||||
[property: JsonPropertyName("Host")] List<Host> Hosts,
|
||||
[property: JsonPropertyName("RunStats")] RunStats RunStats,
|
||||
[property: JsonPropertyName("ScanInfo")] ScanInfo ScanInfo,
|
||||
[property: JsonPropertyName("Scanner")] string Scanner,
|
||||
[property: JsonPropertyName("Start")] int Start,
|
||||
[property: JsonPropertyName("StartStr")] string StartStr,
|
||||
[property: JsonPropertyName("Verbose")] Verbose Verbose,
|
||||
[property: JsonPropertyName("Version")] string Version
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Record))]
|
||||
private partial class RecordSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record RunStats(
|
||||
[property: JsonPropertyName("Finished")] Finished Finished,
|
||||
[property: JsonPropertyName("Hosts")] Hosts Hosts
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(RunStats))]
|
||||
private partial class RunStatsSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record ScanInfo(
|
||||
[property: JsonPropertyName("NumServices")] int NumServices,
|
||||
[property: JsonPropertyName("Protocol")] string Protocol,
|
||||
[property: JsonPropertyName("Services")] string Services,
|
||||
[property: JsonPropertyName("Type")] string Type
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(ScanInfo))]
|
||||
private partial class ScanInfoSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Script(
|
||||
[property: JsonPropertyName("ID")] string ID,
|
||||
[property: JsonPropertyName("Output")] string Output
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Script))]
|
||||
private partial class ScriptSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Service(
|
||||
[property: JsonPropertyName("Conf")] string Conf,
|
||||
[property: JsonPropertyName("CPE")] IReadOnlyList<string> CPEs,
|
||||
[property: JsonPropertyName("ExtraInfo")] string ExtraInfo,
|
||||
[property: JsonPropertyName("Method")] string Method,
|
||||
[property: JsonPropertyName("Name")] string Name,
|
||||
[property: JsonPropertyName("Product")] string Product,
|
||||
[property: JsonPropertyName("Version")] string Version
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Service))]
|
||||
private partial class ServiceSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record State(
|
||||
[property: JsonPropertyName("Reason")] string Reason,
|
||||
[property: JsonPropertyName("ReasonTTL")] string ReasonTTL,
|
||||
[property: JsonPropertyName("State")] string Value
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(State))]
|
||||
private partial class StateSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Status(
|
||||
[property: JsonPropertyName("Reason")] string Reason,
|
||||
[property: JsonPropertyName("State")] string State
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Status))]
|
||||
private partial class StatusSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record TCPSequence(
|
||||
[property: JsonPropertyName("Difficulty")] string Difficulty,
|
||||
[property: JsonPropertyName("Index")] string Index,
|
||||
[property: JsonPropertyName("Values")] string Values
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(TCPSequence))]
|
||||
private partial class TCPSequenceSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record TCPTSSequence(
|
||||
[property: JsonPropertyName("Class")] string Class,
|
||||
[property: JsonPropertyName("Values")] string Values
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(TCPTSSequence))]
|
||||
private partial class TCPTSSequenceSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Trace(
|
||||
[property: JsonPropertyName("Hops")] IReadOnlyList<Hop> Hops,
|
||||
[property: JsonPropertyName("Port")] int Port,
|
||||
[property: JsonPropertyName("Protocol")] string Protocol
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Trace))]
|
||||
private partial class TraceSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Uptime(
|
||||
[property: JsonPropertyName("LastBoot")] string LastBoot,
|
||||
[property: JsonPropertyName("Seconds")] int Seconds
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Uptime))]
|
||||
private partial class UptimeSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Verbose(
|
||||
[property: JsonPropertyName("Level")] int Level
|
||||
);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Verbose))]
|
||||
private partial class VerboseSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private static string[] GetIPV4Segments(string value)
|
||||
{
|
||||
string[] result;
|
||||
string[] subSegments = value.Split('.');
|
||||
if (subSegments.Length != 4)
|
||||
result = [];
|
||||
else
|
||||
{
|
||||
if (!subSegments.All(l => int.TryParse(l, out _)))
|
||||
result = [];
|
||||
else
|
||||
result = value.Split('.');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<ReadOnlyCollection<string>> GetHostLinesSpaceSegments()
|
||||
{
|
||||
List<ReadOnlyCollection<string>> results = [];
|
||||
string hostFile = "C:/Windows/System32/drivers/etc/hosts";
|
||||
string[] lines = !File.Exists(hostFile) ? [] : File.ReadAllLines(hostFile);
|
||||
foreach (string line in lines)
|
||||
results.Add(new(line.Split(' ')));
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private static string[] GetMacAddressSegments(string value)
|
||||
{
|
||||
string[] result;
|
||||
if (value.Length != 17)
|
||||
result = [];
|
||||
else
|
||||
{
|
||||
string v = value.ToLower();
|
||||
if (v[2] is not ':' or '-' || v[5] is not ':' or '-' || v[8] is not ':' or '-' || v[11] is not ':' or '-' || v[14] is not ':' or '-')
|
||||
result = [];
|
||||
else
|
||||
{
|
||||
result = [$"{v[0]}{v[1]}", $"{v[3]}{v[4]}", $"{v[6]}{v[7]}", $"{v[9]}{v[10]}", $"{v[12]}{v[13]}", $"{v[15]}{v[16]}"];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static void SplitJsonFile(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string json;
|
||||
string title;
|
||||
Record? record;
|
||||
string fileName;
|
||||
FileInfo fileInfo;
|
||||
string checkFileName;
|
||||
string sourceFileName;
|
||||
string[] ipV4Segments;
|
||||
string outputDirectory;
|
||||
List<string> lines = [];
|
||||
List<string> links = [];
|
||||
List<string> allLines = [];
|
||||
string[] macAddressSegments;
|
||||
string macAddressWithHyphens;
|
||||
string ipV4SegmentsWithPeriods;
|
||||
List<string> titleSegments = [];
|
||||
string fileNameWithoutExtension;
|
||||
const int ipV4SegmentsLength = 4;
|
||||
string sourceDirectory = args[0];
|
||||
const int macAddressSegmentsLength = 6;
|
||||
string[] fileNameWithoutExtensionSegments;
|
||||
string fileNameWithoutExtensionFirstThreeSegments;
|
||||
if (!Directory.Exists(sourceDirectory))
|
||||
throw new Exception(sourceDirectory);
|
||||
List<ReadOnlyCollection<string>> hostLineMatchSpaceSegments;
|
||||
ReadOnlyCollection<ReadOnlyCollection<string>> hostLinesSpaceSegments = GetHostLinesSpaceSegments();
|
||||
string[] files = Directory.GetFiles(sourceDirectory, args[2], SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
links.Clear();
|
||||
fileInfo = new(file);
|
||||
json = File.ReadAllText(file);
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
||||
outputDirectory = Path.Combine(sourceDirectory, fileNameWithoutExtension);
|
||||
fileNameWithoutExtensionSegments = GetIPV4Segments(fileNameWithoutExtension);
|
||||
if (fileNameWithoutExtensionSegments.Length != ipV4SegmentsLength)
|
||||
continue;
|
||||
fileNameWithoutExtensionFirstThreeSegments = $"{fileNameWithoutExtensionSegments[0]}.{fileNameWithoutExtensionSegments[1]}.{fileNameWithoutExtensionSegments[2]}.";
|
||||
if (!Directory.Exists(outputDirectory))
|
||||
_ = Directory.CreateDirectory(outputDirectory);
|
||||
record = JsonSerializer.Deserialize(json, RecordSourceGenerationContext.Default.Record);
|
||||
if (record is null)
|
||||
continue;
|
||||
foreach (Host host in record.Hosts)
|
||||
{
|
||||
lines.Clear();
|
||||
ipV4Segments = [];
|
||||
titleSegments.Clear();
|
||||
macAddressSegments = [];
|
||||
foreach (HostAddress hostAddress in host.HostAddresses)
|
||||
{
|
||||
if (hostAddress.AddressType == "ipv4")
|
||||
ipV4Segments = GetIPV4Segments(hostAddress.Address);
|
||||
else if (hostAddress.AddressType == "mac")
|
||||
macAddressSegments = GetMacAddressSegments(hostAddress.Address);
|
||||
else
|
||||
continue;
|
||||
}
|
||||
if (ipV4Segments.Length != ipV4SegmentsLength)
|
||||
continue;
|
||||
ipV4SegmentsWithPeriods = string.Join('.', ipV4Segments);
|
||||
macAddressWithHyphens = string.Join('-', macAddressSegments);
|
||||
if (macAddressSegments.Length != macAddressSegmentsLength)
|
||||
hostLineMatchSpaceSegments = (from l in hostLinesSpaceSegments where l.Count > 0 && l.Contains(ipV4SegmentsWithPeriods) select l).ToList();
|
||||
else
|
||||
{
|
||||
hostLineMatchSpaceSegments = (from l in hostLinesSpaceSegments where l.Count > 0 && l.Contains(macAddressWithHyphens) select l).ToList();
|
||||
if (hostLineMatchSpaceSegments.Count == 1)
|
||||
title = string.Join(' ', hostLineMatchSpaceSegments[0]);
|
||||
else
|
||||
hostLineMatchSpaceSegments = (from l in hostLineMatchSpaceSegments where l.Count > 0 && l[0].StartsWith(fileNameWithoutExtensionFirstThreeSegments) select l).ToList();
|
||||
}
|
||||
if (ipV4Segments.Length == ipV4SegmentsLength)
|
||||
titleSegments.Add(ipV4SegmentsWithPeriods);
|
||||
if (hostLineMatchSpaceSegments.Count == 1)
|
||||
title = $"{ipV4SegmentsWithPeriods} {string.Join(' ', hostLineMatchSpaceSegments[0].Skip(1))}";
|
||||
else
|
||||
{
|
||||
if (host.HostNames.HostName is null)
|
||||
titleSegments.Add("unknown #");
|
||||
else
|
||||
titleSegments.Add($"{string.Join("_", host.HostNames.HostName.Select(l => l.Name.Replace(' ', '-')))} #");
|
||||
if (macAddressSegments.Length == macAddressSegmentsLength)
|
||||
titleSegments.Add(macAddressWithHyphens);
|
||||
title = string.Join(" ", titleSegments);
|
||||
logger.LogInformation("{title} Type DeviceInfo ~ needs to be added to host file!", title);
|
||||
}
|
||||
json = JsonSerializer.Serialize(host, HostSourceGenerationContext.Default.Host);
|
||||
fileNameWithoutExtension = macAddressSegments.Length != macAddressSegmentsLength ? $"ipv4-{string.Join(string.Empty, ipV4Segments)}" : $"mac-{string.Join(string.Empty, macAddressSegments)}";
|
||||
fileName = $"{fileNameWithoutExtension}.md";
|
||||
links.Add($"- [{title}]({fileName})");
|
||||
lines.Add("---");
|
||||
allLines.Add(title);
|
||||
lines.Add(string.Empty);
|
||||
lines.Add($"# {title}");
|
||||
lines.Add(string.Empty);
|
||||
lines.Add($"## {fileNameWithoutExtension}");
|
||||
if (host.Ports is not null)
|
||||
{
|
||||
lines.Add(string.Empty);
|
||||
lines.Add("## Port(s)");
|
||||
lines.Add(string.Empty);
|
||||
lines.Add("| Id | Protocol | State | Service |");
|
||||
lines.Add("| - | - | - | - |");
|
||||
foreach (Port port in host.Ports)
|
||||
lines.Add($"| {port.PortID} | {port.Protocol} | {port.State.Value} | {port.Service.Name} |");
|
||||
}
|
||||
lines.Add(string.Empty);
|
||||
lines.Add("```json");
|
||||
lines.Add(json);
|
||||
lines.Add("```");
|
||||
checkFileName = Path.Combine(outputDirectory, fileName);
|
||||
if (File.Exists(checkFileName))
|
||||
File.Delete(checkFileName);
|
||||
File.WriteAllLines(checkFileName, lines);
|
||||
File.SetCreationTime(checkFileName, fileInfo.CreationTime);
|
||||
File.SetLastWriteTime(checkFileName, fileInfo.LastWriteTime);
|
||||
logger.LogInformation("{title} created", title);
|
||||
}
|
||||
lines.Clear();
|
||||
record.Hosts.Clear();
|
||||
title = Path.GetFileNameWithoutExtension(file);
|
||||
json = JsonSerializer.Serialize(record, RecordSourceGenerationContext.Default.Record);
|
||||
lines.Add("---");
|
||||
lines.Add(string.Empty);
|
||||
lines.Add($"# {title}");
|
||||
lines.Add(string.Empty);
|
||||
lines.Add($"## {title}");
|
||||
lines.Add(string.Empty);
|
||||
lines.Add("## Hosts");
|
||||
lines.Add(string.Empty);
|
||||
lines.AddRange(links);
|
||||
lines.Add(string.Empty);
|
||||
lines.Add("```json");
|
||||
lines.Add(json);
|
||||
lines.Add("```");
|
||||
checkFileName = Path.Combine(outputDirectory, $"{title}.md");
|
||||
if (File.Exists(checkFileName))
|
||||
File.Delete(checkFileName);
|
||||
File.WriteAllLines(checkFileName, lines);
|
||||
File.SetCreationTime(checkFileName, fileInfo.CreationTime);
|
||||
File.SetLastWriteTime(checkFileName, fileInfo.LastWriteTime);
|
||||
logger.LogInformation("{title} created", title);
|
||||
checkFileName = Path.Combine(outputDirectory, Path.GetFileName(file));
|
||||
if (File.Exists(checkFileName))
|
||||
File.Delete(checkFileName);
|
||||
File.Move(file, checkFileName);
|
||||
checkFileName = Path.Combine(outputDirectory, $"{Path.GetFileNameWithoutExtension(file)}.xml");
|
||||
if (File.Exists(checkFileName))
|
||||
File.Delete(checkFileName);
|
||||
sourceFileName = Path.ChangeExtension(file, ".xml");
|
||||
if (File.Exists(sourceFileName))
|
||||
File.Move(sourceFileName, checkFileName);
|
||||
}
|
||||
File.WriteAllLines(Path.Combine(sourceDirectory, $"{DateTime.Now.Ticks}.ssv"), allLines);
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
@ -7,14 +9,30 @@ 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<string, string> KeyValuePairs);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Dictionary<string, Dictionary<string, string>>))]
|
||||
private partial class DictionaryDictionarySourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Record(string Key, Dictionary<string, string> KeyValuePairs);
|
||||
|
||||
private static Dictionary<string, Dictionary<string, string>> GetKeyValuePairs(List<Record> collection, bool replaceFound)
|
||||
{
|
||||
Dictionary<string, Dictionary<string, string>> results = [];
|
||||
@ -94,18 +112,116 @@ internal static partial class Helper20240106
|
||||
return results;
|
||||
}
|
||||
|
||||
private static Dictionary<int, Host> GetHosts(string jsonl)
|
||||
{
|
||||
Dictionary<int, Host> results = [];
|
||||
int id;
|
||||
string json = $"[{File.ReadAllText(jsonl).Replace("\r\n", ",")}]";
|
||||
Host[] hosts = JsonSerializer.Deserialize(json, HostSourceGenerationContext.Default.HostArray) ?? throw new NullReferenceException(nameof(json));
|
||||
foreach (Host host in hosts)
|
||||
{
|
||||
if (host.Id is null)
|
||||
continue;
|
||||
if (host.Hyphen is not null and nameof(host.Hyphen))
|
||||
continue;
|
||||
if (!int.TryParse(host.Id, out id))
|
||||
throw new NotSupportedException($"{host.Id} is not a number");
|
||||
if (results.ContainsKey(id))
|
||||
throw new NotSupportedException($"Id {id} is not unique!");
|
||||
results.Add(id, host);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<string> GetIpAddressAndVerify(ILogger<Worker> logger, string key, Dictionary<string, Dictionary<string, string>> keyValuePairs, Dictionary<int, Host> hosts)
|
||||
{
|
||||
List<string> results = [];
|
||||
int id;
|
||||
bool found;
|
||||
Host? host;
|
||||
string text;
|
||||
string? ipAddress;
|
||||
StringBuilder stringBuilder = new();
|
||||
foreach (KeyValuePair<string, Dictionary<string, string>> keyValuePair in keyValuePairs)
|
||||
{
|
||||
found = false;
|
||||
if (!keyValuePair.Value.TryGetValue(key, out ipAddress))
|
||||
throw new NotSupportedException($"{key} isn't present!");
|
||||
if (ipAddress == "0.0.0.0")
|
||||
continue;
|
||||
results.Add(ipAddress);
|
||||
_ = stringBuilder.Clear();
|
||||
foreach (KeyValuePair<string, string> keyValue in keyValuePair.Value)
|
||||
_ = stringBuilder.AppendLine(keyValue.Value);
|
||||
text = stringBuilder.ToString();
|
||||
if (!int.TryParse(ipAddress.Split('.')[^1], out id))
|
||||
throw new NotSupportedException($"{ipAddress} isn't valid!");
|
||||
if (!hosts.TryGetValue(id, out host))
|
||||
throw new NotSupportedException($"{id} isn't valid!");
|
||||
foreach (KeyValuePair<string, string> keyValue in keyValuePair.Value)
|
||||
{
|
||||
if (keyValue.Value != host.Hyphen)
|
||||
continue;
|
||||
found = true;
|
||||
}
|
||||
if (!found)
|
||||
throw new NotSupportedException($"{host}{Environment.NewLine}{text} doesn't match!");
|
||||
if (text.Contains("Unknown", StringComparison.InvariantCultureIgnoreCase))
|
||||
logger.LogWarning($"{text} contains Unknown and should be {host.Device}!");
|
||||
}
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private static void WriteAppendToHostConfFile(FileInfo fileInfo, string hostConfFile, Dictionary<int, Host> hosts, ReadOnlyCollection<string> ipAddress)
|
||||
{
|
||||
int id;
|
||||
Host host;
|
||||
string ip;
|
||||
string line;
|
||||
List<int> distinct = [];
|
||||
List<string> lines = [$"# {fileInfo.LastWriteTime.Ticks}"];
|
||||
string firstSegmentsOfIpAddress = string.Join('.', ipAddress[0].Split('.').Take(3));
|
||||
foreach (KeyValuePair<int, Host> keyValuePair in hosts)
|
||||
{
|
||||
host = keyValuePair.Value;
|
||||
if (host.Hyphen is not null and nameof(host.Hyphen))
|
||||
continue;
|
||||
if (host.Id is null || host.Hyphen is null || host.Device is null || host.Name is null || host.Hyphen.Length != 17)
|
||||
throw new NotSupportedException($"{host.Id} is Null or not 17");
|
||||
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);
|
||||
ip = ipAddress.Contains($"{firstSegmentsOfIpAddress}.{id}") ? $"{firstSegmentsOfIpAddress}.{id}" : $"# {firstSegmentsOfIpAddress}.{id}";
|
||||
line = $"{ip} {host.Name} # https://{host.Name} | {host.Colon} | {host.Hyphen} | {host.Device} |";
|
||||
lines.Add(line);
|
||||
}
|
||||
lines.Add($"# {fileInfo.LastWriteTime.Ticks}");
|
||||
File.AppendAllLines(hostConfFile, lines);
|
||||
}
|
||||
|
||||
internal static void TextToJson(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string json;
|
||||
string[] lines;
|
||||
int? headerLine;
|
||||
FileInfo fileInfo;
|
||||
string replace = args[6];
|
||||
int keyIndex = int.Parse(args[4]);
|
||||
int keyLength = int.Parse(args[5]);
|
||||
string[] headers = args[7].Split(',');
|
||||
string key = args[7];
|
||||
string replace = args[5];
|
||||
int keyIndex = int.Parse(args[3]);
|
||||
int keyLength = int.Parse(args[4]);
|
||||
ReadOnlyCollection<string> ipAddress;
|
||||
string[] headers = args[6].Split(',');
|
||||
string jsonl = Path.Combine(args[0], args[8]);
|
||||
string hostConfFile = Path.Combine(args[0], args[9]);
|
||||
string[] txtFiles = Directory.GetFiles(args[0], args[2]);
|
||||
Dictionary<string, Dictionary<string, string>> keyValuePairs;
|
||||
if (!File.Exists(jsonl))
|
||||
throw new NotSupportedException($"{args[8]} doesn't exist!");
|
||||
Dictionary<int, Host> hosts = GetHosts(jsonl);
|
||||
if (hosts.Count == 0)
|
||||
throw new NotSupportedException($"{args[8]} isn't valid!");
|
||||
foreach (string txtFile in txtFiles)
|
||||
{
|
||||
lines = File.ReadAllLines(txtFile);
|
||||
@ -118,10 +234,14 @@ internal static partial class Helper20240106
|
||||
keyValuePairs = GetKeyValuePairs(keyIndex, keyLength, replace, headers, lines, headerLine.Value);
|
||||
if (keyValuePairs.Count == 0)
|
||||
continue;
|
||||
ipAddress = GetIpAddressAndVerify(logger, key, keyValuePairs, hosts);
|
||||
if (ipAddress.Count == 0)
|
||||
continue;
|
||||
json = JsonSerializer.Serialize(keyValuePairs, DictionaryDictionarySourceGenerationContext.Default.DictionaryStringDictionaryStringString);
|
||||
logger.LogInformation("Writing output file...");
|
||||
File.WriteAllText($"{txtFile}-{fileInfo.LastWriteTime.Ticks}.json", json);
|
||||
File.WriteAllText(txtFile, string.Empty);
|
||||
File.WriteAllText($"{fileInfo.FullName}-{fileInfo.LastWriteTime.Ticks}.json", json);
|
||||
WriteAppendToHostConfFile(fileInfo, hostConfFile, hosts, ipAddress);
|
||||
File.WriteAllLines(txtFile, [string.Empty, string.Empty, lines[^1]]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,16 +6,20 @@ namespace File_Folder_Helper.Day;
|
||||
internal static partial class Helper20240107
|
||||
{
|
||||
|
||||
private static void DirectoryToISO(ILogger<Worker> logger, string destinationDirectory, string directory)
|
||||
private static void DirectoryToISO(ILogger<Worker> logger, string destinationDirectory, bool mapOnly, string directory)
|
||||
{
|
||||
byte[] bytes = [];
|
||||
string relativePath;
|
||||
string directoryName = Path.GetFileName(directory);
|
||||
CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName };
|
||||
IEnumerable<string> files = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories);
|
||||
CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName.Length < 25 ? directoryName : directoryName[..25] };
|
||||
IEnumerable<string> files = Directory.EnumerateFiles(directory, "*", new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = true });
|
||||
foreach (string file in files)
|
||||
{
|
||||
relativePath = Path.GetRelativePath(directory, file);
|
||||
_ = builder.AddFile(relativePath, file);
|
||||
relativePath = Path.GetRelativePath(directory, file).Replace(';', '_');
|
||||
if (!mapOnly)
|
||||
_ = builder.AddFile(relativePath, file);
|
||||
else
|
||||
_ = builder.AddFile(relativePath, bytes);
|
||||
}
|
||||
logger.LogInformation(destinationDirectory);
|
||||
builder.Build(Path.Combine(destinationDirectory, $"{directoryName}.iso"));
|
||||
@ -28,11 +32,16 @@ internal static partial class Helper20240107
|
||||
int directories = int.Parse(args[2]);
|
||||
string destinationDirectory = args[3];
|
||||
logger.LogInformation(sourceDirectory);
|
||||
if (sourceDirectory == "C:/ProgramData")
|
||||
sourceDirectory = destinationDirectory;
|
||||
bool mapOnly = sourceDirectory.Length == 2;
|
||||
if (!Directory.Exists(destinationDirectory))
|
||||
_ = Directory.CreateDirectory(destinationDirectory);
|
||||
string[] subDirectories = directories == 1 ? [sourceDirectory] : Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly);
|
||||
if (subDirectories.Length != directories)
|
||||
throw new Exception($"{directories} != {subDirectories.Length}");
|
||||
foreach (string directory in subDirectories)
|
||||
DirectoryToISO(logger, destinationDirectory, directory);
|
||||
DirectoryToISO(logger, destinationDirectory, mapOnly, directory);
|
||||
}
|
||||
|
||||
}
|
@ -81,7 +81,7 @@ internal static partial class Helper20240108
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int? GetFirstUsedLine(string[] lines, int i, string search, string searchNot, string searchWrap, string searchConstructor, int parameterCount)
|
||||
private static int? GetFirstUsedLine(string[] lines, int i, string search, string searchNot, string searchWrap, string searchDelegate, string searchConstructor, int parameterCount)
|
||||
{
|
||||
int? result = null;
|
||||
string[] segments;
|
||||
@ -100,26 +100,37 @@ internal static partial class Helper20240108
|
||||
segments = lines[j].Split(searchWrap);
|
||||
if (segments.Length == 1)
|
||||
{
|
||||
segments = lines[j].Split(searchConstructor);
|
||||
if (segments.Length == 1)
|
||||
continue;
|
||||
if (!lines[j].EndsWith(searchDelegate))
|
||||
{
|
||||
segments = lines[j].Split(searchConstructor);
|
||||
if (segments.Length == 1)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
lastSegmentBeforeDot = segments[^1].Split(").")[0];
|
||||
if (parameterCount == 0)
|
||||
if (lines[j].EndsWith(searchDelegate))
|
||||
{
|
||||
if (lastSegmentBeforeDot.Contains(','))
|
||||
continue;
|
||||
result = j;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
afterSegments = lastSegmentBeforeDot.Split(',');
|
||||
if (afterSegments.Length != parameterCount)
|
||||
continue;
|
||||
lastSegmentBeforeDot = segments[^1].Split(").")[0];
|
||||
if (parameterCount == 0)
|
||||
{
|
||||
if (lastSegmentBeforeDot.Contains(','))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
afterSegments = lastSegmentBeforeDot.Split(',');
|
||||
if (afterSegments.Length != parameterCount)
|
||||
continue;
|
||||
}
|
||||
result = j;
|
||||
break;
|
||||
}
|
||||
result = j;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -150,6 +161,7 @@ internal static partial class Helper20240108
|
||||
string searchWrap;
|
||||
int parameterCount;
|
||||
int? firstUsedLine;
|
||||
string searchDelegate;
|
||||
string lineSegmentFirst;
|
||||
string searchConstructor;
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
@ -167,11 +179,12 @@ internal static partial class Helper20240108
|
||||
search = $" {name}(";
|
||||
searchNot = $"!{name}(";
|
||||
searchWrap = $"({name}(";
|
||||
searchDelegate = $" += {name};";
|
||||
if (string.IsNullOrEmpty(name))
|
||||
continue;
|
||||
blocks = 0;
|
||||
searchConstructor = $"{name.ToLower()} = new(";
|
||||
startLine = GetStartLine(lines, i);
|
||||
searchConstructor = $"{name.ToLower()} = new(";
|
||||
parameterCount = GetParameterCount(line, search);
|
||||
isLinq = lines[i + 1].Trim() != "{";
|
||||
if (isLinq)
|
||||
@ -190,7 +203,7 @@ internal static partial class Helper20240108
|
||||
endLine = j;
|
||||
if (lines.Length > j + 1 && string.IsNullOrEmpty(lines[j + 1].Trim()))
|
||||
endLine++;
|
||||
firstUsedLine = GetFirstUsedLine(lines, i, search, searchNot, searchWrap, searchConstructor, parameterCount);
|
||||
firstUsedLine = GetFirstUsedLine(lines, i, search, searchNot, searchWrap, searchDelegate, searchConstructor, parameterCount);
|
||||
if (firstUsedLine is null)
|
||||
{
|
||||
lineSegmentFirst = line.Split(search)[0];
|
||||
@ -255,18 +268,24 @@ internal static partial class Helper20240108
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static void SortCodeMethods(ILogger<Worker> logger, List<string> args)
|
||||
internal static void SortCodeMethods(ILogger<Worker> logger, List<string> args, CancellationToken cancellationToken)
|
||||
{
|
||||
bool result = false;
|
||||
bool check;
|
||||
string[] lines;
|
||||
bool usePathCombine = true;
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
logger.LogInformation("{ticks}", ticks);
|
||||
string[] cSharpFiles = Directory.GetFiles(args[0], "*.cs", SearchOption.TopDirectoryOnly);
|
||||
string directory = Path.GetFullPath(args[2]);
|
||||
string repositoryDirectory = Path.GetFullPath(args[0]);
|
||||
string[] cSharpFiles = Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories);
|
||||
ReadOnlyCollection<string> gitOthersModifiedAndDeletedExcludingStandardFiles = Helpers.HelperGit.GetOthersModifiedAndDeletedExcludingStandardFiles(repositoryDirectory, usePathCombine, cancellationToken);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
foreach (string cSharpFile in cSharpFiles)
|
||||
{
|
||||
if (!gitOthersModifiedAndDeletedExcludingStandardFiles.Contains(cSharpFile))
|
||||
continue;
|
||||
lines = File.ReadAllLines(cSharpFile);
|
||||
check = SortFile(logger, cSharpFile, lines);
|
||||
if (check && !result)
|
||||
|
83
Day/2024-Q1/Helper-2024-01-29.cs
Normal file
83
Day/2024-Q1/Helper-2024-01-29.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240129
|
||||
{
|
||||
|
||||
private record Record([property: JsonPropertyName("Part Name")] string? PartName,
|
||||
[property: JsonPropertyName("Part Revision")] string? PartRevision,
|
||||
[property: JsonPropertyName("Test Name")] string? TestName,
|
||||
[property: JsonPropertyName("Description")] string? Description,
|
||||
[property: JsonPropertyName("Lot Number")] string? LotNumber,
|
||||
[property: JsonPropertyName("Job Name")] string? JobName,
|
||||
[property: JsonPropertyName("Process Name")] string? ProcessName,
|
||||
[property: JsonPropertyName("Reasonable Limit (Upper)")] double? ReasonableLimitUpper,
|
||||
[property: JsonPropertyName("Alarm Reasonable Limit (Upper)")] double? AlarmReasonableLimitUpper,
|
||||
[property: JsonPropertyName("Specification Limit (Upper)")] double? SpecificationLimitUpper,
|
||||
[property: JsonPropertyName("Alarm Specification Limit (Upper)")] double? AlarmSpecificationLimitUpper,
|
||||
[property: JsonPropertyName("Warning Limit (Upper)")] double? WarningLimitUpper,
|
||||
[property: JsonPropertyName("Alarm Warning Limit (Upper)")] double? AlarmWarningLimitUpper,
|
||||
[property: JsonPropertyName("Specification Limit (Target)")] double? SpecificationLimitTarget,
|
||||
[property: JsonPropertyName("Warning Limit (Lower)")] double? WarningLimitLower,
|
||||
[property: JsonPropertyName("Alarm Warning Limit (Lower)")] double? AlarmWarningLimitLower,
|
||||
[property: JsonPropertyName("Specification Limit (Lower)")] double? SpecificationLimitLower,
|
||||
[property: JsonPropertyName("Alarm Specification Limit (Lower)")] double? AlarmSpecificationLimitLower,
|
||||
[property: JsonPropertyName("Reasonable Limit (Lower)")] double? ReasonableLimitLower,
|
||||
[property: JsonPropertyName("Alarm Reasonable Limit (Lower)")] double? AlarmReasonableLimitLower,
|
||||
[property: JsonPropertyName("Original Test Name")] string? OriginalTestName,
|
||||
[property: JsonPropertyName("Test Id")] int? TestId,
|
||||
[property: JsonPropertyName("count")] int? Count);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
[JsonSerializable(typeof(Record[]))]
|
||||
private partial class Helper20240129RecordCollectionSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private static List<string> GetLines(Record[] records)
|
||||
{
|
||||
List<string> lines = [];
|
||||
lines.Add($"{nameof(Record.PartName)}\t{nameof(Record.PartRevision)}\t{nameof(Record.TestName)}\t{nameof(Record.Description)}\t{nameof(Record.LotNumber)}\t{nameof(Record.JobName)}\t{nameof(Record.ProcessName)}\t{nameof(Record.ReasonableLimitUpper)}\t{nameof(Record.AlarmReasonableLimitUpper)}\t{nameof(Record.SpecificationLimitUpper)}\t{nameof(Record.AlarmSpecificationLimitUpper)}\t{nameof(Record.WarningLimitUpper)}\t{nameof(Record.AlarmWarningLimitUpper)}\t{nameof(Record.SpecificationLimitTarget)}\t{nameof(Record.WarningLimitLower)}\t{nameof(Record.AlarmWarningLimitLower)}\t{nameof(Record.SpecificationLimitLower)}\t{nameof(Record.AlarmSpecificationLimitLower)}\t{nameof(Record.ReasonableLimitLower)}\t{nameof(Record.AlarmReasonableLimitLower)}\t{nameof(Record.OriginalTestName)}\t{nameof(Record.TestId)}\t{nameof(Record.Count)}");
|
||||
foreach (Record record in records)
|
||||
lines.Add($"{record.PartName}\t{record.PartRevision}\t{record.TestName}\t{record.Description}\t{record.LotNumber}\t{record.JobName}\t{record.ProcessName}\t{record.ReasonableLimitUpper}\t{record.AlarmReasonableLimitUpper}\t{record.SpecificationLimitUpper}\t{record.AlarmSpecificationLimitUpper}\t{record.WarningLimitUpper}\t{record.AlarmWarningLimitUpper}\t{record.SpecificationLimitTarget}\t{record.WarningLimitLower}\t{record.AlarmWarningLimitLower}\t{record.SpecificationLimitLower}\t{record.AlarmSpecificationLimitLower}\t{record.ReasonableLimitLower}\t{record.AlarmReasonableLimitLower}\t{record.OriginalTestName}\t{record.TestId}\t{record.Count}");
|
||||
return lines;
|
||||
}
|
||||
|
||||
private static void ConvertAndWrite(string pattern, string sourceDirectory)
|
||||
{
|
||||
long ticks;
|
||||
string json;
|
||||
string fileName;
|
||||
string checkFile;
|
||||
Record[]? records;
|
||||
List<string> lines;
|
||||
string[] files = Directory.GetFiles(sourceDirectory, pattern, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
ticks = DateTime.Now.Ticks;
|
||||
json = File.ReadAllText(file);
|
||||
fileName = Path.GetFileName(file);
|
||||
checkFile = Path.Combine(sourceDirectory, $"{fileName}.{ticks}.tsv");
|
||||
records = JsonSerializer.Deserialize(json, Helper20240129RecordCollectionSourceGenerationContext.Default.RecordArray);
|
||||
if (records is null)
|
||||
continue;
|
||||
lines = GetLines(records);
|
||||
File.WriteAllLines(checkFile, lines);
|
||||
checkFile = Path.Combine(sourceDirectory, $"{fileName}.{ticks}.done");
|
||||
File.Move(file, checkFile);
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void JsonToTsv(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string pattern = args[2];
|
||||
string sourceDirectory = args[0];
|
||||
logger.LogInformation(sourceDirectory);
|
||||
ConvertAndWrite(pattern, sourceDirectory);
|
||||
}
|
||||
|
||||
}
|
58
Day/2024-Q1/Helper-2024-03-05.cs
Normal file
58
Day/2024-Q1/Helper-2024-03-05.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Globalization;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240305
|
||||
{
|
||||
|
||||
private static void TryArchiveFiles(string sourceDirectory, string pattern, string archiveDirectory, string days)
|
||||
{
|
||||
string checkFile;
|
||||
FileInfo fileInfo;
|
||||
string weekOfYear;
|
||||
string checkDirectory;
|
||||
string[] directorySegments;
|
||||
Calendar calendar = new CultureInfo("en-US").Calendar;
|
||||
DateTime dateTime = DateTime.Now.AddDays(-int.Parse(days));
|
||||
string[] sourceDirectorySegments = sourceDirectory.Split('/');
|
||||
// DirectoryInfo directoryInfo = new(Path.GetFullPath(sourceDirectory));
|
||||
// IEnumerable<FileInfo> fileInfoCollection = directoryInfo.EnumerateFiles(pattern, new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = true });
|
||||
string[] files = Directory.GetFiles(sourceDirectory, pattern, SearchOption.AllDirectories);
|
||||
if (!sourceDirectory.StartsWith('\\') && sourceDirectorySegments.Length < 2)
|
||||
throw new Exception("Can't be root drive!");
|
||||
// foreach (FileInfo fileInfo in fileInfoCollection)
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new FileInfo(file);
|
||||
if (string.IsNullOrEmpty(fileInfo.DirectoryName) || fileInfo.LastWriteTime > dateTime)
|
||||
continue;
|
||||
directorySegments = fileInfo.DirectoryName.Split(Path.DirectorySeparatorChar);
|
||||
if (directorySegments.Length < sourceDirectorySegments.Length)
|
||||
continue;
|
||||
weekOfYear = $"{fileInfo.CreationTime.Year}_Week_{calendar.GetWeekOfYear(fileInfo.CreationTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
||||
// checkDirectory = string.Concat(archiveDirectory, Path.DirectorySeparatorChar, weekOfYear, Path.DirectorySeparatorChar, string.Join(Path.DirectorySeparatorChar, directorySegments.Skip(sourceDirectorySegments.Length)));
|
||||
checkDirectory = string.Concat(archiveDirectory, Path.DirectorySeparatorChar, weekOfYear);
|
||||
for (int i = sourceDirectorySegments.Length; i < directorySegments.Length; i++)
|
||||
checkDirectory = string.Concat(checkDirectory, Path.DirectorySeparatorChar, directorySegments[i]);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
checkFile = Path.Combine(checkDirectory, fileInfo.Name);
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
// File.WriteAllText(checkFile, string.Empty);
|
||||
File.Move(fileInfo.FullName, checkFile);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ArchiveFiles(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string days = args[4];
|
||||
string pattern = args[2];
|
||||
string sourceDirectory = args[0];
|
||||
string archiveDirectory = Path.GetFullPath(args[3]);
|
||||
logger.LogInformation(sourceDirectory);
|
||||
TryArchiveFiles(sourceDirectory, pattern, archiveDirectory, days);
|
||||
}
|
||||
|
||||
}
|
151
Day/2024-Q2/Helper-2024-04-03.cs
Normal file
151
Day/2024-Q2/Helper-2024-04-03.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240403
|
||||
{
|
||||
|
||||
private record DynamicHostConfigurationProtocolConfiguration(string[] Columns,
|
||||
string Directory,
|
||||
string Ignore,
|
||||
int KeyIndex,
|
||||
string Pattern,
|
||||
string Primary);
|
||||
|
||||
private static string? GetMatch(DynamicHostConfigurationProtocolConfiguration dynamicHostConfigurationProtocolConfiguration, string[] files)
|
||||
{
|
||||
string? result = null;
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (file.EndsWith(dynamicHostConfigurationProtocolConfiguration.Primary))
|
||||
result = file;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ReadOnlyDictionary<string, int> GetKeyToCounts(DynamicHostConfigurationProtocolConfiguration dynamicHostConfigurationProtocolConfiguration, string[] files)
|
||||
{
|
||||
Dictionary<string, int> results = [];
|
||||
bool check;
|
||||
string key;
|
||||
string[] lines;
|
||||
string[] segments;
|
||||
List<int>? collection;
|
||||
Dictionary<string, List<int>> keyValuePairs = [];
|
||||
int columnsLength = dynamicHostConfigurationProtocolConfiguration.Columns.Length;
|
||||
foreach (string file in files)
|
||||
{
|
||||
check = false;
|
||||
lines = File.ReadAllLines(file);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (line.Length < 3 || line[0] != '|' || line[^1] != '|')
|
||||
continue;
|
||||
segments = line.Split('|');
|
||||
if (segments.Length != columnsLength)
|
||||
continue;
|
||||
key = segments[dynamicHostConfigurationProtocolConfiguration.KeyIndex].Trim();
|
||||
if (!check)
|
||||
{
|
||||
if (!key.StartsWith('-') || !key.EndsWith('-'))
|
||||
continue;
|
||||
check = true;
|
||||
continue;
|
||||
}
|
||||
if (key == dynamicHostConfigurationProtocolConfiguration.Ignore)
|
||||
break;
|
||||
if (!keyValuePairs.TryGetValue(key, out collection))
|
||||
{
|
||||
keyValuePairs.Add(key, []);
|
||||
if (!keyValuePairs.TryGetValue(key, out collection))
|
||||
throw new Exception();
|
||||
}
|
||||
collection.Add(1);
|
||||
}
|
||||
}
|
||||
foreach (KeyValuePair<string, List<int>> keyValuePair in keyValuePairs.OrderByDescending(l => l.Value.Count))
|
||||
results.Add(keyValuePair.Key, keyValuePair.Value.Count);
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private static ReadOnlyDictionary<string, ReadOnlyDictionary<string, ReadOnlyCollection<string>>> GetKeyValuePairs(DynamicHostConfigurationProtocolConfiguration dynamicHostConfigurationProtocolConfiguration, string[] files)
|
||||
{
|
||||
ReadOnlyDictionary<string, ReadOnlyDictionary<string, ReadOnlyCollection<string>>> results;
|
||||
bool check;
|
||||
string key;
|
||||
string[] lines;
|
||||
string[] segments;
|
||||
List<string> keys = [];
|
||||
Dictionary<string, ReadOnlyCollection<string>> keyValuePairs;
|
||||
Dictionary<string, ReadOnlyDictionary<string, ReadOnlyCollection<string>>> fileTo = [];
|
||||
int columnsLength = dynamicHostConfigurationProtocolConfiguration.Columns.Length;
|
||||
foreach (string file in files)
|
||||
{
|
||||
keys.Clear();
|
||||
check = false;
|
||||
keyValuePairs = [];
|
||||
lines = File.ReadAllLines(file);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (line.Length < 3 || line[0] != '|' || line[^1] != '|')
|
||||
continue;
|
||||
segments = line.Split('|');
|
||||
if (segments.Length != columnsLength)
|
||||
continue;
|
||||
key = segments[dynamicHostConfigurationProtocolConfiguration.KeyIndex].Trim();
|
||||
if (!check)
|
||||
{
|
||||
if (!key.StartsWith('-') || !key.EndsWith('-'))
|
||||
continue;
|
||||
check = true;
|
||||
continue;
|
||||
}
|
||||
if (key == dynamicHostConfigurationProtocolConfiguration.Ignore)
|
||||
break;
|
||||
if (keys.Contains(key))
|
||||
throw new NotSupportedException($"{key}: is a duplicate!");
|
||||
keys.Add(key);
|
||||
keyValuePairs.Add(key, new(segments.Select(l => l.Trim()).ToArray()));
|
||||
}
|
||||
fileTo.Add(Path.GetFullPath(file), new(keyValuePairs));
|
||||
}
|
||||
results = new(fileTo);
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void AlertIfNewDeviceIsConnected(DynamicHostConfigurationProtocolConfiguration dynamicHostConfigurationProtocolConfiguration, ILogger<Worker> logger)
|
||||
{
|
||||
string[] files = Directory.GetFiles(dynamicHostConfigurationProtocolConfiguration.Directory, dynamicHostConfigurationProtocolConfiguration.Pattern, SearchOption.TopDirectoryOnly);
|
||||
string? match = GetMatch(dynamicHostConfigurationProtocolConfiguration, files);
|
||||
if (string.IsNullOrEmpty(match))
|
||||
throw new NotSupportedException($"{dynamicHostConfigurationProtocolConfiguration.Primary} doesn't exist!");
|
||||
ReadOnlyDictionary<string, int> keyToCounts = GetKeyToCounts(dynamicHostConfigurationProtocolConfiguration, files);
|
||||
foreach (KeyValuePair<string, int> keyToCount in keyToCounts)
|
||||
{
|
||||
if (keyToCount.Value < 2)
|
||||
continue;
|
||||
logger.LogInformation("{Key}: {Count}", keyToCount.Key, keyToCount.Value);
|
||||
}
|
||||
ReadOnlyDictionary<string, ReadOnlyDictionary<string, ReadOnlyCollection<string>>> keyValuePairs = GetKeyValuePairs(dynamicHostConfigurationProtocolConfiguration, files);
|
||||
foreach (KeyValuePair<string, ReadOnlyDictionary<string, ReadOnlyCollection<string>>> keyValuePair in keyValuePairs)
|
||||
{
|
||||
if (!keyValuePair.Key.EndsWith(dynamicHostConfigurationProtocolConfiguration.Primary))
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void AlertIfNewDeviceIsConnected(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string ignore = args[6];
|
||||
string pattern = args[2];
|
||||
string primary = args[3];
|
||||
string keyIndex = args[5];
|
||||
string directory = args[0];
|
||||
logger.LogInformation(directory);
|
||||
string[] columns = args[4].Split('|');
|
||||
DynamicHostConfigurationProtocolConfiguration dynamicHostConfigurationProtocolConfiguration = new(columns, directory, ignore, int.Parse(keyIndex), pattern, primary);
|
||||
AlertIfNewDeviceIsConnected(dynamicHostConfigurationProtocolConfiguration, logger);
|
||||
}
|
||||
|
||||
}
|
96
Day/2024-Q2/Helper-2024-04-04.cs
Normal file
96
Day/2024-Q2/Helper-2024-04-04.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240404
|
||||
{
|
||||
|
||||
private record MetaData(int DeviceId,
|
||||
string DeviceType,
|
||||
int DeviceNumber,
|
||||
string DescriptionName,
|
||||
string DescriptionTest,
|
||||
string Frequency,
|
||||
string Date);
|
||||
|
||||
private record CommaSeparatedValuesConfiguration(int Columns,
|
||||
string Directory,
|
||||
int TestIndex,
|
||||
int RegularExpressionGroupCount,
|
||||
string RegularExpressionPattern,
|
||||
string SearchPattern);
|
||||
|
||||
private static MetaData? GetMetaData(CommaSeparatedValuesConfiguration commaSeparatedValuesConfiguration, string fileNameWithoutExtension)
|
||||
{
|
||||
MetaData? result;
|
||||
Match match = Regex.Match(fileNameWithoutExtension, commaSeparatedValuesConfiguration.RegularExpressionPattern);
|
||||
if (!match.Success || match.Groups.Count != commaSeparatedValuesConfiguration.RegularExpressionGroupCount)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
int deviceId = int.Parse(match.Groups["DeviceId"].Value);
|
||||
int deviceNumber = int.Parse(match.Groups["DeviceNumber"].Value);
|
||||
result = new(deviceId,
|
||||
match.Groups["DeviceType"].Value,
|
||||
deviceNumber,
|
||||
match.Groups["DescriptionName"].Value,
|
||||
match.Groups["DescriptionTest"].Value,
|
||||
match.Groups["Frequency"].Value,
|
||||
match.Groups["Date"].Value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ParseCSV(CommaSeparatedValuesConfiguration commaSeparatedValuesConfiguration, ILogger<Worker> logger)
|
||||
{
|
||||
string line;
|
||||
string test;
|
||||
string[] lines;
|
||||
string[] columns;
|
||||
MetaData? metaData;
|
||||
string fileNameWithoutExtension;
|
||||
string[] files = Directory.GetFiles(commaSeparatedValuesConfiguration.Directory, commaSeparatedValuesConfiguration.SearchPattern, SearchOption.TopDirectoryOnly);
|
||||
logger.LogInformation(files.Length.ToString());
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
||||
metaData = GetMetaData(commaSeparatedValuesConfiguration, fileNameWithoutExtension);
|
||||
if (metaData is null)
|
||||
continue;
|
||||
lines = File.ReadAllLines(file);
|
||||
if (lines.Length < 2)
|
||||
continue;
|
||||
line = lines[0];
|
||||
columns = line.Split('"');
|
||||
if (columns.Length != commaSeparatedValuesConfiguration.Columns)
|
||||
continue;
|
||||
test = columns[commaSeparatedValuesConfiguration.TestIndex].Trim().Trim('"');
|
||||
if (test.IndexOf(metaData.DescriptionTest, 0, StringComparison.CurrentCultureIgnoreCase) == -1)
|
||||
continue;
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
line = lines[i];
|
||||
if (line.Length < 1)
|
||||
continue;
|
||||
columns = line.Split(',').Select(l => l.Trim().Trim('"')).ToArray();
|
||||
if (columns.Length != commaSeparatedValuesConfiguration.Columns)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ParseCSV(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string directory = args[0];
|
||||
string regularExpressionPattern = args[2];
|
||||
string regularExpressionGroupCount = args[3];
|
||||
string searchPattern = args[4];
|
||||
string columns = args[5];
|
||||
string testIndex = args[6];
|
||||
logger.LogInformation(directory);
|
||||
CommaSeparatedValuesConfiguration commaSeparatedValuesConfiguration = new(int.Parse(columns), directory, int.Parse(testIndex), int.Parse(regularExpressionGroupCount), regularExpressionPattern, searchPattern);
|
||||
ParseCSV(commaSeparatedValuesConfiguration, logger);
|
||||
}
|
||||
|
||||
}
|
91
Day/2024-Q2/Helper-2024-04-09.cs
Normal file
91
Day/2024-Q2/Helper-2024-04-09.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240409
|
||||
{
|
||||
|
||||
internal record FsSize( // cSpell:disable
|
||||
[property: JsonPropertyName("name")] string Name,
|
||||
[property: JsonPropertyName("object")] string Object,
|
||||
[property: JsonPropertyName("pmon")] string PMon,
|
||||
[property: JsonPropertyName("unit")] string Unit,
|
||||
[property: JsonPropertyName("timeresolution")] string TimeResolution,
|
||||
[property: JsonPropertyName("aggr")] string Aggr,
|
||||
[property: JsonPropertyName("data")] List<double[]> Data
|
||||
); // cSpell:restore
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, AllowTrailingCommas = true)]
|
||||
[JsonSerializable(typeof(FsSize))]
|
||||
internal partial class FsSizeSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
internal static void MonA(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string url;
|
||||
FsSize? fsSize;
|
||||
string[] segments;
|
||||
Task<string> json;
|
||||
string jsonSection;
|
||||
string urlBase = args[3];
|
||||
string selector = args[6];
|
||||
string urlPartA = args[4];
|
||||
string urlPartC = args[5];
|
||||
string directory = args[0];
|
||||
string aggregation = args[8];
|
||||
int skip = int.Parse(args[7]);
|
||||
string searchPattern = args[2];
|
||||
string fileNameWithoutExtension;
|
||||
string[] ignore = args[9].Split(',');
|
||||
Task<HttpResponseMessage> httpResponseMessage;
|
||||
string[] files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||
HttpClient httpClient = new(new HttpClientHandler { UseCookies = false }) { BaseAddress = new Uri(urlBase) };
|
||||
logger.LogInformation("{directory} has {files}(s)", directory, files.Length);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
|
||||
if (ignore.Contains(fileNameWithoutExtension))
|
||||
continue;
|
||||
url = $"{httpClient.BaseAddress}{urlPartA}{fileNameWithoutExtension}{urlPartC}";
|
||||
httpResponseMessage = httpClient.GetAsync(url);
|
||||
httpResponseMessage.Wait();
|
||||
if (!httpResponseMessage.Result.IsSuccessStatusCode)
|
||||
{
|
||||
logger.LogWarning("{StatusCode} for {url}", httpResponseMessage.Result.StatusCode, url);
|
||||
continue;
|
||||
}
|
||||
json = httpResponseMessage.Result.Content.ReadAsStringAsync();
|
||||
json.Wait();
|
||||
if (json.Result.Length < 3)
|
||||
{
|
||||
logger.LogInformation("{Size} | {Julian} | {PMon} | {FileNameWithoutExtension}", -1, -1, "{}", fileNameWithoutExtension);
|
||||
continue;
|
||||
}
|
||||
segments = json.Result.Split($"\"{fileNameWithoutExtension}{selector}");
|
||||
if (segments.Length < 2)
|
||||
{
|
||||
logger.LogInformation("{Size} | {Julian} | {PMon} | {FileNameWithoutExtension}", -1, -1, selector, fileNameWithoutExtension);
|
||||
continue;
|
||||
}
|
||||
for (int i = 1; i < segments.Length; i++)
|
||||
{
|
||||
jsonSection = segments[i][skip..][..^1];
|
||||
fsSize = JsonSerializer.Deserialize(jsonSection, FsSizeSourceGenerationContext.Default.FsSize);
|
||||
if (fsSize is null)
|
||||
continue;
|
||||
if (fsSize.Aggr != aggregation)
|
||||
continue;
|
||||
if (fsSize.Data.Count == 0 || fsSize.Data[0].Length == 0)
|
||||
{
|
||||
logger.LogInformation("{Size} | {Julian} | {PMon} | {FileNameWithoutExtension}", -1, -1, nameof(FsSize.Data), fileNameWithoutExtension);
|
||||
continue;
|
||||
}
|
||||
logger.LogInformation("{Size} | {Julian} | {PMon} | {FileNameWithoutExtension}", fsSize.Data[0][1].ToString().PadLeft(20, '0'), fsSize.Data[0][0], fsSize.PMon, fileNameWithoutExtension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
62
Day/2024-Q2/Helper-2024-04-17.cs
Normal file
62
Day/2024-Q2/Helper-2024-04-17.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using File_Folder_Helper.Helpers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240417
|
||||
{
|
||||
|
||||
private static void RunCommand(ILogger<Worker> logger, string directory, string commandFileName, List<string> files, int before, CancellationToken cancellationToken)
|
||||
{
|
||||
bool usePathCombine = false;
|
||||
string command = $"npx eclint fix {string.Join(' ', files)}";
|
||||
string output = HelperNPM.RunCommand(commandFileName, command, directory);
|
||||
logger.LogInformation(output);
|
||||
if (output.Contains("error", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Environment.ExitCode = 2;
|
||||
logger.LogCritical("Setting exit code {ExitCode}", Environment.ExitCode);
|
||||
throw new Exception(output);
|
||||
}
|
||||
ReadOnlyCollection<string> afterGitOthersModifiedAndDeletedExcludingStandardFiles = HelperGit.GetOthersModifiedAndDeletedExcludingStandardFiles(directory, usePathCombine, cancellationToken);
|
||||
int after = afterGitOthersModifiedAndDeletedExcludingStandardFiles.Count;
|
||||
if (before != after)
|
||||
{
|
||||
List<string> fileNames = [];
|
||||
foreach (string file in afterGitOthersModifiedAndDeletedExcludingStandardFiles)
|
||||
{
|
||||
if (file.Contains(' '))
|
||||
continue;
|
||||
if (files.Contains(file))
|
||||
continue;
|
||||
fileNames.Add(Path.GetFileName(file));
|
||||
}
|
||||
Environment.ExitCode = 1;
|
||||
throw new Exception($"Files were modified exiting with exit code {Environment.ExitCode}!{Environment.NewLine}{string.Join(Environment.NewLine, fileNames)}");
|
||||
}
|
||||
}
|
||||
|
||||
internal static void FilteredRunCommand(ILogger<Worker> logger, List<string> args, CancellationToken cancellationToken)
|
||||
{
|
||||
List<string> files = [];
|
||||
string directory = args[0];
|
||||
bool usePathCombine = false;
|
||||
string commandFileName = args[2];
|
||||
ReadOnlyCollection<string> gitOthersModifiedAndDeletedExcludingStandardFiles = HelperGit.GetOthersModifiedAndDeletedExcludingStandardFiles(directory, usePathCombine, cancellationToken);
|
||||
int before = gitOthersModifiedAndDeletedExcludingStandardFiles.Count;
|
||||
foreach (string file in gitOthersModifiedAndDeletedExcludingStandardFiles)
|
||||
{
|
||||
if (file.Contains(' '))
|
||||
{
|
||||
logger.LogInformation("Skipping <{File}>", file);
|
||||
continue;
|
||||
}
|
||||
files.Add(file);
|
||||
}
|
||||
logger.LogInformation("{directory} has {files} file(s)", directory, before);
|
||||
if (files.Count > 0)
|
||||
RunCommand(logger, directory, commandFileName, files, before, cancellationToken);
|
||||
}
|
||||
|
||||
}
|
93
Day/2024-Q2/Helper-2024-04-26.cs
Normal file
93
Day/2024-Q2/Helper-2024-04-26.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240426
|
||||
{
|
||||
|
||||
private static void Write(string toA, string toB, string matchA, string matchB, string file, List<string> lines)
|
||||
{
|
||||
StringBuilder stringBuilder = new();
|
||||
foreach (string line in lines)
|
||||
_ = stringBuilder.AppendLine(line.Replace(matchB, toB));
|
||||
string checkFile = file.Replace(matchA, toA);
|
||||
_ = Directory.CreateDirectory(Path.GetDirectoryName(checkFile) ?? throw new NullReferenceException());
|
||||
if (!File.Exists(checkFile))
|
||||
File.WriteAllText(checkFile, stringBuilder.ToString());
|
||||
}
|
||||
|
||||
private static void UpdateTestFiles(ILogger<Worker> logger, string to, string match, string staticDirectoryName, List<string> files)
|
||||
{
|
||||
List<string> lines;
|
||||
string directoryName;
|
||||
string fistLineCheck;
|
||||
string lastLineCheck = "#endif";
|
||||
foreach (string file in files)
|
||||
{
|
||||
directoryName = Path.GetFileName(Path.GetDirectoryName(file) ?? throw new NullReferenceException(nameof(directoryName)));
|
||||
if (directoryName == staticDirectoryName)
|
||||
continue;
|
||||
lines = File.ReadAllLines(file).ToList();
|
||||
if (directoryName == match)
|
||||
Write(to, to.Replace('.', '_'), match, match.Replace('.', '_'), file, lines);
|
||||
if (lines.Count < 3)
|
||||
continue;
|
||||
fistLineCheck = $"#if {directoryName.Replace('.', '_')}";
|
||||
if (lines[0].StartsWith("#if true") || lines[0].StartsWith(fistLineCheck))
|
||||
continue;
|
||||
for (int i = lines.Count - 1; i > 0; i--)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(lines[i]))
|
||||
break;
|
||||
lines.RemoveAt(i);
|
||||
}
|
||||
if (lines[^1].StartsWith(lastLineCheck))
|
||||
continue;
|
||||
if (lines.All(l => string.IsNullOrEmpty(l) || l.StartsWith("//")))
|
||||
continue;
|
||||
lines.Add(lastLineCheck);
|
||||
lines.Insert(0, fistLineCheck);
|
||||
File.WriteAllText(file, string.Join(Environment.NewLine, lines));
|
||||
logger.LogDebug(file);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateProjectTests(ILogger<Worker> logger, string to, string match, string searchPattern, string staticDirectoryName, string extractDirectoryName, string createSelfDescriptionDirectoryName, string sourceCodeDirectory)
|
||||
{
|
||||
List<string> files = [];
|
||||
List<string> knownDirectories = [];
|
||||
knownDirectories.AddRange(Directory.GetDirectories(sourceCodeDirectory, createSelfDescriptionDirectoryName, SearchOption.AllDirectories));
|
||||
knownDirectories.AddRange(Directory.GetDirectories(sourceCodeDirectory, extractDirectoryName, SearchOption.AllDirectories));
|
||||
knownDirectories.AddRange(Directory.GetDirectories(sourceCodeDirectory, staticDirectoryName, SearchOption.AllDirectories));
|
||||
foreach (string knownDirectory in knownDirectories)
|
||||
files.AddRange(Directory.GetFiles(knownDirectory, searchPattern, SearchOption.AllDirectories));
|
||||
logger.LogInformation("{directory} has {files} file(s)", sourceCodeDirectory, files.Count);
|
||||
UpdateTestFiles(logger, to, match, staticDirectoryName, files);
|
||||
}
|
||||
|
||||
internal static void UpdateTests(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string to = args[9];
|
||||
string match = args[8];
|
||||
string searchPattern = args[7];
|
||||
string[] sourceCodeDirectories;
|
||||
string staticDirectoryName = args[6];
|
||||
string extractDirectoryName = args[5];
|
||||
string sourceCodeDirectoryName = args[3];
|
||||
string directory = Path.GetFullPath(args[0]);
|
||||
string createSelfDescriptionDirectoryName = args[4];
|
||||
string systemVolumeInformation = Path.Combine(directory, args[2]);
|
||||
string[] subDirectories = Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string subDirectory in subDirectories)
|
||||
{
|
||||
if (subDirectory == systemVolumeInformation)
|
||||
continue;
|
||||
sourceCodeDirectories = Directory.GetDirectories(subDirectory, sourceCodeDirectoryName, SearchOption.AllDirectories);
|
||||
if (sourceCodeDirectories.Length != 1)
|
||||
continue;
|
||||
UpdateProjectTests(logger, to, match, searchPattern, staticDirectoryName, extractDirectoryName, createSelfDescriptionDirectoryName, sourceCodeDirectories[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
145
Day/2024-Q2/Helper-2024-04-27.cs
Normal file
145
Day/2024-Q2/Helper-2024-04-27.cs
Normal file
@ -0,0 +1,145 @@
|
||||
using File_Folder_Helper.Helpers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240427
|
||||
{
|
||||
|
||||
internal record Asset( // cSpell:disable
|
||||
[property: JsonPropertyName("id")] string? Id,
|
||||
[property: JsonPropertyName("deviceAssetId")] string? DeviceAssetId,
|
||||
[property: JsonPropertyName("ownerId")] string? OwnerId,
|
||||
[property: JsonPropertyName("deviceId")] string? DeviceId,
|
||||
[property: JsonPropertyName("type")] string? Type,
|
||||
[property: JsonPropertyName("originalPath")] string? OriginalPath,
|
||||
[property: JsonPropertyName("previewPath")] string? PreviewPath,
|
||||
[property: JsonPropertyName("fileCreatedAt")] DateTime? FileCreatedAt,
|
||||
[property: JsonPropertyName("fileModifiedAt")] DateTime? FileModifiedAt,
|
||||
[property: JsonPropertyName("isFavorite")] bool? IsFavorite,
|
||||
[property: JsonPropertyName("duration")] object? Duration,
|
||||
[property: JsonPropertyName("thumbnailPath")] string? ThumbnailPath,
|
||||
[property: JsonPropertyName("encodedVideoPath")] string? EncodedVideoPath,
|
||||
[property: JsonPropertyName("checksum")] string? Checksum,
|
||||
[property: JsonPropertyName("isVisible")] bool? IsVisible,
|
||||
[property: JsonPropertyName("livePhotoVideoId")] object? LivePhotoVideoId,
|
||||
[property: JsonPropertyName("updatedAt")] DateTime? UpdatedAt,
|
||||
[property: JsonPropertyName("createdAt")] DateTime? CreatedAt,
|
||||
[property: JsonPropertyName("isArchived")] bool? IsArchived,
|
||||
[property: JsonPropertyName("originalFileName")] string? OriginalFileName,
|
||||
[property: JsonPropertyName("sidecarPath")] string? SidecarPath,
|
||||
[property: JsonPropertyName("isReadOnly")] bool? IsReadOnly,
|
||||
[property: JsonPropertyName("thumbhash")] string? Thumbhash,
|
||||
[property: JsonPropertyName("isOffline")] bool? IsOffline,
|
||||
[property: JsonPropertyName("libraryId")] string? LibraryId,
|
||||
[property: JsonPropertyName("isExternal")] bool? IsExternal,
|
||||
[property: JsonPropertyName("deletedAt")] DateTime? DeletedAt,
|
||||
[property: JsonPropertyName("localDateTime")] DateTime? LocalDateTime,
|
||||
[property: JsonPropertyName("stackId")] object? StackId); // cSpell:restore
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, AllowTrailingCommas = true)]
|
||||
[JsonSerializable(typeof(List<Asset>))]
|
||||
internal partial class AssetCollectionSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private static string? MoveAsset(string home, string originalFileName, string lastVarDirectoryName, string path)
|
||||
{
|
||||
string? result;
|
||||
string checkFile;
|
||||
string sourceFile;
|
||||
int? match = null;
|
||||
string checkDirectory = home;
|
||||
string sourceDirectory = home;
|
||||
string originalFileNameWithoutExtension = Path.GetFileNameWithoutExtension(originalFileName);
|
||||
List<string> directoryNames = HelperDirectory.GetDirectoryNames(path);
|
||||
for (int i = 0; i < directoryNames.Count; i++)
|
||||
{
|
||||
if (directoryNames[i] != lastVarDirectoryName)
|
||||
continue;
|
||||
match = i;
|
||||
break;
|
||||
}
|
||||
if (match is null)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
string[] fileNameSegments = directoryNames[^1].Split('-');
|
||||
foreach (string directory in directoryNames.Skip(match.Value + 1).Take(directoryNames.Count - match.Value - 2))
|
||||
sourceDirectory = Path.Combine(sourceDirectory, directory);
|
||||
if (!Directory.Exists(sourceDirectory))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
sourceFile = Path.Combine(sourceDirectory, directoryNames[^1]);
|
||||
if (!File.Exists(sourceFile))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
foreach (string directory in directoryNames.Skip(match.Value + 1).Take(directoryNames.Count - match.Value - 2))
|
||||
checkDirectory = Path.Combine(checkDirectory, directory);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
checkFile = Path.Combine(checkDirectory, $"{originalFileNameWithoutExtension}{fileNameSegments[^1]}");
|
||||
if (File.Exists(checkFile))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
File.Move(sourceFile, checkFile);
|
||||
result = checkFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void MoveAssets(ILogger<Worker> logger, string var, string home, string pictures, List<Asset> assets)
|
||||
{
|
||||
string? checkFile;
|
||||
List<string> varDirectoryNames = HelperDirectory.GetDirectoryNames(home);
|
||||
string lastVarDirectoryName = varDirectoryNames[^1];
|
||||
foreach (Asset asset in assets)
|
||||
{
|
||||
if (asset.OriginalFileName is null)
|
||||
continue;
|
||||
if (asset.Type is null or not "IMAGE")
|
||||
continue;
|
||||
if (asset.PreviewPath is null || !asset.PreviewPath.StartsWith(var))
|
||||
continue;
|
||||
if (asset.ThumbnailPath is null || !asset.ThumbnailPath.StartsWith(var))
|
||||
continue;
|
||||
if (asset.OriginalPath is null || !asset.OriginalPath.StartsWith(pictures))
|
||||
continue;
|
||||
checkFile = MoveAsset(home, asset.OriginalFileName, lastVarDirectoryName, asset.PreviewPath);
|
||||
if (checkFile is null)
|
||||
continue;
|
||||
checkFile = MoveAsset(home, asset.OriginalFileName, lastVarDirectoryName, asset.ThumbnailPath);
|
||||
if (checkFile is null)
|
||||
continue;
|
||||
logger.LogDebug("<{OriginalFileName}> moved.", asset.OriginalFileName);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning restore IDE0055, IDE0059, IDE0060, IDE2000, CS0168
|
||||
|
||||
internal static void Immich(Models.AppSettings appSettings, ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string var = args[5];
|
||||
string home = args[6];
|
||||
string query = args[3];
|
||||
string jsonFile = args[4];
|
||||
string pictures = args[7];
|
||||
string directory = args[0];
|
||||
logger.LogInformation(query);
|
||||
string databasePassword = args[2];
|
||||
string json = File.ReadAllText(Path.Combine(directory, jsonFile));
|
||||
List<Asset> assets = JsonSerializer.Deserialize(json, AssetCollectionSourceGenerationContext.Default.ListAsset) ?? throw new NullReferenceException();
|
||||
string encryptedPassword = RijndaelEncryption.Encrypt(databasePassword, appSettings.Company);
|
||||
MoveAssets(logger, var, home, pictures, assets);
|
||||
logger.LogInformation("Encrypted Password: [{EncryptedPassword}]", encryptedPassword);
|
||||
}
|
||||
|
||||
}
|
56
Day/2024-Q2/Helper-2024-04-29.cs
Normal file
56
Day/2024-Q2/Helper-2024-04-29.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240429
|
||||
{
|
||||
|
||||
internal static void GitConfigCleanUp(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
string[] files;
|
||||
Process? process;
|
||||
string? directory;
|
||||
string standardError;
|
||||
string ignoreError = args[5];
|
||||
string searchPattern = args[3];
|
||||
ProcessStartInfo processStartInfo;
|
||||
string root = Path.GetFullPath(args[0]);
|
||||
string[] removeRemotes = args[4].Split(',');
|
||||
string systemVolumeInformation = Path.Combine(root, args[2]);
|
||||
string[] subDirectories = Directory.GetDirectories(root, "*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string subDirectory in subDirectories)
|
||||
{
|
||||
if (subDirectory == systemVolumeInformation)
|
||||
continue;
|
||||
files = Directory.GetFiles(subDirectory, searchPattern, SearchOption.AllDirectories);
|
||||
foreach (string file in files)
|
||||
{
|
||||
directory = Path.GetDirectoryName(file);
|
||||
if (directory is null)
|
||||
continue;
|
||||
foreach (string removeRemote in removeRemotes)
|
||||
{
|
||||
processStartInfo = new()
|
||||
{
|
||||
FileName = "git",
|
||||
WorkingDirectory = directory,
|
||||
Arguments = $"remote rm {removeRemote}",
|
||||
RedirectStandardError = true
|
||||
};
|
||||
process = Process.Start(processStartInfo);
|
||||
if (process is null)
|
||||
continue;
|
||||
#pragma warning disable IDE0058
|
||||
process.WaitForExit(7000);
|
||||
#pragma warning restore IDE0058
|
||||
standardError = process.StandardError.ReadToEnd();
|
||||
if (!standardError.Contains(ignoreError))
|
||||
logger.LogInformation(standardError);
|
||||
logger.LogInformation("for <{directoryName}> remote rm {removeRemote}", directory, removeRemote);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -6,7 +6,7 @@ namespace File_Folder_Helper.Helpers;
|
||||
internal static class HelperDay
|
||||
{
|
||||
|
||||
internal static void Select(AppSettings appSettings, ILogger<Worker> logger, List<string> args)
|
||||
internal static void Select(AppSettings appSettings, ILogger<Worker> logger, List<string> args, CancellationToken cancellationToken)
|
||||
{
|
||||
if (args[1] == "Day-Helper-2023-09-06")
|
||||
Day.Helper20230906.SaveJson(logger, args[0]);
|
||||
@ -29,7 +29,7 @@ internal static class HelperDay
|
||||
else if (args[1] == "Day-Helper-2023-12-05")
|
||||
Day.Helper20231205.SplitMarkdownFile(logger, args);
|
||||
else if (args[1] == "Day-Helper-2023-12-12")
|
||||
Day.Helper20231212.SplitJsonFile(logger, args);
|
||||
logger.LogError("{arg} - was deleted on 2024-04-08", args[1]);
|
||||
else if (args[1] == "Day-Helper-2023-12-22")
|
||||
Day.Helper20231222.ConvertId(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-01-05")
|
||||
@ -39,7 +39,27 @@ internal static class HelperDay
|
||||
else if (args[1] == "Day-Helper-2024-01-07")
|
||||
Day.Helper20240107.DirectoryToISO(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-01-08")
|
||||
Day.Helper20240108.SortCodeMethods(logger, args);
|
||||
Day.Helper20240108.SortCodeMethods(logger, args, cancellationToken);
|
||||
else if (args[1] == "Day-Helper-2024-01-27")
|
||||
logger.LogError("{arg} - has been migrated to Clipboard_Send_Keys", args[1]);
|
||||
else if (args[1] == "Day-Helper-2024-01-29")
|
||||
Day.Helper20240129.JsonToTsv(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-03-05")
|
||||
Day.Helper20240305.ArchiveFiles(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-04-03")
|
||||
Day.Helper20240403.AlertIfNewDeviceIsConnected(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-04-04")
|
||||
Day.Helper20240404.ParseCSV(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-04-09")
|
||||
Day.Helper20240409.MonA(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-04-17")
|
||||
Day.Helper20240417.FilteredRunCommand(logger, args, cancellationToken);
|
||||
else if (args[1] == "Day-Helper-2024-04-26")
|
||||
Day.Helper20240426.UpdateTests(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-04-27")
|
||||
Day.Helper20240427.Immich(appSettings, logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-04-29")
|
||||
Day.Helper20240429.GitConfigCleanUp(logger, args);
|
||||
else
|
||||
throw new Exception(appSettings.Company);
|
||||
}
|
||||
|
Reference in New Issue
Block a user