Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.TypeSpec.Generator.Input;
using NuGet.Configuration;
using NuGet.Versioning;

namespace Microsoft.TypeSpec.Generator.Utilities
{
Expand Down Expand Up @@ -238,10 +240,30 @@ private static async Task<ResolutionResult> ResolveResultAsync(InputExternalType
{
try
{
var resolvedVersion = !string.IsNullOrEmpty(external.MinVersion)
? external.MinVersion!
: await NugetPackageResolver.ResolveLatestPackageVersion(external.Package!, nugetSettings);

string? resolvedVersion;
// Search for the compatible version.
if (!string.IsNullOrEmpty(external.MinVersion))
{
// If min version was provided, we
// 1. Search if it is in our repositories;
// 2. Get the latest one if it is not.
// 3. If our version is a pre release, include pre released versions in our search.
NuGetVersion minVersion = new(external.MinVersion);
IList<NuGetVersion> versions = await NugetPackageResolver.GetAllVersions(external.Package!, nugetSettings, allowPrerelease: minVersion.IsPrerelease);
if (versions.Any(x => x == minVersion))
{
resolvedVersion = external.MinVersion;
}
else
{
resolvedVersion = versions.Max()?.ToString();
}
}
else
{
// If min version was not provided, get the latest stable version.
resolvedVersion = await NugetPackageResolver.ResolveLatestPackageVersion(external.Package!, nugetSettings);
}
Comment thread
nick863 marked this conversation as resolved.
if (!string.IsNullOrEmpty(resolvedVersion))
{
var downloader = new NugetPackageDownloader(external.Package!, resolvedVersion!, null, nugetSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.Packaging;
Comment thread
nick863 marked this conversation as resolved.
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Repositories;
Expand Down Expand Up @@ -202,6 +203,28 @@ private static NuGetFramework ResolveCurrentFramework()
return null;
}

public static async Task<IList<NuGetVersion>> GetAllVersions(string packageName, ISettings nugetSettings, bool allowPrerelease)
{
HashSet<NuGetVersion> versions = [];
var sources = SettingsUtility.GetEnabledSources(nugetSettings);
using var cacheContext = new SourceCacheContext();
foreach (var source in sources)
{
try
{
var repository = Repository.Factory.GetCoreV3(source.Source);
var resource = await repository.GetResourceAsync<FindPackageByIdResource>();
IEnumerable<NuGetVersion> versionsFromOneRepo = (await resource.GetAllVersionsAsync(packageName, cacheContext, NuGet.Common.NullLogger.Instance, CancellationToken.None)).Where(v => !v.IsPrerelease || allowPrerelease);
versions.AddRange(versionsFromOneRepo);
}
Comment thread
nick863 marked this conversation as resolved.
catch
{
// Skip failed source.
}
}
return [..versions];
}

/// <summary>
/// Queries the configured NuGet feeds for the latest stable version of <paramref name="packageName"/>.
/// When <paramref name="minVersion"/> is provided, the latest stable version greater than or equal to it
Expand Down
Loading