forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (158 loc) · 5 KB
/
Program.cs
File metadata and controls
185 lines (158 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
namespace UnityEditor.Utils
{
internal class Program : IDisposable
{
private ProcessOutputStreamReader _stdout;
private ProcessOutputStreamReader _stderr;
private Stream _stdin;
public Process _process;
protected Program()
{
_process = new Process();
}
public Program(ProcessStartInfo si)
: this()
{
_process.StartInfo = si;
}
public void Start()
{
Start(null);
}
public void Start(EventHandler exitCallback)
{
if (exitCallback != null)
{
_process.EnableRaisingEvents = true;
_process.Exited += exitCallback;
}
_process.StartInfo.RedirectStandardInput = true;
_process.StartInfo.RedirectStandardError = true;
_process.StartInfo.RedirectStandardOutput = true;
_process.StartInfo.UseShellExecute = false;
_process.Start();
_stdout = new ProcessOutputStreamReader(_process, _process.StandardOutput);
_stderr = new ProcessOutputStreamReader(_process, _process.StandardError);
_stdin = _process.StandardInput.BaseStream;
}
public ProcessStartInfo GetProcessStartInfo()
{
return _process.StartInfo;
}
public void LogProcessStartInfo()
{
if (_process != null)
LogProcessStartInfo(_process.StartInfo);
else
Console.WriteLine("Failed to retrieve process startInfo");
}
//please dont kill this code.
private static void LogProcessStartInfo(ProcessStartInfo si)
{
Console.WriteLine("Filename: " + si.FileName);
Console.WriteLine("Arguments: " + si.Arguments);
foreach (DictionaryEntry envVar in si.EnvironmentVariables)
if (envVar.Key.ToString().StartsWith("MONO"))
Console.WriteLine("{0}: {1}", envVar.Key, envVar.Value);
int responsefileindex = si.Arguments.IndexOf("Temp/UnityTempFile");
if (responsefileindex > 0)
{
var responsefile = si.Arguments.Substring(responsefileindex);
Console.WriteLine("Responsefile: " + responsefile + " Contents: ");
Console.WriteLine(System.IO.File.ReadAllText(responsefile));
}
}
public string GetAllOutput()
{
var sb = new System.Text.StringBuilder();
sb.AppendLine("stdout:");
foreach (var s in GetStandardOutput())
sb.AppendLine(s);
sb.AppendLine("stderr:");
foreach (var s in GetErrorOutput())
sb.AppendLine(s);
return sb.ToString();
}
public bool HasExited
{
get
{
if (_process == null)
throw new InvalidOperationException("You cannot call HasExited before calling Start");
try
{
return _process.HasExited;
}
catch (InvalidOperationException)
{
return true;
}
}
}
public int ExitCode
{
get { return _process.ExitCode; }
}
public int Id
{
get { return _process.Id; }
}
public void Dispose()
{
Kill();
_process.Dispose();
}
public void Kill()
{
if (!HasExited)
{
_process.Kill();
_process.WaitForExit();
}
}
public Stream GetStandardInput()
{
return _stdin;
}
public string[] GetStandardOutput()
{
return _stdout.GetOutput();
}
public string GetStandardOutputAsString()
{
var output = GetStandardOutput();
return GetOutputAsString(output);
}
public string[] GetErrorOutput()
{
return _stderr.GetOutput();
}
public string GetErrorOutputAsString()
{
var output = GetErrorOutput();
return GetOutputAsString(output);
}
private static string GetOutputAsString(string[] output)
{
var sb = new System.Text.StringBuilder();
foreach (var t in output)
sb.AppendLine(t);
return sb.ToString();
}
public void WaitForExit()
{
_process.WaitForExit();
}
public bool WaitForExit(int milliseconds)
{
return _process.WaitForExit(milliseconds);
}
}
}