@@ -17,15 +17,11 @@ internal class NugetExeWrapper : IDisposable
17
17
private readonly string ? nugetExe ;
18
18
private readonly Util . Logging . ILogger logger ;
19
19
20
- /// <summary>
21
- /// The list of package files.
22
- /// </summary>
23
- private readonly ICollection < string > packageFiles ;
24
-
25
- public int PackageCount => packageFiles . Count ;
20
+ public int PackageCount => fileProvider . PackagesConfigs . Count ;
26
21
27
22
private readonly string ? backupNugetConfig ;
28
23
private readonly string ? nugetConfigPath ;
24
+ private readonly FileProvider fileProvider ;
29
25
30
26
/// <summary>
31
27
/// The computed packages directory.
@@ -39,15 +35,14 @@ internal class NugetExeWrapper : IDisposable
39
35
/// </summary>
40
36
public NugetExeWrapper ( FileProvider fileProvider , TemporaryDirectory packageDirectory , Util . Logging . ILogger logger )
41
37
{
38
+ this . fileProvider = fileProvider ;
42
39
this . packageDirectory = packageDirectory ;
43
40
this . logger = logger ;
44
41
45
- packageFiles = fileProvider . PackagesConfigs ;
46
-
47
- if ( packageFiles . Count > 0 )
42
+ if ( fileProvider . PackagesConfigs . Count > 0 )
48
43
{
49
44
logger . LogInfo ( $ "Found packages.config files, trying to use nuget.exe for package restore") ;
50
- nugetExe = ResolveNugetExe ( fileProvider . SourceDir . FullName ) ;
45
+ nugetExe = ResolveNugetExe ( ) ;
51
46
if ( HasNoPackageSource ( ) )
52
47
{
53
48
// We only modify or add a top level nuget.config file
@@ -87,25 +82,44 @@ public NugetExeWrapper(FileProvider fileProvider, TemporaryDirectory packageDire
87
82
}
88
83
89
84
/// <summary>
90
- /// Tries to find the location of `nuget.exe` in the nuget directory under the directory
91
- /// containing the executing assembly. If it can't be found, it is downloaded to the
92
- /// `.nuget` directory under the source directory.
85
+ /// Tries to find the location of `nuget.exe`. It looks for
86
+ /// - the environment variable specifying a location,
87
+ /// - files in the repository,
88
+ /// - tries to resolve nuget from the PATH, or
89
+ /// - downloads it if it is not found.
93
90
/// </summary>
94
- /// <param name="sourceDir">The source directory.</param>
95
- private string ResolveNugetExe ( string sourceDir )
91
+ private string ResolveNugetExe ( )
96
92
{
97
- var currentAssembly = System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ;
98
- var directory = Path . GetDirectoryName ( currentAssembly )
99
- ?? throw new FileNotFoundException ( $ "Directory path '{ currentAssembly } ' of current assembly is null") ;
93
+ var envVarPath = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . NugetExePath ) ;
94
+ if ( ! string . IsNullOrEmpty ( envVarPath ) )
95
+ {
96
+ logger . LogInfo ( $ "Using nuget.exe from environment variable: '{ envVarPath } '") ;
97
+ return envVarPath ;
98
+ }
100
99
101
- var nuget = Path . Combine ( directory , "nuget" , "nuget.exe" ) ;
102
- if ( File . Exists ( nuget ) )
100
+ var nugetExesInRepo = fileProvider . NugetExes ;
101
+ if ( nugetExesInRepo . Count > 1 )
103
102
{
104
- logger . LogInfo ( $ "Found nuget.exe at { nuget } ") ;
105
- return nuget ;
103
+ logger . LogInfo ( $ "Found multiple nuget.exe files in the repository: { string . Join ( ", " , nugetExesInRepo . OrderBy ( s => s ) ) } ") ;
106
104
}
107
105
108
- return DownloadNugetExe ( sourceDir ) ;
106
+ if ( nugetExesInRepo . Count > 0 )
107
+ {
108
+ var path = nugetExesInRepo . First ( ) ;
109
+ logger . LogInfo ( $ "Using nuget.exe from path '{ path } '") ;
110
+ return path ;
111
+ }
112
+
113
+ var executableName = Win32 . IsWindows ( ) ? "nuget.exe" : "nuget" ;
114
+ var nugetPath = FileUtils . FindProgramOnPath ( executableName ) ;
115
+ if ( nugetPath is not null )
116
+ {
117
+ nugetPath = Path . Combine ( nugetPath , executableName ) ;
118
+ logger . LogInfo ( $ "Using nuget.exe from PATH: { nugetPath } ") ;
119
+ return nugetPath ;
120
+ }
121
+
122
+ return DownloadNugetExe ( fileProvider . SourceDir . FullName ) ;
109
123
}
110
124
111
125
private string DownloadNugetExe ( string sourceDir )
@@ -135,6 +149,8 @@ private string DownloadNugetExe(string sourceDir)
135
149
}
136
150
}
137
151
152
+ private bool RunWithMono => ! Win32 . IsWindows ( ) && ! string . IsNullOrEmpty ( Path . GetExtension ( nugetExe ) ) ;
153
+
138
154
/// <summary>
139
155
/// Restore all files in a specified package.
140
156
/// </summary>
@@ -150,15 +166,15 @@ private bool TryRestoreNugetPackage(string package)
150
166
*/
151
167
152
168
string exe , args ;
153
- if ( Win32 . IsWindows ( ) )
169
+ if ( RunWithMono )
154
170
{
155
- exe = nugetExe ! ;
156
- args = $ "install -OutputDirectory { packageDirectory } { package } ";
171
+ exe = "mono" ;
172
+ args = $ "{ nugetExe } install -OutputDirectory { packageDirectory } { package } ";
157
173
}
158
174
else
159
175
{
160
- exe = "mono" ;
161
- args = $ "{ nugetExe } install -OutputDirectory { packageDirectory } { package } ";
176
+ exe = nugetExe ! ;
177
+ args = $ "install -OutputDirectory { packageDirectory } { package } ";
162
178
}
163
179
164
180
var pi = new ProcessStartInfo ( exe , args )
@@ -189,7 +205,7 @@ private bool TryRestoreNugetPackage(string package)
189
205
/// </summary>
190
206
public int InstallPackages ( )
191
207
{
192
- return packageFiles . Count ( package => TryRestoreNugetPackage ( package ) ) ;
208
+ return fileProvider . PackagesConfigs . Count ( package => TryRestoreNugetPackage ( package ) ) ;
193
209
}
194
210
195
211
private bool HasNoPackageSource ( )
@@ -219,8 +235,18 @@ private bool HasNoPackageSource()
219
235
220
236
private void RunMonoNugetCommand ( string command , out IList < string > stdout )
221
237
{
222
- var exe = "mono" ;
223
- var args = $ "{ nugetExe } { command } ";
238
+ string exe , args ;
239
+ if ( RunWithMono )
240
+ {
241
+ exe = "mono" ;
242
+ args = $ "{ nugetExe } { command } ";
243
+ }
244
+ else
245
+ {
246
+ exe = nugetExe ! ;
247
+ args = command ;
248
+ }
249
+
224
250
var pi = new ProcessStartInfo ( exe , args )
225
251
{
226
252
RedirectStandardOutput = true ,
0 commit comments