mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
C#: Make ReadOutput more robust and re-factor RunCommand methods.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user