From 456cebb8b5442fa9eaa6c3e6ee2f8601f2ec6eb9 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikov <5785276@gmail.com> Date: Thu, 22 Aug 2024 00:04:11 +0300 Subject: [PATCH] TMP --- bin/pcsf-baseline | 2 +- composer.json | 1 + scripts/phar/create.php | 56 ++++++++++++++++++++++++----------------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/bin/pcsf-baseline b/bin/pcsf-baseline index 2ac7bb4..bc277a8 100644 --- a/bin/pcsf-baseline +++ b/bin/pcsf-baseline @@ -26,7 +26,7 @@ $autoloaderPath = (static function (): string { throw new RuntimeException('Cannot find autoloader'); })(); -require_once $autoloaderPath; +require $autoloaderPath; $projectPath = dirname($autoloaderPath, 2); diff --git a/composer.json b/composer.json index a96cc9e..db8b135 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "baseline" ], "scripts": { + "build": "php --define phar.readonly=0 scripts/phar/create.php", "cs-check": "@cs-fixer-check", "cs-fix": "@cs-fixer-fix", "cs-fixer-check": "vendor/bin/php-cs-fixer fix --dry-run", diff --git a/scripts/phar/create.php b/scripts/phar/create.php index 8984689..ec4003d 100644 --- a/scripts/phar/create.php +++ b/scripts/phar/create.php @@ -7,40 +7,50 @@ ini_set("phar.readonly", 0); -$pharFile = __DIR__ . '/../../pcsf-baseline.phar'; +$basedir = dirname(__DIR__, 2); +require_once $basedir . '/vendor/autoload.php'; + +$pharFile = $basedir . '/pcsf-baseline.phar'; // clean up if (file_exists($pharFile)) { unlink($pharFile); } -/** - * @return iterable<\SplFileInfo> - */ -function getFiles(): iterable -{ - return (new Finder()) - ->files() - ->in([ - __DIR__ . '/../../src', - __DIR__ . '/../../vendor', - ]) - ->append([ - __DIR__ . '/../../bin/pcsf-baseline', - __DIR__ . '/../../LICENSE', - __DIR__ . '/../../README.md', - ]); -} +$finder = (new Finder()) + ->files() + ->in([ + $basedir . '/bin', + $basedir . '/src', + $basedir . '/vendor', + ]) + ->append([ + $basedir . '/bin/pcsf-baseline', + $basedir . '/LICENSE', + $basedir . '/README.md', + ]) + ->filter(static function (SplFileInfo $file): bool { + return !preg_match('~/tests?/~i', $file->getRealPath()); + }); // create phar $phar = new Phar($pharFile); -// creating our library using whole directory -foreach (getFiles() as $file) { - $phar->addFile($file->getRealPath()); +// start buffering. Mandatory to modify stub to add shebang +$phar->startBuffering(); + +//$phar->buildFromIterator($finder, $dirname); +$basedirLen = strlen($basedir); +foreach ($finder as $file) { + $realPath = $file->getRealPath(); + $phar->addFile($realPath, substr($realPath, $basedirLen)); } -// pointing main file which requires all classes $phar->setDefaultStub('bin/pcsf-baseline'); -echo "$pharFile successfully created"; +$phar->stopBuffering(); + +# Make the file executable +chmod($pharFile, 0770); + +echo "$pharFile successfully created\n";