|
1 | 1 | using System;
|
2 | 2 | using System.Diagnostics;
|
| 3 | +using System.Text; |
3 | 4 |
|
4 | 5 | namespace QtSharp
|
5 | 6 | {
|
6 | 7 | public class ProcessHelper
|
7 | 8 | {
|
8 |
| - public static string Run(string path, string args, out string error, bool readOutputByLines = false) |
| 9 | + public static string Run(string path, string args, out string error) |
9 | 10 | {
|
10 |
| - try |
| 11 | + using (Process process = new Process()) |
11 | 12 | {
|
12 |
| - using (Process process = new Process()) |
13 |
| - { |
14 |
| - Console.WriteLine("Run: " + path); |
15 |
| - Console.WriteLine("Args: " + args); |
16 |
| - process.StartInfo.FileName = path; |
17 |
| - process.StartInfo.Arguments = args; |
18 |
| - process.StartInfo.UseShellExecute = false; |
19 |
| - process.StartInfo.RedirectStandardOutput = true; |
20 |
| - process.StartInfo.RedirectStandardError = true; |
| 13 | + Console.WriteLine("Run: " + path); |
| 14 | + Console.WriteLine("Args: " + args); |
| 15 | + process.StartInfo.FileName = path; |
| 16 | + process.StartInfo.Arguments = args; |
| 17 | + process.StartInfo.UseShellExecute = false; |
| 18 | + process.StartInfo.RedirectStandardOutput = true; |
| 19 | + process.StartInfo.RedirectStandardError = true; |
21 | 20 |
|
22 |
| - var reterror = ""; |
23 |
| - var retout = ""; |
24 |
| - process.OutputDataReceived += (sender, outargs) => |
| 21 | + var reterror = new StringBuilder(); |
| 22 | + var retout = new StringBuilder(); |
| 23 | + process.OutputDataReceived += (sender, outargs) => |
25 | 24 | {
|
26 |
| - if (retout != "" && outargs.Data != "") retout += "\r\n"; |
27 |
| - retout += outargs.Data; |
28 |
| - Console.WriteLine("stdout: {0}", retout); |
| 25 | + if (!string.IsNullOrEmpty(outargs.Data)) |
| 26 | + { |
| 27 | + if (retout.Length > 0) |
| 28 | + retout.AppendLine(); |
| 29 | + retout.Append(outargs.Data); |
| 30 | + Console.WriteLine("stdout: {0}", outargs.Data); |
| 31 | + } |
29 | 32 | };
|
30 |
| - process.ErrorDataReceived += (sender, errargs) => |
| 33 | + process.ErrorDataReceived += (sender, errargs) => |
31 | 34 | {
|
32 |
| - if (reterror != "" && errargs.Data != "") reterror += "\r\n"; |
33 |
| - reterror += errargs.Data; |
34 |
| - Console.WriteLine("stderr: {0}", reterror); |
| 35 | + if (!string.IsNullOrEmpty(errargs.Data)) |
| 36 | + { |
| 37 | + if (reterror.Length > 0) |
| 38 | + reterror.AppendLine(); |
| 39 | + reterror.Append(errargs.Data); |
| 40 | + Console.WriteLine("stderr: {0}", errargs.Data); |
| 41 | + } |
35 | 42 | };
|
36 | 43 |
|
37 |
| - process.Start(); |
38 |
| - process.BeginOutputReadLine(); |
39 |
| - process.BeginErrorReadLine(); |
40 |
| - process.WaitForExit(); |
41 |
| - process.CancelOutputRead(); |
42 |
| - process.CancelErrorRead(); |
| 44 | + process.Start(); |
| 45 | + process.BeginOutputReadLine(); |
| 46 | + process.BeginErrorReadLine(); |
| 47 | + process.WaitForExit(); |
| 48 | + process.CancelOutputRead(); |
| 49 | + process.CancelErrorRead(); |
43 | 50 |
|
44 |
| - error = reterror; |
45 |
| - if (process.ExitCode != 0) |
46 |
| - throw new Exception("Exit Code is not 0"); |
47 |
| - return readOutputByLines ? string.Empty : retout.Trim().Replace(@"\\", @"\"); |
48 |
| - } |
49 |
| - } |
50 |
| - catch (Exception exception) |
51 |
| - { |
52 |
| - error = string.Format("Calling {0} caused an exception: {1}.", path, exception.Message); |
53 |
| - return string.Empty; |
| 51 | + error = reterror.ToString(); |
| 52 | + if (process.ExitCode != 0) |
| 53 | + Console.WriteLine($"Error. Process exited with code {process.ExitCode}."); |
| 54 | + return retout.ToString(); |
54 | 55 | }
|
55 | 56 | }
|
56 | 57 | }
|
|
0 commit comments