public static List <string> MapFiles(string p_mapperFile, string p_directory, bool p_replace) { List <string> result = new List <string>(); List <string> files; FilePatternMapper pattern; StreamReader inputFile; try { directory = p_directory; if (!Path.IsPathRooted(directory)) { directory = Path.GetFullPath(Path.Combine(currentDirectory, directory)); } if (!File.Exists(Path.Combine(workDirectory, p_mapperFile + ".json")) || !File.Exists(Path.Combine(workDirectory, p_mapperFile + ".txt"))) { throw new FileNotFoundException("Configuration not found: " + p_mapperFile); } pattern = FilePatternMapper.load(Path.Combine(workDirectory, p_mapperFile + ".json")); if (!string.IsNullOrEmpty(pattern.RelativePath) && !Path.IsPathRooted(pattern.RelativePath)) { directory = Path.GetFullPath(Path.Combine(directory, pattern.RelativePath)); } files = getAllFiles(directory, pattern.FileFilter, pattern.DirFilter, pattern.FileExclude, pattern.DirExclude); foreach (var file in files) { inputFile = new StreamReader(file); result.Add(ProcessFile(pattern, inputFile.ReadToEnd(), file, p_replace)); inputFile.Close(); inputFile.Dispose(); } return(result); } catch (Exception e) { Console.WriteLine(e); throw; } }
private static void initFileInteractive(Dictionary <string, string> inputArgs) { FilePatternMapper file; StreamWriter patternFile; bool hasNext = false; int auxSearchLocation = 0; string patternFileName = ""; string mapperFileName = ""; string pythonFileName = ""; try { mapperFileName = Path.GetFullPath(Path.Combine(FileMapper.workDirectory, inputArgs.GetValueOrDefault("INIT") + ".json")); patternFileName = Path.GetFullPath(Path.Combine(FileMapper.workDirectory, inputArgs.GetValueOrDefault("INIT") + ".txt")); pythonFileName = Path.GetFullPath(Path.Combine(FileMapper.workDirectory, inputArgs.GetValueOrDefault("INIT") + ".py")); file = new FilePatternMapper() { Name = inputArgs.GetValueOrDefault("INIT"), DirFilter = "", FileFilter = "", OutputFileName = "", OutputFolder = "", Variables = new List <FilePatternVariables>() }; Console.WriteLine("Directory filter (default ''): "); file.DirFilter = Console.ReadLine(); Console.WriteLine("File filter (default ''): "); file.FileFilter = Console.ReadLine(); Console.WriteLine("Directory exclusion filter (default ''): "); file.DirFilter = Console.ReadLine(); Console.WriteLine("File exclusion filter (default ''): "); file.FileFilter = Console.ReadLine(); Console.WriteLine("Inform variables? (y or n)"); if (Console.ReadLine().Equals("Y", StringComparison.InvariantCultureIgnoreCase)) { do { file.Variables.Add(new FilePatternVariables()); Console.WriteLine("Variable name:"); file.Variables.Last().Name = Console.ReadLine(); Console.WriteLine("Search location (0 - content, 1 - File name, 2 - File Directory, 3 - File Path, 4 - Literal):"); if (int.TryParse(Console.ReadLine(), out auxSearchLocation)) { file.Variables.Last().SearchLocation = (ESearchLocation)auxSearchLocation; } else { file.Variables.Last().SearchLocation = ESearchLocation.Content; } Console.WriteLine("Variable search pattern:"); file.Variables.Last().SearchPatern = Console.ReadLine(); hasNext = false; Console.WriteLine("do you want to inform another variable? (y or n)"); if (Console.ReadLine().Equals("Y", StringComparison.InvariantCultureIgnoreCase)) { hasNext = true; } } while (hasNext); } file.save(mapperFileName); patternFile = new StreamWriter(patternFileName, false); foreach (FilePatternVariables filePatternVariable in file.Variables) { patternFile.WriteLine("<%" + filePatternVariable.Name + "%>"); } patternFile.Close(); patternFile.Dispose(); Console.WriteLine("Saved: " + inputArgs.GetValueOrDefault("INIT")); Console.WriteLine("Do you want to open the patern files? (y or n)"); if (Console.ReadLine().Equals("Y", StringComparison.InvariantCultureIgnoreCase)) { var p1 = new Process(); p1.StartInfo = new ProcessStartInfo(@patternFileName) { UseShellExecute = true }; p1.Start(); var p2 = new Process(); p2.StartInfo = new ProcessStartInfo(@mapperFileName) { UseShellExecute = true }; p2.Start(); var p3 = new Process(); p3.StartInfo = new ProcessStartInfo(@pythonFileName) { UseShellExecute = true }; p3.Start(); } } catch (Exception e) { Console.WriteLine(e); } }
private static string ProcessFile(FilePatternMapper pattern, string file, string filePath, bool replace) { string result = ""; StreamWriter outputFile; List <VariablesResult> variables; string output = ""; List <string> loopBlocks; string loopBlocksGroup = ""; string outputName = ""; string outputDir = ""; StreamReader patternFile; try { //<%name%> = variable //<@name@> = repeat for each match of variable (begin of block) //<!name!> = value of current item in loop //</@name@> = repeat for each match of variable (end of block) //<%name.subname%> = sub variable pythonScript = LoadPythonScript(pattern.Name); //From body variables = CollectVariables(pattern.Variables.FindAll(patternVariables => patternVariables.SearchLocation == ESearchLocation.Content), file); //From file name variables.AddRange(CollectVariables(pattern.Variables.FindAll(patternVariables => patternVariables.SearchLocation == ESearchLocation.FileName), Path.GetFileName(filePath))); //From file path variables.AddRange(CollectVariables(pattern.Variables.FindAll(patternVariables => patternVariables.SearchLocation == ESearchLocation.FileDirectory), Path.GetDirectoryName(filePath))); patternFile = new StreamReader(Path.Combine(workDirectory, pattern.Name + ".txt")); output = patternFile.ReadToEnd(); patternFile.Close(); patternFile.Dispose(); outputName = pattern.OutputFileName; outputDir = pattern.OutputFolder; //Replace Variables foreach (VariablesResult variablesResult in variables) { //On Output output = processVariable(variablesResult, output, ""); //On File Name outputName = processVariable(variablesResult, outputName, ""); //On File path outputDir = processVariable(variablesResult, outputDir, ""); } //Replace loop blocks foreach (VariablesResult variablesResult in variables) { loopBlocks = CollectLoopBlocks(output, variablesResult.Name); loopBlocks.ForEach(block => { loopBlocksGroup = processBlock(variablesResult, block, "") .Replace("<@" + variablesResult.Name + "@>", "") .Replace("</@" + variablesResult.Name + "@>", ""); output = output.Replace(block, loopBlocksGroup); }); } if (!Path.IsPathRooted(outputDir)) { outputDir = Path.GetFullPath(Path.Combine(directory, outputDir)); } if ((!replace && !File.Exists(Path.Combine(outputDir, outputName))) || replace) { if (File.Exists(Path.Combine(outputDir, outputName)) && replace) { File.Delete(Path.Combine(outputDir, outputName)); result = "File replaced: " + Path.Combine(outputDir, outputName); } else { result = "File created: " + Path.Combine(outputDir, outputName); } outputFile = new StreamWriter(Path.Combine(outputDir, outputName), false); outputFile.Write(output); outputFile.Close(); outputFile.Dispose(); } else { result = "File already exists: " + Path.Combine(outputDir, outputName); } } catch (Exception e) { result = "Error processing file: " + e.Message; } return(result); }
private static void initFile(Dictionary <string, string> inputArgs) { FilePatternMapper file; StreamWriter patternFile; string patternFileName = ""; string mapperFileName = ""; string pythonFileName = ""; try { mapperFileName = Path.GetFullPath(Path.Combine(FileMapper.workDirectory, inputArgs.GetValueOrDefault("INIT") + ".json")); patternFileName = Path.GetFullPath(Path.Combine(FileMapper.workDirectory, inputArgs.GetValueOrDefault("INIT") + ".txt")); pythonFileName = Path.GetFullPath(Path.Combine(FileMapper.workDirectory, inputArgs.GetValueOrDefault("INIT") + ".py")); file = new FilePatternMapper() { Name = inputArgs.GetValueOrDefault("INIT"), DirFilter = "", FileFilter = "", OutputFileName = "", OutputFolder = "", Variables = new List <FilePatternVariables>() { new FilePatternVariables() { Name = "Variabe_Name", SearchLocation = ESearchLocation.Content, SearchPatern = "" } } }; file.save(mapperFileName); patternFile = new StreamWriter(patternFileName); patternFile.WriteLine("<%variable_name%>"); patternFile.Close(); patternFile.Dispose(); Console.WriteLine("Saved: " + inputArgs.GetValueOrDefault("INIT")); Console.WriteLine("Do you want to open the generated files? (y or n)"); if (Console.ReadLine().Equals("Y", StringComparison.InvariantCultureIgnoreCase)) { var p1 = new Process(); p1.StartInfo = new ProcessStartInfo(@patternFileName) { UseShellExecute = true }; p1.Start(); var p2 = new Process(); p2.StartInfo = new ProcessStartInfo(@mapperFileName) { UseShellExecute = true }; p2.Start(); var p3 = new Process(); p3.StartInfo = new ProcessStartInfo(@pythonFileName) { UseShellExecute = true }; p3.Start(); } } catch (Exception e) { Console.WriteLine(e); } }