|
| 1 | +<?php |
| 2 | + |
| 3 | +class Release |
| 4 | +{ |
| 5 | + /** |
| 6 | + * Execute a shell command and throw exception if fails. |
| 7 | + */ |
| 8 | + private function exec(string $command): void |
| 9 | + { |
| 10 | + $return_var = null; |
| 11 | + $fullCommand = "$command 2>&1"; |
| 12 | + passthru($fullCommand, $return_var); |
| 13 | + if ($return_var) { |
| 14 | + throw new Exception('FAILED executing: ' . $command); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + private function checkPhpVersion(): void |
| 19 | + { |
| 20 | + $data = json_decode(file_get_contents('composer.json'), true); |
| 21 | + preg_match_all('~\d\.\d~', $data['require']['php'] ?? $data['require']['php-64bit'], $m); |
| 22 | + |
| 23 | + $expectedVersion = min($m[0]); |
| 24 | + $actualVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION; |
| 25 | + |
| 26 | + if ($actualVersion > $expectedVersion) { |
| 27 | + throw new Exception("Release must be created with PHP $expectedVersion (the oldest supported version), but you are running the newer PHP $actualVersion."); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private function getComposerCommand(): string |
| 32 | + { |
| 33 | + $php = PHP_BINARY; |
| 34 | + $composerPath = trim(`which composer`); |
| 35 | + |
| 36 | + return "$php $composerPath --ansi --no-interaction"; |
| 37 | + } |
| 38 | + |
| 39 | + public function create(): void |
| 40 | + { |
| 41 | + $this->checkPhpVersion(); |
| 42 | + $composer = $this->getComposerCommand(); |
| 43 | + $tempDir = sys_get_temp_dir() . '/dompdf-release'; |
| 44 | + |
| 45 | + $buildDirectory = dirname(__DIR__) . "/build"; |
| 46 | + if (!is_dir($buildDirectory)) { |
| 47 | + mkdir($buildDirectory); |
| 48 | + } |
| 49 | + |
| 50 | + $this->exec("rm -rf $tempDir"); |
| 51 | + mkdir($tempDir); |
| 52 | + chdir($tempDir); |
| 53 | + $this->exec("$composer init --type project"); |
| 54 | + $this->exec("$composer require --fixed dompdf/dompdf"); |
| 55 | + $this->exec("rm composer.json composer.lock"); |
| 56 | + $this->exec("cp vendor/dompdf/dompdf/*.md vendor/dompdf/dompdf/LICENSE.LGPL vendor/dompdf/dompdf/VERSION ."); |
| 57 | + |
| 58 | + file_put_contents($tempDir . '/autoload.inc.php', "<?php require (__DIR__ . '/vendor/autoload.php');"); |
| 59 | + |
| 60 | + // Create zip |
| 61 | + $version = trim(file_get_contents($tempDir . '/VERSION')); |
| 62 | + $destination = $buildDirectory . "/dompdf-$version.zip"; |
| 63 | + $this->zip($tempDir, $destination, 'dompdf'); |
| 64 | + |
| 65 | + $this->exec("rm -rf $tempDir"); |
| 66 | + |
| 67 | + echo "created $destination" . PHP_EOL; |
| 68 | + } |
| 69 | + |
| 70 | + private function zip(string $source, string $destination, string $mainDir): void |
| 71 | + { |
| 72 | + $zip = new ZipArchive(); |
| 73 | + $opened = $zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE); |
| 74 | + if (!$opened) { |
| 75 | + throw new Exception('Could not create zip file: ' . $opened); |
| 76 | + } |
| 77 | + |
| 78 | + $prefix = ''; |
| 79 | + if ($mainDir) { |
| 80 | + $zip->addEmptyDir($mainDir); |
| 81 | + $prefix = $mainDir . '/'; |
| 82 | + } |
| 83 | + |
| 84 | + $source = str_replace('\\', '/', realpath($source)); |
| 85 | + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST) as $file) { |
| 86 | + $file = str_replace('\\', '/', $file); |
| 87 | + |
| 88 | + // Ignore "." and ".." folders |
| 89 | + $basename = basename($file); |
| 90 | + if (in_array($basename, ['.', '..'])) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + $targetFile = str_replace($source . '/', $prefix, $file); |
| 95 | + if (is_dir($file)) { |
| 96 | + $zip->addEmptyDir($targetFile); |
| 97 | + } else { |
| 98 | + $zip->addFile($file, $targetFile); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + $ok = $zip->close(); |
| 103 | + if (!$ok) { |
| 104 | + throw new Exception('Failed to close zip file'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +$release = new Release(); |
| 111 | +$release->create(); |
| 112 | + |
0 commit comments