1<?php
2
3/**
4 * This file is part of the Carbon package.
5 *
6 * (c) Brian Nesbitt <brian@nesbot.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12chdir(__DIR__);
13$currentBranch = 'master';
14if (preg_match('/On branch ([^\n]+)\n/', shell_exec('git status'), $match)) {
15 $currentBranch = $match[1];
16}
17shell_exec('git fetch --all --tags --prune');
18$remotes = explode("\n", trim(shell_exec('git remote')));
19$tagsCommand = \count($remotes)
20 ? 'git ls-remote --tags '.(\in_array('upstream', $remotes, true) ? 'upstream' : (\in_array('origin', $remotes, true) ? 'origin' : $remotes[0]))
21 : 'git tag';
22$tags = array_map(function ($ref) {
23 $ref = explode('refs/tags/', $ref);
24
25 return $ref[1] ?? $ref[0];
26}, array_filter(explode("\n", trim(shell_exec($tagsCommand))), function ($ref) {
27 return substr($ref, -3) !== '^{}';
28}));
29usort($tags, 'version_compare');
30
31$tag = isset($argv[1]) && !\in_array($argv[1], ['last', 'latest']) ? $argv[1] : end($tags);
32
33if (strtolower($tag) !== 'all') {
34 if (!\in_array($tag, $tags, true)) {
35 echo "Tag must be one of remote tags available:\n";
36 foreach ($tags as $_tag) {
37 echo " - $_tag\n";
38 }
39 echo "\"$tag\" does not match.\n";
40
41 exit(1);
42 }
43
44 $tags = [$tag];
45}
46
47foreach ($tags as $tag) {
48 $archive = "Carbon-$tag.zip";
49 if (isset($argv[2]) && $argv[2] === 'missing' && file_exists($archive)) {
50 continue;
51 }
52
53 $branch = "build-$tag";
54 shell_exec('git stash');
55 shell_exec("git branch -d $branch");
56 shell_exec("git checkout tags/$tag -b $branch");
57 $phpVersion = version_compare($tag, '2.0.0-dev', '<') ? '5.3.9' : '7.1.8';
58 shell_exec("composer config platform.php $phpVersion");
59 shell_exec('composer remove friendsofphp/php-cs-fixer --dev');
60 shell_exec('composer remove kylekatarnls/multi-tester --dev');
61 shell_exec('composer remove phpmd/phpmd --dev');
62 shell_exec('composer remove phpstan/phpstan --dev');
63 shell_exec('composer remove phpunit/phpunit --dev');
64 shell_exec('composer remove squizlabs/php_codesniffer --dev');
65 shell_exec('composer update --no-interaction --no-dev --optimize-autoloader');
66 $zip = new ZipArchive();
67
68 $zip->open($archive, ZipArchive::CREATE | ZipArchive::OVERWRITE);
69
70 foreach (['src', 'vendor', 'Carbon', 'lazy'] as $directory) {
71 if (is_dir($directory)) {
72 $directory = realpath($directory);
73 $base = \dirname($directory);
74
75 $files = new RecursiveIteratorIterator(
76 new RecursiveDirectoryIterator($directory),
77 RecursiveIteratorIterator::LEAVES_ONLY
78 );
79
80 foreach ($files as $name => $file) {
81 if (!$file->isDir()) {
82 $filePath = $file->getRealPath();
83
84 $zip->addFile($filePath, substr($filePath, \strlen($base) + 1));
85 }
86 }
87 }
88 }
89
90 $autoload = 'autoload.php';
91 file_put_contents($autoload, "<?php\n\n/**\n * @version $tag\n */\n\nrequire __DIR__.'/vendor/autoload.php';\n");
92 $zip->addFile($autoload, $autoload);
93 $zip->close();
94 unlink($autoload);
95
96 shell_exec('git checkout .');
97 shell_exec("git checkout $currentBranch");
98 shell_exec("git branch -d $branch");
99 shell_exec('git stash pop');
100 shell_exec('composer config platform.php 7.1.8');
101 shell_exec('composer update --no-interaction');
102}
103
104exit(0);
105