Skip to content

Commit 05c5f3e

Browse files
committed
C#: Make ReadOutput more robust and re-factor RunCommand methods.
1 parent d60055b commit 05c5f3e

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotnetCommand.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,10 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardO
3131
return startInfo;
3232
}
3333

34-
public bool RunCommand(string args)
34+
private bool RunCommandAux(string args, bool redirectStandardOutput, out IList<string> output)
3535
{
3636
progressMonitor.RunningProcess($"{Exec} {args}");
37-
using var proc = Process.Start(MakeDotnetStartInfo(args, redirectStandardOutput: false));
38-
proc?.WaitForExit();
39-
var exitCode = proc?.ExitCode ?? -1;
40-
if (exitCode != 0)
41-
{
42-
progressMonitor.CommandFailed(Exec, args, exitCode);
43-
return false;
44-
}
45-
return true;
46-
}
47-
48-
public bool RunCommand(string args, out IList<string> output)
49-
{
50-
progressMonitor.RunningProcess($"{Exec} {args}");
51-
var pi = MakeDotnetStartInfo(args, redirectStandardOutput: true);
37+
var pi = MakeDotnetStartInfo(args, redirectStandardOutput);
5238
var exitCode = pi.ReadOutput(out output);
5339
if (exitCode != 0)
5440
{
@@ -57,5 +43,11 @@ public bool RunCommand(string args, out IList<string> output)
5743
}
5844
return true;
5945
}
46+
47+
public bool RunCommand(string args) =>
48+
RunCommandAux(args, redirectStandardOutput: false, out _);
49+
50+
public bool RunCommand(string args, out IList<string> output) =>
51+
RunCommandAux(args, redirectStandardOutput: true, out output);
6052
}
6153
}

csharp/extractor/Semmle.Util/ProcessStartInfoExtensions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ public static int ReadOutput(this ProcessStartInfo pi, out IList<string> stdout)
1919
return -1;
2020
}
2121

22-
string? s;
23-
do
22+
if (pi.RedirectStandardOutput && !pi.UseShellExecute)
2423
{
25-
s = process.StandardOutput.ReadLine();
26-
if (s is not null)
27-
stdout.Add(s);
24+
string? s;
25+
do
26+
{
27+
s = process.StandardOutput.ReadLine();
28+
if (s is not null)
29+
{
30+
stdout.Add(s);
31+
}
32+
}
33+
while (s is not null);
2834
}
29-
while (s is not null);
3035
process.WaitForExit();
3136
return process.ExitCode;
3237
}

0 commit comments

Comments
 (0)