#pragma warning disable SA1512,SA1124 // Single-line comments should not be followed by blank line using System; using System.IO; using JetBrains.Annotations; using NugetForUnity.Configuration; using UnityEngine; #region No ReShaper // ReSharper disable All // needed because 'JetBrains.Annotations.NotNull' and 'System.Diagnostics.CodeAnalysis.NotNull' collide if this file is compiled with a never version of Unity / C# using SuppressMessageAttribute = System.Diagnostics.CodeAnalysis.SuppressMessageAttribute; // ReSharper restore All #endregion #pragma warning restore SA1512,SA1124 // Single-line comments should not be followed by blank line namespace NugetForUnity.Models { /// /// Represents an identifier for a NuGet package. It contains only an ID and a Version number. /// [Serializable] public class NugetPackageIdentifier : INugetPackageIdentifier, IEquatable, IComparable { /// /// Initializes a new instance of the class with empty ID and Version. /// public NugetPackageIdentifier() { Id = string.Empty; PackageVersion = new NugetPackageVersion(); } /// /// Initializes a new instance of the class with the given ID and Version. /// /// The ID of the package. /// The version number of the package. public NugetPackageIdentifier([NotNull] string id, [CanBeNull] string version) { Id = id; PackageVersion = new NugetPackageVersion(version); } /// [field: SerializeField] public string Id { get; set; } /// [field: SerializeField] public NugetPackageVersion PackageVersion { get; set; } /// [field: SerializeField] public bool IsManuallyInstalled { get; set; } /// public string Version { get => PackageVersion.NormalizedVersion; set { if (PackageVersion.FullVersion == value) { return; } PackageVersion = new NugetPackageVersion(value); } } /// public string PackageFileName => $"{Id}.{Version}.nupkg"; /// public string SpecificationFileName => $"{Id}.nuspec"; /// public bool IsPrerelease => PackageVersion.IsPrerelease; /// public bool HasVersionRange => PackageVersion.HasVersionRange; /// /// Checks to see if the first is less than the second. /// /// The first to compare. /// The second to compare. /// True if the first is less than the second. public static bool operator <([CanBeNull] NugetPackageIdentifier first, [CanBeNull] NugetPackageIdentifier second) { if (first is null) { return true; } return first.CompareTo(second) < 0; } /// /// Checks to see if the first is greater than the second. /// /// The first to compare. /// The second to compare. /// True if the first is greater than the second. public static bool operator >([CanBeNull] NugetPackageIdentifier first, [CanBeNull] NugetPackageIdentifier second) { if (first is null) { return false; } return first.CompareTo(second) > 0; } /// /// Checks to see if the first is less than or equal to the second. /// /// The first to compare. /// The second to compare. /// True if the first is less than or equal to the second. public static bool operator <=([CanBeNull] NugetPackageIdentifier first, [CanBeNull] NugetPackageIdentifier second) { if (first is null) { return second is null; } return first.CompareTo(second) <= 0; } /// /// Checks to see if the first is greater than or equal to the second. /// /// The first to compare. /// The second to compare. /// True if the first is greater than or equal to the second. public static bool operator >=([CanBeNull] NugetPackageIdentifier first, [CanBeNull] NugetPackageIdentifier second) { if (first is null) { return second is null; } return first.CompareTo(second) >= 0; } /// /// Checks to see if the first is equal to the second. /// They are equal if the Id and the Version match. /// /// The first to compare. /// The second to compare. /// True if the first is equal to the second. public static bool operator ==([CanBeNull] NugetPackageIdentifier first, [CanBeNull] NugetPackageIdentifier second) { if (ReferenceEquals(first, second)) { return true; } if (first is null) { return false; } return first.Equals(second); } /// /// Checks to see if the first is not equal to the second. /// They are not equal if the Id or the Version differ. /// /// The first to compare. /// The second to compare. /// True if the first is not equal to the second. public static bool operator !=([CanBeNull] NugetPackageIdentifier first, [CanBeNull] NugetPackageIdentifier second) { return !(first == second); } /// public string GetPackageInstallPath(string prefix = "") { return Path.Combine(ConfigurationManager.NugetConfigFile.RepositoryPath, $"{prefix}{Id}.{Version}"); } /// public int CompareTo([CanBeNull] NugetPackageIdentifier other) { return CompareTo(other as INugetPackageIdentifier); } /// public int CompareTo([CanBeNull] INugetPackageIdentifier other) { if (other is null) { return -1; } var idCompareResult = string.Compare(Id, other.Id, StringComparison.OrdinalIgnoreCase); if (idCompareResult != 0) { return idCompareResult; } return PackageVersion.CompareTo(other.PackageVersion); } /// public bool InRange(INugetPackageIdentifier otherPackage) { return PackageVersion.InRange(otherPackage.PackageVersion); } /// public bool InRange(NugetPackageVersion otherVersion) { return PackageVersion.InRange(otherVersion); } /// /// Checks to see if this is equal to the given one. /// /// The other to check equality with. /// True if the package identifiers are equal, otherwise false. public bool Equals(NugetPackageIdentifier other) { return Equals(other as INugetPackageIdentifier); } /// /// Checks to see if this is equal to the given one. /// /// The other to check equality with. /// True if the package identifiers are equal, otherwise false. public bool Equals(INugetPackageIdentifier other) { if (ReferenceEquals(this, other)) { return true; } return !(other is null) && string.Equals(other.Id, Id, StringComparison.OrdinalIgnoreCase) && other.PackageVersion.Equals(PackageVersion); } /// /// Determines if a given object is equal to this . /// /// The object to check. /// True if the given object is equal to this , otherwise false. public override bool Equals(object obj) { return Equals(obj as INugetPackageIdentifier); } /// /// Gets the hash-code for this . /// /// The hash-code for this instance. [SuppressMessage( "ReSharper", "NonReadonlyMemberInGetHashCode", Justification = "We only edit the version / id before we use the hash (stroe it in a dictionary).")] public override int GetHashCode() { return GetHashCodeOfId() ^ PackageVersion.GetHashCode(); } /// /// Returns the string representation of this in the form "{ID}.{Version}". /// /// A string in the form "{ID}.{Version}". public override string ToString() { return $"{Id}.{Version}"; } private int GetHashCodeOfId() { #if UNITY_2021_2_OR_NEWER return Id.GetHashCode(StringComparison.OrdinalIgnoreCase); #else return StringComparer.OrdinalIgnoreCase.GetHashCode(Id); #endif } } }