using System; using System.Collections.Generic; using System.Globalization; using JetBrains.Annotations; namespace NugetForUnity.PackageSource { /// /// Helper to create instances. /// internal static class NugetPackageSourceCreator { /// /// Creates a new instance. /// /// The name of the source. /// The path or url of the source. /// A optional value to overwrite the API version. /// The list of all source that should be used when creating a combined package source. /// The created source. [NotNull] public static INugetPackageSource CreatePackageSource( [NotNull] string name, [NotNull] string path, [CanBeNull] string protocolVersion, [CanBeNull] [ItemNotNull] List packageSources) { name = name.Trim(); path = path.Trim(); if (name.Equals("All", StringComparison.OrdinalIgnoreCase) && path.Equals("(Aggregate source)", StringComparison.OrdinalIgnoreCase)) { return new NugetPackageSourceCombined( packageSources ?? throw new ArgumentNullException(nameof(packageSources), "For creating combined packages we need a list.")); } if (!path.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { return new NugetPackageSourceLocal(name, path); } // see: https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file // The NuGet server protocol version defaults to version "2" when not pointing to a package source URL ending in .json (e.g. https://api.nuget.org/v3/index.json). if (path.EndsWith(".json", StringComparison.OrdinalIgnoreCase) || (double.TryParse(protocolVersion, NumberStyles.Any, CultureInfo.InvariantCulture, out var protocolVersionNumber) && protocolVersionNumber >= 3)) { return new NugetPackageSourceV3(name, path, protocolVersion); } return new NugetPackageSourceV2(name, path, protocolVersion); } } }