private static void CopyToOutput_Specified(ScriptConfig scriptConfig) { // For each data file for (short i = 0; i < scriptConfig.DataFiles.Length; i++) { string sourceFile = scriptConfig.DataFiles[i]; string workPath = Tools.GetTempPath(sourceFile); string outputPath = Tools.GetOutputPath(scriptConfig, workPath, i); if (string.IsNullOrEmpty(outputPath)) { continue; } bool isFolder = Directory.Exists(outputPath); if (isFolder) { Tools.BackupFolder(outputPath); Tools.CopyToOutput(workPath, outputPath); } else { Tools.BackupFiles(outputPath); File.WriteAllText(outputPath, File.ReadAllText(workPath)); } } }
private static void Replace_Names_Directory(ScriptConfig scriptConfig) { // Get source directory SearchOption SubIncluded = scriptConfig.IncludeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; string sourceDirectory = scriptConfig.DataPath; string workDirectory; // Get work directory if (scriptConfig.ConfigBefore) { // Take the existing temp directory workDirectory = Tools.GetTempPath(sourceDirectory); } else { // Copy to the temp directory workDirectory = Tools.CopyFolderToTemp(sourceDirectory); } if (!Directory.Exists(workDirectory)) { return; } // Renames files before folders Tools.RenameFiles(workDirectory, scriptConfig); // Rename folders Tools.RenameFolders(workDirectory, scriptConfig); }
/// <summary> /// Get the output path /// </summary> public static string GetOutputPath(ScriptConfig scriptConfig, string workPath, int index) { string outputPath = string.Empty; // If folder not existing, abort if (!File.Exists(workPath)) { return(string.Empty); } //If specific output path is set if (scriptConfig.OutputFiles.Length > index) { string outputTemp = scriptConfig.OutputFiles.ElementAt(index); if (!string.IsNullOrEmpty(outputTemp)) { outputPath = scriptConfig.OutputFiles.ElementAt(index); } } // Otherwise, set the default one (output path + base filename) if (outputPath == string.Empty) { outputPath = Path.Combine(scriptConfig.OutputPath, Path.GetFileName(workPath)); } return(outputPath); }
private static void Replace_Names_Specified(ScriptConfig scriptConfig) { // For each data file for (short i = 0; i < scriptConfig.DataFiles.Length; i++) { // Retrieve the file path to the temp folder string sourcePath = scriptConfig.DataFiles[i]; // Copy to temp path if not existing string workPath; // Get working path if (scriptConfig.ConfigBefore) { workPath = Tools.GetTempPath(sourcePath); } else { workPath = Tools.CopyFileToTemp(sourcePath); } bool isFolder = Directory.Exists(workPath); // Renames Tools.Rename(workPath, scriptConfig, isFolder); } }
/// <summary> /// Ranem files in the specified directory /// </summary> public static void RenameFiles(string root, ScriptConfig scriptConfig) { // Rename files foreach (string file in Directory.GetFiles(root, "*.*", (scriptConfig.IncludeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))) { Rename(file, scriptConfig, false); } }
private static void CopyToOutput_Directory(ScriptConfig scriptConfig) { string workPath = Tools.GetTempPath(scriptConfig.DataPath); string outputPath = scriptConfig.OutputPath; if (Directory.Exists(outputPath)) { Tools.BackupFolder(outputPath); } Tools.CopyToOutput(workPath, outputPath); }
/// <summary> /// Replace file content /// </summary> public static void Replace_FileContent(ScriptConfig scriptConfig) { ValidResult valid = CheckValid(scriptConfig); // Run replacers if (valid.DirectoryValid) { Replace_FileContent_Directory(scriptConfig); } if (valid.FilesValid) { Replace_FileContent_Specified(scriptConfig); } }
/// <summary> /// Rename folders in root folder /// </summary> public static void RenameFolders(string root, ScriptConfig scriptConfig) { DirectoryInfo di = new DirectoryInfo(root); // Get all directories foreach (DirectoryInfo dir in di.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)) { // Rename in ascending order if (scriptConfig.IncludeSubDirectories) { RenameFolders(dir.FullName, scriptConfig); } Rename(dir, scriptConfig); } }
private static void Replace_FileContent_Directory(ScriptConfig scriptConfig) { // Get source directory SearchOption SubIncluded = scriptConfig.IncludeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; string sourceDirectory = scriptConfig.DataPath; string workDirectory; // Get work directory if (scriptConfig.ConfigBefore) { workDirectory = Tools.GetTempPath(sourceDirectory); } else { workDirectory = Tools.CopyFolderToTemp(sourceDirectory); } if (!Directory.Exists(workDirectory)) { return; } string data; string sourceData; // Replace content in al files foreach (string file in Directory.GetFiles(workDirectory, "*.*", SubIncluded)) { // Get data from temp path data = File.ReadAllText(file); sourceData = data; data = Tools.ReplaceText(data, scriptConfig); // If any changes made, apply changes if (sourceData != data) { Console.WriteLine("[DEBUG] Replacement done. Data has beed modified : " + file); File.WriteAllText(file, data); } else { Console.WriteLine("[DEBUG] Replacement done. No data has beed modified : " + file); } } }
/// <summary> /// Rename file or directory /// </summary> public static void Rename(string name, ScriptConfig scriptConfig, bool isFolder) { string sourceName = name; name = ReplaceText(Path.GetFileName(name), scriptConfig); if (Path.GetFileName(sourceName) != name) { if (isFolder) { Directory.Move(sourceName, Path.Combine(Path.GetDirectoryName(sourceName), name)); } else { File.Move(sourceName, Path.Combine(Path.GetDirectoryName(sourceName), name)); } } }
/// <summary> /// Copy to output path /// </summary> public static void CopyToOutput(ScriptConfig scriptConfig) { // If script after this one, don't copy now if (scriptConfig.ConfigAfter) { return; } ValidResult valid = CheckValid(scriptConfig); // Run replacers if (valid.DirectoryValid) { CopyToOutput_Directory(scriptConfig); } if (valid.FilesValid) { CopyToOutput_Specified(scriptConfig); } }
private static void Replace_FileContent_Specified(ScriptConfig scriptConfig) { // For each data file for (short i = 0; i < scriptConfig.DataFiles.Length; i++) { // Retrieve the file path to the temp folder string sourceFile = scriptConfig.DataFiles[i]; // Copy to temp path if not existing string workFile; // Get working path if (scriptConfig.ConfigBefore) { workFile = Tools.GetTempPath(sourceFile); } else { workFile = Tools.CopyFileToTemp(sourceFile); } // Read data string data = File.ReadAllText(workFile); string sourceData = data; data = Tools.ReplaceText(data, scriptConfig); // If any changes made, apply changes if (sourceData != data) { Console.WriteLine("[DEBUG] Replacement done. Data has beed modified : " + workFile); File.WriteAllText(workFile, data); } else { Console.WriteLine("[DEBUG] Replacement done. No data has beed modified : " + workFile); } } }
private static ValidResult CheckValid(ScriptConfig scriptConfig) { ValidResult validResult = new ValidResult() { DirectoryValid = true, FilesValid = true, }; // Determine if Datadirectory is valid if (scriptConfig.DataPath == null) { validResult.DirectoryValid = false; } if (scriptConfig.DataPath == string.Empty) { validResult.DirectoryValid = false; } if (!Directory.Exists(scriptConfig.DataPath)) { validResult.DirectoryValid = false; } // Determine if DataFiles is valid if (scriptConfig.DataFiles == null) { validResult.FilesValid = false; } if (scriptConfig.DataFiles.Length == 0) { validResult.FilesValid = false; } return(validResult); }
/// <summary> /// Rename directory /// </summary> public static void Rename(DirectoryInfo directoryInfo, ScriptConfig scriptConfig) { Rename(directoryInfo.FullName, scriptConfig, true); }
/// <summary> /// Replace text from the config /// </summary> public static string ReplaceText(string data, ScriptConfig scriptConfig) { // For each content file foreach (string contentfile in scriptConfig.ContentFiles) { // Load variables Dictionary <string, Variable> loadedVars = null; try { loadedVars = VariableSettingsManager.Load(contentfile); } catch (Exception ex) { // Skip this content file Console.WriteLine("Invalid content file : " + contentfile + " :\n" + ex); continue; } Console.WriteLine(loadedVars.Count + " variables found in " + contentfile); // For each variable in that file foreach (var loadedVar in loadedVars) { var varData = loadedVar.Value.Data; var varName = loadedVar.Key; // If no name, skip if (varName == string.Empty) { continue; } // Add delimiters varName = "{" + varName + "}"; switch (scriptConfig.FilterVariableMode) { // No filter case ScriptConfig.FilterMode.None: // Search in data if found if (data.Contains(varName)) { // If found replace Console.WriteLine("found variable " + varName + " in " + contentfile); data = data.Replace(varName, varData); } break; // Whitelist case ScriptConfig.FilterMode.Whitelist: // check that this variable is whitelisted if (scriptConfig.Variables.Contains(varName)) { // Search in data if found if (data.Contains(varName)) { // If found replace Console.WriteLine("found variable " + varName + " in " + contentfile); data = data.Replace(varName, varData); } } break; // Blacklist case ScriptConfig.FilterMode.Blacklist: // check that this variable is not in the blacklist if (!scriptConfig.Variables.Contains(varName)) { // Search in data if found if (data.Contains(varName)) { // If found replace Console.WriteLine("found variable " + varName + " in " + contentfile); data = data.Replace(varName, varData); } } break; default: break; } } } return(data); }