public void Run(PathTree pathTree, CommandRunnerActionKind actionKind) { pathTree.Traverse((node) => { if (!node.IsTarget) { return; } ReplaceName(actionKind, node); ReplaceContent(actionKind, node); }); }
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); } } }