The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

238 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. BlocksVersion::BlocksVersion (const String& versionString)
  20. {
  21. evaluate (versionString);
  22. }
  23. String BlocksVersion::toString (bool extended) const
  24. {
  25. String output = String (major) + "." + String (minor) + "." + String (patch);
  26. if (extended)
  27. {
  28. if (releaseType.isNotEmpty())
  29. output += "-" + releaseType + "-" + String (releaseCount);
  30. if (commit.isNotEmpty())
  31. output += "-" + commit;
  32. if (forced)
  33. output += "-f";
  34. }
  35. return output;
  36. }
  37. static std::regex getRegEx()
  38. {
  39. static const std::string majorMinorPatchRegex { "([0-9]+)\\.([0-9]+)\\.([0-9]+)" };
  40. static const std::string releaseAndCommitDetailsRegex { "(?:-(alpha|beta|rc))?(?:-([0-9]+))?(?:-g([A-Za-z0-9]+))?" };
  41. static const std::string forcedUpdateRegex { "(-f)?" };
  42. static const std::regex regEx ("(?:.+)?" + majorMinorPatchRegex + releaseAndCommitDetailsRegex + forcedUpdateRegex + "(?:.+)?");
  43. return regEx;
  44. }
  45. bool BlocksVersion::isValidVersion (const String& versionString)
  46. {
  47. return std::regex_match (versionString.toRawUTF8(), getRegEx());
  48. }
  49. bool BlocksVersion::evaluate (const String& versionString)
  50. {
  51. std::cmatch groups;
  52. const bool result = std::regex_match (versionString.toRawUTF8(), groups, getRegEx());
  53. jassert (result);
  54. auto toInt = [] (const std::sub_match<const char*> match)
  55. {
  56. return std::atoi (match.str().c_str());
  57. };
  58. enum tags { FULL, MAJOR, MINOR, PATCH, RELEASE, COUNT, COMMIT, FORCED};
  59. major = toInt (groups[MAJOR]);
  60. minor = toInt (groups[MINOR]);
  61. patch = toInt (groups[PATCH]);
  62. releaseType = String (groups[RELEASE]);
  63. releaseCount = toInt (groups[COUNT]);
  64. commit = String (groups[COMMIT]);
  65. forced = groups[FORCED].matched;
  66. return result;
  67. }
  68. bool BlocksVersion::isEqualTo (const BlocksVersion& other) const
  69. {
  70. return major == other.major &&
  71. minor == other.minor &&
  72. patch == other.patch &&
  73. releaseType == other.releaseType &&
  74. releaseCount == other.releaseCount;
  75. }
  76. bool BlocksVersion::isGreaterThan (const BlocksVersion& other) const
  77. {
  78. if (major != other.major) return (major > other.major);
  79. if (minor != other.minor) return (minor > other.minor);
  80. if (patch != other.patch) return (patch > other.patch);
  81. return releaseTypeGreaterThan (other);
  82. }
  83. bool BlocksVersion::releaseTypeGreaterThan (const BlocksVersion& other) const
  84. {
  85. auto getReleaseTypePriority = [] (const BlocksVersion& version)
  86. {
  87. String releaseTypes[4] = { "alpha", "beta", "rc", {} };
  88. for (int i = 0; i < 4; ++i)
  89. if (version.releaseType == releaseTypes[i])
  90. return i;
  91. return -1;
  92. };
  93. if (releaseType != other.releaseType)
  94. return getReleaseTypePriority (*this) > getReleaseTypePriority (other);
  95. return releaseCount > other.releaseCount;
  96. }
  97. bool BlocksVersion::operator== (const BlocksVersion& other) const
  98. {
  99. return isEqualTo (other);
  100. }
  101. bool BlocksVersion::operator!=(const BlocksVersion& other) const
  102. {
  103. return ! (*this == other);
  104. }
  105. bool BlocksVersion::operator> (const BlocksVersion& other) const
  106. {
  107. return isGreaterThan (other);
  108. }
  109. bool BlocksVersion::operator< (const BlocksVersion& other) const
  110. {
  111. return ! (*this > other) && (*this != other);
  112. }
  113. bool BlocksVersion::operator<= (const BlocksVersion& other) const
  114. {
  115. return (*this < other) || (*this == other);
  116. }
  117. bool BlocksVersion::operator>= (const BlocksVersion& other) const
  118. {
  119. return (*this > other) || (*this == other);
  120. }
  121. //==============================================================================
  122. //==============================================================================
  123. #if JUCE_UNIT_TESTS
  124. class BlocksVersionUnitTests : public UnitTest
  125. {
  126. public:
  127. BlocksVersionUnitTests()
  128. : UnitTest ("BlocksVersionUnitTests", UnitTestCategories::blocks)
  129. {}
  130. void runTest() override
  131. {
  132. beginTest ("Compare patch number");
  133. expect (BlocksVersion ("4.6.7") < BlocksVersion ("4.6.11"));
  134. expect (BlocksVersion ("4.6.6") > BlocksVersion ("4.6.2"));
  135. expect (BlocksVersion ("4.6.5") <= BlocksVersion ("4.6.8"));
  136. expect (BlocksVersion ("4.6.4") >= BlocksVersion ("4.6.3"));
  137. beginTest ("Compare minor number");
  138. expect (BlocksVersion ("4.5.9") < BlocksVersion ("4.6.7"));
  139. expect (BlocksVersion ("4.15.2") > BlocksVersion ("4.6.6"));
  140. expect (BlocksVersion ("4.4.8") <= BlocksVersion ("4.6.5"));
  141. expect (BlocksVersion ("4.7.4") >= BlocksVersion ("4.6.3"));
  142. beginTest ("Compare major number");
  143. expect (BlocksVersion ("4.6.9") < BlocksVersion ("8.5.7"));
  144. expect (BlocksVersion ("15.6.2") > BlocksVersion ("4.9.6"));
  145. expect (BlocksVersion ("4.6.8") <= BlocksVersion ("7.4.5"));
  146. expect (BlocksVersion ("5.6.4") >= BlocksVersion ("4.7.3"));
  147. beginTest ("Compare build number");
  148. expect (BlocksVersion ("0.3.2-alpha-3-gjduh") < BlocksVersion ("0.3.2-alpha-12-gjduh"));
  149. expect (BlocksVersion ("0.3.2-alpha-4-gjduh") > BlocksVersion ("0.3.2-alpha-1-gjduh"));
  150. expect (BlocksVersion ("0.3.2-beta-5-gjduh") <= BlocksVersion ("0.3.2-beta-6-gjduh"));
  151. expect (BlocksVersion ("0.3.2-beta-6-gjduh") >= BlocksVersion ("0.3.2-beta-3-gjduh"));
  152. beginTest ("Compare build type");
  153. expect (BlocksVersion ("0.3.2-alpha-3-gjduhenf") < BlocksVersion ("0.3.2-beta-1-gjduhenf"));
  154. expect (BlocksVersion ("0.3.2-beta-3-gjduhenf") < BlocksVersion ("0.3.2"));
  155. expect (BlocksVersion ("0.3.2") > BlocksVersion ("0.3.2-alpha-3-gjduhenf"));
  156. beginTest ("Compare equal numbers");
  157. expect (BlocksVersion ("4.6.7") == BlocksVersion ("4.6.7"));
  158. expect (BlocksVersion ("4.6.7-alpha-3-gsdfsf") == BlocksVersion ("4.6.7-alpha-3-gsdfsf"));
  159. beginTest ("Identify forced version");
  160. expect (BlocksVersion("0.2.2-2-g25eaec8a-f").forced);
  161. expect (BlocksVersion("0.2.2-2-f").forced);
  162. expect (! BlocksVersion("0.2.2-2-g25eaec8-d7").forced);
  163. beginTest ("Valid Strings");
  164. expect (BlocksVersion::isValidVersion ("Rainbow 0.4.5-beta-1-g4c36e"));
  165. expect (! BlocksVersion::isValidVersion ("0.4-beta-1-g4c36e"));
  166. expect (! BlocksVersion::isValidVersion ("a.0.4-beta-1-g4c36e"));
  167. expect (BlocksVersion::isValidVersion ("BLOCKS control 0.2.2-2-g25eaec8a-f.syx"));
  168. expect (BlocksVersion("BLOCKS control 0.2.2-2-g25eaec8a-f.syx") == BlocksVersion("0.2.2-2-g25eaec8a-f"));
  169. beginTest ("Default constructors");
  170. {
  171. BlocksVersion v1 ("4.5.9");
  172. BlocksVersion v2 (v1);
  173. BlocksVersion v3;
  174. v3 = v1;
  175. expect (v2 == v1);
  176. expect (v3 == v1);
  177. BlocksVersion emptyVersion;
  178. expect (emptyVersion == BlocksVersion ("0.0.0"));
  179. }
  180. }
  181. };
  182. static BlocksVersionUnitTests BlocksVersionUnitTests;
  183. #endif
  184. } // namespace juce