SemVersion.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // Backported to net35 from https://github.com/maxhauser/semver
  2. using System;
  3. using System.Globalization;
  4. using System.Runtime.Serialization;
  5. using System.Security.Permissions;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace BepInEx.Core
  9. {
  10. internal static class IntExtensions
  11. {
  12. /// <summary>
  13. /// The number of digits in a non-negative number. Returns 1 for all
  14. /// negative numbers. That is ok because we are using it to calculate
  15. /// string length for a <see cref="StringBuilder"/> for numbers that
  16. /// aren't supposed to be negative, but when they are it is just a little
  17. /// slower.
  18. /// </summary>
  19. /// <remarks>
  20. /// This approach is based on https://stackoverflow.com/a/51099524/268898
  21. /// where the poster offers performance benchmarks showing this is the
  22. /// fastest way to get a number of digits.
  23. /// </remarks>
  24. public static int Digits(this int n)
  25. {
  26. if (n < 10)
  27. return 1;
  28. if (n < 100)
  29. return 2;
  30. if (n < 1_000)
  31. return 3;
  32. if (n < 10_000)
  33. return 4;
  34. if (n < 100_000)
  35. return 5;
  36. if (n < 1_000_000)
  37. return 6;
  38. if (n < 10_000_000)
  39. return 7;
  40. if (n < 100_000_000)
  41. return 8;
  42. if (n < 1_000_000_000)
  43. return 9;
  44. return 10;
  45. }
  46. }
  47. [Serializable]
  48. public sealed class SemVersion : IComparable<SemVersion>, IComparable, ISerializable
  49. {
  50. private static readonly Regex ParseEx =
  51. new Regex(@"^(?<major>\d+)" +
  52. @"(?>\.(?<minor>\d+))?" +
  53. @"(?>\.(?<patch>\d+))?" +
  54. @"(?>\-(?<pre>[0-9A-Za-z\-\.]+))?" +
  55. @"(?>\+(?<build>[0-9A-Za-z\-\.]+))?$",
  56. RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.ExplicitCapture,
  57. TimeSpan.FromSeconds(0.5));
  58. #pragma warning disable CA1801 // Parameter unused
  59. /// <summary>
  60. /// Deserialize a <see cref="SemVersion"/>.
  61. /// </summary>
  62. /// <exception cref="ArgumentNullException">The <paramref name="info"/> parameter is null.</exception>
  63. private SemVersion(SerializationInfo info, StreamingContext context)
  64. #pragma warning restore CA1801 // Parameter unused
  65. {
  66. if (info == null)
  67. throw new ArgumentNullException(nameof(info));
  68. var semVersion = Parse(info.GetString("SemVersion"));
  69. Major = semVersion.Major;
  70. Minor = semVersion.Minor;
  71. Patch = semVersion.Patch;
  72. Prerelease = semVersion.Prerelease;
  73. Build = semVersion.Build;
  74. }
  75. /// <summary>
  76. /// Constructs a new instance of the <see cref="SemVersion" /> class.
  77. /// </summary>
  78. /// <param name="major">The major version.</param>
  79. /// <param name="minor">The minor version.</param>
  80. /// <param name="patch">The patch version.</param>
  81. /// <param name="prerelease">The prerelease version (e.g. "alpha").</param>
  82. /// <param name="build">The build metadata (e.g. "nightly.232").</param>
  83. public SemVersion(int major, int minor = 0, int patch = 0, string prerelease = "", string build = "")
  84. {
  85. Major = major;
  86. Minor = minor;
  87. Patch = patch;
  88. Prerelease = prerelease ?? "";
  89. Build = build ?? "";
  90. }
  91. /// <summary>
  92. /// Constructs a new instance of the <see cref="SemVersion"/> class from
  93. /// a <see cref="System.Version"/>.
  94. /// </summary>
  95. /// <param name="version">The <see cref="Version"/> that is used to initialize
  96. /// the Major, Minor, Patch and Build.</param>
  97. /// <returns>A <see cref="SemVersion"/> with the same Major and Minor version.
  98. /// The Patch version will be the fourth part of the version number. The
  99. /// build meta data will contain the third part of the version number if
  100. /// it is greater than zero.</returns>
  101. public SemVersion(Version version)
  102. {
  103. if (version == null)
  104. throw new ArgumentNullException(nameof(version));
  105. Major = version.Major;
  106. Minor = version.Minor;
  107. if (version.Revision >= 0)
  108. Patch = version.Revision;
  109. Prerelease = "";
  110. Build = version.Build > 0 ? version.Build.ToString(CultureInfo.InvariantCulture) : "";
  111. }
  112. /// <summary>
  113. /// Converts the string representation of a semantic version to its <see cref="SemVersion"/> equivalent.
  114. /// </summary>
  115. /// <param name="version">The version string.</param>
  116. /// <param name="strict">If set to <see langword="true"/> minor and patch version are required,
  117. /// otherwise they are optional.</param>
  118. /// <returns>The <see cref="SemVersion"/> object.</returns>
  119. /// <exception cref="ArgumentNullException">The <paramref name="version"/> is <see langword="null"/>.</exception>
  120. /// <exception cref="ArgumentException">The <paramref name="version"/> has an invalid format.</exception>
  121. /// <exception cref="InvalidOperationException">The <paramref name="version"/> is missing Minor or Patch versions and <paramref name="strict"/> is <see langword="true"/>.</exception>
  122. /// <exception cref="OverflowException">The Major, Minor, or Patch versions are larger than <code>int.MaxValue</code>.</exception>
  123. public static SemVersion Parse(string version, bool strict = false)
  124. {
  125. var match = ParseEx.Match(version);
  126. if (!match.Success)
  127. throw new ArgumentException($"Invalid version '{version}'.", nameof(version));
  128. var major = int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture);
  129. var minorMatch = match.Groups["minor"];
  130. int minor = 0;
  131. if (minorMatch.Success)
  132. minor = int.Parse(minorMatch.Value, CultureInfo.InvariantCulture);
  133. else if (strict)
  134. throw new InvalidOperationException("Invalid version (no minor version given in strict mode)");
  135. var patchMatch = match.Groups["patch"];
  136. int patch = 0;
  137. if (patchMatch.Success)
  138. patch = int.Parse(patchMatch.Value, CultureInfo.InvariantCulture);
  139. else if (strict)
  140. throw new InvalidOperationException("Invalid version (no patch version given in strict mode)");
  141. var prerelease = match.Groups["pre"].Value;
  142. var build = match.Groups["build"].Value;
  143. return new SemVersion(major, minor, patch, prerelease, build);
  144. }
  145. /// <summary>
  146. /// Converts the string representation of a semantic version to its <see cref="SemVersion"/>
  147. /// equivalent and returns a value that indicates whether the conversion succeeded.
  148. /// </summary>
  149. /// <param name="version">The version string.</param>
  150. /// <param name="semver">When the method returns, contains a <see cref="SemVersion"/> instance equivalent
  151. /// to the version string passed in, if the version string was valid, or <see langword="null"/> if the
  152. /// version string was not valid.</param>
  153. /// <param name="strict">If set to <see langword="true"/> minor and patch version are required,
  154. /// otherwise they are optional.</param>
  155. /// <returns><see langword="false"/> when a invalid version string is passed, otherwise <see langword="true"/>.</returns>
  156. public static bool TryParse(string version, out SemVersion semver, bool strict = false)
  157. {
  158. semver = null;
  159. if (version is null)
  160. return false;
  161. var match = ParseEx.Match(version);
  162. if (!match.Success)
  163. return false;
  164. if (!int.TryParse(match.Groups["major"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var major))
  165. return false;
  166. var minorMatch = match.Groups["minor"];
  167. int minor = 0;
  168. if (minorMatch.Success)
  169. {
  170. if (!int.TryParse(minorMatch.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out minor))
  171. return false;
  172. }
  173. else if (strict)
  174. return false;
  175. var patchMatch = match.Groups["patch"];
  176. int patch = 0;
  177. if (patchMatch.Success)
  178. {
  179. if (!int.TryParse(patchMatch.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out patch))
  180. return false;
  181. }
  182. else if (strict)
  183. return false;
  184. var prerelease = match.Groups["pre"].Value;
  185. var build = match.Groups["build"].Value;
  186. semver = new SemVersion(major, minor, patch, prerelease, build);
  187. return true;
  188. }
  189. /// <summary>
  190. /// Checks whether two semantic versions are equal.
  191. /// </summary>
  192. /// <param name="versionA">The first version to compare.</param>
  193. /// <param name="versionB">The second version to compare.</param>
  194. /// <returns><see langword="true"/> if the two values are equal, otherwise <see langword="false"/>.</returns>
  195. public static bool Equals(SemVersion versionA, SemVersion versionB)
  196. {
  197. if (ReferenceEquals(versionA, versionB))
  198. return true;
  199. if (versionA is null || versionB is null)
  200. return false;
  201. return versionA.Equals(versionB);
  202. }
  203. /// <summary>
  204. /// Compares the specified versions.
  205. /// </summary>
  206. /// <param name="versionA">The first version to compare.</param>
  207. /// <param name="versionB">The second version to compare.</param>
  208. /// <returns>A signed number indicating the relative values of <paramref name="versionA"/> and <paramref name="versionB"/>.</returns>
  209. public static int Compare(SemVersion versionA, SemVersion versionB)
  210. {
  211. if (ReferenceEquals(versionA, versionB))
  212. return 0;
  213. if (versionA is null)
  214. return -1;
  215. if (versionB is null)
  216. return 1;
  217. return versionA.CompareTo(versionB);
  218. }
  219. /// <summary>
  220. /// Make a copy of the current instance with changed properties.
  221. /// </summary>
  222. /// <param name="major">The value to replace the major version or <see langword="null"/> to leave it unchanged.</param>
  223. /// <param name="minor">The value to replace the minor version or <see langword="null"/> to leave it unchanged.</param>
  224. /// <param name="patch">The value to replace the patch version or <see langword="null"/> to leave it unchanged.</param>
  225. /// <param name="prerelease">The value to replace the prerelease version or <see langword="null"/> to leave it unchanged.</param>
  226. /// <param name="build">The value to replace the build metadata or <see langword="null"/> to leave it unchanged.</param>
  227. /// <returns>The new version object.</returns>
  228. /// <remarks>
  229. /// The change method is intended to be called using named argument syntax, passing only
  230. /// those fields to be changed.
  231. /// </remarks>
  232. /// <example>
  233. /// To change only the patch version:
  234. /// <code>version.Change(patch: 4)</code>
  235. /// </example>
  236. public SemVersion Change(int? major = null, int? minor = null, int? patch = null,
  237. string prerelease = null, string build = null)
  238. {
  239. return new SemVersion(
  240. major ?? Major,
  241. minor ?? Minor,
  242. patch ?? Patch,
  243. prerelease ?? Prerelease,
  244. build ?? Build);
  245. }
  246. /// <summary>
  247. /// Gets the major version.
  248. /// </summary>
  249. /// <value>
  250. /// The major version.
  251. /// </value>
  252. public int Major { get; }
  253. /// <summary>
  254. /// Gets the minor version.
  255. /// </summary>
  256. /// <value>
  257. /// The minor version.
  258. /// </value>
  259. public int Minor { get; }
  260. /// <summary>
  261. /// Gets the patch version.
  262. /// </summary>
  263. /// <value>
  264. /// The patch version.
  265. /// </value>
  266. public int Patch { get; }
  267. /// <summary>
  268. /// Gets the prerelease version.
  269. /// </summary>
  270. /// <value>
  271. /// The prerelease version. Empty string if this is a release version.
  272. /// </value>
  273. public string Prerelease { get; }
  274. /// <summary>
  275. /// Gets the build metadata.
  276. /// </summary>
  277. /// <value>
  278. /// The build metadata. Empty string if there is no build metadata.
  279. /// </value>
  280. public string Build { get; }
  281. /// <summary>
  282. /// Returns the <see cref="string" /> equivalent of this version.
  283. /// </summary>
  284. /// <returns>
  285. /// The <see cref="string" /> equivalent of this version.
  286. /// </returns>
  287. public override string ToString()
  288. {
  289. // Assume all separators ("..-+"), at most 2 extra chars
  290. var estimatedLength = 4 + Major.Digits() + Minor.Digits() + Patch.Digits()
  291. + Prerelease.Length + Build.Length;
  292. var version = new StringBuilder(estimatedLength);
  293. version.Append(Major);
  294. version.Append('.');
  295. version.Append(Minor);
  296. version.Append('.');
  297. version.Append(Patch);
  298. if (Prerelease.Length > 0)
  299. {
  300. version.Append('-');
  301. version.Append(Prerelease);
  302. }
  303. if (Build.Length > 0)
  304. {
  305. version.Append('+');
  306. version.Append(Build);
  307. }
  308. return version.ToString();
  309. }
  310. /// <summary>
  311. /// Compares the current instance with another object of the same type and returns an integer that indicates
  312. /// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
  313. /// other object.
  314. /// </summary>
  315. /// <param name="obj">An object to compare with this instance.</param>
  316. /// <returns>
  317. /// A value that indicates the relative order of the objects being compared.
  318. /// The return value has these meanings:
  319. /// Less than zero: This instance precedes <paramref name="obj" /> in the sort order.
  320. /// Zero: This instance occurs in the same position in the sort order as <paramref name="obj" />.
  321. /// Greater than zero: This instance follows <paramref name="obj" /> in the sort order.
  322. /// </returns>
  323. /// <exception cref="InvalidCastException">The <paramref name="obj"/> is not a <see cref="SemVersion"/>.</exception>
  324. public int CompareTo(object obj)
  325. {
  326. return CompareTo((SemVersion)obj);
  327. }
  328. /// <summary>
  329. /// Compares the current instance with another object of the same type and returns an integer that indicates
  330. /// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
  331. /// other object.
  332. /// </summary>
  333. /// <param name="other">An object to compare with this instance.</param>
  334. /// <returns>
  335. /// A value that indicates the relative order of the objects being compared.
  336. /// The return value has these meanings:
  337. /// Less than zero: This instance precedes <paramref name="other" /> in the sort order.
  338. /// Zero: This instance occurs in the same position in the sort order as <paramref name="other" />.
  339. /// Greater than zero: This instance follows <paramref name="other" /> in the sort order.
  340. /// </returns>
  341. public int CompareTo(SemVersion other)
  342. {
  343. var r = CompareByPrecedence(other);
  344. if (r != 0)
  345. return r;
  346. #pragma warning disable CA1062 // Validate arguments of public methods
  347. // If other is null, CompareByPrecedence() returns 1
  348. return CompareComponent(Build, other.Build);
  349. #pragma warning restore CA1062 // Validate arguments of public methods
  350. }
  351. /// <summary>
  352. /// Returns whether two semantic versions have the same precedence. Versions
  353. /// that differ only by build metadata have the same precedence.
  354. /// </summary>
  355. /// <param name="other">The semantic version to compare to.</param>
  356. /// <returns><see langword="true"/> if the version precedences are equal.</returns>
  357. public bool PrecedenceMatches(SemVersion other)
  358. {
  359. return CompareByPrecedence(other) == 0;
  360. }
  361. /// <summary>
  362. /// Compares two semantic versions by precedence as defined in the SemVer spec. Versions
  363. /// that differ only by build metadata have the same precedence.
  364. /// </summary>
  365. /// <param name="other">The semantic version.</param>
  366. /// <returns>
  367. /// A value that indicates the relative order of the objects being compared.
  368. /// The return value has these meanings:
  369. /// Less than zero: This instance precedes <paramref name="other" /> in the sort order.
  370. /// Zero: This instance occurs in the same position in the sort order as <paramref name="other" />.
  371. /// Greater than zero: This instance follows <paramref name="other" /> in the sort order.
  372. /// </returns>
  373. public int CompareByPrecedence(SemVersion other)
  374. {
  375. if (other is null)
  376. return 1;
  377. var r = Major.CompareTo(other.Major);
  378. if (r != 0)
  379. return r;
  380. r = Minor.CompareTo(other.Minor);
  381. if (r != 0)
  382. return r;
  383. r = Patch.CompareTo(other.Patch);
  384. if (r != 0)
  385. return r;
  386. return CompareComponent(Prerelease, other.Prerelease, true);
  387. }
  388. private static int CompareComponent(string a, string b, bool nonemptyIsLower = false)
  389. {
  390. var aEmpty = string.IsNullOrEmpty(a);
  391. var bEmpty = string.IsNullOrEmpty(b);
  392. if (aEmpty && bEmpty)
  393. return 0;
  394. if (aEmpty)
  395. return nonemptyIsLower ? 1 : -1;
  396. if (bEmpty)
  397. return nonemptyIsLower ? -1 : 1;
  398. var aComps = a.Split('.');
  399. var bComps = b.Split('.');
  400. var minLen = Math.Min(aComps.Length, bComps.Length);
  401. for (int i = 0; i < minLen; i++)
  402. {
  403. var ac = aComps[i];
  404. var bc = bComps[i];
  405. var aIsNum = int.TryParse(ac, out var aNum);
  406. var bIsNum = int.TryParse(bc, out var bNum);
  407. int r;
  408. if (aIsNum && bIsNum)
  409. {
  410. r = aNum.CompareTo(bNum);
  411. if (r != 0)
  412. return r;
  413. }
  414. else
  415. {
  416. if (aIsNum)
  417. return -1;
  418. if (bIsNum)
  419. return 1;
  420. r = string.CompareOrdinal(ac, bc);
  421. if (r != 0)
  422. return r;
  423. }
  424. }
  425. return aComps.Length.CompareTo(bComps.Length);
  426. }
  427. /// <summary>
  428. /// Determines whether the specified <see cref="object" /> is equal to this instance.
  429. /// </summary>
  430. /// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
  431. /// <returns>
  432. /// <see langword="true"/> if the specified <see cref="object" /> is equal to this instance, otherwise <see langword="false"/>.
  433. /// </returns>
  434. /// <exception cref="InvalidCastException">The <paramref name="obj"/> is not a <see cref="SemVersion"/>.</exception>
  435. public override bool Equals(object obj)
  436. {
  437. if (obj is null)
  438. return false;
  439. if (ReferenceEquals(this, obj))
  440. return true;
  441. var other = (SemVersion)obj;
  442. return Major == other.Major
  443. && Minor == other.Minor
  444. && Patch == other.Patch
  445. && string.Equals(Prerelease, other.Prerelease, StringComparison.Ordinal)
  446. && string.Equals(Build, other.Build, StringComparison.Ordinal);
  447. }
  448. /// <summary>
  449. /// Returns a hash code for this instance.
  450. /// </summary>
  451. /// <returns>
  452. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  453. /// </returns>
  454. public override int GetHashCode()
  455. {
  456. unchecked
  457. {
  458. // TODO verify this. Some versions start result = 17. Some use 37 instead of 31
  459. int result = Major.GetHashCode();
  460. result = result * 31 + Minor.GetHashCode();
  461. result = result * 31 + Patch.GetHashCode();
  462. result = result * 31 + Prerelease.GetHashCode();
  463. result = result * 31 + Build.GetHashCode();
  464. return result;
  465. }
  466. }
  467. /// <summary>
  468. /// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object.
  469. /// </summary>
  470. /// <param name="info">The <see cref="SerializationInfo"/> to populate with data.</param>
  471. /// <param name="context">The destination (see <see cref="SerializationInfo"/>) for this serialization.</param>
  472. [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
  473. public void GetObjectData(SerializationInfo info, StreamingContext context)
  474. {
  475. if (info == null)
  476. throw new ArgumentNullException(nameof(info));
  477. info.AddValue("SemVersion", ToString());
  478. }
  479. #pragma warning disable CA2225 // Operator overloads have named alternates
  480. /// <summary>
  481. /// Implicit conversion from <see cref="string"/> to <see cref="SemVersion"/>.
  482. /// </summary>
  483. /// <param name="version">The semantic version.</param>
  484. /// <returns>The <see cref="SemVersion"/> object.</returns>
  485. /// <exception cref="ArgumentNullException">The <paramref name="version"/> is <see langword="null"/>.</exception>
  486. /// <exception cref="ArgumentException">The version number has an invalid format.</exception>
  487. /// <exception cref="OverflowException">The Major, Minor, or Patch versions are larger than <code>int.MaxValue</code>.</exception>
  488. public static implicit operator SemVersion(string version)
  489. #pragma warning restore CA2225 // Operator overloads have named alternates
  490. {
  491. return Parse(version);
  492. }
  493. /// <summary>
  494. /// Compares two semantic versions for equality.
  495. /// </summary>
  496. /// <param name="left">The left value.</param>
  497. /// <param name="right">The right value.</param>
  498. /// <returns>If left is equal to right <see langword="true"/>, otherwise <see langword="false"/>.</returns>
  499. public static bool operator ==(SemVersion left, SemVersion right)
  500. {
  501. return Equals(left, right);
  502. }
  503. /// <summary>
  504. /// Compares two semantic versions for inequality.
  505. /// </summary>
  506. /// <param name="left">The left value.</param>
  507. /// <param name="right">The right value.</param>
  508. /// <returns>If left is not equal to right <see langword="true"/>, otherwise <see langword="false"/>.</returns>
  509. public static bool operator !=(SemVersion left, SemVersion right)
  510. {
  511. return !Equals(left, right);
  512. }
  513. /// <summary>
  514. /// Compares two semantic versions.
  515. /// </summary>
  516. /// <param name="left">The left value.</param>
  517. /// <param name="right">The right value.</param>
  518. /// <returns>If left is greater than right <see langword="true"/>, otherwise <see langword="false"/>.</returns>
  519. public static bool operator >(SemVersion left, SemVersion right)
  520. {
  521. return Compare(left, right) > 0;
  522. }
  523. /// <summary>
  524. /// Compares two semantic versions.
  525. /// </summary>
  526. /// <param name="left">The left value.</param>
  527. /// <param name="right">The right value.</param>
  528. /// <returns>If left is greater than or equal to right <see langword="true"/>, otherwise <see langword="false"/>.</returns>
  529. public static bool operator >=(SemVersion left, SemVersion right)
  530. {
  531. return Equals(left, right) || Compare(left, right) > 0;
  532. }
  533. /// <summary>
  534. /// Compares two semantic versions.
  535. /// </summary>
  536. /// <param name="left">The left value.</param>
  537. /// <param name="right">The right value.</param>
  538. /// <returns>If left is less than right <see langword="true"/>, otherwise <see langword="false"/>.</returns>
  539. public static bool operator <(SemVersion left, SemVersion right)
  540. {
  541. return Compare(left, right) < 0;
  542. }
  543. /// <summary>
  544. /// Compares two semantic versions.
  545. /// </summary>
  546. /// <param name="left">The left value.</param>
  547. /// <param name="right">The right value.</param>
  548. /// <returns>If left is less than or equal to right <see langword="true"/>, otherwise <see langword="false"/>.</returns>
  549. public static bool operator <=(SemVersion left, SemVersion right)
  550. {
  551. return Equals(left, right) || Compare(left, right) < 0;
  552. }
  553. }
  554. }