/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * dmex * */ namespace CustomBuildTool { /// /// Provides methods for retrieving NuGet package metadata, including the latest available versions, using NuGet v3 APIs. /// public static class NugetMetadataClient { private const string ServiceIndexUri = "https://api.nuget.org/v3/index.json"; private const string FlatContainerBaseUri = "https://api.nuget.org/v3-flatcontainer"; /// /// Gets the latest package version from NuGet using service index discovery. /// Query order: RegistrationsBaseUrl -> SearchQueryService -> flat-container fallback. /// /// Package ID. /// Cancellation token. /// The latest stable version if available; otherwise the latest prerelease; null if not found. public static async Task GetLatestVersion(string PackageId, CancellationToken CancellationToken = default) { if (string.IsNullOrWhiteSpace(PackageId)) return null; using var httpClient = BuildHttpClient.CreateHttpClient(); var versions = new HashSet(StringComparer.OrdinalIgnoreCase); string registrationsBase = null; string searchQueryService = null; try { using var request = new HttpRequestMessage(HttpMethod.Get, ServiceIndexUri); using var httpResponse = await httpClient.SendAsync(request, CancellationToken); if (httpResponse.IsSuccessStatusCode) { await using var stream = await httpResponse.Content.ReadAsStreamAsync(CancellationToken); var doc = await JsonSerializer.DeserializeAsync(stream, NugetJsonContext.Default.NugetServiceIndexResponse, CancellationToken); if (doc.Resources != null) { foreach (var resource in doc.Resources) { var id = resource.Id; if (string.IsNullOrWhiteSpace(id)) continue; if (ResourceTypeContains(resource.Type, "RegistrationsBaseUrl") && string.IsNullOrWhiteSpace(registrationsBase)) registrationsBase = id; if (ResourceTypeContains(resource.Type, "SearchQueryService") && string.IsNullOrWhiteSpace(searchQueryService)) searchQueryService = id; } } } } catch { // ignore } if (!string.IsNullOrWhiteSpace(registrationsBase)) { await foreach (var version in GetVersionsFromRegistrations(httpClient, registrationsBase, PackageId, CancellationToken)) { versions.Add(version); } } if (versions.Count == 0 && !string.IsNullOrWhiteSpace(searchQueryService)) { await foreach (var version in GetVersionsFromSearch(httpClient, searchQueryService, PackageId, CancellationToken)) { versions.Add(version); } } if (versions.Count == 0) { await foreach (var version in GetVersionsFromFlatContainer(httpClient, PackageId, CancellationToken)) { versions.Add(version); } } return SelectBestVersion(versions); } /// /// Convenience wrapper for Microsoft.Windows.SDK.CPP.x64 package. /// public static Task GetLatestBuildToolsVersion(CancellationToken CancellationToken = default) { return GetLatestVersion("Microsoft.Windows.SDK.BuildTools", CancellationToken); } /// /// Convenience wrapper for Microsoft.Windows.SDK.CPP.x64 package. /// public static Task GetLatestWindowsSdkVersion(CancellationToken CancellationToken = default) { return GetLatestVersion("Microsoft.Windows.SDK.CPP", CancellationToken); } /// /// Convenience wrapper for Microsoft.Windows.SDK.CPP.x64 package. /// public static Task GetLatestWindowsSdk64Version(CancellationToken CancellationToken = default) { return GetLatestVersion("Microsoft.Windows.SDK.CPP.x64", CancellationToken); } /// /// Queries NuGet registrations API to retrieve all versions of a package. /// /// HTTP client for making requests. /// Base URL for registrations API. /// Package ID to query. /// Cancellation token. private static async IAsyncEnumerable GetVersionsFromRegistrations( HttpClient HttpClient, string RegistrationsBase, string PackageId, [EnumeratorCancellation] CancellationToken CancellationToken) { var registrationIndexUri = $"{RegistrationsBase.TrimEnd('/')}/{PackageId.ToLowerInvariant()}/index.json"; NugetRegistrationIndexResponse index = null; try { using var request = new HttpRequestMessage(HttpMethod.Get, registrationIndexUri); using var httpResponse = await HttpClient.SendAsync(request, CancellationToken); if (httpResponse.IsSuccessStatusCode) { await using var stream = await httpResponse.Content.ReadAsStreamAsync(CancellationToken); index = await JsonSerializer.DeserializeAsync(stream, NugetJsonContext.Default.NugetRegistrationIndexResponse, CancellationToken); } } catch { index = null; } if (index?.Items == null) yield break; foreach (var page in index.Items) { if (page.Items != null && page.Items.Length != 0) { foreach (var item in page.Items) { if (!string.IsNullOrWhiteSpace(item.CatalogEntry?.Version)) yield return item.CatalogEntry.Version.Trim(); } continue; } if (string.IsNullOrWhiteSpace(page.Id)) continue; NugetRegistrationPageResponse pageResponse = null; try { using var request = new HttpRequestMessage(HttpMethod.Get, page.Id); using var httpResponse = await HttpClient.SendAsync(request, CancellationToken); if (httpResponse.IsSuccessStatusCode) { await using var stream = await httpResponse.Content.ReadAsStreamAsync(CancellationToken); pageResponse = await JsonSerializer.DeserializeAsync(stream, NugetJsonContext.Default.NugetRegistrationPageResponse, CancellationToken); } } catch { pageResponse = null; } if (pageResponse?.Items != null) { foreach (var item in pageResponse.Items) { if (!string.IsNullOrWhiteSpace(item.CatalogEntry?.Version)) yield return item.CatalogEntry.Version.Trim(); } } } } private static async IAsyncEnumerable GetVersionsFromSearch( HttpClient HttpClient, string SearchQueryService, string PackageId, [EnumeratorCancellation] CancellationToken CancellationToken) { var searchUri = $"{SearchQueryService.TrimEnd('/')}?q=packageid:{Uri.EscapeDataString(PackageId)}&prerelease=true&take=20"; NugetSearchResponse search = null; try { using var request = new HttpRequestMessage(HttpMethod.Get, searchUri); using var httpResponse = await HttpClient.SendAsync(request, CancellationToken); if (httpResponse.IsSuccessStatusCode) { await using var stream = await httpResponse.Content.ReadAsStreamAsync(CancellationToken); search = await JsonSerializer.DeserializeAsync(stream, NugetJsonContext.Default.NugetSearchResponse, CancellationToken); } } catch { search = null; } if (search?.Data == null) yield break; foreach (var package in search.Data) { if (!string.Equals(package.Id, PackageId, StringComparison.OrdinalIgnoreCase)) continue; if (package.Versions != null) { foreach (var v in package.Versions) if (!string.IsNullOrWhiteSpace(v.Version)) yield return v.Version.Trim(); } if (!string.IsNullOrWhiteSpace(package.Version)) yield return package.Version.Trim(); } } private static async IAsyncEnumerable GetVersionsFromFlatContainer( HttpClient HttpClient, string PackageId, [EnumeratorCancellation] CancellationToken CancellationToken) { var versionsUri = $"{NugetMetadataClient.FlatContainerBaseUri}/{PackageId.ToLowerInvariant()}/index.json"; NugetFlatContainerResponse container = null; try { using var request = new HttpRequestMessage(HttpMethod.Get, versionsUri); using var httpResponse = await HttpClient.SendAsync(request, CancellationToken); if (httpResponse.IsSuccessStatusCode) { await using var stream = await httpResponse.Content.ReadAsStreamAsync(CancellationToken); container = await JsonSerializer.DeserializeAsync(stream, NugetJsonContext.Default.NugetFlatContainerResponse, CancellationToken); } } catch { container = null; } if (container?.Versions == null) yield break; foreach (var version in container.Versions) { if (!string.IsNullOrWhiteSpace(version)) yield return version.Trim(); } } /// /// Adds non-empty version strings from a source collection to a target set. /// /// Target set to add versions to. /// Source collection of version strings. private static void AddVersions(HashSet Target, IEnumerable Source) { if (Target == null || Source == null) return; foreach (var version in Source) { if (!string.IsNullOrWhiteSpace(version)) { Target.Add(version); } } } /// /// Retrieves the endpoint URL for a specific resource type from the NuGet service index. /// /// NuGet service index response. /// Resource type to find. /// Resource endpoint URL; null if not found. private static string GetResourceEndpoint(NugetServiceIndexResponse ServiceIndex, string ResourceType) { if (ServiceIndex.Resources == null) return null; foreach (var resource in ServiceIndex.Resources) { if (!string.IsNullOrWhiteSpace(resource.Id) && ResourceTypeContains(resource.Type, ResourceType)) return resource.Id; } return null; } /// /// Checks if a JSON element (string or array of strings) contains a specific value. /// /// JSON element to check. /// Value to search for. /// True if value is found; false otherwise. private static bool ResourceTypeContains(string Type, string Value) { return Type?.Contains(Value, StringComparison.OrdinalIgnoreCase) == true; } /// /// Selects the best version from a collection, preferring the latest stable version or the latest prerelease if no stable version exists. /// /// Collection of version strings. /// Best version string; null if the collection is empty. private static string SelectBestVersion(IEnumerable Versions) { var ordered = Versions .Where(V => !string.IsNullOrWhiteSpace(V)) .Select(V => NugetSemVer.TryParse(V, out var semVer) ? semVer : null) .Where(V => V != null) .OrderBy(V => V) .ToList(); if (ordered.Count == 0) return null; var stable = ordered.LastOrDefault(V => !V.IsPrerelease); return (stable ?? ordered[^1]).Original; } /// /// Represents a NuGet-compatible semantic version, supporting parsing and comparison according to semantic /// versioning rules. /// public sealed class NugetSemVer : IComparable { private static readonly SearchValues SemVerDelimiters = SearchValues.Create(".+-"); public string Original { get; init; } public int Major { get; init; } public int Minor { get; init; } public int Patch { get; init; } public int[] ExtraNumbers { get; init; } public string Prerelease { get; init; } public bool IsPrerelease => !string.IsNullOrEmpty(Prerelease); /// /// Attempts to parse a semantic version string into a NugetSemVer object. /// /// Version string to parse. /// Parsed a result if successful; null otherwise. /// True if parsing succeeded; false otherwise. public static bool TryParse(string Value, out NugetSemVer Result) { Result = null; if (string.IsNullOrWhiteSpace(Value)) return false; ReadOnlySpan span = Value.AsSpan(); int major = 0, minor = 0, patch = 0; string prerelease = null; var extra = new List(); int partIndex = 0; while (!span.IsEmpty) { int nextDelimiter = span.IndexOfAny(SemVerDelimiters); ReadOnlySpan part = nextDelimiter >= 0 ? span[..nextDelimiter] : span; char delimiter = nextDelimiter >= 0 ? span[nextDelimiter] : '\0'; if (partIndex == 0) { if (!int.TryParse(part, out major)) return false; } else if (partIndex == 1) { if (!int.TryParse(part, out minor)) return false; } else if (partIndex == 2) { if (!int.TryParse(part, out patch)) return false; } else if (partIndex > 2 && delimiter == '.') { if (int.TryParse(part, out int ex)) extra.Add(ex); } if (delimiter == '-') { span = span[(nextDelimiter + 1)..]; int buildIndex = span.IndexOf('+'); prerelease = (buildIndex >= 0 ? span[..buildIndex] : span).ToString(); break; } if (delimiter == '+') break; if (nextDelimiter < 0) break; span = span[(nextDelimiter + 1)..]; partIndex++; } if (partIndex < 1) return false; Result = new NugetSemVer { Original = Value, Major = major, Minor = minor, Patch = patch, ExtraNumbers = extra.ToArray(), Prerelease = prerelease }; return true; } /// /// Compares this version to another version using semantic versioning rules. /// Stable versions are considered greater than prerelease versions with the same base version. /// /// Version to compare to. /// Negative if this is less than other; zero if equal; positive if greater. public int CompareTo(NugetSemVer Other) { if (Other == null) return 1; var cmp = Major.CompareTo(Other.Major); if (cmp != 0) return cmp; cmp = Minor.CompareTo(Other.Minor); if (cmp != 0) return cmp; cmp = Patch.CompareTo(Other.Patch); if (cmp != 0) return cmp; var max = Math.Max(ExtraNumbers.Length, Other.ExtraNumbers.Length); for (var i = 0; i < max; i++) { var left = i < ExtraNumbers.Length ? ExtraNumbers[i] : 0; var right = i < Other.ExtraNumbers.Length ? Other.ExtraNumbers[i] : 0; cmp = left.CompareTo(right); if (cmp != 0) return cmp; } if (!IsPrerelease && Other.IsPrerelease) return 1; if (IsPrerelease && !Other.IsPrerelease) return -1; return string.Compare(Prerelease, Other.Prerelease, StringComparison.OrdinalIgnoreCase); } public override string ToString() { return this.Original; } } } [JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, GenerationMode = JsonSourceGenerationMode.Default)] [JsonSerializable(typeof(OpenAiResponse))] [JsonSerializable(typeof(GeminiResponse))] [JsonSerializable(typeof(NugetFlatContainerResponse))] [JsonSerializable(typeof(NugetServiceIndexResponse))] [JsonSerializable(typeof(NugetRegistrationIndexResponse))] [JsonSerializable(typeof(NugetRegistrationPageResponse))] [JsonSerializable(typeof(NugetSearchResponse))] internal partial class NugetJsonContext : JsonSerializerContext; internal class OpenAiResponse { [JsonPropertyName("choices")] public OpenAiChoice[] Choices { get; init; } } internal class OpenAiChoice { [JsonPropertyName("message")] public OpenAiMessage Message { get; init; } } internal class OpenAiMessage { [JsonPropertyName("content")] public string Content { get; init; } } internal class GeminiResponse { [JsonPropertyName("candidates")] public GeminiCandidate[] Candidates { get; init; } } internal class GeminiCandidate { [JsonPropertyName("content")] public GeminiContent Content { get; init; } } internal class GeminiContent { [JsonPropertyName("parts")] public GeminiPart[] Parts { get; init; } } internal class GeminiPart { [JsonPropertyName("text")] public string Text { get; init; } } internal class NugetSearchVersionResponse { [JsonPropertyName("version")] public string Version { get; init; } } internal class NugetRegistrationCatalogEntryResponse { [JsonPropertyName("@id")] public string Id { get; init; } [JsonPropertyName("id")] public string Id2 { get; init; } [JsonPropertyName("@type")] public string Type { get; init; } [JsonPropertyName("packageContent")] public string PackageContent { get; init; } [JsonPropertyName("published")] public string Published { get; init; } [JsonPropertyName("version")] public string Version { get; init; } public override string ToString() { return $"{this.Id} [{this.Version}] [{this.Published}]"; } } internal class NugetServiceIndexResource { [JsonPropertyName("@id")] public string Id { get; init; } [JsonPropertyName("@type")] public string Type { get; init; } public override string ToString() { return $"{this.Id} [{this.Type}]"; } } internal class NugetRegistrationLeafResponse { [JsonPropertyName("@id")] public string Id { get; init; } [JsonPropertyName("commitId")] public string CommitId { get; init; } [JsonPropertyName("commitTimeStamp")] public string CommitTimeStamp { get; init; } [JsonPropertyName("catalogEntry")] public NugetRegistrationCatalogEntryResponse CatalogEntry { get; init; } public override string ToString() { return $"{this.Id} [{this.CommitTimeStamp}] [{this.CommitId}]"; } } internal class NugetSearchPackageResponse { [JsonPropertyName("id")] public string Id { get; init; } [JsonPropertyName("version")] public string Version { get; init; } [JsonPropertyName("versions")] public NugetSearchVersionResponse[] Versions { get; init; } public override string ToString() { return $"{this.Id} [{this.Version}]"; } } internal class NugetServiceIndexResponse { [JsonPropertyName("resources")] public NugetServiceIndexResource[] Resources { get; init; } } internal class NugetRegistrationIndexResponse { [JsonPropertyName("items")] public NugetRegistrationPageResponse[] Items { get; init; } } internal class NugetRegistrationPageResponse { [JsonPropertyName("@id")] public string Id { get; init; } [JsonPropertyName("commitId")] public string CommitId { get; init; } [JsonPropertyName("commitTimeStamp")] public string CommitTimeStamp { get; init; } [JsonPropertyName("items")] public NugetRegistrationLeafResponse[] Items { get; init; } public override string ToString() { return $"{this.Id} [{this.CommitTimeStamp}] [{this.CommitId}]"; } } internal class NugetSearchResponse { [JsonPropertyName("data")] public NugetSearchPackageResponse[] Data { get; init; } } }