Skip to content

Commit d48ab36

Browse files
michaelnebeltamasvajk
authored andcommitted
C#: Run dotnet exec command silently.
1 parent d391246 commit d48ab36

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
133133
}
134134

135135
var views = GetFiles("*.cshtml")
136-
.Concat(GetFiles("*.razor"))
137-
.ToArray();
136+
.Concat(GetFiles("*.razor"));
138137

139-
if (views.Length > 0)
138+
if (views.Any())
140139
{
141140
// TODO: use SDK specified in global.json
142141
// TODO: add feature flag to control razor generation

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,35 @@ private void Info()
3434
}
3535
}
3636

37-
private bool RunCommand(string args)
37+
private static ProcessStartInfo MakeDotnetStartInfo(string args) =>
38+
new ProcessStartInfo(dotnet, args)
39+
{
40+
UseShellExecute = false,
41+
RedirectStandardOutput = true
42+
};
43+
44+
private bool RunCommandAux(string args, bool silent)
3845
{
3946
progressMonitor.RunningProcess($"{dotnet} {args}");
40-
using var proc = Process.Start(dotnet, args);
41-
proc.WaitForExit();
42-
if (proc.ExitCode != 0)
47+
using var proc = silent
48+
? Process.Start(MakeDotnetStartInfo(args))
49+
: Process.Start(dotnet, args);
50+
proc?.WaitForExit();
51+
var exitCode = proc?.ExitCode ?? -1;
52+
if (exitCode != 0)
4353
{
44-
progressMonitor.CommandFailed(dotnet, args, proc.ExitCode);
54+
progressMonitor.CommandFailed(dotnet, args, exitCode);
4555
return false;
4656
}
47-
4857
return true;
4958
}
5059

60+
private bool RunCommand(string args) =>
61+
RunCommandAux(args, false);
62+
63+
private bool RunCommandSilently(string args) =>
64+
RunCommandAux(args, true);
65+
5166
public bool RestoreToDirectory(string projectOrSolutionFile, string packageDirectory, string? pathToNugetConfig = null)
5267
{
5368
var args = $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
@@ -75,11 +90,7 @@ public bool AddPackage(string folder, string package)
7590
private IList<string> GetListed(string args, string artifact)
7691
{
7792
progressMonitor.RunningProcess($"{dotnet} {args}");
78-
var pi = new ProcessStartInfo(dotnet, args)
79-
{
80-
RedirectStandardOutput = true,
81-
UseShellExecute = false
82-
};
93+
var pi = MakeDotnetStartInfo(args);
8394
var exitCode = pi.ReadOutput(out var artifacts);
8495
if (exitCode != 0)
8596
{
@@ -92,9 +103,8 @@ private IList<string> GetListed(string args, string artifact)
92103

93104
public bool Exec(string execArgs)
94105
{
95-
// TODO: we might need to swallow the stdout of the started process to not pollute the logs of the extraction.
96106
var args = $"exec {execArgs}";
97-
return RunCommand(args);
107+
return RunCommandSilently(args);
98108
}
99109
}
100110
}

0 commit comments

Comments
 (0)