// ------------------- not in use, MyLib.replaceVarsInLine will do similar and better job -------------------- private string findVariableValue(string batFilePath) { string parentVar = this.parent.Replace("%", ""); string[] all = File.ReadAllLines(batFilePath); string varDefinitionLine = ""; foreach (string line in all) { string str = MyLib.dumpBatComment(line); if (str.ToLower().Contains(parentVar.ToLower()) && str.Contains("=")) { varDefinitionLine = str.Replace("\"", "").Replace("'", "").Replace("/", "\\").Trim(); string[] strs = varDefinitionLine.Split('=', ' '); // space in line would be trouble if (strs.Length > 1) { for (int i = 0; i < strs.Length; i++) { if (strs[i] == parentVar && i < strs.Length - 1) { return(MyLib.getLastPartInPath(strs[i + 1])); } } } } } return(""); }
public static string replaceVarsInLine(string line, string sourcePath) { // pass in: "%VAR1%\%VAR3%%VAR4%\%VAR2%\dummydummy\file.htm" ".\test.bat" if (!line.Contains("%")) { return(line); } string str1 = line; // know vars MatchCollection matches = Regex.Matches(str1, "%[^%]*%"); List <KeyValue> kvs = new List <KeyValue>(); foreach (var m in matches) { KeyValue kv = new KeyValue(m.ToString().Replace("%", "")); kvs.Add(kv); } // know values string[] allLines = File.ReadAllLines(sourcePath); foreach (var kv in kvs) { foreach (string l in allLines) { string s = MyLib.dumpBatComment(l); if (s.ToLower().Contains(kv.key.ToLower()) && s.Contains("=")) { s = s.Replace("\"", "").Replace("'", "").Replace("/", "\\").Trim(); // reformat string[] arr = s.Split('=', ' '); // space in line would be trouble if (arr.Length > 1) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == kv.key.ToString() && i < arr.Length - 1) { kv.value = arr[i + 1]; break; } } } } } } foreach (var kv in kvs) { if (kv.value != "") { str1 = str1.Replace("%" + kv.key + "%", kv.value); } } return(str1); }
public void setPaths(string binFolderPath) { if (binFolderPath.Length > 0 && Directory.Exists(binFolderPath)) { //1. find batPath, targetLine (containing this.name) string[] batPaths = Directory.GetFiles(binFolderPath, "*.bat"); string targetLine = ""; foreach (var b in batPaths) { string[] all = File.ReadAllLines(b); foreach (string line in all) { string str = MyLib.dumpBatComment(line); //if (str.ToLower().Contains(this.name.ToLower()) && MyLib.isBatFormat(str)) if (str.ToLower().Contains("\\" + this.name.ToLower() + ".htm") && MyLib.isBatFormat(str)) { targetLine = str; this.batPath = b; break; } } } // extract from targetLine: CALL ..\..\Roof.bat "-DseleniumTestCases=%ROOF_LOCAL%\RDM\SetUIVars_RDM.htm;RDM\tests\FVT\Administration\RDM3642_CompoundKeys\RDM3642_CompoundKeys_RDM799.htm" //2. find testPath this.testPath = targetLine; this.testPath = this.testPath.Replace('/', '\\').Replace("\"", "").Replace("\'", "").Replace("%ROOF_LOCAL%", ""); string[] arr = this.testPath.Split(' ', ';'); for (int i = arr.Length - 1; i >= 0; i--) { if (arr[i].EndsWith(this.name + ".htm")) { this.testPath = arr[i]; break; } } // in case of %RDM12345_PATH% if (this.testPath.Contains("%")) { this.testPath = MyLib.replaceVarsInLine(this.testPath, this.batPath); } // 3. find testFolderPath File.Exists(this.testPath) always returns false even though testPath exist and valid, better to fix it if (this.testPath.Length > 0) { this.testFolderPath = Path.GetDirectoryName(this.testPath); // relative like in bat \RDM\XXX\YYY\ } //3. find parent ( use it in csv ) targetLine = MyLib.replaceVarsInLine(targetLine, this.batPath); targetLine = targetLine.Replace('/', '\\').Replace("\"", "|").Replace("\'", "|").Replace(".htm", "|").Replace(";", "").Trim(); string[] targetLineArray = targetLine.Replace('/', '\\').Split('\\', '|'); for (int i = targetLineArray.Length - 1; i > 0; i--) // if i=0 then parent not valid { if (targetLineArray[i] == this.name) { this.parent = targetLineArray[i - 1]; break; } } //if (this.parent.Contains("%") && this.batPath != "") this.parent = this.findVariableValue(this.batPath); } }