Files
se-launcher/NuGet/PackageSourceMapping.cs
zznty f2d75e5408
All checks were successful
Build / Compute Version (push) Successful in 6s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 2m13s
Build / Build Nuget package (NuGet) (push) Successful in 2m13s
Build / Build Nuget package (CringePlugins) (push) Successful in 2m55s
Build / Build Nuget package (SharedCringe) (push) Successful in 2m35s
Build / Build Launcher (push) Successful in 3m23s
sources editor
2024-11-03 18:07:57 +07:00

28 lines
1.1 KiB
C#

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace NuGet;
public class PackageSourceMapping(ImmutableArray<PackageSource> sources)
{
private readonly ImmutableArray<(string pattern, Task<NuGetClient> client)> _clients = [
..sources.Select(b =>
(b.Pattern,
NuGetClient.CreateFromIndexUrlAsync(b.Url)))
];
public Task<NuGetClient> GetClientAsync(string packageId) =>
_clients.FirstOrDefault(b => Regex.IsMatch(packageId, b.pattern)).client;
public ConfiguredCancelableAsyncEnumerable<NuGetClient>.Enumerator GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return _clients.ToAsyncEnumerable()
.SelectAwait(b => new ValueTask<NuGetClient>(b.client))
.WithCancellation(cancellationToken)
.GetAsyncEnumerator();
}
}
public record PackageSource(string Name, [StringSyntax("Regex")] string Pattern, [StringSyntax("Uri")] string Url);