Changeset 742 for xmlTools/trunk
- Timestamp:
- Mar 26, 2013, 3:30:48 AM (12 years ago)
- Location:
- xmlTools/trunk/posUpdate
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
xmlTools/trunk/posUpdate/ParametersParser.cs
r710 r742 15 15 private static string globalElement=""; 16 16 private static string globalParentElement=""; 17 private static bool globalNoBackups = false; 17 18 18 19 [Verb] … … 22 23 { 23 24 initialChecks(); 24 XmlTools myTools = new XmlTools(globalElement, globalParentElement );25 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups); 25 26 List<string> filesToProcess = getFilesToProcess(globalFileName); 26 27 foreach (string currentFile in filesToProcess) … … 37 38 { 38 39 initialChecks(); 39 XmlTools myTools = new XmlTools(globalElement, globalParentElement );40 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups); 40 41 List<string> filesToProcess = getFilesToProcess(globalFileName); 41 42 foreach (string currentFile in filesToProcess) … … 58 59 { 59 60 initialChecks(); 60 XmlTools myTools = new XmlTools(globalElement, globalParentElement );61 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups); 61 62 List<string> filesToProcess = getFilesToProcess(globalFileName); 62 63 foreach (string currentFile in filesToProcess) … … 71 72 { 72 73 initialChecks(); 73 XmlTools myTools = new XmlTools(globalElement, globalParentElement );74 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups); 74 75 List<string> filesToProcess = getFilesToProcess(globalFileName); 75 76 foreach (string currentFile in filesToProcess) … … 86 87 ) 87 88 { 88 XmlTools myTools = new XmlTools(globalElement, globalParentElement );89 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups); 89 90 List<string> filesToProcess = getFilesToProcess(globalFileName); 90 91 foreach (string currentFile in filesToProcess) … … 102 103 { 103 104 initialChecks(); 104 XmlTools myTools = new XmlTools(globalElement, globalParentElement );105 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups); 105 106 List<string> filesToProcess = getFilesToProcess(globalFileName); 106 107 foreach (string currentFile in filesToProcess) … … 131 132 if (forceInFiles != "") 132 133 { 133 myPatch = new XmlPatch(globalFileName, forceInFiles );134 myPatch = new XmlPatch(globalFileName, forceInFiles, globalNoBackups); 134 135 } 135 136 else 136 137 { 137 myPatch = new XmlPatch(globalFileName );138 myPatch = new XmlPatch(globalFileName, globalNoBackups); 138 139 } 139 140 … … 149 150 150 151 // Global Parameters 151 [Global ]152 [Global(Description = "Filename to apply the operations (with patchFile specifies the patch filename). Wildcards accepted for multiple files. No filename = search all .xml files in current path.")] 152 153 public static void fileName( 153 [Parameter(Required = true , Description = "Filename to apply the operations (with patchFile specifies the patch filename). Wildcards accepted for multiple files. No filename = search all .xml files in current path.")] string filename // xml filename. Wildcards accepted.154 [Parameter(Required = true)] string filename // xml filename. Wildcards accepted. 154 155 ) 155 156 { … … 157 158 } 158 159 159 [Global ]160 [Global(Description = "Element to apply the operation.")] 160 161 public static void element( 161 [Parameter(Required = true , Description = "Element to apply the operation.")] string element162 [Parameter(Required = true)] string element 162 163 ) 163 164 { … … 165 166 } 166 167 167 [Global ]168 [Global(Description = "Parent of the Element to apply the operation.")] 168 169 public static void parElement( 169 [Parameter(Required = true , Description = "Parent of the Element to apply the operation.")] string parentElement170 [Parameter(Required = true)] string parentElement 170 171 ) 171 172 { … … 173 174 } 174 175 175 //[PreVerbExecution] 176 //private static void InitialChecks(PreVerbExecutionContext context) 177 //{ 178 // bool elementFound = false; 179 // foreach(ParameterAndValue param in context.Parameters){ 180 // if (param.Parameter.Names[0] == "element") 181 // { 182 // if(!String.IsNullOrEmpty(param.Value.ToString().Trim())){ 183 // elementFound = true; 184 // } 185 // break; 186 // } 187 // } 188 // if (!elementFound) 189 // { 190 // Console.Error.WriteLine("You must specify the element parameter where the operations will be processed."); 191 // Console.ReadLine(); 192 // System.Environment.Exit(1); 193 // } 194 //} 176 [Global(Description = "Don't make backup of the files modified. Improves the overall program processing performance.")] 177 public static void noBackups() 178 { 179 globalNoBackups = true; 180 } 195 181 196 182 // Private functions -
xmlTools/trunk/posUpdate/Program.cs
r718 r742 11 11 class Program 12 12 { 13 public static readonly string toolsVersion = "0. 7d";13 public static readonly string toolsVersion = "0.8"; 14 14 15 15 public enum appErrors … … 43 43 } 44 44 45 static void Main(string[] args)45 public static void Main(string[] args) 46 46 { 47 47 try -
xmlTools/trunk/posUpdate/Util.cs
r718 r742 137 137 return Type.GetType("Mono.Runtime") != null; 138 138 } 139 140 public static bool ContainsIgnoreCase(string source, string sToSearch) 141 { 142 return source.IndexOf(sToSearch, StringComparison.OrdinalIgnoreCase) >= 0; 143 } 139 144 } 140 145 } -
xmlTools/trunk/posUpdate/XmlPatch.cs
r710 r742 14 14 String fileName; 15 15 String forceFiles = ""; 16 17 public XmlPatch(String file) 16 bool globalNoBackups = false; 17 18 public XmlPatch(String file, bool noBackups) 18 19 { 19 20 fileName = file; 20 } 21 22 public XmlPatch(String file, String forceInFiles) 21 globalNoBackups = noBackups; 22 } 23 24 public XmlPatch(String file, String forceInFiles, bool noBackups) 23 25 { 24 26 fileName = file; 25 27 forceFiles = forceInFiles; //We support apply the operation in diverse forced files (NameOfFile parameter will be ignored) 28 globalNoBackups = noBackups; 26 29 } 27 30 … … 135 138 foreach (String currFile in filesToProcess) 136 139 { 137 138 Util.backupFile(currFile); 140 if (!this.globalNoBackups && !Util.ContainsIgnoreCase(operation, "NoBackups")) // only skip backup if specified via global parameter or in patch file 141 { 142 Util.backupFile(currFile); 143 } 139 144 140 145 XmlDocument xdoc = new XmlDocument(); … … 228 233 { 229 234 230 Util.backupFile(currFile); 235 if (!this.globalNoBackups && !Util.ContainsIgnoreCase(operation, "NoBackups")) // only skip backup if specified via global parameter or in patch file 236 { 237 Util.backupFile(currFile); 238 } 231 239 232 240 XmlDocument xdoc = new XmlDocument(); … … 288 296 else 289 297 { 290 command = command.Insert(command.Length-1," -filename:" + this.forceFiles); // - 2to be inside quotes298 command = command.Insert(command.Length-1," -filename:" + this.forceFiles); // -1 to be inside quotes 291 299 } 292 300 … … 305 313 } 306 314 315 if (this.globalNoBackups && !Util.ContainsIgnoreCase(command,"nobackups")) // add noBackup flag if provided as global parameter 316 { 317 command = command.Insert(command.Length - 1, " -nobackups"); // -1 to be inside quotes 318 } 319 307 320 command = command.Replace("\"", ""); // remove quotes 308 321 309 ProcessStartInfo startInfo = new ProcessStartInfo(); 310 if (!Util.IsRunningOnMono()) 311 { 312 startInfo.FileName = Util.getExeFileName(); 313 } 314 else{ 315 startInfo.FileName = "mono"; 316 } 317 if (!Util.IsRunningOnMono()) 318 { 319 startInfo.Arguments = command; 320 } 321 else{ 322 startInfo.Arguments = Util.getExeFileName() + " " + command; 323 } 324 startInfo.UseShellExecute = false; // necessary to redirect output 325 startInfo.RedirectStandardOutput = true; 326 startInfo.RedirectStandardError = true; 327 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // hide new process window 328 329 Process p = System.Diagnostics.Process.Start(startInfo); 330 p.OutputDataReceived += commandStdOutputReceived; 331 p.ErrorDataReceived += commandStdErrorReceived; 332 p.BeginOutputReadLine(); 333 p.BeginErrorReadLine(); 334 p.WaitForExit(); 322 Program.Main(command.Split(' ')); // use the current process is more efficient than start a new one 335 323 } 336 324 catch (Exception e) … … 341 329 342 330 return true; 343 }344 345 /// <summary>346 /// Reads asynchronously output from the new process where the command will be executed347 /// </summary>348 /// <param name="sender"></param>349 /// <param name="e"></param>350 private void commandStdOutputReceived(object sender, DataReceivedEventArgs e)351 {352 353 string myData = e.Data;354 355 if (myData != null)356 {357 if (myData.EndsWith("\n"))358 {359 Console.Write(myData);360 }361 else362 {363 Console.WriteLine(myData);364 }365 }366 367 }368 369 private void commandStdErrorReceived(object sender, DataReceivedEventArgs e)370 {371 372 string myData = e.Data;373 374 if (myData != null)375 {376 if (myData.EndsWith("\n"))377 {378 Console.Error.Write(myData);379 }380 else381 {382 Console.Error.WriteLine(myData);383 }384 }385 386 331 } 387 332 -
xmlTools/trunk/posUpdate/XmlTools.cs
r710 r742 10 10 private XmlDocument xdoc; 11 11 private string posElement, posParentElement; 12 13 /// <summary> 12 private bool noBackups; 13 14 14 /// Our constructor 15 /// <summary> 15 16 /// </summary> 16 17 /// <param name="file"></param> 17 18 /// <param name="posElement"></param> 18 19 /// <param name="posParentElement"></param> 19 public XmlTools(string posElement, string posParentElement = "" )20 public XmlTools(string posElement, string posParentElement = "", bool noBackups = false) 20 21 { 21 22 this.posElement = posElement; 22 23 this.posParentElement = posParentElement; 24 this.noBackups = noBackups; 23 25 } 24 26 … … 30 32 public void replaceAll(string file, string value, string valuePositions = "") 31 33 { 32 Util.backupFile(file); 33 loadXmlFile(file); 34 35 List<XmlNode> myElements = new List<XmlNode>(); 36 Util.getAllSpecificElements(xdoc.DocumentElement, ref myElements,this.posElement,this.posParentElement); //Returns all after "Oni" element 34 if (!this.noBackups) 35 { 36 Util.backupFile(file); 37 } 38 loadXmlFile(file); 39 40 List<XmlNode> myElements = new List<XmlNode>(); 41 Util.getAllSpecificElements(xdoc.DocumentElement, ref myElements, this.posElement, this.posParentElement); //Returns all after "Oni" element 37 42 38 43 if (valuePositions != "") … … 71 76 public void addValues(string file, string values) 72 77 { 73 Util.backupFile(file); 78 if (!this.noBackups) 79 { 80 Util.backupFile(file); 81 } 74 82 loadXmlFile(file); 75 83 … … 89 97 XmlTextValue myXmlSubValues = new XmlTextValue(element.InnerText); 90 98 91 foreach(String myInputValue in myInputValues.myValues){ 99 foreach (String myInputValue in myInputValues.myValues) 100 { 92 101 bool alreadyExists = false; 93 102 foreach (String myXmlSubValue in myXmlSubValues.myValues) … … 103 112 if (!alreadyExists) 104 113 { 105 element.InnerText += " " +myInputValue;114 element.InnerText += " " + myInputValue; 106 115 } 107 116 } … … 119 128 public void replaceValue(string file, string oldValue, string newValue) 120 129 { 121 Util.backupFile(file); 130 if (!this.noBackups) 131 { 132 Util.backupFile(file); 133 } 122 134 loadXmlFile(file); 123 135 … … 135 147 XmlTextValue myXmlSubValues = new XmlTextValue(element.InnerText); 136 148 137 for(int i=0; i<myXmlSubValues.myValues.Count; i++){ 138 //Found a match with the old value? 139 if (myXmlSubValues.myValues[i] == oldValue) 140 { 141 //replace with the new match 142 myXmlSubValues.myValues[i] = newValue; 143 } 144 } 145 element.InnerText = myXmlSubValues.ToString(); 149 for (int i = 0; i < myXmlSubValues.myValues.Count; i++) 150 { 151 //Found a match with the old value? 152 if (myXmlSubValues.myValues[i] == oldValue) 153 { 154 //replace with the new match 155 myXmlSubValues.myValues[i] = newValue; 156 } 157 } 158 element.InnerText = myXmlSubValues.ToString(); 146 159 } 147 160 } … … 157 170 public void removeValues(string file, string values) 158 171 { 159 Util.backupFile(file); 172 if (!this.noBackups) 173 { 174 Util.backupFile(file); 175 } 160 176 loadXmlFile(file); 161 177 … … 177 193 foreach (String myInputValue in myInputValues.myValues) 178 194 { 179 for(int i=0; i<myXmlSubValues.myValues.Count; i++){ 195 for (int i = 0; i < myXmlSubValues.myValues.Count; i++) 196 { 180 197 //It already exists in the xml? 181 198 if (myInputValue == myXmlSubValues.myValues[i]) … … 202 219 public void changeValue(string file, string newValue, string valueRelation = "", string valuePositions = "") 203 220 { 204 Util.backupFile(file); 221 if (!this.noBackups) 222 { 223 Util.backupFile(file); 224 } 205 225 loadXmlFile(file); 206 226 … … 256 276 public void invert(string file) 257 277 { 258 Util.backupFile(file); 278 if (!this.noBackups) 279 { 280 Util.backupFile(file); 281 } 259 282 loadXmlFile(file); 260 283 … … 267 290 foreach (XmlNode element in myElements) //Returns all after "Oni" element 268 291 { 269 292 invertedOrder.Add(element.InnerText); 270 293 } 271 294 … … 310 333 if (pos > testFirstRealValue.myValues.Count - 1 || pos < 0) //Are positions valid for the current values? //-1 because starts at 0 311 334 { 312 Program.printAppError(Program.appErrors.INVALID_POSITIONS_RANGE, "The positions values are not in the range of the value to replace (pos index < 0 or > newValueIndexesNumber).", true);335 Program.printAppError(Program.appErrors.INVALID_POSITIONS_RANGE, "The positions values are not in the range of the value to replace (pos index < 0 or > newValueIndexesNumber).", true); 313 336 } 314 337 } -
xmlTools/trunk/posUpdate/xmlTools.csproj.user
r710 r742 2 2 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3 3 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> 4 <StartArguments>replaceall -element:FirstLevel -value:0 - filename:TRAMKONCOMbk_fw_kick.xml</StartArguments>4 <StartArguments>replaceall -element:FirstLevel -value:0 -nobackups -filename:"C:\Users\home\Documents\Visual Studio 2012\Projects\posUpdate\posUpdate\bin\Debug\%2a.xml"</StartArguments> 5 5 </PropertyGroup> 6 6 <PropertyGroup> … … 15 15 </PropertyGroup> 16 16 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> 17 <StartArguments>replaceall -element:FirstLevel -value:0 </StartArguments>17 <StartArguments>replaceall -element:FirstLevel -value:0 -nobackups -filename:"C:\Users\home\Documents\Visual Studio 2012\Projects\posUpdate\posUpdate\bin\Debug\%2a.xml"</StartArguments> 18 18 </PropertyGroup> 19 19 </Project>
Note:
See TracChangeset
for help on using the changeset viewer.