Skip to content

Commit 0127b77

Browse files
committed
C#: Address review comments.
1 parent 6bfaa90 commit 0127b77

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,10 @@ private void AnalyseProject(FileInfo project)
352352

353353
}
354354

355-
private bool RestoreProject(string project, string? pathToNugetConfig = null) =>
356-
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, pathToNugetConfig);
355+
private bool RestoreProject(string project, out string stdout, string? pathToNugetConfig = null) =>
356+
dotnet.RestoreProjectToDirectory(project, packageDirectory.DirInfo.FullName, out stdout, pathToNugetConfig);
357357

358-
private bool RestoreSolution(string solution, out IList<string> projects) =>
358+
private bool RestoreSolution(string solution, out IEnumerable<string> projects) =>
359359
dotnet.RestoreSolutionToDirectory(solution, packageDirectory.DirInfo.FullName, out projects);
360360

361361
/// <summary>
@@ -370,8 +370,22 @@ private IEnumerable<string> RestoreSolutions(IEnumerable<string> solutions) =>
370370
return restoredProjects;
371371
});
372372

373-
private void RestoreProjects(IEnumerable<string> projects) =>
374-
Parallel.ForEach(projects, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, project => RestoreProject(project));
373+
private void RestoreProjects(IEnumerable<string> projects)
374+
{
375+
var stdoutLines = projects
376+
.AsParallel()
377+
.WithDegreeOfParallelism(options.Threads)
378+
.Select(project =>
379+
{
380+
RestoreProject(project, out var stdout);
381+
return stdout;
382+
})
383+
.ToList();
384+
foreach (var line in stdoutLines)
385+
{
386+
Console.WriteLine(line);
387+
}
388+
}
375389

376390
private void DownloadMissingPackages(List<FileInfo> allFiles)
377391
{
@@ -412,7 +426,8 @@ private void DownloadMissingPackages(List<FileInfo> allFiles)
412426
continue;
413427
}
414428

415-
success = RestoreProject(tempDir.DirInfo.FullName, nugetConfig);
429+
success = RestoreProject(tempDir.DirInfo.FullName, out var stdout, nugetConfig);
430+
Console.WriteLine(stdout);
416431

417432
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
418433
if (!success)

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ private void Info()
3333
}
3434
}
3535

36-
private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput) =>
37-
new ProcessStartInfo(dotnet, args)
36+
private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardOutput)
37+
{
38+
var startInfo = new ProcessStartInfo(dotnet, args)
3839
{
3940
UseShellExecute = false,
4041
RedirectStandardOutput = redirectStandardOutput
4142
};
43+
// Set the .NET CLI language to English to avoid localized output.
44+
startInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en";
45+
return startInfo;
46+
}
4247

4348
private bool RunCommand(string args)
4449
{
@@ -70,17 +75,19 @@ private bool RunCommand(string args, out IList<string> output)
7075
private static string GetRestoreArgs(string projectOrSolutionFile, string packageDirectory) =>
7176
$"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
7277

73-
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, string? pathToNugetConfig = null)
78+
public bool RestoreProjectToDirectory(string projectFile, string packageDirectory, out string stdout, string? pathToNugetConfig = null)
7479
{
7580
var args = GetRestoreArgs(projectFile, packageDirectory);
7681
if (pathToNugetConfig != null)
7782
{
7883
args += $" --configfile \"{pathToNugetConfig}\"";
7984
}
80-
return RunCommand(args);
85+
var success = RunCommand(args, out var output);
86+
stdout = string.Join("\n", output);
87+
return success;
8188
}
8289

83-
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IList<string> projects)
90+
public bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IEnumerable<string> projects)
8491
{
8592
var args = GetRestoreArgs(solutionFile, packageDirectory);
8693
args += " --verbosity normal";
@@ -90,12 +97,11 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
9097
projects = output
9198
.Select(line => regex.Match(line))
9299
.Where(match => match.Success)
93-
.Select(match => match.Groups[1].Value)
94-
.ToList();
100+
.Select(match => match.Groups[1].Value);
95101
return true;
96102
}
97103

98-
projects = new List<string>();
104+
projects = Array.Empty<string>();
99105
return false;
100106
}
101107

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
44
{
55
internal interface IDotNet
66
{
7-
bool RestoreProjectToDirectory(string project, string directory, string? pathToNugetConfig = null);
8-
bool RestoreSolutionToDirectory(string solution, string directory, out IList<string> projects);
7+
bool RestoreProjectToDirectory(string project, string directory, out string stdout, string? pathToNugetConfig = null);
8+
bool RestoreSolutionToDirectory(string solutionFile, string packageDirectory, out IEnumerable<string> projects);
99
bool New(string folder);
1010
bool AddPackage(string folder, string package);
1111
IList<string> GetListedRuntimes();

csharp/extractor/Semmle.Extraction.Tests/Runtime.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Xunit;
2+
using System;
23
using System.Collections.Generic;
34
using Semmle.Extraction.CSharp.DependencyFetching;
45

@@ -18,11 +19,15 @@ public DotNetStub(IList<string> runtimes, IList<string> sdks)
1819

1920
public bool New(string folder) => true;
2021

21-
public bool RestoreProjectToDirectory(string project, string directory, string? pathToNugetConfig = null) => true;
22+
public bool RestoreProjectToDirectory(string project, string directory, out string stdout, string? pathToNugetConfig = null)
23+
{
24+
stdout = "";
25+
return true;
26+
}
2227

23-
public bool RestoreSolutionToDirectory(string solution, string directory, out IList<string> projects)
28+
public bool RestoreSolutionToDirectory(string solution, string directory, out IEnumerable<string> projects)
2429
{
25-
projects = new List<string>();
30+
projects = Array.Empty<string>();
2631
return true;
2732
}
2833

0 commit comments

Comments
 (0)