Refactor dotnet restore command invocation

This commit is contained in:
Tamas Vajk
2023-07-04 12:32:56 +02:00
parent d2b0c872f5
commit 728f3bce2d
3 changed files with 68 additions and 29 deletions

View File

@@ -27,6 +27,7 @@ namespace Semmle.BuildAnalyser
private int conflictedReferences = 0;
private readonly Options options;
private readonly DirectoryInfo sourceDir;
private readonly DotNet dotnet;
/// <summary>
/// Performs a C# build analysis.
@@ -41,6 +42,16 @@ namespace Semmle.BuildAnalyser
this.progressMonitor = progressMonitor;
this.sourceDir = new DirectoryInfo(options.SrcDir);
try
{
this.dotnet = new DotNet(progressMonitor);
}
catch
{
progressMonitor.MissingDotNet();
throw;
}
this.progressMonitor.FindingFiles(options.SrcDir);
this.allSources = GetFiles("*.cs").ToArray();
@@ -82,7 +93,7 @@ namespace Semmle.BuildAnalyser
// TODO: remove the below when the required SDK is installed
using (new FileRenamer(sourceDir.GetFiles("global.json", SearchOption.AllDirectories)))
{
RestoreSolutions(solutions);
Restore(solutions);
}
}
@@ -304,33 +315,17 @@ namespace Semmle.BuildAnalyser
}
private void Restore(string projectOrSolution)
private void Restore(string target)
{
int exit;
try
{
exit = DotNet.RestoreToDirectory(projectOrSolution, packageDirectory.DirInfo.FullName);
}
catch (FileNotFoundException)
{
exit = 2;
}
switch (exit)
{
case 0:
case 1:
// No errors
break;
default:
progressMonitor.CommandFailed("dotnet", $"restore \"{projectOrSolution}\"", exit);
break;
}
dotnet.RestoreToDirectory(target, packageDirectory.DirInfo.FullName);
}
private void RestoreSolutions(IEnumerable<string> solutions)
private void Restore(IEnumerable<string> targets)
{
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = 4 }, Restore);
foreach (var target in targets)
{
Restore(target);
}
}
private void AnalyseSolutions(IEnumerable<string> solutions)

View File

@@ -1,17 +1,51 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
namespace Semmle.BuildAnalyser
{
/// <summary>
/// Utilities to run the "dotnet" command.
/// </summary>
internal static class DotNet
internal class DotNet
{
public static int RestoreToDirectory(string projectOrSolutionFile, string packageDirectory)
private readonly ProgressMonitor progressMonitor;
public DotNet(ProgressMonitor progressMonitor)
{
using var proc = Process.Start("dotnet", $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true");
this.progressMonitor = progressMonitor;
Info();
}
private void Info()
{
try
{
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}.");
}
}
catch (Exception ex)
{
throw new Exception("dotnet --info failed.", ex);
}
}
public void RestoreToDirectory(string projectOrSolutionFile, string packageDirectory)
{
var args = $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
progressMonitor.RunningProcess($"dotnet {args}");
using var proc = Process.Start("dotnet", args);
proc.WaitForExit();
return proc.ExitCode;
if (proc.ExitCode != 0)
{
progressMonitor.CommandFailed("dotnet", args, proc.ExitCode);
}
}
}
}

View File

@@ -97,5 +97,15 @@ namespace Semmle.BuildAnalyser
{
logger.Log(Severity.Error, "Missing nuget.exe");
}
public void MissingDotNet()
{
logger.Log(Severity.Error, "Missing dotnet CLI");
}
public void RunningProcess(string command)
{
logger.Log(Severity.Info, $"Running {command}");
}
}
}