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:
2024-01-08 13:57:27 -07:00
parent 84ad97ac6e
commit 4e3f06bb44
35 changed files with 1914 additions and 884 deletions

View File

@ -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]]);
}
}

View File

@ -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);
}
}

View File

@ -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)

View 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);
}
}

View 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);
}
}