|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using System.Diagnostics; |
4 | 3 | using System.IO; |
5 | 4 | using System.Linq; |
@@ -43,25 +42,68 @@ public NugetPackages(string sourceDir, TemporaryDirectory packageDirectory, Prog |
43 | 42 | PackageDirectory = packageDirectory; |
44 | 43 | this.progressMonitor = progressMonitor; |
45 | 44 |
|
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 | + { |
47 | 59 | var currentAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location; |
48 | 60 | var directory = Path.GetDirectoryName(currentAssembly) |
49 | 61 | ?? throw new FileNotFoundException($"Directory path '{currentAssembly}' of current assembly is null"); |
50 | | - nugetExe = Path.Combine(directory, "nuget", "nuget.exe"); |
51 | 62 |
|
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 | + } |
54 | 75 |
|
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 | + } |
58 | 101 | } |
59 | 102 |
|
60 | 103 | /// <summary> |
61 | 104 | /// Restore all files in a specified package. |
62 | 105 | /// </summary> |
63 | 106 | /// <param name="package">The package file.</param> |
64 | | - /// <param name="pm">Where to log progress/errors.</param> |
65 | 107 | private void RestoreNugetPackage(string package) |
66 | 108 | { |
67 | 109 | progressMonitor.NugetInstall(package); |
@@ -120,7 +162,6 @@ private void RestoreNugetPackage(string package) |
120 | 162 | /// <summary> |
121 | 163 | /// Download the packages to the temp folder. |
122 | 164 | /// </summary> |
123 | | - /// <param name="pm">The progress monitor used for reporting errors etc.</param> |
124 | 165 | public void InstallPackages() |
125 | 166 | { |
126 | 167 | foreach (var package in PackageFiles) |
|
0 commit comments