update_cucumber 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env php
  2. <?php
  3. if (PHP_VERSION_ID < 70400) {
  4. echo "Updater requires PHP 7.4";
  5. exit(1);
  6. }
  7. $composerFile = __DIR__ . '/../composer.json';
  8. $composerConfig = file_get_contents($composerFile);
  9. foreach (json_decode($composerConfig, true, 512, JSON_THROW_ON_ERROR)['repositories'] as $repository)
  10. {
  11. if ($repository['type'] !== 'package') {
  12. continue;
  13. }
  14. if ($repository['package']['name'] == 'cucumber/cucumber') {
  15. $oldTag = preg_replace('/^dev-gherkin-/', '', $repository['package']['version']);
  16. $oldHash = $repository['package']['source']['reference'];
  17. break;
  18. }
  19. }
  20. if (!isset($oldHash)) {
  21. echo "ERROR: Could not parse the composer configuration\n";
  22. exit(1);
  23. }
  24. echo "Latest local hash is {$oldHash} (tagged {$oldTag})\n";
  25. if(!preg_match(
  26. '/^(?<hash>[0-9a-z]+)\s+\S+\\/v(?<tag>[0-9.]+)/',
  27. shell_exec('git ls-remote --tags https://github.com/cucumber/cucumber.git | grep cucumber-gherkin | sort --version-sort -k2 | tail -n 1'),
  28. $matches
  29. )) {
  30. echo "ERROR: Could not parse the repository tags\n";
  31. exit(1);
  32. }
  33. ['hash' => $newHash, 'tag' => $newTag] = $matches;
  34. echo "Latest remote hash is {$newHash} (tagged {$newTag})\n";
  35. if ($matches['hash'] == $oldHash) {
  36. echo "Hashes match, nothing to do\n";
  37. exit(0);
  38. }
  39. $newJson = str_replace(
  40. [$oldHash, 'dev-gherkin-'.$oldTag],
  41. [$newHash, 'dev-gherkin-'.$newTag],
  42. $composerConfig
  43. );
  44. file_put_contents($composerFile, $newJson);
  45. echo "Updated composer config:\n$newJson";
  46. echo "::set-output name=cucumber_version::$newTag\n";