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:
parent
2657ad16ea
commit
8abd5b5990
38
Program.cs
38
Program.cs
|
|
@ -21,7 +21,7 @@ using Serilog.Events;
|
|||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Diagnostics;
|
||||
// using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace csSiteGen;
|
||||
|
||||
|
|
@ -127,6 +127,42 @@ class Program
|
|||
}
|
||||
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();
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user