Skip to content

Commit f9f4c75

Browse files
committed
Use MonoAndroidHelper.RunProcess
1 parent efcba2e commit f9f4c75

File tree

2 files changed

+34
-54
lines changed

2 files changed

+34
-54
lines changed

src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ void AppendLines (string prefix, List<string> lines, StringBuilder sb)
5252
}
5353
}
5454

55-
public static int RunProcess (string command, string arguments, TaskLoggingHelper log, DataReceivedEventHandler? onOutput = null, DataReceivedEventHandler? onError = null)
55+
public static int RunProcess (string command, string arguments, TaskLoggingHelper log, DataReceivedEventHandler? onOutput = null, DataReceivedEventHandler? onError = null, CancellationToken? cancellationToken = null, Action? cancelTask = null, bool logWarningOnFailure = true)
56+
{
57+
return RunProcess ("Running process", command, arguments, log, onOutput, onError, cancellationToken, cancelTask, logWarningOnFailure);
58+
}
59+
60+
public static int RunProcess (string logLabel, string command, string arguments, TaskLoggingHelper log, DataReceivedEventHandler? onOutput = null, DataReceivedEventHandler? onError = null,
61+
CancellationToken? cancellationToken = null, Action? cancelTask = null, bool logWarningOnFailure = true)
5662
{
5763
var stdout_completed = new ManualResetEvent (false);
5864
var stderr_completed = new ManualResetEvent (false);
@@ -69,7 +75,7 @@ public static int RunProcess (string command, string arguments, TaskLoggingHelpe
6975
var stdoutLines = new List<string> ();
7076
var stderrLines = new List<string> ();
7177

72-
log.LogDebugMessage ($"Running process: {psi.FileName} {psi.Arguments}");
78+
log.LogDebugMessage ($"{logLabel}: {psi.FileName} {psi.Arguments}");
7379
using var proc = new Process ();
7480
proc.OutputDataReceived += (s, e) => {
7581
if (e.Data != null) {
@@ -91,6 +97,7 @@ public static int RunProcess (string command, string arguments, TaskLoggingHelpe
9197
proc.Start ();
9298
proc.BeginOutputReadLine ();
9399
proc.BeginErrorReadLine ();
100+
cancellationToken?.Register (() => { try { proc.Kill (); } catch (Exception) { } });
94101
proc.WaitForExit ();
95102

96103
if (psi.RedirectStandardError) {
@@ -101,9 +108,13 @@ public static int RunProcess (string command, string arguments, TaskLoggingHelpe
101108
stdout_completed.WaitOne (TimeSpan.FromSeconds (30));
102109
}
103110

111+
log.LogDebugMessage ($"{logLabel}: exit code == {proc.ExitCode}");
104112
if (proc.ExitCode != 0) {
105-
var sb = MergeStdoutAndStderrMessages (stdoutLines, stderrLines);
106-
log.LogCodedError ("XA0142", Properties.Resources.XA0142, $"{psi.FileName} {psi.Arguments}", sb.ToString ());
113+
if (logWarningOnFailure) {
114+
var sb = MergeStdoutAndStderrMessages (stdoutLines, stderrLines);
115+
log.LogCodedError ("XA0142", Properties.Resources.XA0142, $"{psi.FileName} {psi.Arguments}", sb.ToString ());
116+
}
117+
cancelTask?.Invoke ();
107118
}
108119

109120
try {

src/Xamarin.Android.Build.Tasks/Utilities/NativeLinker.cs

+19-50
Original file line numberDiff line numberDiff line change
@@ -356,62 +356,31 @@ bool RunLinker (List<string> args, ITaskItem outputSharedLibrary)
356356

357357
bool RunCommand (string label, string binaryPath, List<string> args, List<string> stdoutLines, List<string> stderrLines)
358358
{
359-
using var stdout_completed = new ManualResetEvent (false);
360-
using var stderr_completed = new ManualResetEvent (false);
361-
var psi = new ProcessStartInfo () {
362-
FileName = binaryPath,
363-
Arguments = String.Join (" ", args),
364-
UseShellExecute = false,
365-
RedirectStandardOutput = true,
366-
RedirectStandardError = true,
367-
CreateNoWindow = true,
368-
WindowStyle = ProcessWindowStyle.Hidden,
369-
};
370-
371359
string binaryName = Path.GetFileName (ld);
372-
log.LogDebugMessage ($"[{label}] {psi.FileName} {psi.Arguments}");
373-
374-
using var proc = new Process ();
375-
proc.OutputDataReceived += (s, e) => {
376-
if (e.Data != null) {
360+
return MonoAndroidHelper.RunProcess (
361+
label,
362+
binaryPath,
363+
String.Join (" ", args),
364+
log,
365+
onOutput: (s, e) => {
366+
if (e.Data == null) {
367+
return;
368+
}
377369
OnOutputData (binaryName, s, e);
378370
stdoutLines.Add (e.Data);
379-
} else {
380-
stdout_completed.Set ();
381-
}
382-
};
371+
},
372+
onError: (s, e) => {
373+
if (e.Data != null) {
374+
return;
375+
}
383376

384-
proc.ErrorDataReceived += (s, e) => {
385-
if (e.Data != null) {
386377
OnErrorData (binaryName, s, e);
387378
stderrLines.Add (e.Data);
388-
} else {
389-
stderr_completed.Set ();
390-
}
391-
};
392-
393-
proc.StartInfo = psi;
394-
proc.Start ();
395-
proc.BeginOutputReadLine ();
396-
proc.BeginErrorReadLine ();
397-
cancellationToken?.Register (() => { try { proc.Kill (); } catch (Exception) { } });
398-
proc.WaitForExit ();
399-
400-
if (psi.RedirectStandardError) {
401-
stderr_completed.WaitOne (TimeSpan.FromSeconds (30));
402-
}
403-
404-
if (psi.RedirectStandardOutput) {
405-
stdout_completed.WaitOne (TimeSpan.FromSeconds (30));
406-
}
407-
408-
log.LogDebugMessage ($"[{label}] exit code == {proc.ExitCode}");
409-
if (proc.ExitCode != 0) {
410-
cancelTask?.Invoke ();
411-
return false;
412-
}
413-
414-
return true;
379+
},
380+
cancellationToken,
381+
cancelTask,
382+
logWarningOnFailure: false
383+
) == 0;
415384
}
416385

417386
void OnOutputData (string linkerName, object sender, DataReceivedEventArgs e)

0 commit comments

Comments
 (0)