|
| 1 | +<?php |
| 2 | + |
| 3 | +// Catch missing write rights |
| 4 | +if (ini_get('phar.readonly') == 1) { |
| 5 | + echo 'Run »php --define phar.readonly=0 build-phar.php« to build the phar' . PHP_EOL; |
| 6 | + exit(1); |
| 7 | +} |
| 8 | + |
| 9 | +$exclude = [ |
| 10 | + '.claude', |
| 11 | + '.editorconfig', |
| 12 | + '.env', |
| 13 | + '.env.example', |
| 14 | + '.git', |
| 15 | + '.gitattributes', |
| 16 | + '.gitignore', |
| 17 | + '.gitlab-ci.example.yml', |
| 18 | + '.gitlab-ci.yml', |
| 19 | + '.idea', |
| 20 | + '.notes', |
| 21 | + '.php-version', |
| 22 | + '.php_cs.cache', |
| 23 | + 'CLAUDE.md', |
| 24 | + 'CONTRIBUTING.md', |
| 25 | + 'build', |
| 26 | + 'build-phar.php', |
| 27 | + 'composer.phar', |
| 28 | + 'docs', |
| 29 | + 'patches', |
| 30 | + 'phpacker.json', |
| 31 | + 'repositories.json', |
| 32 | + 'tests', |
| 33 | +]; |
| 34 | + |
| 35 | +$baseDir = realpath(__DIR__); |
| 36 | +$filter = function ($file, $key, $iterator) use ($exclude, $baseDir) { |
| 37 | + // Only check exclude list for root-level entries |
| 38 | + if (realpath($file->getPath()) === $baseDir && in_array($file->getFilename(), $exclude)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + return $iterator->hasChildren() || $file->isFile() || $file->isLink(); |
| 42 | +}; |
| 43 | + |
| 44 | +$iterator = new RecursiveIteratorIterator( |
| 45 | + new RecursiveCallbackFilterIterator( |
| 46 | + new RecursiveDirectoryIterator(__DIR__, RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::FOLLOW_SYMLINKS), |
| 47 | + $filter |
| 48 | + ) |
| 49 | +); |
| 50 | + |
| 51 | +// Inject version from Git tag |
| 52 | +exec('git describe --tags --dirty --always', $gitVersion); |
| 53 | +$version = trim($gitVersion[0] ?? 'dev'); |
| 54 | +file_put_contents(__DIR__ . '/.version', $version); |
| 55 | +echo 'Building version: ' . $version . PHP_EOL; |
| 56 | + |
| 57 | +// Create entry script to avoid shebang duplicates |
| 58 | +$file = file(__DIR__ . '/bin/patchbot'); |
| 59 | +unset($file[0]); |
| 60 | +file_put_contents(__DIR__ . '/bin/patchbot.php', $file); |
| 61 | + |
| 62 | +// Create phar |
| 63 | +$phar = new \Phar('patchbot.phar'); |
| 64 | +$phar->setSignatureAlgorithm(\Phar::SHA1); |
| 65 | +$phar->startBuffering(); |
| 66 | +$phar->buildFromIterator($iterator, __DIR__); |
| 67 | +//default executable |
| 68 | +$phar->setStub( |
| 69 | + '#!/usr/bin/env php ' . PHP_EOL . $phar->createDefaultStub('bin/patchbot.php') |
| 70 | +); |
| 71 | +$phar->stopBuffering(); |
| 72 | + |
| 73 | +// Make phar executable |
| 74 | +chmod(__DIR__ . '/patchbot.phar', 0770); |
| 75 | + |
| 76 | +// Remove generated entry script and version file |
| 77 | +unlink(__DIR__ . '/bin/patchbot.php'); |
| 78 | +unlink(__DIR__ . '/.version'); |
| 79 | + |
| 80 | +echo 'Done'; |
0 commit comments