C#: Address review comments.

This commit is contained in:
Michael Nebel
2023-09-13 11:25:39 +02:00
parent 6bfaa90fe4
commit 0127b779b5
4 changed files with 46 additions and 20 deletions

View File

@@ -352,10 +352,10 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}
private bool RestoreProject(string project, string? pathToNugetConfig = null) =>
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, pathToNugetConfig);
private bool RestoreProject(string project, out string stdout, string? pathToNugetConfig = null) =>
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, out stdout, pathToNugetConfig);
private bool RestoreSolution(string solution, out IList<string> projects) =>
private bool RestoreSolution(string solution, out IEnumerable<string> projects) =>
dotnet.RestoreSolutionToDirectory(solution, packageDirectory.DirInfo.FullName, out projects);
/// <summary>
@@ -370,8 +370,22 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return restoredProjects;
});
private void RestoreProjects(IEnumerable<string> projects) =>
Parallel.ForEach(projects, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, project => RestoreProject(project));
private void RestoreProjects(IEnumerable<string> projects)
{
var stdoutLines = projects
.AsParallel()
.WithDegreeOfParallelism(options.Threads)
.Select(project =>
{
RestoreProject(project, out var stdout);
return stdout;
})
.ToList();
foreach (var line in stdoutLines)
{
Console.WriteLine(line);
}
}
private void DownloadMissingPackages(List<FileInfo> allFiles)
{
@@ -412,7 +426,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
continue;
}
success = RestoreProject(tempDir.DirInfo.FullName, nugetConfig);
success = RestoreProject(tempDir.DirInfo.FullName, out var stdout, nugetConfig);
Console.WriteLine(stdout);
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
if (!success)

View File

@@ -33,12 +33,17 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}
}
private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput) =>
new ProcessStartInfo(dotnet, args)
private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput)
{
var startInfo = new ProcessStartInfo(dotnet, args)
{
UseShellExecute = false,
RedirectStandardOutput = redirectStandardOutput
};
// Set the .NET CLI language to English to avoid localized output.
startInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en";
return startInfo;
}
private bool RunCommand(string args)
{
@@ -70,17 +75,19 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
private static string GetRestoreArgs(string projectOrSolutionFile, string packageDirectory) =>
$"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, string? pathToNugetConfig = null)
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, out string stdout, string? pathToNugetConfig = null)
{
var args = GetRestoreArgs(projectFile, packageDirectory);
if (pathToNugetConfig != null)
{
args += $" --configfile \"{pathToNugetConfig}\"";
}
return RunCommand(args);
var success = RunCommand(args, out var output);
stdout = string.Join("\n", output);
return success;
}
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IList<string> projects)
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IEnumerable<string> projects)
{
var args = GetRestoreArgs(solutionFile, packageDirectory);
args += " --verbosity normal";
@@ -90,12 +97,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
projects = output
.Select(line => regex.Match(line))
.Where(match => match.Success)
.Select(match => match.Groups[1].Value)
.ToList();
.Select(match => match.Groups[1].Value);
return true;
}
projects = new List<string>();
projects = Array.Empty<string>();
return false;
}

View File

@@ -4,8 +4,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
{
internal interface IDotNet
{
bool RestoreProjectToDirectory(string project, string directory, string? pathToNugetConfig = null);
bool RestoreSolutionToDirectory(string solution, string directory, out IList<string> projects);
bool RestoreProjectToDirectory(string project, string directory, out string stdout, string? pathToNugetConfig = null);
bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IEnumerable<string> projects);
bool New(string folder);
bool AddPackage(string folder, string package);
IList<string> GetListedRuntimes();

View File

@@ -1,4 +1,5 @@
using Xunit;
using System;
using System.Collections.Generic;
using Semmle.Extraction.CSharp.DependencyFetching;
@@ -18,11 +19,15 @@ namespace Semmle.Extraction.Tests
public bool New(string folder) => true;
public bool RestoreProjectToDirectory(string project, string directory, string? pathToNugetConfig = null) => true;
public bool RestoreSolutionToDirectory(string solution, string directory, out IList<string> projects)
public bool RestoreProjectToDirectory(string project, string directory, out string stdout, string? pathToNugetConfig = null)
{
projects = new List<string>();
stdout = "";
return true;
}
public bool RestoreSolutionToDirectory(string solution, string directory, out IEnumerable<string> projects)
{
projects = Array.Empty<string>();
return true;
}