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); }
private IEnumerable <SyncHistoryView> GetHistory(string folder) { var histories = new List <SyncHistoryView>(); foreach (var historyFile in syncFileService.GetFiles(folder, "*.history")) { var content = syncFileService.LoadContent(historyFile); if (!string.IsNullOrWhiteSpace(content)) { var history = JsonConvert.DeserializeObject <SyncHistoryView>(content); if (history != null) { histories.Add(history); } } } foreach (var subFolder in syncFileService.GetDirectories(folder)) { histories.AddRange(GetHistory(subFolder)); } return(histories.OrderByDescending(x => x.When)); }
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 = GetImportFiles(folder); var flags = SerializerFlags.None; if (force) { flags |= SerializerFlags.Force; } if (config.BatchSave) { flags |= SerializerFlags.DoNotSave; } var cleanMarkers = new List <string>(); 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) { if (attempt.Change == ChangeType.Clean) { cleanMarkers.Add(file); } else if (attempt.Item != null) { updates.Add(file, attempt.Item); } } var action = uSyncActionHelper <TObject> .SetAction(attempt, file, this.Alias, IsTwoPass); if (attempt.Details != null && attempt.Details.Any()) { action.Details = attempt.Details; } if (attempt.Change != ChangeType.Clean) { 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)); } if (actions.All(x => x.Success) && cleanMarkers.Count > 0) { // this is just extra messaging, given how quickly the next message will be sent. // callback?.Invoke("Cleaning Folders", 1, cleanMarkers.Count); foreach (var item in cleanMarkers.Select((filePath, Index) => new { filePath, Index })) { var folderName = Path.GetFileName(item.filePath); callback?.Invoke($"Cleaning {folderName}", item.Index, cleanMarkers.Count); var cleanActions = CleanFolder(item.filePath, false, config.UseFlatStructure); if (cleanActions.Any()) { actions.AddRange(cleanActions); } else { // nothing to delete, we report this as a no change actions.Add(uSyncAction.SetAction(true, $"Folder {Path.GetFileName(item.filePath)}", change: ChangeType.NoChange, filename: item.filePath)); } } // remove the actual cleans (they will have been replaced by the deletes actions.RemoveAll(x => x.Change == ChangeType.Clean); } return(actions); }