person-key-to-immich-import birthday json (Day-Helper-2024-05-18)

csharp_prefer_braces = true
This commit is contained in:
2025-09-06 11:16:55 -07:00
parent 8ec89953bc
commit 6102da7266
54 changed files with 2218 additions and 1721 deletions

View File

@ -10,8 +10,7 @@ using System.Text.RegularExpressions;
namespace File_Folder_Helper.ADO2024.PI2;
internal static partial class Helper20240623
{
internal static partial class Helper20240623 {
[GeneratedRegex("([A-Z]+(.))")]
private static partial Regex UpperCase();
@ -36,34 +35,34 @@ internal static partial class Helper20240623
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Input))]
private partial class InputSourceGenerationContext : JsonSerializerContext
{
private partial class InputSourceGenerationContext : JsonSerializerContext {
}
private static Record GetRecord(Input input, FileInfo fileInfo)
{
private static Record GetRecord(Input input, FileInfo fileInfo) {
Record result;
string line;
int? stopLine = null;
int? subTasksLine = null;
int? codeInsidersLine = null;
LineNumber lineNumber = HelperMarkdown.GetLineNumbers(fileInfo);
for (int i = 0; i < lineNumber.Lines.Count; i++)
{
for (int i = 0; i < lineNumber.Lines.Count; i++) {
line = lineNumber.Lines[i];
if (line.StartsWith(input.CodeInsiders) && line[^1] == ')')
if (line.StartsWith(input.CodeInsiders) && line[^1] == ')') {
codeInsidersLine = i;
if (line != input.SubTasks)
}
if (line != input.SubTasks) {
continue;
}
subTasksLine = i;
if (codeInsidersLine is null)
if (codeInsidersLine is null) {
break;
if (lineNumber.Lines.Count > i)
{
for (int j = i + 1; j < lineNumber.Lines.Count; j++)
{
if (lineNumber.Lines[j].Length > 0 && lineNumber.Lines[j][0] == '#')
{
}
if (lineNumber.Lines.Count > i) {
for (int j = i + 1; j < lineNumber.Lines.Count; j++) {
if (lineNumber.Lines[j].Length > 0 && lineNumber.Lines[j][0] == '#') {
stopLine = j;
break;
}
@ -76,22 +75,18 @@ internal static partial class Helper20240623
return result;
}
private static List<Record> GetRecords(Input input)
{
private static List<Record> GetRecords(Input input) {
List<Record> results = [];
Record record;
FileInfo fileInfo;
string sourceDirectory = input.SourceDirectory;
ReadOnlyCollection<string> directoryNames = HelperDirectory.GetDirectoryNames(input.SourceDirectory);
if (!directoryNames.Any(l => l.StartsWith(input.DirectoryFilter, StringComparison.CurrentCultureIgnoreCase)))
{
if (!directoryNames.Any(l => l.StartsWith(input.DirectoryFilter, StringComparison.CurrentCultureIgnoreCase))) {
string directoryName;
string[] checkDirectories = Directory.GetDirectories(input.SourceDirectory, "*", SearchOption.TopDirectoryOnly);
foreach (string checkDirectory in checkDirectories)
{
foreach (string checkDirectory in checkDirectories) {
directoryName = Path.GetFileName(checkDirectory);
if (directoryName.StartsWith(input.DirectoryFilter, StringComparison.CurrentCultureIgnoreCase))
{
if (directoryName.StartsWith(input.DirectoryFilter, StringComparison.CurrentCultureIgnoreCase)) {
sourceDirectory = checkDirectory;
break;
}
@ -99,10 +94,10 @@ internal static partial class Helper20240623
}
string[] subDirectories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly);
List<string> files = Directory.GetFiles(sourceDirectory, input.SearchPattern, SearchOption.TopDirectoryOnly).ToList();
foreach (string subDirectory in subDirectories)
foreach (string subDirectory in subDirectories) {
files.AddRange(Directory.GetFiles(subDirectory, input.SearchPattern, SearchOption.TopDirectoryOnly));
foreach (string file in files)
{
}
foreach (string file in files) {
fileInfo = new(file);
record = GetRecord(input, fileInfo);
results.Add(record);
@ -110,20 +105,19 @@ internal static partial class Helper20240623
return results;
}
private static string GetParamCase(string value)
{
private static string GetParamCase(string value) {
string result;
StringBuilder stringBuilder = new(value);
Match[] matches = UpperCase().Matches(value).ToArray();
for (int i = matches.Length - 1; i > -1; i--)
for (int i = matches.Length - 1; i > -1; i--) {
_ = stringBuilder.Insert(matches[i].Index, '-');
}
string[] segments = InvalidCharacter().Split(stringBuilder.ToString().ToLower());
result = string.Join('-', segments).Trim('-');
return result;
}
private static ReadOnlyCollection<SubTaskLine> GetSubTaskLines(Input input, bool? foundStarted, bool? foundCompleted, string fallbackLine, Record record)
{
private static ReadOnlyCollection<SubTaskLine> GetSubTaskLines(Input input, bool? foundStarted, bool? foundCompleted, string fallbackLine, Record record) {
List<SubTaskLine> results = [];
char done;
string line;
@ -134,15 +128,17 @@ internal static partial class Helper20240623
bool foundSubTasks = false;
int tasksZeroLength = input.Tasks[0].Length;
long ticks = record.FileInfo.LastWriteTime.Ticks;
for (int i = 0; i < record.LineNumber.Lines.Count; i++)
{
for (int i = 0; i < record.LineNumber.Lines.Count; i++) {
line = record.LineNumber.Lines[i];
if (!foundSubTasks && line == input.SubTasks)
if (!foundSubTasks && line == input.SubTasks) {
foundSubTasks = true;
if (!foundSubTasks)
}
if (!foundSubTasks) {
continue;
if (line.Length <= tasksZeroLength || !line.StartsWith(input.Tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']')
}
if (line.Length <= tasksZeroLength || !line.StartsWith(input.Tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']') {
continue;
}
startedValue = foundStarted is not null && foundStarted.Value;
completedValue = foundCompleted is not null && foundCompleted.Value;
subTaskLine = new(Text: $" {line}", Started: startedValue, Completed: completedValue, Ticks: ticks, Line: i);
@ -150,10 +146,9 @@ internal static partial class Helper20240623
}
startedValue = foundStarted is not null && foundStarted.Value;
completedValue = foundCompleted is not null && foundCompleted.Value;
if (record.LineNumber.H1 is null)
if (record.LineNumber.H1 is null) {
subTaskLine = new(Text: fallbackLine, Started: startedValue, Completed: completedValue, Ticks: ticks, Line: null);
else
{
} else {
done = foundCompleted is null || !foundCompleted.Value ? ' ' : 'x';
string codeInsidersLine = record.CodeInsidersLine is null ? string.Empty : $" ~~{record.LineNumber.Lines[record.CodeInsidersLine.Value]}~~";
text = $"- [{done}] {ticks} {record.LineNumber.Lines[record.LineNumber.H1.Value]}{codeInsidersLine}";
@ -163,10 +158,8 @@ internal static partial class Helper20240623
return new(results);
}
private static string GetSeasonName(int dayOfYear)
{
string result = dayOfYear switch
{
private static string GetSeasonName(int dayOfYear) {
string result = dayOfYear switch {
< 78 => "0.Winter",
< 124 => "1.Spring",
< 171 => "2.Spring",
@ -259,21 +252,20 @@ internal static partial class Helper20240623
}
""".Replace("{}", directory.Replace('\\', '/'));
private static void FileWriteAllText(string path, string contents)
{
private static void FileWriteAllText(string path, string contents) {
// string checkJson = Regex.Replace(File.ReadAllText(path), @"\s+", " ", RegexOptions.Multiline);
// if (Regex.Replace(singletonJson, @"\s+", " ", RegexOptions.Multiline) != checkJson)
// File.WriteAllText(path, singletonJson);
string old = !File.Exists(path) ? string.Empty : File.ReadAllText(path);
if (old != contents)
if (old != contents) {
File.WriteAllText(path, contents);
}
}
private static void FileWriteAllText(string path, string[] contents) =>
FileWriteAllText(path, string.Join(Environment.NewLine, contents));
private static ReadOnlyCollection<H1AndParamCase> GetH1ParamCaseCollection(Input input, ReadOnlyCollection<string> lines)
{
private static ReadOnlyCollection<H1AndParamCase> GetH1ParamCaseCollection(Input input, ReadOnlyCollection<string> lines) {
List<H1AndParamCase> results = [];
string h1;
string line;
@ -281,18 +273,21 @@ internal static partial class Helper20240623
bool foundSubTasks = false;
H1AndParamCase h1AndParamCase;
int tasksZeroLength = input.Tasks[0].Length;
for (int i = 0; i < lines.Count; i++)
{
for (int i = 0; i < lines.Count; i++) {
line = lines[i];
if (!foundSubTasks && line == input.SubTasks)
if (!foundSubTasks && line == input.SubTasks) {
foundSubTasks = true;
if (!foundSubTasks)
}
if (!foundSubTasks) {
continue;
if (line.Length <= tasksZeroLength || !line.StartsWith(input.Tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']')
}
if (line.Length <= tasksZeroLength || !line.StartsWith(input.Tasks[0]) || line[tasksZeroLength] is not ' ' and not 'x' || line[tasksZeroLength + 1] != ']') {
continue;
}
h1 = line[(tasksZeroLength + 3)..];
if (string.IsNullOrEmpty(h1))
if (string.IsNullOrEmpty(h1)) {
continue;
}
paramCase = GetParamCase(h1);
h1AndParamCase = new(h1, paramCase);
results.Add(h1AndParamCase);
@ -300,23 +295,24 @@ internal static partial class Helper20240623
return results.AsReadOnly();
}
private static void CreateFiles(string directory, ReadOnlyCollection<H1AndParamCase> h1ParamCaseCollection)
{
foreach (H1AndParamCase h1ParamCase in h1ParamCaseCollection)
private static void CreateFiles(string directory, ReadOnlyCollection<H1AndParamCase> h1ParamCaseCollection) {
foreach (H1AndParamCase h1ParamCase in h1ParamCaseCollection) {
FileWriteAllText(Path.Combine(directory, $"{h1ParamCase.ParamCase}.md"), $"# {h1ParamCase.H1}");
}
}
private static string WriteAndGetIndexFile(string h1, string verifiedDirectory, ReadOnlyCollection<H1AndParamCase> h1ParamCaseCollection)
{
private static string WriteAndGetIndexFile(string h1, string verifiedDirectory, ReadOnlyCollection<H1AndParamCase> h1ParamCaseCollection) {
string result;
string[] indexLines = GetIndexLines(h1, h1ParamCaseCollection);
string kanbanDirectory = Path.Combine(verifiedDirectory, ".kanbn");
string tasksKanbanDirectory = Path.Combine(kanbanDirectory, "tasks");
if (!Directory.Exists(tasksKanbanDirectory))
if (!Directory.Exists(tasksKanbanDirectory)) {
_ = Directory.CreateDirectory(tasksKanbanDirectory);
}
string verifiedVisualStudioCodeDirectory = Path.Combine(verifiedDirectory, ".vscode");
if (!Directory.Exists(verifiedVisualStudioCodeDirectory))
if (!Directory.Exists(verifiedVisualStudioCodeDirectory)) {
_ = Directory.CreateDirectory(verifiedVisualStudioCodeDirectory);
}
result = Path.Combine(kanbanDirectory, "index.md");
CreateFiles(tasksKanbanDirectory, h1ParamCaseCollection);
FileWriteAllText(result, indexLines);
@ -326,48 +322,49 @@ internal static partial class Helper20240623
return result;
}
private static ReadOnlyCollection<string> GetXColumns(Input input, int frontMatterYamlEnd, int value, ReadOnlyCollection<string> lines)
{
private static ReadOnlyCollection<string> GetXColumns(Input input, int frontMatterYamlEnd, int value, ReadOnlyCollection<string> lines) {
List<string> results = [];
string[] segments;
for (int i = value + 1; i < frontMatterYamlEnd; i++)
{
for (int i = value + 1; i < frontMatterYamlEnd; i++) {
segments = lines[i].Replace("\t", " ").Split(" - ");
if (segments.Length != 2)
if (segments.Length != 2) {
break;
}
results.Add($"## {segments[1].Replace("'", string.Empty)}");
}
if (results.Count == 0)
if (results.Count == 0) {
results.Add(input.Done);
}
return results.AsReadOnly();
}
private static ReadOnlyCollection<string> GetCompletedColumns(Input input, LineNumber lineNumber)
{
private static ReadOnlyCollection<string> GetCompletedColumns(Input input, LineNumber lineNumber) {
List<string> results;
if (lineNumber.FrontMatterYamlEnd is null || lineNumber.CompletedColumns is null)
if (lineNumber.FrontMatterYamlEnd is null || lineNumber.CompletedColumns is null) {
results = [];
else
} else {
results = GetXColumns(input, lineNumber.FrontMatterYamlEnd.Value, lineNumber.CompletedColumns.Value, lineNumber.Lines).ToList();
if (results.Count == 0)
}
if (results.Count == 0) {
results.Add(input.Done);
}
return results.AsReadOnly();
}
private static ReadOnlyCollection<string> GetStartedColumns(Input input, LineNumber lineNumber)
{
private static ReadOnlyCollection<string> GetStartedColumns(Input input, LineNumber lineNumber) {
List<string> results;
if (lineNumber.FrontMatterYamlEnd is null || lineNumber.StartedColumns is null)
if (lineNumber.FrontMatterYamlEnd is null || lineNumber.StartedColumns is null) {
results = [];
else
} else {
results = GetXColumns(input, lineNumber.FrontMatterYamlEnd.Value, lineNumber.StartedColumns.Value, lineNumber.Lines).ToList();
if (results.Count == 0)
}
if (results.Count == 0) {
results.Add(input.Done);
}
return results.AsReadOnly();
}
private static ReadOnlyCollection<SubTaskLine> GetSubTaskLines(Input input, FileInfo fileInfo, LineNumber lineNumber)
{
private static ReadOnlyCollection<SubTaskLine> GetSubTaskLines(Input input, FileInfo fileInfo, LineNumber lineNumber) {
List<SubTaskLine> results = [];
FileInfo f;
Record record;
@ -382,37 +379,39 @@ internal static partial class Helper20240623
ReadOnlyCollection<string> startedColumns = GetStartedColumns(input, lineNumber);
ReadOnlyCollection<string> completedColumns = GetCompletedColumns(input, lineNumber);
int start = lineNumber.FrontMatterYamlEnd is null ? 0 : lineNumber.FrontMatterYamlEnd.Value + 1;
for (int i = start; i < lineNumber.Lines.Count; i++)
{
if ((foundStarted is null || !foundStarted.Value) && startedColumns.Any(lineNumber.Lines[i].StartsWith))
for (int i = start; i < lineNumber.Lines.Count; i++) {
if ((foundStarted is null || !foundStarted.Value) && startedColumns.Any(lineNumber.Lines[i].StartsWith)) {
foundStarted = true;
if ((foundCompleted is null || !foundCompleted.Value) && completedColumns.Any(lineNumber.Lines[i].StartsWith))
}
if ((foundCompleted is null || !foundCompleted.Value) && completedColumns.Any(lineNumber.Lines[i].StartsWith)) {
foundCompleted = true;
}
segments = lineNumber.Lines[i].Split(input.Tasks[1]);
startedValue = foundStarted is not null && foundStarted.Value;
completedValue = foundCompleted is not null && foundCompleted.Value;
if (segments.Length > 2 || !segments[0].StartsWith(input.Tasks[0]))
if (segments.Length > 2 || !segments[0].StartsWith(input.Tasks[0])) {
continue;
}
completed = foundCompleted is null || !foundCompleted.Value ? ' ' : 'x';
fallbackLine = $"- [{completed}] {segments[0][input.Tasks[0].Length..]} ~~FallbackLine~~";
if (string.IsNullOrEmpty(fileInfo.DirectoryName))
if (string.IsNullOrEmpty(fileInfo.DirectoryName)) {
continue;
}
f = new(Path.GetFullPath(Path.Combine(fileInfo.DirectoryName, segments[1][..^1])));
if (!f.Exists)
{
if (!f.Exists) {
results.Add(new(Text: fallbackLine, Started: startedValue, Completed: completedValue, Ticks: null, Line: null));
continue;
}
record = GetRecord(input, f);
subTaskLines = GetSubTaskLines(input, startedValue, completedValue, fallbackLine, record);
for (int j = subTaskLines.Count - 1; j >= 0; j--)
for (int j = subTaskLines.Count - 1; j >= 0; j--) {
results.Add(subTaskLines[j]);
}
}
return results.AsReadOnly();
}
private static Input GetInput(List<string> args)
{
private static Input GetInput(List<string> args) {
string indexFile = args[5];
string searchPattern = args[2];
string directoryFilter = args[8];
@ -433,46 +432,42 @@ internal static partial class Helper20240623
SubTasks: subTasks,
SourceDirectory: sourceDirectory,
Tasks: tasks.AsReadOnly());
if (input.Tasks[0] != "- [" || input.Tasks[1] != "](")
if (input.Tasks[0] != "- [" || input.Tasks[1] != "](") {
throw new Exception(JsonSerializer.Serialize(input, InputSourceGenerationContext.Default.Input));
}
return input;
}
private static string? MaybeWriteAndGetIndexFile(Input input, Record record, string? checkDirectory)
{
private static string? MaybeWriteAndGetIndexFile(Input input, Record record, string? checkDirectory) {
string? result;
if (string.IsNullOrEmpty(checkDirectory) || input.AfterEpochTotalMilliseconds is null || input.DestinationDirectories.Count == 0)
if (string.IsNullOrEmpty(checkDirectory) || input.AfterEpochTotalMilliseconds is null || input.DestinationDirectories.Count == 0) {
result = null;
else
{
if (!input.DestinationDirectories.Any(checkDirectory.Contains))
} else {
if (!input.DestinationDirectories.Any(checkDirectory.Contains)) {
result = null;
else
{
if (record.LineNumber.H1 is null)
} else {
if (record.LineNumber.H1 is null) {
result = null;
else
{
} else {
string segment = Path.GetFileName(checkDirectory);
string h1 = record.LineNumber.Lines[record.LineNumber.H1.Value];
DateTime utcEpochDateTime = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long utcEpochTotalMilliseconds = (long)Math.Floor(DateTime.UtcNow.Subtract(utcEpochDateTime).TotalMilliseconds);
if (!long.TryParse(segment, out long check) || check < input.AfterEpochTotalMilliseconds || check > utcEpochTotalMilliseconds)
if (!long.TryParse(segment, out long check) || check < input.AfterEpochTotalMilliseconds || check > utcEpochTotalMilliseconds) {
result = null;
else
{
} else {
ReadOnlyCollection<H1AndParamCase> h1ParamCaseCollection = GetH1ParamCaseCollection(input, record.LineNumber.Lines);
if (h1ParamCaseCollection.Count == 0)
if (h1ParamCaseCollection.Count == 0) {
result = null;
else
{
} else {
DateTime dateTime = utcEpochDateTime.AddMilliseconds(check).ToLocalTime();
string seasonName = GetSeasonName(dateTime.DayOfYear);
ReadOnlyCollection<string> directoryNames = HelperDirectory.GetDirectoryNames(checkDirectory);
if (!directoryNames.Contains(dateTime.Year.ToString()) || !directoryNames.Contains($"{dateTime.Year}-{seasonName}") || !directoryNames.Contains(check.ToString()))
if (!directoryNames.Contains(dateTime.Year.ToString()) || !directoryNames.Contains($"{dateTime.Year}-{seasonName}") || !directoryNames.Contains(check.ToString())) {
result = null;
else
} else {
result = WriteAndGetIndexFile(h1, checkDirectory, h1ParamCaseCollection);
}
}
}
}
@ -481,53 +476,50 @@ internal static partial class Helper20240623
return result;
}
private static bool FileWrite(long ticks, Record record, List<string> newLines, double percent)
{
private static bool FileWrite(long ticks, Record record, List<string> newLines, double percent) {
bool result = false;
if (record.StopLine is not null && record.SubTasksLine is not null)
{
if (record.StopLine is not null && record.SubTasksLine is not null) {
string contents;
string progressLine;
List<string> resultLines;
if (record.FileInfo.LastWriteTime.Ticks <= ticks)
if (record.FileInfo.LastWriteTime.Ticks <= ticks) {
resultLines = record.LineNumber.Lines.ToList();
else
} else {
resultLines = File.ReadAllLines(record.FileInfo.FullName).ToList();
if (record.LineNumber.FrontMatterYamlEnd is not null)
{
}
if (record.LineNumber.FrontMatterYamlEnd is not null) {
progressLine = $"progress: {percent}";
if (record.LineNumber.Progress is not null)
if (record.LineNumber.Progress is not null) {
resultLines[record.LineNumber.Progress.Value] = progressLine;
else
{
} else {
resultLines.Insert(record.LineNumber.FrontMatterYamlEnd.Value, progressLine);
contents = string.Join(Environment.NewLine, resultLines);
FileWriteAllText(record.FileInfo.FullName, contents);
result = true;
}
if (!result && record.LineNumber.Completed is null && percent > 99.9)
{
if (!result && record.LineNumber.Completed is null && percent > 99.9) {
resultLines.Insert(record.LineNumber.FrontMatterYamlEnd.Value, $"completed: {DateTime.Now:yyyy-MM-dd}");
contents = string.Join(Environment.NewLine, resultLines);
FileWriteAllText(record.FileInfo.FullName, contents);
result = true;
}
if (!result && record.LineNumber.Completed is not null && percent < 99.9)
{
if (!result && record.LineNumber.Completed is not null && percent < 99.9) {
resultLines.RemoveAt(record.LineNumber.Completed.Value);
contents = string.Join(Environment.NewLine, resultLines);
FileWriteAllText(record.FileInfo.FullName, contents);
result = true;
}
}
if (!result)
{
for (int i = record.StopLine.Value - 1; i > record.SubTasksLine.Value + 1; i--)
if (!result) {
for (int i = record.StopLine.Value - 1; i > record.SubTasksLine.Value + 1; i--) {
resultLines.RemoveAt(i);
if (record.StopLine.Value == record.LineNumber.Lines.Count && resultLines[^1].Length == 0)
}
if (record.StopLine.Value == record.LineNumber.Lines.Count && resultLines[^1].Length == 0) {
resultLines.RemoveAt(resultLines.Count - 1);
for (int i = 0; i < newLines.Count; i++)
}
for (int i = 0; i < newLines.Count; i++) {
resultLines.Insert(record.SubTasksLine.Value + 1 + i, newLines[i]);
}
resultLines.Insert(record.SubTasksLine.Value + 1, string.Empty);
contents = string.Join(Environment.NewLine, resultLines);
FileWriteAllText(record.FileInfo.FullName, contents);
@ -536,102 +528,95 @@ internal static partial class Helper20240623
return result;
}
private static string? GetInferredCheckDirectory(string directory)
{
private static string? GetInferredCheckDirectory(string directory) {
string? result = null;
List<string> directoryNames = [];
DirectoryInfo directoryInfo;
string? checkDirectory = directory;
directoryNames.Add(Path.GetFileName(checkDirectory));
string pathRoot = Path.GetPathRoot(directory) ?? throw new Exception();
for (int i = 0; i < directory.Length; i++)
{
for (int i = 0; i < directory.Length; i++) {
checkDirectory = Path.GetDirectoryName(checkDirectory);
if (string.IsNullOrEmpty(checkDirectory) || checkDirectory == pathRoot)
if (string.IsNullOrEmpty(checkDirectory) || checkDirectory == pathRoot) {
break;
}
directoryInfo = new(checkDirectory);
if (!directoryInfo.Exists)
if (!directoryInfo.Exists) {
directoryNames.Add(directoryInfo.Name);
else
{
} else {
directoryNames.Reverse();
result = string.IsNullOrEmpty(directoryInfo.LinkTarget) ? checkDirectory : directoryInfo.LinkTarget;
for (int j = 0; j < directoryNames.Count; j++)
for (int j = 0; j < directoryNames.Count; j++) {
result = Path.GetDirectoryName(result) ?? throw new Exception();
foreach (string directoryName in directoryNames)
}
foreach (string directoryName in directoryNames) {
result = Path.Combine(result, directoryName);
}
break;
}
}
return result;
}
private static void UpdateFileAndStartNewProcess(ILogger<Worker> logger, Input input, Record record, string inferredCheckDirectory)
{
if (record.CodeInsidersLine is null)
private static void UpdateFileAndStartNewProcess(ILogger<Worker> logger, Input input, Record record, string inferredCheckDirectory) {
if (record.CodeInsidersLine is null) {
throw new Exception();
}
List<string> lines = record.LineNumber.Lines.ToList();
lines[record.CodeInsidersLine.Value] = $"{input.CodeInsiders}{inferredCheckDirectory})";
string text = string.Join(Environment.NewLine, lines);
File.WriteAllText(record.FileInfo.FullName, text);
record.FileInfo.Refresh();
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", "Microsoft VS Code Insiders", "Code - Insiders.exe");
try
{ _ = Process.Start(file, $"\"{inferredCheckDirectory}\""); }
catch (Exception) { logger.LogWarning("Failed to start code-insiders!"); }
try { _ = Process.Start(file, $"\"{inferredCheckDirectory}\""); } catch (Exception) { logger.LogWarning("Failed to start code-insiders!"); }
}
private static FileInfo GetIndexFileInfo(ILogger<Worker> logger, Input input, Record record)
{
private static FileInfo GetIndexFileInfo(ILogger<Worker> logger, Input input, Record record) {
FileInfo result;
string? indexFile;
List<string> results;
if (record.CodeInsidersLine is null)
if (record.CodeInsidersLine is null) {
throw new Exception();
}
string codeInsidersLine = record.LineNumber.Lines[record.CodeInsidersLine.Value];
string raw = codeInsidersLine[input.CodeInsiders.Length..^1];
string checkDirectory = $"{raw[..2].ToUpper()}{raw[2..]}";
if (!Directory.Exists(checkDirectory))
{
if (input.DestinationDirectories.Count > 0 && input.DestinationDirectories.Any(checkDirectory.Contains))
{
if (!Directory.Exists(checkDirectory)) {
if (input.DestinationDirectories.Count > 0 && input.DestinationDirectories.Any(checkDirectory.Contains)) {
string? inferredCheckDirectory = GetInferredCheckDirectory(checkDirectory);
if (!string.IsNullOrEmpty(inferredCheckDirectory))
{
if (!string.IsNullOrEmpty(inferredCheckDirectory)) {
checkDirectory = inferredCheckDirectory;
_ = Directory.CreateDirectory(inferredCheckDirectory);
UpdateFileAndStartNewProcess(logger, input, record, inferredCheckDirectory);
}
}
}
if (!Directory.Exists(checkDirectory))
if (!Directory.Exists(checkDirectory)) {
results = [];
else
{
} else {
results = Directory.GetFiles(checkDirectory, input.IndexFile, SearchOption.AllDirectories).ToList();
if (results.Count != 1)
{
for (int i = results.Count - 1; i > -1; i--)
{
if (!results[i].Contains(input.DirectoryFilter, StringComparison.CurrentCultureIgnoreCase))
if (results.Count != 1) {
for (int i = results.Count - 1; i > -1; i--) {
if (!results[i].Contains(input.DirectoryFilter, StringComparison.CurrentCultureIgnoreCase)) {
results.RemoveAt(i);
}
}
}
if (results.Count == 0)
{
if (results.Count == 0) {
indexFile = MaybeWriteAndGetIndexFile(input, record, checkDirectory);
if (!string.IsNullOrEmpty(indexFile))
if (!string.IsNullOrEmpty(indexFile)) {
results.Add(indexFile);
else
} else {
logger.LogInformation("<{checkDirectory}>", checkDirectory);
}
}
}
result = results.Count == 0 ? new(Path.Combine(checkDirectory, input.IndexFile)) : new(results[0]);
return result;
}
internal static void UpdateSubTasksInMarkdownFiles(ILogger<Worker> logger, List<string> args)
{
internal static void UpdateSubTasksInMarkdownFiles(ILogger<Worker> logger, List<string> args) {
bool reload;
int allCount;
int lineCheck;
@ -650,26 +635,24 @@ internal static partial class Helper20240623
string fileNameWithoutExtension;
long ticks = DateTime.Now.Ticks;
ReadOnlyCollection<SubTaskLine> subTaskLines;
for (int z = 0; z < 9; z++)
{
for (int z = 0; z < 9; z++) {
records = GetRecords(input);
foreach (Record record in from l in records orderby l.SubTasksLine is null, l.CodeInsidersLine is null select l)
{
if (record.SubTasksLine is null)
foreach (Record record in from l in records orderby l.SubTasksLine is null, l.CodeInsidersLine is null select l) {
if (record.SubTasksLine is null) {
continue;
}
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(record.FileInfo.FullName);
if (record.CodeInsidersLine is not null)
if (record.CodeInsidersLine is not null) {
logger.LogInformation("<{file}> has [{subTasks}]", fileNameWithoutExtension, input.SubTasks);
else
{
} else {
logger.LogWarning("<{file}> has [{subTasks}] but doesn't have [{codeInsiders}]!", fileNameWithoutExtension, input.SubTasks, input.CodeInsiders);
continue;
}
if (record.StopLine is null)
if (record.StopLine is null) {
continue;
}
fileInfo = GetIndexFileInfo(logger, input, record);
if (!fileInfo.Exists)
{
if (!fileInfo.Exists) {
logger.LogError("<{checkDirectory}> doesn't have a [{indexFile}]", fileInfo.DirectoryName, input.IndexFile);
continue;
}
@ -677,18 +660,17 @@ internal static partial class Helper20240623
checkDirectory = fileInfo.DirectoryName;
lineNumber = HelperMarkdown.GetLineNumbers(fileInfo);
subTaskLines = GetSubTaskLines(input, fileInfo, lineNumber);
if (subTaskLines.Count == 0)
if (subTaskLines.Count == 0) {
continue;
}
lineCheck = 0;
for (int i = record.SubTasksLine.Value + 1; i < record.StopLine.Value - 1; i++)
for (int i = record.SubTasksLine.Value + 1; i < record.StopLine.Value - 1; i++) {
oldLines.Add(record.LineNumber.Lines[i]);
if (subTaskLines.Count == 0)
{
}
if (subTaskLines.Count == 0) {
percent = 0;
replace = "0";
}
else
{
} else {
allCount = (from l in subTaskLines where l.Line is not null && l.Line.Value == 0 select 1).Count();
completedCount = (from l in subTaskLines where l.Line is not null && l.Line.Value == 0 && l.Completed select 1).Count();
startedCount = (from l in subTaskLines where l.Line is not null && l.Line.Value == 0 && l.Started && !l.Completed select 1).Count();
@ -696,38 +678,40 @@ internal static partial class Helper20240623
// newLines.Insert(0, $"- [{done}] Sub-tasks {doneCount} of {allCount} [{percent * 100}%]");
replace = $"{allCount} » {startedCount} ✓ {completedCount} {Math.Floor(percent * 100)}%".Replace(" ✓ 0 0%", string.Empty).Replace(" 100%", string.Empty).Replace(" » 0", string.Empty);
}
if (subTaskLines.Any(l => l.Ticks is null))
if (subTaskLines.Any(l => l.Ticks is null)) {
newLines = (from l in subTaskLines
select l.Text).ToList();
else
{
} else {
newLines = (from l in subTaskLines
orderby l.Completed descending, l.Started descending, l.Ticks, l.Line
select l.Text.Replace($"{l.Ticks}", replace)).ToList();
}
if (newLines.Count == oldLines.Count)
{
for (int i = 0; i < newLines.Count; i++)
{
if (newLines[i] != record.LineNumber.Lines[record.SubTasksLine.Value + 1 + i])
if (newLines.Count == oldLines.Count) {
for (int i = 0; i < newLines.Count; i++) {
if (newLines[i] != record.LineNumber.Lines[record.SubTasksLine.Value + 1 + i]) {
continue;
}
lineCheck++;
}
if (lineCheck == newLines.Count)
if (lineCheck == newLines.Count) {
continue;
}
}
if (string.IsNullOrEmpty(checkDirectory))
if (string.IsNullOrEmpty(checkDirectory)) {
continue;
}
checkDirectory = Path.Combine(checkDirectory, DateTime.Now.Ticks.ToString());
_ = Directory.CreateDirectory(checkDirectory);
Thread.Sleep(500);
Directory.Delete(checkDirectory);
reload = FileWrite(ticks, record, newLines, percent);
if (!reloadAny && reload)
if (!reloadAny && reload) {
reloadAny = true;
}
}
if (!reloadAny)
if (!reloadAny) {
break;
}
}
}