You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Hive.Versioning;
  5. using HVersion = Hive.Versioning.Version;
  6. namespace SemVer
  7. {
  8. [Obsolete("Use Hive.Versioning.VersionRange instead.")]
  9. public class Range : IEquatable<Range>, IEquatable<VersionRange>
  10. {
  11. public VersionRange UnderlyingRange { get; }
  12. private Range(VersionRange real) => UnderlyingRange = real;
  13. public Range(string rangeSpec, bool loose = false) : this(new(rangeSpec))
  14. => _ = loose; // loose is ignored because Hive doesn't have an equivalent
  15. public static Range ForHiveRange(VersionRange real) => new(real);
  16. public bool IsSatisfied(Version version) => IsSatisfied(version.UnderlyingVersion);
  17. public bool IsSatisfied(HVersion version) => UnderlyingRange.Matches(version);
  18. public bool IsSatisfied(string versionString, bool loose = false) => IsSatisfied(new Version(versionString, loose));
  19. public IEnumerable<Version> Satisfying(IEnumerable<Version> versions) => versions.Where(IsSatisfied);
  20. public IEnumerable<string> Satisfying(IEnumerable<string> versions, bool loose = false)
  21. => versions.Where(v => IsSatisfied(v, loose));
  22. public Version? MaxSatisfying(IEnumerable<Version> versions) => Satisfying(versions).Max();
  23. public string? MaxSatisfying(IEnumerable<string> versionStrings, bool loose = false)
  24. => MaxSatisfying(ValidVersions(versionStrings, loose))?.ToString();
  25. public Range Intersect(Range other) => new(UnderlyingRange & other.UnderlyingRange); // the conjunction is the intersection
  26. public override string ToString() => UnderlyingRange.ToString();
  27. public bool Equals(Range? other) => UnderlyingRange.Equals(other?.UnderlyingRange);
  28. public bool Equals(VersionRange? other) => UnderlyingRange.Equals(other);
  29. public override bool Equals(object? obj)
  30. => obj switch
  31. {
  32. Range r => Equals(r),
  33. VersionRange vr => Equals(vr),
  34. _ => false
  35. };
  36. public static bool operator ==(Range? a, Range? b) => a?.Equals(b) ?? b is null;
  37. public static bool operator !=(Range? a, Range? b) => !(a == b);
  38. public override int GetHashCode() => UnderlyingRange.GetHashCode();
  39. public static bool IsSatisfied(string rangeSpec, string versionString, bool loose = false)
  40. => new Range(rangeSpec, loose).IsSatisfied(versionString, loose);
  41. public static IEnumerable<string> Satisfying(string rangeSpec, IEnumerable<string> versions, bool loose = false)
  42. => new Range(rangeSpec, loose).Satisfying(versions, loose);
  43. public static string? MaxSatisfying(string rangeSpec, IEnumerable<string> versions, bool loose = false)
  44. => new Range(rangeSpec, loose).MaxSatisfying(versions, loose);
  45. private IEnumerable<Version> ValidVersions(IEnumerable<string> versionStrings, bool loose)
  46. {
  47. foreach (string versionString in versionStrings)
  48. {
  49. Version? version = null;
  50. try
  51. {
  52. version = new Version(versionString, loose);
  53. }
  54. catch (ArgumentException)
  55. {
  56. }
  57. if (version is not null)
  58. {
  59. yield return version;
  60. }
  61. }
  62. }
  63. }
  64. }