|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * @file |
| 6 | + * Compiles DCG into a PHAR file. |
| 7 | + */ |
| 8 | + |
| 9 | +set_error_handler(function ($errno, $errstr, $errfile, $errline): void { |
| 10 | + fprintf(STDERR, "Error: %s on %s: %s\n", $errstr, $errfile, $errline); |
| 11 | + exit(1); |
| 12 | +}); |
| 13 | + |
| 14 | +$name = 'dcg.phar'; |
| 15 | +$phar = new \Phar($name); |
| 16 | +$phar->startBuffering(); |
| 17 | + |
| 18 | +$stub = <<< EOF |
| 19 | +#!/usr/bin/env php |
| 20 | +<?php |
| 21 | +
|
| 22 | +Phar::mapPhar('$name'); |
| 23 | +require 'phar://$name/bin/dcg'; |
| 24 | +__HALT_COMPILER(); |
| 25 | +EOF; |
| 26 | + |
| 27 | +$phar->setStub($stub); |
| 28 | + |
| 29 | +$files = array_merge( |
| 30 | + ['bin/dcg', 'LICENSE.txt'], |
| 31 | + dcg_scan_dir('src'), |
| 32 | + dcg_scan_dir('templates'), |
| 33 | + dcg_scan_dir('resources'), |
| 34 | + dcg_scan_dir('vendor', 'php'), |
| 35 | +); |
| 36 | +foreach ($files as $file) { |
| 37 | + $extension = pathinfo($file, PATHINFO_EXTENSION); |
| 38 | + $content = $extension == 'php' ? |
| 39 | + php_strip_whitespace($file) : file_get_contents($file); |
| 40 | + if ($file == 'bin/dcg') { |
| 41 | + $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content); |
| 42 | + } |
| 43 | + $phar->addFromString($file, $content); |
| 44 | + printf("Added file: %s\n", $file); |
| 45 | +} |
| 46 | + |
| 47 | +$meta_data = [ |
| 48 | + 'build_time' => date('c'), |
| 49 | + 'total_files' => count($files), |
| 50 | + 'php_version' => PHP_VERSION, |
| 51 | +]; |
| 52 | +$phar->setMetadata($meta_data); |
| 53 | + |
| 54 | +$phar->stopBuffering(); |
| 55 | +print "---------------------------------\n"; |
| 56 | +printf("Total added: %s\n" , count($files)); |
| 57 | +printf("PHAR file: %s\n", $phar->getPath()); |
| 58 | + |
| 59 | +/** |
| 60 | + * Recursively scans directory. |
| 61 | + */ |
| 62 | +function dcg_scan_dir(string $path, string $extension = NULL): array { |
| 63 | + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
| 64 | + $files = []; |
| 65 | + foreach ($iterator as $file) { |
| 66 | + if ($file->isDir() || ($extension && $extension != $file->getExtension())) { |
| 67 | + fprintf(STDERR, "\033[33mSkipped %s\033[0m\n", $file->getPathName()); |
| 68 | + continue; |
| 69 | + } |
| 70 | + $files[] = $file->getPathname(); |
| 71 | + } |
| 72 | + return $files; |
| 73 | +} |
0 commit comments