ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/xmlTools/trunk/posUpdate/XmlPatch.cs
(Generate patch)

Comparing xmlTools/trunk/posUpdate/XmlPatch.cs (file contents):
Revision 714 by s10k, Thu Mar 21 10:43:10 2013 UTC vs.
Revision 868 by s10k, Fri May 10 14:42:11 2013 UTC

# Line 13 | Line 13 | namespace xmlTools
13      {
14          String fileName;
15          String forceFiles = "";
16 +        bool globalNoBackups = false;
17  
18 <        public XmlPatch(String file)
18 >        public XmlPatch(String file, bool noBackups)
19          {
20              fileName = file;
21 +            globalNoBackups = noBackups;
22          }
23  
24 <        public XmlPatch(String file, String forceInFiles)
24 >        public XmlPatch(String file, String forceInFiles, bool noBackups)
25          {
26              fileName = file;
27              forceFiles = forceInFiles; //We support apply the operation in diverse forced files (NameOfFile parameter will be ignored)
28 +            globalNoBackups = noBackups;
29          }
30  
31          /// <summary>
# Line 92 | Line 95 | namespace xmlTools
95              //---------------------------------------------------Parse Operation command (start)
96              try
97              {
98 <                if (forceFiles == null)
98 >                if (String.IsNullOrEmpty(forceFiles))
99                  {
100                      File = getPatchParameter(operation, "File");
101                  }
# Line 111 | Line 114 | namespace xmlTools
114                  return false;
115              }
116  
117 <            if (Element == "")
117 >            if (String.IsNullOrEmpty(Element))
118              {
119                  return false;
120              }
121  
122              //---------------------------------------------------Parse Operation command (end)
123              List<String> filesToProcess = new List<String>();
124 <            if (File == "")
124 >            if (String.IsNullOrEmpty(File))
125              {
126                  filesToProcess = Util.getAllXmlFiles(); //no file specified, use all xml files found in same folder
127              }
# Line 134 | Line 137 | namespace xmlTools
137              //---------------------------------------------------XML Injection (start)
138              foreach (String currFile in filesToProcess)
139              {
140 <
141 <                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 >                }
144  
145                  XmlDocument xdoc = new XmlDocument();
146                  xdoc.Load(currFile);
# Line 182 | Line 187 | namespace xmlTools
187              //---------------------------------------------------Parse Operation command (start)
188              try
189              {
190 <                if (forceFiles == null)
190 >                if (String.IsNullOrEmpty(forceFiles))
191                  {
192                      File = getPatchParameter(operation, "File");
193                  }
# Line 201 | Line 206 | namespace xmlTools
206                  return false;
207              }
208  
209 <            if (Element == "")
209 >            if (String.IsNullOrEmpty(Element))
210              {
211                  return false;
212              }
# Line 209 | Line 214 | namespace xmlTools
214              //---------------------------------------------------Parse Operation command (end)
215  
216              List<String> filesToProcess = new List<String>();
217 <            if (File == "")
217 >            if (String.IsNullOrEmpty(File))
218              {
219                  filesToProcess = Util.getAllXmlFiles(); //no file specified, use all xml files found in same folder
220              }
# Line 227 | Line 232 | namespace xmlTools
232              foreach (String currFile in filesToProcess)
233              {
234  
235 <                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 >                }
239  
240                  XmlDocument xdoc = new XmlDocument();
241                  xdoc.Load(currFile);
# Line 263 | Line 271 | namespace xmlTools
271  
272              command = command.Replace("@COMMAND ", ""); //get only the command to process
273  
274 <            if (command.Trim() == "")
274 >            if (String.IsNullOrEmpty(command.Trim()))
275              {
276                  Program.printAppError(Program.appErrors.PATCH_COMMAND_NOT_FOUND, "Error parsing commandOperation in Patch file: Command is empty.");
277                  return false;
# Line 271 | Line 279 | namespace xmlTools
279  
280              try
281              {
282 <                if (forceFiles != null)
282 >                if (!String.IsNullOrEmpty(forceFiles))
283                  {
284                      string paramType = "";
285  
286                      // Filename already exists?
287 <                    if (command.IndexOf("filename:") != -1)
287 >                    if (Util.ContainsIgnoreCase(command,"filename:"))
288                      {
289                          paramType = "filename:";
290                      }
291 <                    else if (command.IndexOf("filename=") != -1)
291 >                    else if (Util.ContainsIgnoreCase(command, "filename="))
292                      {
293                          paramType = "filename=";
294                      }
295                      // Add the filename if it doesn't exists
296                      else
297                      {
298 <                        command = command.Insert(command.Length-1," -filename:" + this.forceFiles); // -2 to be inside quotes
298 >                        command = command.Insert(command.Length," -filename:" + this.forceFiles);
299                      }
300  
301 <                    if (paramType != "")
301 >                    if (!String.IsNullOrEmpty(paramType))
302                      {
303                          int startIdx = command.IndexOf(paramType) + paramType.Length;
304                          int endIdx = command.IndexOf(" ", startIdx); // it may end with space
305                          if (endIdx == -1)
306                          {
307 <                            endIdx = command.IndexOf("\"", startIdx); // or with quotes
307 >                            endIdx = command.IndexOf("\n", startIdx); // or with endline
308 >                            if(endIdx==-1){ // Filename parameters is the last one in the file (file ends with this parameter)
309 >                                endIdx=command.Length-1;
310 >                            }
311                          }
312                          string currFilename = command.Substring(startIdx, endIdx - startIdx);
313                          command = command.Replace(currFilename, this.forceFiles);
# Line 304 | Line 315 | namespace xmlTools
315  
316                  }
317  
318 <                command = command.Replace("\"", ""); // remove quotes
308 <
309 <                ProcessStartInfo startInfo = new ProcessStartInfo();
310 <                if (!Util.IsRunningOnMono())
318 >                if (this.globalNoBackups && !Util.ContainsIgnoreCase(command,"nobackups")) // add noBackup flag if provided as global parameter
319                  {
320 <                    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;
320 >                    command = command.Insert(command.Length, " -nobackups");
321                  }
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
322  
323 <                Process p = System.Diagnostics.Process.Start(startInfo);
330 <                p.OutputDataReceived += commandStdOutputReceived;
331 <                p.ErrorDataReceived += commandStdErrorReceived;
332 <                p.BeginOutputReadLine();
333 <                p.BeginErrorReadLine();
334 <                p.WaitForExit();
323 >                Program.Main(Util.stringToArgsArray(command)); // use the current process is more efficient than start a new one
324              }
325              catch (Exception e)
326              {
# Line 342 | Line 331 | namespace xmlTools
331              return true;
332          }
333  
345        /// <summary>
346        /// Reads asynchronously output from the new process where the command will be executed
347        /// </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                else
362                {
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                else
381                {
382                    Console.Error.WriteLine(myData);
383                }
384            }
385
386        }
387
334          private string getPatchParameter(string line, string parameterName)
335          {
336              string result = "";

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)