build.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. chdir(__DIR__);
  3. $currentBranch = 'master';
  4. if (preg_match('/On branch ([^\n]+)\n/', shell_exec('git status'), $match)) {
  5. $currentBranch = $match[1];
  6. }
  7. shell_exec('git fetch --all --tags --prune');
  8. $remotes = explode("\n", trim(shell_exec('git remote')));
  9. $tagsCommand = count($remotes)
  10. ? 'git ls-remote --tags '.(in_array('upstream', $remotes) ? 'upstream' : (in_array('origin', $remotes) ? 'origin' : $remotes[0]))
  11. : 'git tag';
  12. $tags = array_map(function ($ref) {
  13. $ref = explode('refs/tags/', $ref);
  14. return isset($ref[1]) ? $ref[1] : $ref[0];
  15. }, array_filter(explode("\n", trim(shell_exec($tagsCommand))), function ($ref) {
  16. return substr($ref, -3) !== '^{}';
  17. }));
  18. usort($tags, 'version_compare');
  19. $tag = isset($argv[1]) && !in_array($argv[1], array('last', 'latest')) ? $argv[1] : end($tags);
  20. if (strtolower($tag) !== 'all') {
  21. if (!in_array($tag, $tags)) {
  22. echo "Tag must be one of remote tags available:\n";
  23. foreach ($tags as $_tag) {
  24. echo " - $_tag\n";
  25. }
  26. echo "\"$tag\" does not match.\n";
  27. exit(1);
  28. }
  29. $tags = array($tag);
  30. }
  31. foreach ($tags as $tag) {
  32. $archive = "Carbon-$tag.zip";
  33. if (isset($argv[2]) && $argv[2] === 'missing' && file_exists($archive)) {
  34. continue;
  35. }
  36. $branch = "build-$tag";
  37. shell_exec('git stash');
  38. shell_exec("git branch -d $branch");
  39. shell_exec("git checkout tags/$tag -b $branch");
  40. shell_exec('composer config platform.php 5.3.9');
  41. shell_exec('composer update --no-interaction --no-dev --optimize-autoloader');
  42. $zip = new ZipArchive();
  43. $zip->open($archive, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  44. foreach (array('src', 'vendor', 'Carbon') as $directory) {
  45. if (is_dir($directory)) {
  46. $directory = realpath($directory);
  47. $base = dirname($directory);
  48. $files = new RecursiveIteratorIterator(
  49. new RecursiveDirectoryIterator($directory),
  50. RecursiveIteratorIterator::LEAVES_ONLY
  51. );
  52. foreach ($files as $name => $file) {
  53. if (!$file->isDir()) {
  54. $filePath = $file->getRealPath();
  55. $zip->addFile($filePath, substr($filePath, strlen($base) + 1));
  56. }
  57. }
  58. }
  59. }
  60. $autoload = 'autoload.php';
  61. file_put_contents($autoload, "<?php\n\n/**\n * @version $tag\n */\n\nrequire __DIR__.'/vendor/autoload.php';\n");
  62. $zip->addFile($autoload, $autoload);
  63. $zip->close();
  64. unlink($autoload);
  65. shell_exec('git checkout .');
  66. shell_exec("git checkout $currentBranch");
  67. shell_exec("git branch -d $branch");
  68. shell_exec('git stash pop');
  69. shell_exec('composer update --no-interaction');
  70. }
  71. exit(0);