Update Subtasks In Markdown Files

Better ISO support

Only reviewing Files when comparing

Extracted sections from UpdateSubTasksInMarkdownFiles
This commit is contained in:
2024-12-26 14:14:31 -07:00
parent 2361796bbf
commit fb9289a572
13 changed files with 1208 additions and 395 deletions

View File

@ -133,31 +133,30 @@ internal static partial class HelperKanbanMetadata
List<string> results = [kanbanIndexH1, string.Empty];
string h1;
TimeSpan timeSpan;
List<string> lines;
LineNumber lineNumber;
Record[] sorted = (from l in records orderby l.GroupCount, l.FileInfo.LastWriteTime descending select l).ToArray();
foreach (Record record in sorted)
{
if (record.ItemLineNumber == 0)
throw new NotSupportedException();
(lines, lineNumber) = HelperMarkdown.GetStatusAndFrontMatterYamlEndLineNumbers(record.FileInfo);
if (lines.Count == 0)
lineNumber = HelperMarkdown.GetLineNumbers(record.FileInfo);
if (lineNumber.Lines.Count == 0)
continue;
timeSpan = new(record.FileInfo.LastWriteTime.Ticks - record.FileInfo.CreationTime.Ticks);
h1 = lineNumber.H1 is null ? Path.GetFileNameWithoutExtension(record.FileInfo.Name) : lines[lineNumber.H1.Value];
h1 = lineNumber.H1 is null ? Path.GetFileNameWithoutExtension(record.FileInfo.Name) : lineNumber.Lines[lineNumber.H1.Value];
results.Add($"#{h1}");
results.Add(string.Empty);
results.Add("```yaml");
results.Add($"CreationTime: {record.FileInfo.CreationTime:yyyy-MM-dd}");
results.Add($"LastWriteTime: {record.FileInfo.LastWriteTime:yyyy-MM-dd}");
results.Add($"TotalDays: {Math.Round(timeSpan.TotalDays, 2)}");
if (lineNumber.FrontMatterYamlEnd is not null && lines.Count >= lineNumber.FrontMatterYamlEnd.Value)
if (lineNumber.FrontMatterYamlEnd is not null && lineNumber.Lines.Count >= lineNumber.FrontMatterYamlEnd.Value)
{
for (int i = 0; i < lineNumber.FrontMatterYamlEnd; i++)
{
if (lines[i] == "---")
if (lineNumber.Lines[i] == "---")
continue;
results.Add(lines[i]);
results.Add(lineNumber.Lines[i]);
}
}
results.Add($"status: {record.GroupCount}-{record.Group}");
@ -175,7 +174,7 @@ internal static partial class HelperKanbanMetadata
File.WriteAllText(file, string.Join(Environment.NewLine, results));
}
internal static void SetMetadata(string sourceDirectory, ReadOnlyCollection<string> kanbanIndexFileLines, LineNumber kanbanIndexFileLineNumber, ReadOnlyCollection<string> gitOthersModifiedAndDeletedExcludingStandardFiles)
internal static void SetMetadata(string sourceDirectory, LineNumber kanbanIndexFileLineNumber, ReadOnlyCollection<string> gitOthersModifiedAndDeletedExcludingStandardFiles)
{
bool? match;
bool gitCheck;
@ -184,23 +183,24 @@ internal static partial class HelperKanbanMetadata
List<string> lines;
LineNumber lineNumber;
string? directory = Path.GetDirectoryName(sourceDirectory);
List<Record> records = GetCollectionFromIndex(sourceDirectory, kanbanIndexFileLines);
List<Record> records = GetCollectionFromIndex(sourceDirectory, kanbanIndexFileLineNumber.Lines);
if (directory is not null && kanbanIndexFileLineNumber.H1 is not null)
{
string checkDirectory = Path.Combine(directory, ".vscode", "helper");
if (Directory.Exists(checkDirectory))
{
WriteKanbanBoardFile(checkDirectory, records, kanbanIndexFileLines[kanbanIndexFileLineNumber.H1.Value]);
WriteKanbanBoardYmlView(checkDirectory, records, kanbanIndexFileLines[kanbanIndexFileLineNumber.H1.Value]);
WriteKanbanBoardFile(checkDirectory, records, kanbanIndexFileLineNumber.Lines[kanbanIndexFileLineNumber.H1.Value]);
WriteKanbanBoardYmlView(checkDirectory, records, kanbanIndexFileLineNumber.Lines[kanbanIndexFileLineNumber.H1.Value]);
}
}
foreach (Record record in records)
{
if (record.ItemLineNumber == 0)
throw new NotSupportedException();
(lines, lineNumber) = HelperMarkdown.GetStatusAndFrontMatterYamlEndLineNumbers(record.FileInfo);
if (lines.Count == 0)
lineNumber = HelperMarkdown.GetLineNumbers(record.FileInfo);
if (lineNumber.Lines.Count == 0)
continue;
lines = lineNumber.Lines.ToList();
statusLine = $"status: {record.GroupCount}-{record.Group}";
paramCase = lineNumber.H1 is null ? null : GetParamCase(lines[lineNumber.H1.Value]);
match = lineNumber.H1 is null || paramCase is null ? null : Path.GetFileNameWithoutExtension(record.FileInfo.Name) == paramCase;
@ -235,8 +235,8 @@ internal static partial class HelperKanbanMetadata
else
{
FileInfo fileInfo = new(indexFile);
(List<string> lines, LineNumber lineNumber) = HelperMarkdown.GetStatusAndFrontMatterYamlEndLineNumbers(fileInfo);
SetMetadata(fullPath, new(lines), lineNumber, gitOthersModifiedAndDeletedExcludingStandardFiles: new([]));
LineNumber lineNumber = HelperMarkdown.GetLineNumbers(fileInfo);
SetMetadata(fullPath, lineNumber, gitOthersModifiedAndDeletedExcludingStandardFiles: new([]));
}
}

View File

@ -193,7 +193,7 @@ internal static partial class HelperMarkdown
return result;
}
private static ReadOnlyCollection<string> GetFromMatterYamlLines(List<string> lines, LineNumber lineNumber)
private static ReadOnlyCollection<string> GetFromMatterYamlLines(ReadOnlyCollection<string> lines, LineNumber lineNumber)
{
List<string> results = [];
if (lineNumber.FrontMatterYamlEnd is not null && lines.Count >= lineNumber.FrontMatterYamlEnd.Value)
@ -307,7 +307,7 @@ internal static partial class HelperMarkdown
return new(results);
}
internal static (List<string>, LineNumber) GetStatusAndFrontMatterYamlEndLineNumbers(FileInfo fileInfo)
internal static LineNumber GetLineNumbers(FileInfo fileInfo)
{
string line;
int? h1LineNumber = null;
@ -315,6 +315,8 @@ internal static partial class HelperMarkdown
int? statusLineNumber = null;
int? createdLineNumber = null;
int? updatedLineNumber = null;
int? progressLineNumber = null;
int? completedLineNumber = null;
int? frontMatterYamlEndLineNumber = null;
Encoding? encoding = GetEncoding(fileInfo.FullName) ?? Encoding.Default;
string[] lines = File.ReadAllLines(fileInfo.FullName, encoding);
@ -350,6 +352,16 @@ internal static partial class HelperMarkdown
updatedLineNumber = i;
continue;
}
if (line.Length > 10 && line[..10] == "progress: ")
{
progressLineNumber = i;
continue;
}
if (line.Length > 11 && line[..11] == "completed: ")
{
completedLineNumber = i;
continue;
}
if (h1LineNumber is null && line.Length > 2 && line[0] == '#' && line[1] == ' ')
{
h1LineNumber = i;
@ -357,12 +369,15 @@ internal static partial class HelperMarkdown
}
}
LineNumber lineNumber = new(createdLineNumber,
completedLineNumber,
h1LineNumber,
frontMatterYamlEndLineNumber,
lines.AsReadOnly(),
progressLineNumber,
statusLineNumber,
typeLineNumber,
updatedLineNumber);
return (lines.ToList(), lineNumber);
return lineNumber;
}
private static Dictionary<string, object> GetFromMatterYaml(ReadOnlyCollection<string> frontMatterYamlLines)
@ -387,7 +402,7 @@ internal static partial class HelperMarkdown
return results;
}
private static ReadOnlyDictionary<string, object> GetFromMatterYaml(List<string> lines, LineNumber lineNumber)
private static ReadOnlyDictionary<string, object> GetFromMatterYaml(ReadOnlyCollection<string> lines, LineNumber lineNumber)
{
Dictionary<string, object> results = [];
#pragma warning disable IL3050
@ -660,12 +675,12 @@ internal static partial class HelperMarkdown
string key;
string type;
bool isKanbanIndex;
List<string> lines;
bool isWithinSource;
bool isKanbanMarkdown;
LineNumber lineNumber;
MarkdownFile markdownFile;
string fileNameWithoutExtension;
ReadOnlyCollection<string> lines;
ReadOnlyDictionary<string, object> frontMatterYaml;
bool isGitOthersModifiedAndDeletedExcludingStandard;
ReadOnlyCollection<FileInfo> files = GetFiles(appSettings, input);
@ -678,7 +693,8 @@ internal static partial class HelperMarkdown
isGitOthersModifiedAndDeletedExcludingStandard = gitOthersModifiedAndDeletedExcludingStandardFiles.Contains(fileInfo.FullName);
if (!isWithinSource && results.ContainsKey(key))
continue;
(lines, lineNumber) = GetStatusAndFrontMatterYamlEndLineNumbers(fileInfo);
lineNumber = GetLineNumbers(fileInfo);
lines = lineNumber.Lines;
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
h1 = fileNameWithoutExtension.ToLower().Replace("%20", "-").Replace(' ', '-');
frontMatterYaml = GetFromMatterYaml(lines, lineNumber);
@ -690,7 +706,7 @@ internal static partial class HelperMarkdown
continue;
type = appSettings.DefaultNoteType;
File.WriteAllLines(fileInfo.FullName, ["---", $"type: {type}\"", "---", string.Empty, $"# {h1}"]);
lines = File.ReadAllLines(fileInfo.FullName).ToList();
lines = File.ReadAllLines(fileInfo.FullName).AsReadOnly();
}
isKanbanMarkdown = fileInfo.Name.EndsWith(".knb.md");
isKanbanIndex = fileNameWithoutExtension == "index" && type.StartsWith("kanb", StringComparison.OrdinalIgnoreCase);
@ -1042,7 +1058,7 @@ internal static partial class HelperMarkdown
}
}
private static (string type, string h1) GetTypeAndH1(AppSettings appSettings, string h1, List<string> lines, LineNumber lineNumber)
private static (string type, string h1) GetTypeAndH1(AppSettings appSettings, string h1, ReadOnlyCollection<string> lines, LineNumber lineNumber)
{
string type = lineNumber.Type is null ? appSettings.DefaultNoteType : lines[lineNumber.Type.Value][5..].Trim().Trim('"');
string h1FromFile = lineNumber.H1 is null ? h1 : lines[lineNumber.H1.Value][2..];
@ -1080,7 +1096,7 @@ internal static partial class HelperMarkdown
createdLine = $"created: {creationDateTime.ToUniversalTime():yyyy-MM-ddTHH:mm:ss.fffZ}";
updatedLine = $"updated: {markdownFile.LastWriteDateTime.ToUniversalTime():yyyy-MM-ddTHH:mm:ss.fffZ}";
if (markdownFile.IsKanbanIndex)
HelperKanbanMetadata.SetMetadata(markdownFile.Directory, new(lines), markdownFile.LineNumber, gitOthersModifiedAndDeletedExcludingStandardFiles);
HelperKanbanMetadata.SetMetadata(markdownFile.Directory, markdownFile.LineNumber, gitOthersModifiedAndDeletedExcludingStandardFiles);
if (markdownFile.LineNumber.FrontMatterYamlEnd is null)
{
if (markdownFile.LineNumber.H1 is not null)