Skip to content

Commit

Permalink
Xml comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Feb 18, 2025
1 parent d06afee commit 629d6c8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
78 changes: 77 additions & 1 deletion Nuget/NugetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

using Ecng.Reflection;

/// <summary>
/// NuGET extensions.
/// </summary>
public static class NugetExtensions
{
/// <summary>
/// Get the target frameworks for the package.
/// </summary>
/// <param name="reader"><see cref="PackageArchiveReader"/></param>
/// <returns>Target frameworks.</returns>
public static string[] GetTargetFrameworks(this PackageArchiveReader reader)
{
var targetFrameworks = reader
Expand All @@ -22,16 +30,40 @@ public static string[] GetTargetFrameworks(this PackageArchiveReader reader)
return [.. targetFrameworks];
}

/// <summary>
/// Remove the platform version.
/// </summary>
/// <param name="fwk"><see cref="NuGetFramework"/></param>
/// <returns>The framework without the platform version.</returns>
public static NuGetFramework RemovePlatformVersion(this NuGetFramework fwk)
=> new(fwk.Framework, fwk.Version, fwk.Platform, FrameworkConstants.EmptyVersion);

/// <summary>
/// Get all versions.
/// </summary>
/// <param name="repo"><see cref="SourceRepository"/></param>
/// <param name="packageId">The package ID.</param>
/// <param name="logger"><see cref="ILogger"/></param>
/// <param name="cache"><see cref="SourceCacheContext"/></param>
/// <param name="token"><see cref="CancellationToken"/></param>
/// <returns>All versions ordered.</returns>
public static async Task<NuGetVersion[]> GetAllVersionsOrderedAsync(this SourceRepository repo, string packageId, ILogger logger, SourceCacheContext cache, CancellationToken token)
{
var resource = await repo.GetResourceAsync<FindPackageByIdResource>(token);

return [.. (await resource.GetAllVersionsAsync(packageId, cache, logger, token)).OrderBy(v => v)];
}

/// <summary>
/// Get the last version.
/// </summary>
/// <param name="repo"><see cref="SourceRepository"/></param>
/// <param name="packageId">The package ID.</param>
/// <param name="allowPreview">Allow preview versions.</param>
/// <param name="logger"><see cref="ILogger"/></param>
/// <param name="cache"><see cref="SourceCacheContext"/></param>
/// <param name="token"><see cref="CancellationToken"/></param>
/// <returns>The last version.</returns>
public static async Task<NuGetVersion> GetLastVersionAsync(this SourceRepository repo, string packageId, bool allowPreview, ILogger logger, SourceCacheContext cache, CancellationToken token)
{
var versions = await repo.GetAllVersionsOrderedAsync(packageId, logger, cache, token);
Expand All @@ -40,6 +72,16 @@ public static async Task<NuGetVersion> GetLastVersionAsync(this SourceRepository
return versions.LastOrDefault(cond);
}

/// <summary>
/// Get the last version in the floating range.
/// </summary>
/// <param name="repo"><see cref="SourceRepository"/></param>
/// <param name="packageId">The package ID.</param>
/// <param name="floatingVer">The floating version.</param>
/// <param name="logger"><see cref="ILogger"/></param>
/// <param name="cache"><see cref="SourceCacheContext"/></param>
/// <param name="token"><see cref="CancellationToken"/></param>
/// <returns>The last version in the floating range.</returns>
public static async Task<NuGetVersion> GetLastVersionInFloatingRangeAsync(this SourceRepository repo, string packageId, string floatingVer, ILogger logger, SourceCacheContext cache, CancellationToken token)
{
if (!FloatRange.TryParse(floatingVer, out var range))
Expand Down Expand Up @@ -82,6 +124,9 @@ event EventHandler ISettings.SettingsChanged
void ISettings.SaveToDisk() => throw new NotSupportedException();
}

/// <summary>
/// Disable access to the nuget.config file.
/// </summary>
public static void DisableNugetConfig()
{
// disable access nuget.config file
Expand All @@ -93,6 +138,11 @@ public static void DisableNugetConfig()
lazy.SetValue(proxy);
}

/// <summary>
/// Increment the version.
/// </summary>
/// <param name="version"><see cref="NuGetVersion"/></param>
/// <returns>The incremented version.</returns>
public static NuGetVersion Increment(this NuGetVersion version)
{
if (version is null)
Expand All @@ -101,6 +151,12 @@ public static NuGetVersion Increment(this NuGetVersion version)
return new(version.Major, version.Minor, version.Patch + 1);
}

/// <summary>
/// Add a suffix to the version.
/// </summary>
/// <param name="version"><see cref="NuGetVersion"/></param>
/// <param name="suffix">The suffix to add.</param>
/// <returns>The version with the suffix.</returns>
public static NuGetVersion WithSuffix(this NuGetVersion version, string suffix)
{
if (version is null)
Expand All @@ -112,6 +168,12 @@ public static NuGetVersion WithSuffix(this NuGetVersion version, string suffix)
return new(version.Major, version.Minor, version.Patch, suffix);
}

/// <summary>
/// Get the base URL for the repository.
/// </summary>
/// <param name="repo"><see cref="SourceRepository"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>The base URL for the repository.</returns>
public static async Task<Uri> GetBaseUrl(this SourceRepository repo, CancellationToken cancellationToken)
{
if (repo is null)
Expand All @@ -131,13 +193,27 @@ public static async Task<Uri> GetBaseUrl(this SourceRepository repo, Cancellatio
return baseUrl;
}

/// <summary>
/// Create a private HTTP client.
/// </summary>
/// <param name="apiKey">The API key.</param>
/// <returns>The private HTTP client.</returns>
public static HttpClient CreatePrivateHttp(string apiKey)
{
var http = new HttpClient();
http.DefaultRequestHeaders.Add(ProtocolConstants.ApiKeyHeader, apiKey);
return http;
}

/// <summary>
/// Get the nuspec file.
/// </summary>
/// <param name="http"><see cref="HttpClient"/></param>
/// <param name="baseUrl">The base URL for the repository.</param>
/// <param name="packageId">The package ID.</param>
/// <param name="version">The package version.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The nuspec file.</returns>
public static Task<Stream> GetNuspecAsync(this HttpClient http, Uri baseUrl, string packageId, NuGetVersion version, CancellationToken cancellationToken)
{
if (http is null) throw new ArgumentNullException(nameof(http));
Expand All @@ -147,4 +223,4 @@ public static Task<Stream> GetNuspecAsync(this HttpClient http, Uri baseUrl, str

return http.GetStreamAsync(new Uri(baseUrl, $"{packageId}/{version}/{packageId}{NuGetConstants.ManifestExtension}".ToLowerInvariant()), cancellationToken);
}
}
}
6 changes: 6 additions & 0 deletions Nuget/NugetLogger.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace Ecng.Nuget;

/// <summary>
/// The logger for Nuget.
/// </summary>
/// <param name="logger"><see cref="ILogReceiver"/></param>
public class NugetLogger(ILogReceiver logger) : LoggerBase
{
private readonly ILogReceiver _logger = logger ?? throw new ArgumentNullException(nameof(logger));

/// <inheritdoc />
public override void Log(ILogMessage message)
{
switch (message.Level)
Expand All @@ -20,6 +25,7 @@ public override void Log(ILogMessage message)
}
}

/// <inheritdoc />
public override Task LogAsync(ILogMessage message)
{
Log(message);
Expand Down
6 changes: 6 additions & 0 deletions Nuget/NugetProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

using Newtonsoft.Json.Linq;

/// <summary>
/// The factory for the Nuget provider.
/// </summary>
/// <param name="log"><see cref="ILogReceiver"/></param>
/// <param name="privateNugetToken">The private Nuget token.</param>
public class NugetProviderFactory(ILogReceiver log, SecureString privateNugetToken) : Repository.ProviderFactory
{
private class NugetRegistrationResourceProvider : ResourceProvider
Expand Down Expand Up @@ -111,6 +116,7 @@ public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceReposito
private readonly SecureString _privateNugetToken = privateNugetToken ?? throw new ArgumentNullException(nameof(privateNugetToken));
private readonly ILogReceiver _log = log ?? throw new ArgumentNullException(nameof(log));

/// <inheritdoc />
public override IEnumerable<Lazy<INuGetResourceProvider>> GetCoreV3()
{
yield return new Lazy<INuGetResourceProvider>(() => new NugetHttpHandlerProvider(_log, _privateNugetToken));
Expand Down
36 changes: 36 additions & 0 deletions Nuget/NugetRepoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Ecng.Nuget;
using Nito.AsyncEx;

/// <summary>
/// Nuget repository provider.
/// </summary>
public class NugetRepoProvider : CachingSourceProvider
{
Expand Down Expand Up @@ -59,9 +60,24 @@ private PrivatePackageSource(string addr) : base(addr, PrivateRepoKey) {}
private static readonly AsyncLock _instanceLock = new();
private static NugetRepoProvider _instance;

/// <summary>
/// Get instance.
/// </summary>
/// <param name="getAuthToken">Get auth token.</param>
/// <param name="packagesFolder"><see cref="Directory"/></param>
/// <param name="token"><see cref="CancellationToken"/></param>
/// <returns>Task.</returns>
public static Task<NugetRepoProvider> GetInstanceAsync(Func<string> getAuthToken, string packagesFolder, CancellationToken token)
=> GetInstanceAsync("https://nuget.stocksharp.com/x/v3/index.json", getAuthToken, packagesFolder, token);

/// <summary>
/// Get instance.
/// </summary>
/// <param name="privateUrl">Private url.</param>
/// <param name="getAuthToken">Get auth token.</param>
/// <param name="packagesFolder"><see cref="Directory"/></param>
/// <param name="token"><see cref="CancellationToken"/></param>
/// <returns>Task.</returns>
public static async Task<NugetRepoProvider> GetInstanceAsync(string privateUrl, Func<string> getAuthToken, string packagesFolder, CancellationToken token)
{
await PrivatePackageSource.GetAsync(privateUrl, token);
Expand Down Expand Up @@ -90,6 +106,7 @@ public static async Task<NugetRepoProvider> GetInstanceAsync(string privateUrl,
private static readonly ISettings _settings = NullSettings.Instance;

/// <summary>
/// Settings.
/// </summary>
public ISettings Settings => ((PackageSourceProvider)PackageSourceProvider).Settings;

Expand Down Expand Up @@ -125,6 +142,15 @@ async Task initBaseUrl(SourceRepository repo)
await initBaseUrl(_privateRepo);
}

/// <summary>
/// Try to find version.
/// </summary>
/// <param name="packageId">The package id.</param>
/// <param name="versionRange">The version range.</param>
/// <param name="cache"><see cref="SourceCacheContext"/></param>
/// <param name="logger"><see cref="ILogger"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>Found version, repository and base url.</returns>
public async Task<(NuGetVersion version, SourceRepository repo, Uri baseUrl)> TryFindVersion(string packageId, VersionRange versionRange, SourceCacheContext cache, ILogger logger, CancellationToken cancellationToken)
{
foreach (var (repo, baseUrl) in _repoUrls)
Expand All @@ -148,6 +174,16 @@ async Task initBaseUrl(SourceRepository repo)
throw new InvalidOperationException($"Package {packageId} for version range {versionRange} not found.");
}

/// <summary>
/// Get dependencies.
/// </summary>
/// <param name="identities">The package identities.</param>
/// <param name="framework">The framework.</param>
/// <param name="localFiles">The local files.</param>
/// <param name="cache"><see cref="SourceCacheContext"/></param>
/// <param name="logger"><see cref="ILogger"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>Dependencies.</returns>
public async Task<IDictionary<PackageIdentity, IEnumerable<(PackageIdentity identity, SourceRepository repo)>>> GetDependenciesAsync(IEnumerable<PackageIdentity> identities, NuGetFramework framework, IDictionary<string, NuGetVersion> localFiles, SourceCacheContext cache, ILogger logger, CancellationToken cancellationToken)
{
if (identities is null) throw new ArgumentNullException(nameof(identities));
Expand Down

0 comments on commit 629d6c8

Please sign in to comment.