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) => private bool RestoreProject(string project, out string stdout, string? pathToNugetConfig = null) =>
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, pathToNugetConfig); 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); dotnet.RestoreSolutionToDirectory(solution, packageDirectory.DirInfo.FullName, out projects);
/// <summary> /// <summary>
@@ -370,8 +370,22 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return restoredProjects; return restoredProjects;
}); });
private void RestoreProjects(IEnumerable<string> projects) => private void RestoreProjects(IEnumerable<string> projects)
Parallel.ForEach(projects, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, project => RestoreProject(project)); {
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) private void DownloadMissingPackages(List<FileInfo> allFiles)
{ {
@@ -412,7 +426,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
continue; 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. // TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
if (!success) if (!success)

View File

@@ -33,12 +33,17 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
} }
} }
private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput) => private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput)
new ProcessStartInfo(dotnet, args) {
var startInfo = new ProcessStartInfo(dotnet, args)
{ {
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = redirectStandardOutput 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) private bool RunCommand(string args)
{ {
@@ -70,17 +75,19 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
private static string GetRestoreArgs(string projectOrSolutionFile, string packageDirectory) => private static string GetRestoreArgs(string projectOrSolutionFile, string packageDirectory) =>
$"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true"; $"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); var args = GetRestoreArgs(projectFile, packageDirectory);
if (pathToNugetConfig != null) if (pathToNugetConfig != null)
{ {
args += $" --configfile \"{pathToNugetConfig}\""; 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); var args = GetRestoreArgs(solutionFile, packageDirectory);
args += " --verbosity normal"; args += " --verbosity normal";
@@ -90,12 +97,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
projects = output projects = output
.Select(line => regex.Match(line)) .Select(line => regex.Match(line))
.Where(match => match.Success) .Where(match => match.Success)
.Select(match => match.Groups[1].Value) .Select(match => match.Groups[1].Value);
.ToList();
return true; return true;
} }
projects = new List<string>(); projects = Array.Empty<string>();
return false; return false;
} }

View File

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

View File

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