C#: Make ReadOutput more robust and re-factor RunCommand methods.

This commit is contained in:
Michael Nebel
2023-09-15 11:49:07 +02:00
parent d60055b148
commit 05c5f3e050
2 changed files with 19 additions and 22 deletions

View File

@@ -31,24 +31,10 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return startInfo;
}
public bool RunCommand(string args)
private bool RunCommandAux(string args, bool redirectStandardOutput, out IList<string> output)
{
progressMonitor.RunningProcess($"{Exec} {args}");
using var proc = Process.Start(MakeDotnetStartInfo(args, redirectStandardOutput: false));
proc?.WaitForExit();
var exitCode = proc?.ExitCode ?? -1;
if (exitCode != 0)
{
progressMonitor.CommandFailed(Exec, args, exitCode);
return false;
}
return true;
}
public bool RunCommand(string args, out IList<string> output)
{
progressMonitor.RunningProcess($"{Exec} {args}");
var pi = MakeDotnetStartInfo(args, redirectStandardOutput: true);
var pi = MakeDotnetStartInfo(args, redirectStandardOutput);
var exitCode = pi.ReadOutput(out output);
if (exitCode != 0)
{
@@ -57,5 +43,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}
return true;
}
public bool RunCommand(string args) =>
RunCommandAux(args, redirectStandardOutput: false, out _);
public bool RunCommand(string args, out IList<string> output) =>
RunCommandAux(args, redirectStandardOutput: true, out output);
}
}

View File

@@ -19,14 +19,19 @@ namespace Semmle.Util
return -1;
}
string? s;
do
if (pi.RedirectStandardOutput && !pi.UseShellExecute)
{
s = process.StandardOutput.ReadLine();
if (s is not null)
stdout.Add(s);
string? s;
do
{
s = process.StandardOutput.ReadLine();
if (s is not null)
{
stdout.Add(s);
}
}
while (s is not null);
}
while (s is not null);
process.WaitForExit();
return process.ExitCode;
}