update-versions 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env php
  2. <?php
  3. namespace vierbergenlars\SemVer\Application\UpdateVersions;
  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. //Defaults
  15. /**
  16. * Input file containing the version
  17. * @var string
  18. */
  19. $input = 'composer.json';
  20. /**
  21. * Array of paths to write to
  22. * @var array
  23. */
  24. $writeto = array();
  25. /**
  26. * Root directory
  27. * @var string
  28. */
  29. $root = '.';
  30. /**
  31. * Do not do anything
  32. * @var bool
  33. */
  34. $dry_run = false;
  35. /**
  36. * Shell command to execute for each updated file
  37. * @var string
  38. */
  39. $shell = NULL;
  40. // Get all arguments
  41. while (\count($argv) > 0) {
  42. $arg = \array_shift($argv);
  43. switch ($arg) {
  44. case '-p':
  45. case '--package':
  46. $input = \array_shift($argv);
  47. break;
  48. case '-s':
  49. case '--source':
  50. $writeto[] = \array_shift($argv);
  51. break;
  52. case '-b':
  53. case '--base':
  54. $root = \array_shift($argv);
  55. break;
  56. case '--dry-run':
  57. $dry_run = true;
  58. break;
  59. case '--shell':
  60. $shell = \array_shift($argv);
  61. break;
  62. case '-h':
  63. case '--help':
  64. help();
  65. }
  66. }
  67. //Defaults writeto
  68. if ($writeto === array()) {
  69. $writeto = array('src', 'bin');
  70. }
  71. //Add root paths
  72. $input = $root . '/' . $input;
  73. foreach ($writeto as &$write) {
  74. $write = $root . '/' . $write;
  75. }
  76. //Read those JSON files
  77. if (!\file_exists($input))
  78. fail('Package file does not exist');
  79. $input = \json_decode(\file_get_contents($input), true);
  80. if (!$input) {
  81. fail('Invalid JSON file!');
  82. }
  83. //Initialize the version from package file
  84. try {
  85. $version = new version($input['version']);
  86. } catch (SemVerException $e) {
  87. fail($e->getMessage());
  88. }
  89. $version = $version->getString();
  90. foreach ($writeto as $output) {
  91. $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($output));
  92. foreach ($dir as $file) {
  93. if (\preg_match('/[\\\\\\/]\\./', $file))
  94. continue; //Ignore . directories
  95. $contents1 = \file_get_contents($file);
  96. $contents2 = \str_replace(array('2.0.0--', '{{{' . 'version}}}'), $version, $contents1);
  97. if ($contents1 != $contents2) {
  98. fwrite(\STDOUT, 'Writing version information to file ' . $file . \PHP_EOL);
  99. if ($shell !== null) {
  100. \system($shell . ' "' . $file . '"', $exit_code);
  101. if ($exit_code != 0)
  102. fail('Subshell exited ' . $exit_code);
  103. }
  104. if ($dry_run) {
  105. \fwrite(\STDOUT, '\\_Not writing to disk' . \PHP_EOL);
  106. } else {
  107. \file_put_contents($file, $contents2);
  108. }
  109. }
  110. }
  111. }
  112. function help() {
  113. $e = array(
  114. 'Usage: update-versions [options]'
  115. , ''
  116. , ' -p <composerfile> Use this file as composer.json file'
  117. , ' --package <composerfile>'
  118. , ' -s <dir> Add directory to sources to scan. May be repeated.'
  119. , ' --source <dir>'
  120. , ' -b <dir> Use this directory as base directory.'
  121. , ' --base <dir>'
  122. , ' --shell <command> Execute <command> for each changed file.'
  123. , ' --dry-run Do not write files.'
  124. , ''
  125. , 'This program exits 0 on success or 1 on failure.'
  126. , 'Defaults to "--package composer.json --source src --source bin --base ."'
  127. );
  128. echo \implode(PHP_EOL, $e);
  129. exit;
  130. }
  131. function fail($message = '') {
  132. \fwrite(\STDERR, $message . \PHP_EOL);
  133. exit(1);
  134. }