Files
codeql/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs
2023-08-21 10:38:22 +02:00

112 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Semmle.Util;
namespace Semmle.Extraction.CSharp.DependencyFetching
{
/// <summary>
/// Utilities to run the "dotnet" command.
/// </summary>
internal class DotNet : IDotNet
{
private const string dotnet = "dotnet";
private readonly ProgressMonitor progressMonitor;
public DotNet(ProgressMonitor progressMonitor)
{
this.progressMonitor = progressMonitor;
Info();
}
private void Info()
{
// TODO: make sure the below `dotnet` version is matching the one specified in global.json
progressMonitor.RunningProcess($"{dotnet} --info");
using var proc = Process.Start(dotnet, "--info");
proc.WaitForExit();
var ret = proc.ExitCode;
if (ret != 0)
{
progressMonitor.CommandFailed(dotnet, "--info", ret);
throw new Exception($"{dotnet} --info failed with exit code {ret}.");
}
}
private static ProcessStartInfo MakeDotnetStartInfo(string args) =>
new ProcessStartInfo(dotnet, args)
{
UseShellExecute = false,
RedirectStandardOutput = true
};
private bool RunCommandAux(string args, bool silent)
{
progressMonitor.RunningProcess($"{dotnet} {args}");
using var proc = silent
? Process.Start(MakeDotnetStartInfo(args))
: Process.Start(dotnet, args);
proc?.WaitForExit();
var exitCode = proc?.ExitCode ?? -1;
if (exitCode != 0)
{
progressMonitor.CommandFailed(dotnet, args, exitCode);
return false;
}
return true;
}
private bool RunCommand(string args) =>
RunCommandAux(args, false);
private bool RunCommandSilently(string args) =>
RunCommandAux(args, true);
public bool RestoreToDirectory(string projectOrSolutionFile, string packageDirectory, string? pathToNugetConfig = null)
{
var args = $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
if (pathToNugetConfig != null)
args += $" --configfile \"{pathToNugetConfig}\"";
return RunCommand(args);
}
public bool New(string folder)
{
var args = $"new console --no-restore --output \"{folder}\"";
return RunCommand(args);
}
public bool AddPackage(string folder, string package)
{
var args = $"add \"{folder}\" package \"{package}\" --no-restore";
return RunCommand(args);
}
public IList<string> GetListedRuntimes() => GetListed("--list-runtimes", "runtime");
public IList<string> GetListedSdks() => GetListed("--list-sdks", "SDK");
private IList<string> GetListed(string args, string artifact)
{
progressMonitor.RunningProcess($"{dotnet} {args}");
var pi = MakeDotnetStartInfo(args);
var exitCode = pi.ReadOutput(out var artifacts);
if (exitCode != 0)
{
progressMonitor.CommandFailed(dotnet, args, exitCode);
return new List<string>();
}
progressMonitor.LogInfo($"Found {artifact}s: {string.Join("\n", artifacts)}");
return artifacts;
}
public bool Exec(string execArgs)
{
var args = $"exec {execArgs}";
//return RunCommandSilently(args);
return RunCommand(args);
}
}
}