SemVer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using UnityEngine;
  3. namespace Artees.UnitySemVer
  4. {
  5. /// <summary>
  6. /// A semantic version based on the <a href="https://semver.org/">Semantic Versioning 2.0.0</a> specification.
  7. /// </summary>
  8. [Serializable]
  9. public class SemVer : IComparable<SemVer>, IEquatable<SemVer>
  10. {
  11. public const char IdentifiersSeparator = '.';
  12. public const char PreReleasePrefix = '-';
  13. public const char BuildPrefix = '+';
  14. public static bool operator ==(SemVer left, SemVer right)
  15. {
  16. return Equals(left, right);
  17. }
  18. public static bool operator !=(SemVer left, SemVer right)
  19. {
  20. return !Equals(left, right);
  21. }
  22. public static bool operator >(SemVer left, SemVer right)
  23. {
  24. return left.CompareTo(right) > 0;
  25. }
  26. public static bool operator <(SemVer left, SemVer right)
  27. {
  28. return left.CompareTo(right) < 0;
  29. }
  30. public static bool operator >=(SemVer left, SemVer right)
  31. {
  32. return left.CompareTo(right) >= 0;
  33. }
  34. public static bool operator <=(SemVer left, SemVer right)
  35. {
  36. return left.CompareTo(right) <= 0;
  37. }
  38. public static implicit operator string(SemVer s)
  39. {
  40. return s.ToString();
  41. }
  42. public static implicit operator SemVer(string s)
  43. {
  44. return Parse(s);
  45. }
  46. public static SemVer Parse(string semVer)
  47. {
  48. return SemVerConverter.FromString(semVer);
  49. }
  50. /// <summary>
  51. /// Major version X (X.y.z | X > 0) MUST be incremented if any backwards incompatible changes are introduced
  52. /// to the public API. It MAY include minor and patch level changes. Patch and minor version MUST be reset to 0
  53. /// when major version is incremented.
  54. /// <seealso cref="IncrementMajor"/>
  55. /// </summary>
  56. public uint major;
  57. /// <summary>
  58. /// Minor version Y (x.Y.z | x > 0) MUST be incremented if new, backwards compatible functionality is
  59. /// introduced to the public API. It MUST be incremented if any public API functionality is marked as
  60. /// deprecated. It MAY be incremented if substantial new functionality or improvements are introduced within
  61. /// the private code. It MAY include patch level changes. Patch version MUST be reset to 0 when minor version
  62. /// is incremented.
  63. /// <seealso cref="IncrementMinor"/>
  64. /// </summary>
  65. public uint minor;
  66. /// <summary>
  67. /// Patch version Z (x.y.Z | x > 0) MUST be incremented if only backwards compatible bug fixes are introduced.
  68. /// </summary>
  69. public uint patch;
  70. /// <summary>
  71. /// A pre-release version indicates that the version is unstable and might not satisfy the intended
  72. /// compatibility requirements as denoted by its associated normal version.
  73. /// </summary>
  74. /// <example>1.0.0-<b>alpha</b>, 1.0.0-<b>alpha.1</b>, 1.0.0-<b>0.3.7</b>, 1.0.0-<b>x.7.z.92</b></example>
  75. public string preRelease;
  76. /// <summary>
  77. /// Set the <see cref="Build">build</see> metadata automatically
  78. /// </summary>
  79. /// <seealso cref="Build"/>
  80. public SemVerAutoBuild.Type autoBuild;
  81. [SerializeField] private string build = string.Empty;
  82. /// <summary>
  83. /// Build metadata MUST be ignored when determining version precedence. Thus two versions that differ only in
  84. /// the build metadata, have the same precedence.
  85. /// </summary>
  86. /// <example>1.0.0-alpha+<b>001</b>, 1.0.0+<b>20130313144700</b>, 1.0.0-beta+<b>exp.sha.5114f85</b></example>
  87. public string Build
  88. {
  89. get => SemVerAutoBuild.Instances[autoBuild].Get(build);
  90. set => build = SemVerAutoBuild.Instances[autoBuild].Set(value);
  91. }
  92. /// <summary>
  93. /// The base part of the version number (Major.Minor.Patch).
  94. /// </summary>
  95. /// <example>1.9.0</example>
  96. /// <returns>Major.Minor.Patch</returns>
  97. public string Core => $"{major}.{minor}.{patch}";
  98. /// <summary>
  99. /// An internal version number. This number is used only to determine whether one version is more recent than
  100. /// another, with higher numbers indicating more recent versions.
  101. /// <a href="https://developer.android.com/studio/publish/versioning"/>
  102. /// </summary>
  103. /// <returns><c>Major * 10000 + Minor * 100 + Patch</c></returns>
  104. public int AndroidBundleVersionCode
  105. {
  106. get
  107. {
  108. var clampedPatch = ClampAndroidBundleVersionCode(patch, "Patch");
  109. var clampedMinor = ClampAndroidBundleVersionCode(minor, "Minor");
  110. return (int) (major * 10000 + clampedMinor * 100 + clampedPatch);
  111. }
  112. }
  113. private static uint ClampAndroidBundleVersionCode(uint value, string name)
  114. {
  115. uint clamped;
  116. const uint max = 100;
  117. if (value >= max)
  118. {
  119. clamped = max - 1;
  120. Debug.LogWarning(name + " should be less than " + max);
  121. }
  122. else
  123. {
  124. clamped = value;
  125. }
  126. return clamped;
  127. }
  128. public SemVer()
  129. {
  130. minor = 1;
  131. preRelease = string.Empty;
  132. autoBuild = SemVerAutoBuild.Type.Manual;
  133. }
  134. /// <summary>
  135. /// Increment the major version, reset the patch and the minor version to 0.
  136. /// </summary>
  137. public void IncrementMajor()
  138. {
  139. major++;
  140. minor = patch = 0;
  141. }
  142. /// <summary>
  143. /// Increment the minor version, reset the patch version to 0.
  144. /// </summary>
  145. public void IncrementMinor()
  146. {
  147. minor++;
  148. patch = 0;
  149. }
  150. /// <summary>
  151. /// Increment the patch version.
  152. /// </summary>
  153. public void IncrementPatch()
  154. {
  155. patch++;
  156. }
  157. /// <summary>
  158. /// Check if this semantic version meets the <a href="https://semver.org/">Semantic Versioning 2.0.0</a>
  159. /// specification.
  160. /// </summary>
  161. /// <returns>The result of validation and automatically corrected version number.</returns>
  162. public SemVerValidationResult Validate()
  163. {
  164. return new SemVerValidator().Validate(this);
  165. }
  166. /// <summary>
  167. /// Creates a copy of this semantic version.
  168. /// </summary>
  169. public SemVer Clone()
  170. {
  171. return new SemVer
  172. {
  173. major = major,
  174. minor = minor,
  175. patch = patch,
  176. preRelease = preRelease,
  177. Build = Build,
  178. autoBuild = autoBuild,
  179. };
  180. }
  181. public int CompareTo(SemVer other)
  182. {
  183. return new SemVerComparer().Compare(this, other);
  184. }
  185. public bool Equals(SemVer other)
  186. {
  187. if (ReferenceEquals(null, other)) return false;
  188. if (ReferenceEquals(this, other)) return true;
  189. return CompareTo(other) == 0;
  190. }
  191. public override bool Equals(object obj)
  192. {
  193. if (ReferenceEquals(null, obj)) return false;
  194. if (ReferenceEquals(this, obj)) return true;
  195. return obj.GetType() == GetType() && Equals((SemVer) obj);
  196. }
  197. public override int GetHashCode()
  198. {
  199. throw new NotImplementedException();
  200. }
  201. public override string ToString()
  202. {
  203. return SemVerConverter.ToString(this);
  204. }
  205. }
  206. }