Skip to content

Commit 6e4865d

Browse files
committed
C#: Download nuget.exe to the source directory in case it is not installed.
1 parent b6c2ea5 commit 6e4865d

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

csharp/autobuilder/Semmle.Autobuild.Shared/MsBuildRule.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using Semmle.Util.Logging;
21
using System.Collections.Generic;
32
using System.Linq;
3+
using Semmle.Util;
4+
using Semmle.Util.Logging;
45

56
namespace Semmle.Autobuild.Shared
67
{
@@ -190,7 +191,7 @@ private static BuildScript DownloadNugetExe<TAutobuildOptions>(IAutobuilder<TAut
190191
})
191192
&
192193
BuildScript.DownloadFile(
193-
"https://dist.nuget.org/win-x86-commandline/latest/nuget.exe",
194+
FileUtils.nugetExeUrl,
194195
path,
195196
e => builder.Log(Severity.Warning, $"Failed to download 'nuget.exe': {e.Message}"))
196197
&

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

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Linq;
@@ -43,25 +42,68 @@ public NugetPackages(string sourceDir, TemporaryDirectory packageDirectory, Prog
4342
PackageDirectory = packageDirectory;
4443
this.progressMonitor = progressMonitor;
4544

46-
// Expect nuget.exe to be in a `nuget` directory under the directory containing this exe.
45+
nugetExe = ResolveNugetExe(sourceDir);
46+
PackageFiles = new DirectoryInfo(SourceDirectory)
47+
.EnumerateFiles("packages.config", SearchOption.AllDirectories)
48+
.ToArray();
49+
}
50+
51+
/// <summary>
52+
/// Tries to find the location of `nuget.exe` in the nuget directory under the directory
53+
/// containing the executing assembly. If it can't be found, it is downloaded to the
54+
/// `.nuget` directory under the source directory.
55+
/// </summary>
56+
/// <param name="sourceDir">The source directory.</param>
57+
private string ResolveNugetExe(string sourceDir)
58+
{
4759
var currentAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
4860
var directory = Path.GetDirectoryName(currentAssembly)
4961
?? throw new FileNotFoundException($"Directory path '{currentAssembly}' of current assembly is null");
50-
nugetExe = Path.Combine(directory, "nuget", "nuget.exe");
5162

52-
if (!File.Exists(nugetExe))
53-
throw new FileNotFoundException(string.Format("NuGet could not be found at {0}", nugetExe));
63+
var nuget = Path.Combine(directory, "nuget", "nuget.exe");
64+
if (File.Exists(nuget))
65+
{
66+
progressMonitor.LogInfo($"Found nuget.exe at {nuget}");
67+
return nuget;
68+
}
69+
else
70+
{
71+
progressMonitor.LogInfo($"Nuget.exe could not be found at {nuget}");
72+
return DownloadNugetExe(sourceDir);
73+
}
74+
}
5475

55-
PackageFiles = new DirectoryInfo(SourceDirectory)
56-
.EnumerateFiles("packages.config", SearchOption.AllDirectories)
57-
.ToArray();
76+
private string DownloadNugetExe(string sourceDir)
77+
{
78+
var directory = Path.Combine(sourceDir, ".nuget");
79+
var nuget = Path.Combine(directory, "nuget.exe");
80+
81+
// Nuget.exe already exists in the .nuget directory.
82+
if (File.Exists(nuget))
83+
{
84+
progressMonitor.LogInfo($"Found nuget.exe at {nuget}");
85+
return nuget;
86+
}
87+
88+
Directory.CreateDirectory(directory);
89+
progressMonitor.LogInfo("Attempting to download nuget.exe");
90+
try
91+
{
92+
FileUtils.DownloadFile(FileUtils.nugetExeUrl, nuget);
93+
progressMonitor.LogInfo($"Downloaded nuget.exe to {nuget}");
94+
return nuget;
95+
}
96+
catch
97+
{
98+
// Download failed.
99+
throw new FileNotFoundException("Download of nuget.exe failed.");
100+
}
58101
}
59102

60103
/// <summary>
61104
/// Restore all files in a specified package.
62105
/// </summary>
63106
/// <param name="package">The package file.</param>
64-
/// <param name="pm">Where to log progress/errors.</param>
65107
private void RestoreNugetPackage(string package)
66108
{
67109
progressMonitor.NugetInstall(package);
@@ -120,7 +162,6 @@ private void RestoreNugetPackage(string package)
120162
/// <summary>
121163
/// Download the packages to the temp folder.
122164
/// </summary>
123-
/// <param name="pm">The progress monitor used for reporting errors etc.</param>
124165
public void InstallPackages()
125166
{
126167
foreach (var package in PackageFiles)

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Semmle.Util
1010
{
1111
public static class FileUtils
1212
{
13+
public const string nugetExeUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
14+
1315
public static string ConvertToWindows(string path)
1416
{
1517
return path.Replace('/', '\\');

0 commit comments

Comments
 (0)