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.

70 lines
3.3 KiB

  1. using System;
  2. using System.Linq;
  3. using HVersion = Hive.Versioning.Version;
  4. namespace SemVer
  5. {
  6. [Obsolete("Use Hive.Versioning.Version instead.")]
  7. public class Version : IComparable<Version>, IComparable<HVersion>, IComparable, IEquatable<Version>, IEquatable<HVersion>
  8. {
  9. public HVersion UnderlyingVersion { get; }
  10. private Version(HVersion real) => UnderlyingVersion = real;
  11. public static Version ForHiveVersion(HVersion real) => new(real);
  12. public Version(string input, bool loose = false) : this(new HVersion(input))
  13. => _ = loose; // specifically unused because Hive has no equivalent (by design)
  14. public Version(int major, int minor, int patch, string? preRelease = null, string? build = null)
  15. : this(new HVersion(major, minor, patch,
  16. preRelease is null ? Enumerable.Empty<string>() : preRelease.Split('.'),
  17. build is null ? Enumerable.Empty<string>() : build.Split('.')))
  18. {
  19. }
  20. public int Major => (int)UnderlyingVersion.Major;
  21. public int Minor => (int)UnderlyingVersion.Minor;
  22. public int Patch => (int)UnderlyingVersion.Patch;
  23. public string PreRelease => string.Join(".", UnderlyingVersion.PreReleaseIds);
  24. public string Build => string.Join(".", UnderlyingVersion.BuildIds);
  25. public Version BaseVersion() => new(new(UnderlyingVersion.Major, UnderlyingVersion.Minor, UnderlyingVersion.Patch));
  26. public override string ToString() => UnderlyingVersion.ToString();
  27. public string Clean() => ToString(); // normally this is the other way around kek
  28. public override int GetHashCode() => UnderlyingVersion.GetHashCode();
  29. public bool Equals(Version? other) => UnderlyingVersion.Equals(other?.UnderlyingVersion);
  30. public bool Equals(HVersion? other) => UnderlyingVersion.Equals(other);
  31. public override bool Equals(object? obj)
  32. => obj switch
  33. {
  34. Version v => Equals(v),
  35. HVersion h => Equals(h),
  36. _ => false
  37. };
  38. public int CompareTo(Version? other) => UnderlyingVersion.CompareTo(other?.UnderlyingVersion);
  39. public int CompareTo(HVersion? other) => UnderlyingVersion.CompareTo(other);
  40. public int CompareTo(object? obj)
  41. => obj switch
  42. {
  43. null => 1,
  44. Version v => CompareTo(v),
  45. HVersion h => CompareTo(h),
  46. _ => throw new ArgumentException("Object is not a Version")
  47. };
  48. public static bool operator ==(Version? a, Version? b)
  49. => a?.UnderlyingVersion == b?.UnderlyingVersion;
  50. public static bool operator !=(Version? a, Version? b)
  51. => a?.UnderlyingVersion != b?.UnderlyingVersion;
  52. public static bool operator >(Version? a, Version? b)
  53. => a is null ? b is not null && b.CompareTo(a) < 0 : a.CompareTo(b) > 0;
  54. public static bool operator >=(Version? a, Version? b)
  55. => !(a < b);
  56. public static bool operator <(Version? a, Version? b)
  57. => a is null ? b is not null && b.CompareTo(a) > 0 : a.CompareTo(b) < 0;
  58. public static bool operator <=(Version? a, Version? b)
  59. => !(a > b);
  60. }
  61. }