Skip to content

Commit 728f3bc

Browse files
committed
Refactor dotnet restore command invocation
1 parent d2b0c87 commit 728f3bc

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal sealed class BuildAnalysis : IDisposable
2727
private int conflictedReferences = 0;
2828
private readonly Options options;
2929
private readonly DirectoryInfo sourceDir;
30+
private readonly DotNet dotnet;
3031

3132
/// <summary>
3233
/// Performs a C# build analysis.
@@ -41,6 +42,16 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
4142
this.progressMonitor = progressMonitor;
4243
this.sourceDir = new DirectoryInfo(options.SrcDir);
4344

45+
try
46+
{
47+
this.dotnet = new DotNet(progressMonitor);
48+
}
49+
catch
50+
{
51+
progressMonitor.MissingDotNet();
52+
throw;
53+
}
54+
4455
this.progressMonitor.FindingFiles(options.SrcDir);
4556

4657
this.allSources = GetFiles("*.cs").ToArray();
@@ -82,7 +93,7 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
8293
// TODO: remove the below when the required SDK is installed
8394
using (new FileRenamer(sourceDir.GetFiles("global.json", SearchOption.AllDirectories)))
8495
{
85-
RestoreSolutions(solutions);
96+
Restore(solutions);
8697
}
8798
}
8899

@@ -304,33 +315,17 @@ private void AnalyseProject(FileInfo project)
304315

305316
}
306317

307-
private void Restore(string projectOrSolution)
318+
private void Restore(string target)
308319
{
309-
int exit;
310-
try
311-
{
312-
exit = DotNet.RestoreToDirectory(projectOrSolution, packageDirectory.DirInfo.FullName);
313-
}
314-
catch (FileNotFoundException)
315-
{
316-
exit = 2;
317-
}
318-
319-
switch (exit)
320-
{
321-
case 0:
322-
case 1:
323-
// No errors
324-
break;
325-
default:
326-
progressMonitor.CommandFailed("dotnet", $"restore \"{projectOrSolution}\"", exit);
327-
break;
328-
}
320+
dotnet.RestoreToDirectory(target, packageDirectory.DirInfo.FullName);
329321
}
330322

331-
private void RestoreSolutions(IEnumerable<string> solutions)
323+
private void Restore(IEnumerable<string> targets)
332324
{
333-
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = 4 }, Restore);
325+
foreach (var target in targets)
326+
{
327+
Restore(target);
328+
}
334329
}
335330

336331
private void AnalyseSolutions(IEnumerable<string> solutions)
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Diagnostics;
23

34
namespace Semmle.BuildAnalyser
45
{
56
/// <summary>
67
/// Utilities to run the "dotnet" command.
78
/// </summary>
8-
internal static class DotNet
9+
internal class DotNet
910
{
10-
public static int RestoreToDirectory(string projectOrSolutionFile, string packageDirectory)
11+
private readonly ProgressMonitor progressMonitor;
12+
13+
public DotNet(ProgressMonitor progressMonitor)
14+
{
15+
this.progressMonitor = progressMonitor;
16+
Info();
17+
}
18+
19+
private void Info()
20+
{
21+
try
22+
{
23+
progressMonitor.RunningProcess("dotnet --info");
24+
using var proc = Process.Start("dotnet", "--info");
25+
proc.WaitForExit();
26+
var ret = proc.ExitCode;
27+
if (ret != 0)
28+
{
29+
progressMonitor.CommandFailed("dotnet", "--info", ret);
30+
throw new Exception($"dotnet --info failed with exit code {ret}.");
31+
}
32+
}
33+
catch (Exception ex)
34+
{
35+
throw new Exception("dotnet --info failed.", ex);
36+
}
37+
}
38+
39+
public void RestoreToDirectory(string projectOrSolutionFile, string packageDirectory)
1140
{
12-
using var proc = Process.Start("dotnet", $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true");
41+
var args = $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
42+
progressMonitor.RunningProcess($"dotnet {args}");
43+
using var proc = Process.Start("dotnet", args);
1344
proc.WaitForExit();
14-
return proc.ExitCode;
45+
if (proc.ExitCode != 0)
46+
{
47+
progressMonitor.CommandFailed("dotnet", args, proc.ExitCode);
48+
}
1549
}
1650
}
1751
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/ProgressMonitor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,15 @@ public void MissingNuGet()
9797
{
9898
logger.Log(Severity.Error, "Missing nuget.exe");
9999
}
100+
101+
public void MissingDotNet()
102+
{
103+
logger.Log(Severity.Error, "Missing dotnet CLI");
104+
}
105+
106+
public void RunningProcess(string command)
107+
{
108+
logger.Log(Severity.Info, $"Running {command}");
109+
}
100110
}
101111
}

0 commit comments

Comments
 (0)