C#: Use dotnet --list-runtimes to find runtime locations.

This commit is contained in:
Michael Nebel
2023-07-05 13:36:44 +02:00
parent bb521d7c75
commit 6a87755ff1
3 changed files with 139 additions and 44 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Semmle.Util;
namespace Semmle.BuildAnalyser
{
@@ -8,6 +10,7 @@ namespace Semmle.BuildAnalyser
/// </summary>
internal class DotNet
{
private const string dotnet = "dotnet";
private readonly ProgressMonitor progressMonitor;
public DotNet(ProgressMonitor progressMonitor)
@@ -19,26 +22,26 @@ namespace Semmle.BuildAnalyser
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");
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}.");
progressMonitor.CommandFailed(dotnet, "--info", ret);
throw new Exception($"{dotnet} --info failed with exit code {ret}.");
}
}
private bool RunCommand(string args)
{
progressMonitor.RunningProcess($"dotnet {args}");
using var proc = Process.Start("dotnet", args);
progressMonitor.RunningProcess($"{dotnet} {args}");
using var proc = Process.Start(dotnet, args);
proc.WaitForExit();
if (proc.ExitCode != 0)
{
progressMonitor.CommandFailed("dotnet", args, proc.ExitCode);
progressMonitor.CommandFailed(dotnet, args, proc.ExitCode);
return false;
}
@@ -62,5 +65,22 @@ namespace Semmle.BuildAnalyser
var args = $"add \"{folder}\" package \"{package}\" --no-restore";
return RunCommand(args);
}
public IList<string> GetListedRuntimes()
{
var args = "--list-runtimes";
var pi = new ProcessStartInfo(dotnet, args)
{
RedirectStandardOutput = true,
UseShellExecute = false
};
var exitCode = pi.ReadOutput(out var runtimes);
if (exitCode != 0)
{
progressMonitor.CommandFailed(dotnet, args, exitCode);
return new List<string>();
}
return runtimes;
}
}
}