Skip to content

Commit e300c0b

Browse files
committed
Improve nuget.exe download logic with versioned URL support
- Use versioned URL if version is specified, fallback to /latest/ if it fails - Use /latest/ directly if no version is specified - Respect custom nugetExePath URLs - Simplified download loop with automatic fallback - More flexible and reliable than previous implementation
1 parent 6ddbe5c commit e300c0b

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/main/groovy/com/ullink/BaseNuGet.groovy

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,23 @@ class BaseNuGet extends Exec {
306306
if (!folder.isDirectory())
307307
folder.mkdirs()
308308

309-
def nugetUrl = getNugetDownloadLink()
310-
project.logger.info "Downloading NuGet from $nugetUrl ..."
311-
312-
new URL(nugetUrl).withInputStream {
313-
inputStream ->
314-
localNuget.withOutputStream { outputStream ->
315-
outputStream << inputStream
309+
def urls = getNugetDownloadUrls()
310+
for (def url : urls) {
311+
try {
312+
project.logger.info "Downloading NuGet from $url ..."
313+
new URL(url).withInputStream { inputStream ->
314+
localNuget.withOutputStream { outputStream ->
315+
outputStream << inputStream
316+
}
317+
}
318+
break
319+
} catch (Exception e) {
320+
if (urls.indexOf(url) < urls.size() - 1) {
321+
project.logger.warn "Failed to download from $url, trying next URL (${e.message})"
322+
} else {
323+
throw new RuntimeException("Failed to download NuGet from all URLs: ${urls.join(', ')}", e)
316324
}
325+
}
317326
}
318327

319328
// Make the file executable on Unix systems (required for Mono)
@@ -325,16 +334,23 @@ class BaseNuGet extends Exec {
325334
}
326335

327336
@Internal
328-
protected String getNugetDownloadLink() {
329-
// If a custom URL is provided, use it
337+
protected List<String> getNugetDownloadUrls() {
338+
// If a custom URL is provided, use it only
330339
if (nugetExePath != null && !nugetExePath.empty && nugetExePath.startsWith("http")) {
331340
project.logger.debug("Nuget url path is resolved from property 'nugetExePath'")
332-
return nugetExePath
341+
return [nugetExePath]
342+
}
343+
344+
// If version is specified, try versioned URL first, then fallback to /latest/
345+
def version = project.extensions.nuget.version
346+
if (version != null && !version.empty) {
347+
def exeName = version < '3.4.4' ? 'nuget.exe' : 'NuGet.exe'
348+
def versionedUrl = "https://dist.nuget.org/win-x86-commandline/v${version}/${exeName}"
349+
return [versionedUrl, "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"]
333350
}
334351

335-
// Use /latest/ endpoint for simplicity - always gets the latest version
336-
// This is more reliable than versioned URLs which may not exist
337-
return "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
352+
// No version specified, use /latest/
353+
return ["https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"]
338354
}
339355

340356
@Internal

0 commit comments

Comments
 (0)