Repository navigation
Expand file tree
/
Copy pathDotNetCliInvoker.cs
More file actions
87 lines (74 loc) · 3.4 KB
/
Copy pathDotNetCliInvoker.cs
File metadata and controls
87 lines (74 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Semmle.Util;
using Semmle.Util.Logging;
namespace Semmle.Extraction.CSharp.DependencyFetching
{
/// <summary>
/// Low level utilities to run the "dotnet" command.
/// </summary>
internal sealed class DotNetCliInvoker : IDotNetCliInvoker
{
private readonly ILogger logger;
private readonly IRegistryProxy? proxy;
public string Exec { get; }
public DotNetCliInvoker(ILogger logger, string exec, IRegistryProxy? registryProxy)
{
this.logger = logger;
this.proxy = registryProxy;
this.Exec = exec;
logger.LogInfo($"Using .NET CLI executable: '{Exec}'");
}
private ProcessStartInfo MakeDotnetStartInfo(List<string> args, string? workingDirectory)
{
var startInfo = new ProcessStartInfo(Exec, args)
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
if (!string.IsNullOrWhiteSpace(workingDirectory))
{
startInfo.WorkingDirectory = workingDirectory;
}
// Set minimal environment variables.
foreach (var kvp in IDotNetCliInvoker.MinimalEnvironment)
{
startInfo.EnvironmentVariables[kvp.Key] = kvp.Value;
}
// Configure the proxy settings, if applicable.
proxy?.SetProcessEnvironment(startInfo);
return startInfo;
}
private int RunCommandExitCodeAux(List<string> args, string? workingDirectory, out IList<string> output, out string dirLog, bool silent)
{
dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
var pi = MakeDotnetStartInfo(args, workingDirectory);
var threadId = Environment.CurrentManagedThreadId;
void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId);
void onError(string s) => logger.LogError(s, threadId);
logger.LogInfo($"Running '{Exec} {string.Join(" ", args)}'{dirLog}");
var exitCode = pi.ReadOutput(out output, onOut, onError);
return exitCode;
}
private bool RunCommandAux(List<string> args, string? workingDirectory, out IList<string> output, bool silent)
{
var exitCode = RunCommandExitCodeAux(args, workingDirectory, out output, out var dirLog, silent);
if (exitCode != 0)
{
logger.LogError($"Command '{Exec} {string.Join(" ", args)}'{dirLog} failed with exit code {exitCode}");
return false;
}
return true;
}
public bool RunCommand(List<string> args, bool silent = true) =>
RunCommandAux(args, null, out _, silent);
public int RunCommandExitCode(List<string> args, bool silent = true) =>
RunCommandExitCodeAux(args, null, out _, out _, silent);
public bool RunCommand(List<string> args, out IList<string> output, bool silent = true) =>
RunCommandAux(args, null, out output, silent);
public bool RunCommand(List<string> args, string? workingDirectory, out IList<string> output, bool silent = true) =>
RunCommandAux(args, workingDirectory, out output, silent);
}
}