semver 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env php
  2. <?php
  3. namespace vierbergenlars\SemVer\Application\SemVer;
  4. if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
  5. require_once __DIR__ . '/../vendor/autoload.php';
  6. } else {
  7. if (file_exists(__DIR__ . '/../../../autoload.php')) {
  8. require_once __DIR__ . '/../../../autoload.php';
  9. }
  10. }
  11. use vierbergenlars\SemVer\version;
  12. use vierbergenlars\SemVer\expression;
  13. use vierbergenlars\SemVer\SemVerException;
  14. $version = false;
  15. $range = array();
  16. $increment = false;
  17. // Get all arguments
  18. while (\count($argv) > 0) {
  19. $arg = \array_shift($argv);
  20. switch ($arg) {
  21. case '-v':
  22. case '--version':
  23. $version[] = \array_shift($argv);
  24. break;
  25. case '-r':
  26. case '--range':
  27. $range[] = \array_shift($argv);
  28. break;
  29. case '-i':
  30. case '--increment':
  31. $increment = \array_shift($argv);
  32. break;
  33. case '-?':
  34. case '-h':
  35. case '--help':
  36. help();
  37. break;
  38. }
  39. }
  40. main();
  41. function help() {
  42. $e = array(
  43. 'Usage: semver -v|--version <version> [options]'
  44. , ''
  45. , ' -r <range> Test if version satisfies the supplied range.'
  46. , ' --range <range>'
  47. , ' -i [major|minor|patch|build] Increment the given version number.'
  48. , ' --increment [major|minor|patch|build]'
  49. , ''
  50. , 'Multiple versions or ranges may be supplied.'
  51. , ''
  52. , 'Program exits successfully if any valid version satisfies'
  53. , 'all supplied ranges, and prints all satisfying versions.'
  54. , ''
  55. , 'If no versions are valid, or ranges are not satisfied,'
  56. , 'then exits failure.'
  57. , ''
  58. , 'Versions are printed in ascending order, so supplying'
  59. , 'multiple versions to the utility will just sort them.'
  60. );
  61. echo \implode(PHP_EOL, $e);
  62. exit;
  63. }
  64. function main() {
  65. global $version, $range, $increment;
  66. if ($increment !== false) {
  67. increment($version[0], $increment);
  68. }
  69. if ($version !== false) {
  70. filter($version, $range);
  71. }
  72. help();
  73. }
  74. function fail($message = '') {
  75. fwrite(STDERR, $message);
  76. exit(1);
  77. }
  78. function increment($version, $what) {
  79. if (\file_exists($version))
  80. $version = \file_get_contents($version);
  81. if ($version == '-')
  82. $version = \fgets(STDIN);
  83. try {
  84. $v = new version($version);
  85. echo $v->inc($what);
  86. exit;
  87. } catch (SemVerException $e) {
  88. fail($e->getMessage());
  89. }
  90. }
  91. function filter($versions, $ranges) {
  92. if (\file_exists($versions[0]))
  93. $versions = \file($versions[0], \FILE_IGNORE_NEW_LINES | \FILE_SKIP_EMPTY_LINES);
  94. if ($versions[0] == '-') {
  95. unset($versions[0]);
  96. while ($version = \fgets(\STDIN)) {
  97. $versions[] = $version;
  98. }
  99. }
  100. $matching_versions = array();
  101. foreach ($versions as $version) {
  102. $ok = true;
  103. try {
  104. $v = new version($version);
  105. foreach ($ranges as $range) {
  106. if ($v->satisfies(new expression($range)))
  107. continue;
  108. $ok = false;
  109. break;
  110. }
  111. if ($ok)
  112. $matching_versions[] = $v;
  113. } catch (SemVerException $e) {
  114. }
  115. }
  116. \usort($matching_versions, '\\vierbergenlars\\SemVer\\version::compare');
  117. foreach ($matching_versions as $version) {
  118. echo $version . PHP_EOL;
  119. }
  120. if (\count($matching_versions) == 0)
  121. fail();
  122. exit;
  123. }