Skip to content

Commit

Permalink
Allow diferent versions for NuGet V3 API endpoints + fix NuGet prefer…
Browse files Browse the repository at this point in the history
…ence path warning
  • Loading branch information
JoC0de committed Oct 20, 2023
1 parent 9b86916 commit 6d74db4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using UnityEngine;
#if !((UNITY_EDITOR_WIN && UNITY_2023_1_OR_NEWER) || NUGETFORUNITY_CLI)
#if !((UNITY_EDITOR_WIN && UNITY_2023_1_OR_NEWER) || NUGETFORUNITY_CLI)
using JetBrains.Annotations;

#else
using System.Security.Cryptography;
#endif
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using UnityEngine;

namespace NugetForUnity.Configuration
{
Expand Down
57 changes: 46 additions & 11 deletions src/NuGetForUnity/Editor/PackageSource/NugetApiClientV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,29 +436,64 @@ private async Task InitializeApiAddressesAsync(NugetPackageSourceV3 packageSourc
var resources = resourceList.resources ??
throw new InvalidOperationException(
$"missing '{nameof(resourceList.resources)}' property inside index response:\n{responseString}");

// we only support v3 so if v4 is released we skip it.
var maxSupportedApiVersion = new NugetPackageVersion("4.0.0");
NugetPackageVersion highestPackageBaseAddressApiVersion = null;
NugetPackageVersion highestRegistrationsBaseUrlApiVersion = null;
NugetPackageVersion highestSearchQueryServiceApiVersion = null;
foreach (var resource in resources)
{
var resourceAtId = resource.atId ??
throw new InvalidOperationException($"missing '@id' property inside resource of type '{resource.atType}'");
switch (resource.atType)
var resourceAtType = resource.atType ??
throw new InvalidOperationException($"missing '@type' property inside resource with id '{resource.atId}'");

var resourceTypeParts = resourceAtType.Split('/');
NugetPackageVersion resourceTypeVersion = null;

// need to skip if version is no number like in: 'RegistrationsBaseUrl/Versioned'
if (resourceTypeParts.Length > 1 && !string.IsNullOrEmpty(resourceTypeParts[1]) && char.IsDigit(resourceTypeParts[1][0]))
{
resourceTypeVersion = new NugetPackageVersion(resourceTypeParts[1]);
}

switch (resourceTypeParts[0])
{
case "SearchQueryService":
var comment = resource.comment ?? string.Empty;
if (comment.IndexOf("(primary)", StringComparison.OrdinalIgnoreCase) >= 0)
if (highestSearchQueryServiceApiVersion == null ||
(resourceTypeVersion > highestSearchQueryServiceApiVersion && resourceTypeVersion < maxSupportedApiVersion))
{
foundSearchQueryServices.Insert(0, resourceAtId.Trim('/'));
highestSearchQueryServiceApiVersion = resourceTypeVersion;
var comment = resource.comment ?? string.Empty;
if (comment.IndexOf("(primary)", StringComparison.OrdinalIgnoreCase) >= 0)
{
foundSearchQueryServices.Insert(0, resourceAtId.Trim('/'));
}
else
{
foundSearchQueryServices.Add(resourceAtId.Trim('/'));
}
}
else

break;
case "PackageBaseAddress":
if (highestPackageBaseAddressApiVersion == null ||
(resourceTypeVersion > highestPackageBaseAddressApiVersion && resourceTypeVersion < maxSupportedApiVersion))
{
foundSearchQueryServices.Add(resourceAtId.Trim('/'));
highestPackageBaseAddressApiVersion = resourceTypeVersion;
packageBaseAddress = resourceAtId.Trim('/') + '/';
}

break;
case "PackageBaseAddress/3.0.0":
packageBaseAddress = resourceAtId.Trim('/') + '/';
break;
case "RegistrationsBaseUrl/3.6.0":
registrationsBaseUrl = resourceAtId.Trim('/') + '/';
case "RegistrationsBaseUrl":
if (highestRegistrationsBaseUrlApiVersion == null ||
(resourceTypeVersion > highestRegistrationsBaseUrlApiVersion && resourceTypeVersion < maxSupportedApiVersion))
{
highestRegistrationsBaseUrlApiVersion = resourceTypeVersion;
registrationsBaseUrl = resourceAtId.Trim('/') + '/';
}

break;
}
}
Expand Down

0 comments on commit 6d74db4

Please sign in to comment.