diff --git a/.gitattributes b/.gitattributes index dfbf618..91ebcd4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,6 +12,7 @@ /composer.lock export-ignore /docker-compose.yml export-ignore /docker-entrypoint.sh export-ignore +/pcsf-baseline.phar export-ignore /phpstan.neon.dist export-ignore /phpstan-baseline.neon export-ignore /phpunit.xml export-ignore diff --git a/bin/pcsf-baseline b/bin/pcsf-baseline index bc277a8..c8f6633 100644 --- a/bin/pcsf-baseline +++ b/bin/pcsf-baseline @@ -5,6 +5,10 @@ use Aeliot\PhpCsFixerBaseline\Service\Builder; use Aeliot\PhpCsFixerBaseline\Service\Saver; $autoloaderPath = (static function (): string { + if (isset($GLOBALS['_composer_autoload_path'])) { + return $GLOBALS['_composer_autoload_path']; + } + $paths = [ __DIR__ . '/vendor/autoload.php', __DIR__ . '/../vendor/autoload.php', @@ -13,19 +17,17 @@ $autoloaderPath = (static function (): string { __DIR__ . '/../../../../vendor/autoload.php', ]; - if (isset($GLOBALS['_composer_autoload_path'])) { - array_unshift($paths, $GLOBALS['_composer_autoload_path']); - } - foreach ($paths as $path) { if (file_exists($path)) { - return realpath($path); + return $path; } } throw new RuntimeException('Cannot find autoloader'); })(); +echo "autoloader: $autoloaderPath \n";//DELETE + require $autoloaderPath; $projectPath = dirname($autoloaderPath, 2); @@ -57,6 +59,8 @@ $options = (static function () use ($absolutePathMaker): array { return $values; })(); +echo json_encode($options)."\n"; + $baselinePath = $options['baseline']; /** @var PhpCsFixer\Config $config */ $config = require $options['config']; diff --git a/scripts/phar/create.php b/scripts/phar/create.php index ec4003d..3cd94bf 100644 --- a/scripts/phar/create.php +++ b/scripts/phar/create.php @@ -19,13 +19,13 @@ $finder = (new Finder()) ->files() + ->name('/\.php$/') ->in([ - $basedir . '/bin', $basedir . '/src', $basedir . '/vendor', ]) ->append([ - $basedir . '/bin/pcsf-baseline', + $basedir . '/pcsf-baseline', $basedir . '/LICENSE', $basedir . '/README.md', ]) @@ -34,7 +34,11 @@ }); // create phar -$phar = new Phar($pharFile); +$phar = new Phar( + $pharFile, + FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS | FilesystemIterator::KEY_AS_PATHNAME, + 'pcsf-baseline.phar', +); // start buffering. Mandatory to modify stub to add shebang $phar->startBuffering(); @@ -46,7 +50,8 @@ $phar->addFile($realPath, substr($realPath, $basedirLen)); } -$phar->setDefaultStub('bin/pcsf-baseline'); +// $phar->setDefaultStub('bin/pcsf-baseline'); +$phar->setStub(Phar::createDefaultStub(__DIR__ . '/stub')); $phar->stopBuffering(); diff --git a/scripts/phar/stub b/scripts/phar/stub new file mode 100644 index 0000000..102d688 --- /dev/null +++ b/scripts/phar/stub @@ -0,0 +1,86 @@ +#!/usr/bin/env php +<?php + +use Aeliot\PhpCsFixerBaseline\Service\Builder; +use Aeliot\PhpCsFixerBaseline\Service\Saver; + + +define('PCSF_BASELINE_DIR_RUN', dirname(Phar::running(false))); + +$autoloaderPath = (static function (): string { + $paths = [ + __DIR__ . '/vendor/autoload.php', + __DIR__ . '/../vendor/autoload.php', + __DIR__ . '/../../vendor/autoload.php', + __DIR__ . '/../../../vendor/autoload.php', + __DIR__ . '/../../../../vendor/autoload.php', + ]; + + foreach ($paths as $path) { + if (file_exists($path)) { + return $path; + } + } + + throw new RuntimeException('Cannot find autoloader'); +})(); + +echo "autoloader: $autoloaderPath \n";//DELETE + +require_once $autoloaderPath; + +$absolutePathMaker = static function (string $path): string { + if (preg_match('#^(?:[[:alpha:]]:[/\\\\]|/)#', $path)) { + return $path; + } + + return PCSF_BASELINE_DIR_RUN . '/' . $path; +}; + +$options = (static function () use ($absolutePathMaker): array { + $values = []; + $options = getopt('b:c:f:', ['baseline', 'config', 'finder']); + $defaults = [ + '.php-cs-fixer-baseline.json' => ['b', 'baseline'], + '.php-cs-fixer.dist.php' => ['c', 'config'], + '.php-cs-fixer-finder.php' => ['f', 'finder'], + ]; + + foreach ($defaults as $default => [$short, $long]) { + if (isset($options['b'], $options['baseline'])) { + throw new InvalidArgumentException(sprintf('%s is duplicated', $long)); + } + $values[$long] = $absolutePathMaker($options[$short] ?? $options[$long] ?? $default); + } + + return $values; +})(); + +echo json_encode($options) . "\n"; + +Phar::mount('phar://pcsf-baseline.phar/.php-cs-fixer-baseline.json', $options['baseline']); +Phar::mount('phar://pcsf-baseline.phar/.php-cs-fixer.dist.php', $options['config']); +Phar::mount('phar://pcsf-baseline.phar/.php-cs-fixer.dist.php', $options['finder']); + +$baselinePath = $options['baseline']; +/** @var PhpCsFixer\Config $config */ +$config = require $options['config']; +/** @var PhpCsFixer\Finder $finder */ +$finder = require $options['finder']; + +$baseline = (new Builder())->create($baselinePath, $config, $finder); +(new Saver())->save($baseline); + +echo sprintf("Ok, %s files added to baseline\n", $baseline->getLockedFilesCount()); + +Phar::mapPhar('pcsf-baseline.phar'); + +//try { +// Phar::mapPhar('pcsf-baseline.phar'); +// include 'phar://pcsf-baseline.phar/bin/pcsf-baseline'; +//} catch (PharException $e) { +// echo $e->getMessage(); +// die('Cannot initialize Phar'); +//} + +__HALT_COMPILER();