Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliot-Tm committed Aug 22, 2024
1 parent 456cebb commit 2002698
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 9 additions & 5 deletions bin/pcsf-baseline
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
Expand Down Expand Up @@ -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'];
Expand Down
13 changes: 9 additions & 4 deletions scripts/phar/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
])
Expand All @@ -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();
Expand All @@ -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();

Expand Down
86 changes: 86 additions & 0 deletions scripts/phar/stub
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 2002698

Please sign in to comment.