diff --git a/.gitignore b/.gitignore index e3acc427..5eed8636 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,8 @@ /.phpunit.result.cache /.php-cs-fixer.cache /composer.lock +/composer.phar /phpstan.neon /phpunit.xml /vendor/ -test_db* \ No newline at end of file +test_db* diff --git a/bin/compile b/bin/compile new file mode 100755 index 00000000..df8fb74f --- /dev/null +++ b/bin/compile @@ -0,0 +1,39 @@ +#!/usr/bin/bash + +ROOTPATH="`dirname $0`" +ROOTPATH="`dirname $ROOTPATH`" +COMPOSER="$ROOTPATH/composer.phar" + +echo " ==> Using composer $COMPOSER" +echo " ==> Will generate $ROOTPATH/db-tools.phar" + +# Make a backup of the composer.json file. +cp "$ROOTPATH/composer.json" "$ROOTPATH/composer.json.dist" + +if [ ! -e "$COMPOSER" ]; then + echo " ==> Download composer in $COMPOSER" + wget --quiet https://getcomposer.org/download/latest-stable/composer.phar -o "$COMPOSER" +fi + +# Prepare composer, install without depdendencies. +echo " ==> Prepare environment" +rm -rf "$ROOTPATH/composer.lock" +rm -rf "$ROOTPATH/vendor" + +# Install PHAR only tooling. +echo " ==> Require compile-only dependencies" +php "$COMPOSER" -n require --no-audit composer/pcre:'^3.1' seld/phar-utils:'^1.2' +php "$COMPOSER" -n -q config autoloader-suffix DbToolsPhar +php "$COMPOSER" -n install --no-dev +php "$COMPOSER" -n config autoloader-suffix --unset + +# Compile PHAR file +echo " ==> Running compilation" +php -d phar.readonly=0 bin/compile.php +chmod +x "$ROOTPATH/db-tools.phar" + +# Clean up environment +echo " ==> Cleaning up environment" +cp "$ROOTPATH/composer.json.dist" "$ROOTPATH/composer.json" +rm -rf "$ROOTPATH/composer.lock" "$ROOTPATH/composer.json.dist" +php "$COMPOSER" -n -q install diff --git a/bin/compile.php b/bin/compile.php new file mode 100644 index 00000000..e1b9f3c0 --- /dev/null +++ b/bin/compile.php @@ -0,0 +1,57 @@ +compile(); + exit(1); +})(); diff --git a/composer.json b/composer.json index 1544681a..f3ef10ca 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ "makinacorpus/query-builder": "^1.6.1", "psr/log": "^3.0", "symfony/config": "^6.0|^7.0", + "symfony/console": "^6.0|^7.0", "symfony/filesystem": "^6.0|^7.0", "symfony/finder": "^6.0|^7.0", "symfony/options-resolver": "^6.0|^7.0", @@ -34,12 +35,13 @@ "symfony/validator": "^6.3|^7.0" }, "suggest": { - "doctrine/doctrine-bundle": "For autoconfiguration in Symfony project context", - "symfony/console": "In order to use the standalone CLI tool or Symfony console commands", "symfony/password-hasher": "In order to use the password hash anonymizer" }, "conflict": { - "symfony/console": "<6.0|>=8.0", + "composer/pcre": "<3.1|>=4.0", + "doctrine/dbal": "^<3.0|>=5.0", + "doctrine/orm": "^<2.15|>=4.0", + "seld/phar-utils": "<1.2|>=2.0", "symfony/password-hasher": "<6.0|>=8.0" }, "autoload": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 8e18fb09..f9a6985f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,3 +6,6 @@ parameters: excludePaths: - src/DependencyInjection/DbToolsConfiguration.php checkMissingOverrideMethodAttribute: true + ignoreErrors: + - '#Instantiated class Seld\\PharUtils\\Timestamps not found.#' + - '#on an unknown class Seld\\PharUtils\\Timestamps.#' \ No newline at end of file diff --git a/src/Bridge/Standalone/PharCompiler.php b/src/Bridge/Standalone/PharCompiler.php new file mode 100644 index 00000000..faa8df58 --- /dev/null +++ b/src/Bridge/Standalone/PharCompiler.php @@ -0,0 +1,181 @@ +run() !== 0) { + throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from composer git repository clone and that git binary is available.'); + } + + $this->versionDate = new \DateTime(\trim($process->getOutput())); + $this->versionDate->setTimezone(new \DateTimeZone('UTC')); + + $phar = new \Phar($pharFile, 0, 'db-tools.phar'); + $phar->setSignatureAlgorithm(\Phar::SHA512); + + $phar->startBuffering(); + + $finderSort = static fn ($a, $b): int => \strcmp(\strtr($a->getRealPath(), '\\', '/'), \strtr($b->getRealPath(), '\\', '/')); + + // Local package sources. + $finder = new Finder(); + $finder->files() + ->ignoreVCS(true) + ->name('*.php') + ->notName('Compiler.php') + ->notName('ClassLoader.php') + ->notName('InstalledVersions.php') + ->in($rootDir.'/src') + ->sort($finderSort) + ; + foreach ($finder as $file) { + $this->addFile($phar, $file); + } + // Add runtime utilities separately to make sure they retains the docblocks as these will get copied into projects. + $this->addFile($phar, new \SplFileInfo($rootDir . '/vendor/composer/ClassLoader.php'), false); + $this->addFile($phar, new \SplFileInfo($rootDir . '/vendor/composer/InstalledVersions.php'), false); + + // Add vendor files + $finder = new Finder(); + $finder->files() + ->ignoreVCS(true) + ->notPath('/\/(composer\.(json|lock)|[A-Z]+\.md(?:own)?|\.gitignore|appveyor.yml|phpunit\.xml\.dist|phpstan\.neon\.dist|phpstan-config\.neon|phpstan-baseline\.neon)$/') + ->notPath('/(.*\.(md|xml|twig|svg)|Dockerfile|phpbench\.json|yaml-lint|dev\.sh|docker-compose\.(yaml|yml)|run-tests\.sh)/') + ->notPath('/bin\/(jsonlint|validate-json|simple-phpunit|phpstan|phpstan\.phar)(\.bat)?$/') + ->notPath('justinrainbow/json-schema/demo/') + ->notPath('justinrainbow/json-schema/dist/') + ->notPath('composer/LICENSE') + ->exclude('Tests') + ->exclude('tests') + ->exclude('docs') + ->in($rootDir.'/vendor/') + ->sort($finderSort) + ; + + $extraFiles = []; + foreach ([ + $rootDir . '/vendor/composer/installed.json', + // CaBundle::getBundledCaBundlePath(), + $rootDir . '/vendor/composer/installed.json', + $rootDir . '/vendor/symfony/console/Resources/bin/hiddeninput.exe', + $rootDir . '/vendor/symfony/console/Resources/completion.bash', + $rootDir . '/vendor/symfony/console/Resources/completion.fish', + $rootDir . '/vendor/symfony/console/Resources/completion.zsh', + $rootDir . '/vendor/composer/installed.json', + ] as $file) { + $extraFiles[$file] = \realpath($file); + if (!\file_exists($file)) { + throw new \RuntimeException('Extra file listed is missing from the filesystem: '.$file); + } + } + $unexpectedFiles = []; + + foreach ($finder as $file) { + if (false !== ($index = \array_search($file->getRealPath(), $extraFiles, true))) { + unset($extraFiles[$index]); + } elseif (!Preg::isMatch('{(^LICENSE$|\.php$)}', $file->getFilename())) { + $unexpectedFiles[] = (string) $file; + } + + if (Preg::isMatch('{\.php[\d.]*$}', $file->getFilename())) { + $this->addFile($phar, $file); + } else { + $this->addFile($phar, $file, false); + } + } + + if (\count($extraFiles) > 0) { + throw new \RuntimeException('These files were expected but not added to the phar, they might be excluded or gone from the source package:'.PHP_EOL.var_export($extraFiles, true)); + } + if (\count($unexpectedFiles) > 0) { + throw new \RuntimeException('These files were unexpectedly added to the phar, make sure they are excluded or listed in $extraFiles:'.PHP_EOL.var_export($unexpectedFiles, true)); + } + + // Add binary. + $phar->addFile($rootDir . '/bin/db-tools.php', 'bin/db-tools.php'); + $content = \file_get_contents($rootDir.'/bin/db-tools'); + $content = Preg::replace('{^#!/usr/bin/env php\s*}', '', $content); + $phar->addFromString('bin/db-tools', $content); + + // Stubs + $phar->setStub( + <<<'EOT' + #!/usr/bin/env php + stopBuffering(); + + //$this->addFile($phar, new \SplFileInfo($rootDir.'/LICENSE.md'), false); + + unset($phar); + + // re-sign the phar with reproducible timestamp / signature + $util = new Timestamps($pharFile); + $util->updateTimestamps($this->versionDate); + $util->save($pharFile, \Phar::SHA512); + } + + private function getRelativeFilePath(\SplFileInfo $file): string + { + $rootDir = \dirname(__DIR__, 3); + + $realPath = $file->getRealPath(); + $pathPrefix = $rootDir . DIRECTORY_SEPARATOR; + $pos = \strpos($realPath, $pathPrefix); + $relativePath = ($pos !== false) ? \substr_replace($realPath, '', $pos, \strlen($pathPrefix)) : $realPath; + + return \strtr($relativePath, '\\', '/'); + } + + private function addFile(\Phar $phar, \SplFileInfo $file, bool $strip = true): void + { + $phar->addFile($this->getRelativeFilePath($file)); + } +}