From fa456946a2284fd6893659de0334a3045bf6a68b Mon Sep 17 00:00:00 2001 From: Konrad Date: Fri, 12 Jul 2024 15:56:02 +0200 Subject: [PATCH 1/3] fix-init-V2Feed --- .../DependencyInjectionExtensions.cs | 45 ++++++++++--------- .../Upstream/Clients/V2UpstreamClient.cs | 6 +-- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs b/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs index c12e60bc..27f7971a 100644 --- a/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs +++ b/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs @@ -192,38 +192,41 @@ private static HttpClient HttpClientFactory(IServiceProvider provider) private static List CreateUpstreamClients(IServiceProvider provider) { - var options = provider.GetRequiredService>().Value; + var mirrorOptions = provider.GetRequiredService>().Value; var httpClient = provider.GetRequiredService(); - + var clients = new List(); - foreach (var source in options.Sources) + foreach (var source in mirrorOptions.Sources) { - IUpstreamClient client; - - if (source.Legacy) - { - using var scope = provider.CreateScope(); - var scopedProvider = scope.ServiceProvider; - - var logger = scopedProvider.GetRequiredService>(); - var optionSnapshot = scopedProvider.GetRequiredService>(); - client = new V2UpstreamClient(optionSnapshot, logger); - } - else - { - var logger = provider.GetRequiredService>(); - var nugetClientFactory = new NuGetClientFactory(httpClient, source.PackageSource.ToString()); - - client = new V3UpstreamClient(new NuGetClient(nugetClientFactory), logger); - } + using var scope = provider.CreateScope(); + var scopedProvider = scope.ServiceProvider; + IUpstreamClient client = source.Legacy + ? CreateV2Client(scopedProvider, source) + : CreateV3Client(scopedProvider, httpClient, source); clients.Add(client); } return clients; } + private static V2UpstreamClient CreateV2Client(IServiceProvider scopedProvider, MirrorSource mirrorSource) + { + var logger = scopedProvider.GetRequiredService>(); + var mirrorOptions = new MirrorOptions() { PackageSource = mirrorSource.PackageSource, Legacy = true }; + + return new V2UpstreamClient(mirrorOptions, logger); + } + + private static V3UpstreamClient CreateV3Client(IServiceProvider scopedProvider, HttpClient httpClient, MirrorSource source) + { + var logger = scopedProvider.GetRequiredService>(); + var nugetClientFactory = new NuGetClientFactory(httpClient, source.PackageSource.ToString()); + + return new V3UpstreamClient(new NuGetClient(nugetClientFactory), logger); + } + private static NuGetClientFactory NuGetClientFactoryFactory(IServiceProvider provider) { var httpClient = provider.GetRequiredService(); diff --git a/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs b/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs index 55af99ea..109a5f6d 100644 --- a/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs +++ b/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs @@ -31,12 +31,12 @@ public class V2UpstreamClient : IUpstreamClient, IDisposable private static readonly char[] AuthorsSeparators = new[] { ',', ';', '\t', '\n', '\r' }; public V2UpstreamClient( - IOptionsSnapshot options, + MirrorOptions options, ILogger logger) { ArgumentNullException.ThrowIfNull(options); - if (options.Value?.PackageSource?.AbsolutePath == null) + if (options.PackageSource?.AbsolutePath == null) { throw new ArgumentException("No mirror package source has been set."); } @@ -45,7 +45,7 @@ public V2UpstreamClient( _ngLogger = NullLogger.Instance; _cache = new SourceCacheContext(); - _repository = Repository.Factory.GetCoreV2(new PackageSource(options.Value.PackageSource.AbsoluteUri)); + _repository = Repository.Factory.GetCoreV2(new PackageSource(options.PackageSource.AbsoluteUri)); } public async Task> ListPackageVersionsAsync(string id, CancellationToken cancellationToken) From 06bebe34f449143375f363206403326f48efe976 Mon Sep 17 00:00:00 2001 From: Lio Konrad Date: Fri, 19 Jul 2024 09:26:09 +0200 Subject: [PATCH 2/3] Move MirrorSource class to own file --- .../Configuration/MirrorOptions.cs | 26 --------------- .../Configuration/MirrorSource.cs | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 src/BaGetter.Core/Configuration/MirrorSource.cs diff --git a/src/BaGetter.Core/Configuration/MirrorOptions.cs b/src/BaGetter.Core/Configuration/MirrorOptions.cs index 0214e085..edfd019f 100644 --- a/src/BaGetter.Core/Configuration/MirrorOptions.cs +++ b/src/BaGetter.Core/Configuration/MirrorOptions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.Linq; namespace BaGetter.Core; @@ -79,28 +78,3 @@ private void UpdateSources() Sources = [new MirrorSource { PackageSource = PackageSource, Legacy = Legacy }]; } } - -public class MirrorSource -{ - public Uri PackageSource { get; set; } - public bool Legacy { get; set; } - - public override bool Equals(object obj) - { - // Check for null and compare run-time types. - if (obj is null || !GetType().Equals(obj.GetType())) - { - return false; - } - else - { - var ms = (MirrorSource)obj; - return (PackageSource.Equals(ms.PackageSource)) && (Legacy == ms.Legacy); - } - } - - public override int GetHashCode() - { - return HashCode.Combine(PackageSource, Legacy); - } -} diff --git a/src/BaGetter.Core/Configuration/MirrorSource.cs b/src/BaGetter.Core/Configuration/MirrorSource.cs new file mode 100644 index 00000000..c689af0f --- /dev/null +++ b/src/BaGetter.Core/Configuration/MirrorSource.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BaGetter.Core; + +public class MirrorSource +{ + public Uri PackageSource { get; set; } + public bool Legacy { get; set; } + + public override bool Equals(object obj) + { + // Check for null and compare run-time types. + if (obj is null || !GetType().Equals(obj.GetType())) + { + return false; + } + else + { + var ms = (MirrorSource)obj; + return (PackageSource.Equals(ms.PackageSource)) && (Legacy == ms.Legacy); + } + } + + public override int GetHashCode() + { + return HashCode.Combine(PackageSource, Legacy); + } +} + From aebfe84c8d66c8e32a56f3ba9d1175b204772a6a Mon Sep 17 00:00:00 2001 From: Lio Konrad Date: Fri, 19 Jul 2024 09:26:41 +0200 Subject: [PATCH 3/3] Use MirrorSource instead of MirrorOptions --- src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs | 2 +- src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs b/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs index 27f7971a..a4c567f2 100644 --- a/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs +++ b/src/BaGetter.Core/Extensions/DependencyInjectionExtensions.cs @@ -214,7 +214,7 @@ private static List CreateUpstreamClients(IServiceProvider prov private static V2UpstreamClient CreateV2Client(IServiceProvider scopedProvider, MirrorSource mirrorSource) { var logger = scopedProvider.GetRequiredService>(); - var mirrorOptions = new MirrorOptions() { PackageSource = mirrorSource.PackageSource, Legacy = true }; + var mirrorOptions = new MirrorSource() { PackageSource = mirrorSource.PackageSource, Legacy = true }; return new V2UpstreamClient(mirrorOptions, logger); } diff --git a/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs b/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs index 109a5f6d..b7f5a0cb 100644 --- a/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs +++ b/src/BaGetter.Core/Upstream/Clients/V2UpstreamClient.cs @@ -31,7 +31,7 @@ public class V2UpstreamClient : IUpstreamClient, IDisposable private static readonly char[] AuthorsSeparators = new[] { ',', ';', '\t', '\n', '\r' }; public V2UpstreamClient( - MirrorOptions options, + MirrorSource options, ILogger logger) { ArgumentNullException.ThrowIfNull(options);