Skip to content

Commit

Permalink
add preload in build
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 29, 2021
1 parent 00fcdc1 commit f15648e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
$autoloadIncluder->autoloadProjectAutoloaderFile();
$autoloadIncluder->autoloadFromCommandLine();

// make local php-parser a priority to avoid conflict
require_once __DIR__ . '/../preload.php';

$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesCaller());
$symfonyStyle = $symfonyStyleFactory->create();

Expand Down
52 changes: 52 additions & 0 deletions build/build-preload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

// inspired at /~https://github.com/phpstan/phpstan-src/commit/87897c2a4980d68efa1c46049ac2eefe767ec946#diff-e897e523125a694bd8ea69bf83374c206803c98720c46d7401b7a7cf53915a26

declare(strict_types=1);

use Symfony\Component\Finder\Finder;

require __DIR__ . '/../vendor/autoload.php';

$buildDirectory = $argv[1];

buildPreloadScript($buildDirectory);

function buildPreloadScript(string $buildDirectory): void
{
$vendorDir = $buildDirectory . '/vendor';
if (!is_dir($vendorDir . '/nikic/php-parser/lib/PhpParser')) {
return;
}

$preloadScript = $buildDirectory . '/preload.php';
$template = <<<'php'
<?php
declare(strict_types = 1);
%s
php;
$root = realpath(__DIR__ . '/..');
if ($root === false) {
return;
}
$output = '';

$finder = (new Finder())
->files()
->name('*.php')
->in($vendorDir . '/nikic/php-parser/lib/PhpParser');

foreach ($finder->getIterator() as $fileInfo) {
$realPath = $fileInfo->getRealPath();
if ($realPath === false) {
continue;
}

$path = substr($realPath, strlen($root));
$output .= 'require_once __DIR__ . ' . var_export($path, true) . ';' . PHP_EOL;
}

file_put_contents($preloadScript, sprintf($template, $output));
}
4 changes: 3 additions & 1 deletion build/build-rector-scoped.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ note "Running scoper to $RESULT_DIRECTORY"
wget /~https://github.com/humbug/php-scoper/releases/download/0.14.0/php-scoper.phar -N --no-verbose

# Work around possible PHP memory limits
php -d memory_limit=-1 php-scoper.phar add-prefix bin config src packages rules vendor composer.json --output-dir "../$RESULT_DIRECTORY" --config scoper.php --force --ansi --working-dir "$BUILD_DIRECTORY"
php -d memory_limit=-1 php-scoper.phar add-prefix preload.php bin config src packages rules vendor composer.json --output-dir "../$RESULT_DIRECTORY" --config scoper.php --force --ansi --working-dir "$BUILD_DIRECTORY"


# note "Dumping Composer Autoload"
composer dump-autoload --working-dir "$RESULT_DIRECTORY" --ansi --classmap-authoritative --no-dev

php "$BUILD_DIRECTORY/build/build-preload.php" $RESULT_DIRECTORY

rm -rf "$BUILD_DIRECTORY"


Expand Down
Empty file added preload.php
Empty file.

0 comments on commit f15648e

Please sign in to comment.