public static void SyncFolder <T>(ref bool?replace, ref bool?delete, string folder, List <T> should, Func <T, string> fileName, Func <T, XDocument> toXML) { if (should.Any() && !Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var deleteLocal = delete; var replaceLocal = replace; SafeConsole.WriteLineColor(ConsoleColor.Gray, "Exporting to " + folder); Synchronizer.Synchronize( newDictionary: should.ToDictionary(fileName), oldDictionary: Directory.GetFiles(folder).ToDictionary(a => Path.GetFileName(a)), createNew: (fileName, entity) => { toXML(entity).Save(Path.Combine(folder, fileName)); SafeConsole.WriteLineColor(ConsoleColor.Green, " Created " + fileName); }, removeOld: (fileName, fullName) => { if (SafeConsole.Ask(ref deleteLocal, "Delete {0}?".FormatWith(fileName))) { File.Delete(fullName); SafeConsole.WriteLineColor(ConsoleColor.Red, " Deleted " + fileName); } }, merge: (fileName, entity, fullName) => { var xml = toXML(entity); var newBytes = new MemoryStream().Do(ms => xml.Save(ms)).ToArray(); var oldBytes = File.ReadAllBytes(fullName); if (!MemComparer.Equals(newBytes, oldBytes)) { if (SafeConsole.Ask(ref replaceLocal, " Override {0}?".FormatWith(fileName))) { xml.Save(Path.Combine(folder, fileName)); SafeConsole.WriteLineColor(ConsoleColor.Yellow, " Overriden " + fileName); } } else { SafeConsole.WriteLineColor(ConsoleColor.DarkGray, " Identical " + fileName); } }); delete = deleteLocal; replace = replaceLocal; }
private bool IsEquals(object newValue, object?oldValue) { if (newValue is byte[] && oldValue is byte[]) { return(MemComparer.Equals((byte[])newValue, (byte[])oldValue)); } if (newValue is DateTime && oldValue is DateTime) { return(Math.Abs(((DateTime)newValue).Subtract((DateTime)oldValue).TotalMilliseconds) < 10); //Json dates get rounded } if (newValue is DateTimeOffset && oldValue is DateTimeOffset) { return(Math.Abs(((DateTimeOffset)newValue).Subtract((DateTimeOffset)oldValue).TotalMilliseconds) < 10); //Json dates get rounded } return(object.Equals(newValue, oldValue)); }