Executor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.CodeDom.Compiler.Executor.cs
  3. //
  4. // Author(s):
  5. // Andreas Nahr ([email protected])
  6. //
  7. // (C) 2003 Andreas Nahr
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Diagnostics;
  32. using System.Threading;
  33. namespace System.CodeDom.Compiler
  34. {
  35. public sealed class Executor
  36. {
  37. class ProcessResultReader
  38. {
  39. StreamReader reader;
  40. string file;
  41. public ProcessResultReader (StreamReader reader, string file)
  42. {
  43. this.reader = reader;
  44. this.file = file;
  45. }
  46. public void Read ()
  47. {
  48. StreamWriter sw = new StreamWriter (file);
  49. try
  50. {
  51. string line;
  52. while ((line = reader.ReadLine()) != null)
  53. sw.WriteLine (line);
  54. }
  55. finally
  56. {
  57. sw.Close ();
  58. }
  59. }
  60. }
  61. private Executor ()
  62. {
  63. }
  64. public static void ExecWait (string cmd, TempFileCollection tempFiles)
  65. {
  66. string outputName = null;
  67. string errorName = null;
  68. ExecWaitWithCapture (IntPtr.Zero, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  69. }
  70. [MonoTODO ("Do something with userToken")]
  71. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  72. {
  73. if (outputName == null)
  74. outputName = tempFiles.AddExtension ("out");
  75. if (errorName == null)
  76. errorName = tempFiles.AddExtension ("err");
  77. Process proc = new Process();
  78. proc.StartInfo.FileName = cmd;
  79. proc.StartInfo.CreateNoWindow = true;
  80. proc.StartInfo.UseShellExecute = false;
  81. proc.StartInfo.RedirectStandardOutput = true;
  82. proc.StartInfo.RedirectStandardError = true;
  83. proc.StartInfo.WorkingDirectory = currentDir;
  84. try
  85. {
  86. proc.Start();
  87. ProcessResultReader outReader = new ProcessResultReader (proc.StandardOutput, outputName);
  88. ProcessResultReader errReader = new ProcessResultReader (proc.StandardError, errorName);
  89. Thread t = new Thread (new ThreadStart (errReader.Read));
  90. t.Start ();
  91. outReader.Read ();
  92. t.Join ();
  93. proc.WaitForExit();
  94. }
  95. finally
  96. {
  97. proc.Close();
  98. }
  99. return proc.ExitCode;
  100. }
  101. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  102. {
  103. return ExecWaitWithCapture (userToken, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  104. }
  105. public static Int32 ExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName )
  106. {
  107. return ExecWaitWithCapture (IntPtr.Zero, cmd, currentDir, tempFiles, ref outputName, ref errorName);
  108. }
  109. public static Int32 ExecWaitWithCapture (string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  110. {
  111. return ExecWaitWithCapture (IntPtr.Zero, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  112. }
  113. }
  114. }