Add functionality to determine if files are new

This currently relies on the last modified date and a dotfile.
I may change this later to use a hash to detect changes
however this method seems good enough.
This commit is contained in:
Robert Morrison 2022-06-21 22:39:22 +01:00
parent 2657ad16ea
commit 8abd5b5990
Signed by: robert
GPG Key ID: 73E012EB3F4EC696

View File

@ -21,7 +21,7 @@ using Serilog.Events;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Diagnostics; using System.Diagnostics;
// using System.Text.Json; using System.Text.Json;
namespace csSiteGen; namespace csSiteGen;
@ -127,6 +127,42 @@ class Program
} }
Log.Debug("Files matching conversiontypes: {@conversionType} found {count} \n {files} ",_conversionTypes,ConvertableInputFiles.Count(),ConvertableInputFiles); Log.Debug("Files matching conversiontypes: {@conversionType} found {count} \n {files} ",_conversionTypes,ConvertableInputFiles.Count(),ConvertableInputFiles);
Dictionary<string,DateTime>? metadata = null;
if (File.Exists($"{_inputDirectory}/.files"))
{
Log.Information("Loading metadata for {src}",_inputDirectory);
metadata = new();
string metaJSON = File.ReadAllText($"{_inputDirectory}/.files");
try
{
Type metadataType = metadata.GetType();
metadata = JsonSerializer.Deserialize<Dictionary<string,DateTime>>(metaJSON);
}
catch (JsonException e)
{
Log.Debug(e,"Cannot deserialize .files Json data");
Log.Debug("As this is possibly caused by an internal interface change the file will be deleted");
File.Delete($"{_inputDirectory}/.files");
}
}
if (metadata is not null)
{
// Use metadata to determine if files have been updated
// A shadow list is used here so we can iterate and modify at the same time
List<string> shadow = new List<string>(ConvertableInputFiles);
foreach (string file in shadow)
{
if (metadata[file] == new FileInfo(file).LastWriteTimeUtc)
{
Log.Debug("File {file} has not been updated since last run. ignoring",file);
ConvertableInputFiles.Remove(file);
}
}
}
Log.Information("{count} Files need converting",ConvertableInputFiles.Count());
Log.Debug("Files: {files}",ConvertableInputFiles);
Log.CloseAndFlush(); Log.CloseAndFlush();
return 0; return 0;