83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
using File_Folder_Helper.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace File_Folder_Helper.Helpers;
|
|
|
|
internal static class HelperKanbanMetadata
|
|
{
|
|
|
|
private static List<(int, int, string, FileInfo)> GetCollectionFromIndex(string sourceDirectory, string[] lines)
|
|
{
|
|
List<(int, int, string, FileInfo)> results = new();
|
|
string line;
|
|
FileInfo fileInfo;
|
|
string[] segments;
|
|
int groupCount = 0;
|
|
string? group = null;
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
line = lines[i];
|
|
if (line.Length < 4)
|
|
continue;
|
|
if (line[..3] == "## ")
|
|
{
|
|
group = line[3..];
|
|
groupCount += 1;
|
|
continue;
|
|
}
|
|
if (group is null || line[..3] != "- [" || line[^1] != ')')
|
|
continue;
|
|
segments = line.Split("](");
|
|
if (segments.Length != 2)
|
|
continue;
|
|
fileInfo = new(Path.Combine(sourceDirectory, segments[1][..^1]));
|
|
if (!fileInfo.Exists)
|
|
continue;
|
|
results.Add((groupCount, i, group, fileInfo));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
internal static void SetMetadata(ILogger log, AppSettings appSettings, string sourceDirectory)
|
|
{
|
|
string statusLine;
|
|
List<string> lines;
|
|
LineNumber lineNumber;
|
|
if (log is null)
|
|
throw new NullReferenceException();
|
|
string fullPath = Path.GetFullPath(sourceDirectory);
|
|
if (!Directory.Exists(fullPath))
|
|
_ = Directory.CreateDirectory(fullPath);
|
|
List<(MarkdownFile, string[])> collection;
|
|
collection = HelperMarkdown.GetCollection(appSettings, HelperMarkdown.GetFiles(appSettings, fullPath));
|
|
string indexFile = Path.Combine(fullPath, "index.md");
|
|
if (File.Exists(indexFile))
|
|
{
|
|
string[] indexFileLines = File.ReadAllLines(indexFile);
|
|
List<(int, int, string, FileInfo)> collectionFromIndex = GetCollectionFromIndex(sourceDirectory, indexFileLines);
|
|
foreach ((int groupCount, int itemLineNumber, string group, FileInfo fileInfo) in collectionFromIndex)
|
|
{
|
|
if (itemLineNumber == 0)
|
|
throw new NotSupportedException();
|
|
(lines, lineNumber) = HelperMarkdown.GetStatusAndFrontMatterYamlEndLineNumbers(fileInfo);
|
|
if (!lines.Any())
|
|
continue;
|
|
statusLine = $"status: \"{groupCount}-{group}\"";
|
|
indexFileLines[itemLineNumber] = $"{fileInfo.LastWriteTime.Ticks}~~~{indexFileLines[itemLineNumber]}";
|
|
if (lineNumber.FrontMatterYamlEnd is null)
|
|
throw new NotSupportedException($"{nameof(SetMetadata)} must be executed first!");
|
|
if (lineNumber.Status is null)
|
|
lines.Insert(lineNumber.FrontMatterYamlEnd.Value, statusLine);
|
|
else
|
|
{
|
|
if (lines[lineNumber.Status.Value] == statusLine)
|
|
continue;
|
|
lines[lineNumber.Status.Value] = statusLine;
|
|
}
|
|
File.WriteAllLines(fileInfo.FullName, lines);
|
|
}
|
|
File.WriteAllLines(indexFile, indexFileLines);
|
|
}
|
|
}
|
|
|
|
} |