//TODO extract to general BuildTree function inside DirectoryFileTree ?? private DirectoryFileTreeSimple ToDirectoryTree(Backup.Models.Backup backup, string rootDirectory) { var tree = new DirectoryFileTreeSimple(rootDirectory); var dirNodes = new Dictionary <string, DirectoryFileTreeNodeSimple>(); dirNodes.Add(rootDirectory, tree.Root); var pathBuilder = new StringBuilder(); foreach (var replayBackup in backup.ReplayBackups) { pathBuilder.Append(rootDirectory); var directories = FileHandler.ExtractDirectoriesFromPath(replayBackup.FileName, string.Empty).Select(d => d.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).TrimEnd(' ')).Where(d => !string.IsNullOrWhiteSpace(d)).ToList(); foreach (var directory in directories) { var previousDir = pathBuilder.ToString(); pathBuilder.Append(Path.DirectorySeparatorChar + directory); var currentDir = pathBuilder.ToString(); //TODO it could be cool I guess to allow inspecting a backup to also inspect a replay, this would however require parsing on the fly... For now I am using SimpleFile which // only has a string for the filename AddOrModify(tree, dirNodes, currentDir, previousDir, directory, pathBuilder.ToString() == rootDirectory + Path.DirectorySeparatorChar + Path.GetDirectoryName(replayBackup.FileName) ? FileHandler.GetFileNameWithoutExtension(replayBackup.FileName) : null); } pathBuilder.Clear(); } return(tree); }
//TODO extract to general BuildTree function inside the DirectoryFileTree ?? Because i'm also using this inside the sorter to render output... private void AddOrModify(DirectoryFileTreeSimple tree, Dictionary <string, DirectoryFileTreeNodeSimple> directories, string directoryPath, string previousDirectoryPath, string directory, string fileName) { if (directories == null || string.IsNullOrWhiteSpace(directoryPath)) { return; } if (directories.ContainsKey(directoryPath)) { if (fileName != null) { tree.AddToNode(directories[directoryPath], new SimpleFile(fileName)); } } else { if (fileName != null) { directories.Add( directoryPath, tree.AddToNode( directories[previousDirectoryPath], directory, new List <SimpleFile>() { new SimpleFile(fileName) }.AsEnumerable() ) ); } else { directories.Add( directoryPath, tree.AddToNode( directories[previousDirectoryPath], directory) ); } } }