static void TraverseDirectory(PathTreeNode directoryNode) { void onChild(string childPath, bool childIsDirectory) { string childName = Path.GetFileName(childPath); PathTreeNode childNode; if (!directoryNode.Children.TryGetValue(childName, out childNode)) { directoryNode.Children[childName] = childNode = new PathTreeNode(childName, directoryNode, childIsDirectory); } childNode.IsTarget = true; if (childIsDirectory) { TraverseDirectory(childNode); } } foreach (var childPath in Directory.EnumerateFiles(directoryNode.Path)) { onChild(childPath, childIsDirectory: false); } foreach (var childPath in Directory.EnumerateDirectories(directoryNode.Path)) { onChild(childPath, childIsDirectory: true); } }
public PathTreeNode(string name, PathTreeNode parent, bool isDirectory) { Name = name; IsDirectory = isDirectory; Parent = parent; OriginalPath = Path; }
void Traverse(PathTreeNode node, Action <PathTreeNode> onNode) { foreach (var childNode in node.Children.Values) { onNode(childNode); Traverse(childNode, onNode); } }
void ReplaceContent(CommandRunnerActionKind actionKind, PathTreeNode node) { if (node.IsDirectory) { return; } bool replaced = false; string srcPath = actionKind == CommandRunnerActionKind.Replace ? node.Path : node.OriginalPath; var srcInfo = new TextFileInfo(srcPath); if (!srcInfo.IsValid) { return; } var lines = srcInfo.ReadAllLines(); var destLines = new List <string>(); for (int i = 0; i < lines.Count; ++i) { string resultLine = commands.Aggregate(lines[i], (l, c) => Regex.Replace(l, c.Pattern, c.Replacement)); destLines.Add(resultLine); if (resultLine == lines[i]) { continue; } if (!replaced) { Console_WriteLine("Content of \"{0}\":", node.Name); replaced = true; } Console_WriteLine(" {0:0000}: {1}", i + 1, resultLine); } if (actionKind == CommandRunnerActionKind.Replace && replaced || actionKind == CommandRunnerActionKind.Genearte) { using (var writer = new StreamWriter(node.Path, /* append: */ false, srcInfo.Encoding)) { for (int i = 0; i < destLines.Count; ++i) { writer.Write(destLines[i] + ((i != destLines.Count - 1) ? srcInfo.NewLine : "")); } } } }
void ReplaceName(CommandRunnerActionKind actionKind, PathTreeNode node) { string resultName = commands.Aggregate(node.Name, (n, c) => Regex.Replace(n, c.Pattern, c.Replacement)); if (resultName == node.Name) { return; } string kindLabel = node.IsDirectory ? "Directory name" : "Filename"; string prevPath = node.Path; node.Name = resultName; Console_WriteLine("{0}:", kindLabel); Console_WriteLine(" From: {0}", prevPath); Console_WriteLine(" To : {0}", node.Path); if (actionKind == CommandRunnerActionKind.Replace) { if (node.IsDirectory) { Directory.Move(prevPath, node.Path); } else { File.Move(prevPath, node.Path); } } else if (actionKind == CommandRunnerActionKind.Genearte) { if (node.IsDirectory) { Directory.CreateDirectory(node.Path); } else { File.Copy(node.OriginalPath, node.Path); } } }
public bool TryAdd(string path, out string errorMessage) { bool directoryExists = Directory.Exists(path); bool fileExists = File.Exists(path); if (!directoryExists && !fileExists) { errorMessage = "Not found: " + path; return(false); } var tokens = TokenizePath(path); var curr = Root; while (tokens.Any()) { var token = tokens.First(); tokens = tokens.Skip(1).ToArray(); if (token == "") { continue; } bool childIsLeaf = !tokens.Any(); PathTreeNode child; if (!curr.Children.TryGetValue(token, out child)) { curr.Children[token] = child = new PathTreeNode(token, curr, isDirectory: !childIsLeaf || directoryExists); } curr = child; curr.IsTarget |= childIsLeaf; } if (curr.IsDirectory) { TraverseDirectory(curr); } errorMessage = ""; return(true); }