private void ProcessSecondPasses(IDictionary <string, TObject> updates, List <uSyncAction> actions, HandlerSettings config, SyncUpdateCallback callback = null) { List <TObject> updatedItems = new List <TObject>(); foreach (var item in updates.Select((update, Index) => new { update, Index })) { callback?.Invoke($"Second Pass {Path.GetFileName(item.update.Key)}", item.Index, updates.Count); var attempt = ImportSecondPass(item.update.Key, item.update.Value, config, callback); if (attempt.Success) { // if the second attempt has a message on it, add it to the first attempt. if (!string.IsNullOrWhiteSpace(attempt.Message)) { if (actions.Any(x => x.FileName == item.update.Key)) { var action = actions.FirstOrDefault(x => x.FileName == item.update.Key); actions.Remove(action); action.Message += attempt.Message; actions.Add(action); } } if (attempt.Change > ChangeType.NoChange) { updatedItems.Add(attempt.Item); } } else { // the second attempt failed - update the action. if (actions.Any(x => x.FileName == item.update.Key)) { var action = actions.FirstOrDefault(x => x.FileName == item.update.Key); actions.Remove(action); action.Success = attempt.Success; action.Message = $"Second Pass Fail: {attempt.Message}"; action.Exception = attempt.Exception; actions.Add(action); } } } if (config.BatchSave) { callback?.Invoke($"Saving {updatedItems.Count} Second Pass Items", 2, 3); serializer.Save(updatedItems); } }
protected virtual IEnumerable <uSyncAction> ImportFolder(string folder, HandlerSettings config, Dictionary <string, TObject> updates, bool force, SyncUpdateCallback callback) { List <uSyncAction> actions = new List <uSyncAction>(); var files = syncFileService.GetFiles(folder, "*.config"); var flags = SerializerFlags.None; if (force) { flags |= SerializerFlags.Force; } if (config.BatchSave) { flags |= SerializerFlags.DoNotSave; } int count = 0; int total = files.Count(); foreach (string file in files) { count++; callback?.Invoke($"Importing {Path.GetFileNameWithoutExtension(file)}", count, total); var attempt = Import(file, config, flags); if (attempt.Success && attempt.Item != null) { updates.Add(file, attempt.Item); } var action = uSyncActionHelper <TObject> .SetAction(attempt, file, IsTwoPass); if (attempt.Details != null && attempt.Details.Any()) { action.Details = attempt.Details; } actions.Add(action); } // bulk save .. if (flags.HasFlag(SerializerFlags.DoNotSave) && updates.Any()) { callback?.Invoke($"Saving {updates.Count()} changes", 1, 1); serializer.Save(updates.Select(x => x.Value)); } var folders = syncFileService.GetDirectories(folder); foreach (var children in folders) { actions.AddRange(ImportFolder(children, config, updates, force, callback)); } callback?.Invoke("", 1, 1); return(actions); }