From 0de7038d895dd2351a73fddfe9ee2590e9f5bf92 Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 25 Nov 2026 16:54:18 +0000 Subject: [PATCH] pin all php versions external updates are the biggest source of surprise-breakage, let's pin the versions to make each specific build reproducible, and make it easy (semi-automatic, with a cronjob sending pull requests) to manually update? --- .github/workflows/auto-regenerate.yml | 4 + generator/safe.php | 2 + generator/src/Commands/GenerateCommand.php | 56 ++++--------- .../src/Commands/UpdateVersionsCommand.php | 65 +++++++++++++++ generator/src/VersionManager.php | 82 +++++++++++++++++++ generator/versions.json | 30 +++++++ 6 files changed, 197 insertions(+), 42 deletions(-) create mode 100644 generator/src/Commands/UpdateVersionsCommand.php create mode 100644 generator/src/VersionManager.php create mode 100644 generator/versions.json diff --git a/.github/workflows/auto-regenerate.yml b/.github/workflows/auto-regenerate.yml index 4835ba05..91775bfe 100644 --- a/.github/workflows/auto-regenerate.yml +++ b/.github/workflows/auto-regenerate.yml @@ -33,6 +33,10 @@ jobs: run: "./safe.php download-docs" working-directory: "generator" + - name: "Update documentation versions" + run: "./safe.php update-versions" + working-directory: "generator" + - name: "Regenerate files" run: "./safe.php generate" working-directory: "generator" diff --git a/generator/safe.php b/generator/safe.php index cb450a85..c0af0ca9 100755 --- a/generator/safe.php +++ b/generator/safe.php @@ -8,6 +8,7 @@ use Safe\Commands\GenerateCommand; use Safe\Commands\ScanObjectsCommand; use Safe\Commands\DownloadDocsCommand; +use Safe\Commands\UpdateVersionsCommand; use Symfony\Component\Console\Application; $application = new Application(); @@ -15,5 +16,6 @@ $application->addCommands([new ScanObjectsCommand()]); $application->addCommands([new FunctionInfoCommand()]); $application->addCommands([new DownloadDocsCommand()]); +$application->addCommands([new UpdateVersionsCommand()]); $application->run(); diff --git a/generator/src/Commands/GenerateCommand.php b/generator/src/Commands/GenerateCommand.php index db903d04..46c1b473 100644 --- a/generator/src/Commands/GenerateCommand.php +++ b/generator/src/Commands/GenerateCommand.php @@ -5,6 +5,7 @@ namespace Safe\Commands; use Safe\Filesystem\PathHelper; +use Safe\VersionManager; use Safe\XmlDocParser\Scanner; use Safe\XmlDocParser\DocPage; use Safe\Generator\FileCreator; @@ -36,46 +37,15 @@ protected function execute( ): int { $this->rmGenerated(); - // PHP documentation is a living document, which broadly reflects - // "the current state of PHP". There is no guarantee that any version - // of the documentation accurately reflects the state of PHP at any - // given time, but these are some best-guess commits that approximately - // match the state of PHP at the time of writing. - // Each tuple is [doc-en, doc-base]. The doc-base revision is the latest - // one available when the corresponding doc-en revision was committed. - $versions = [ - "8.1" => [ - "9097ea48f608dbbbf795235a31af82b85bd94430", - "03bc5fa4060c36c8af4ef3d64b8e272929d6ac0a", - ], - "8.2" => [ - "8f4e8cf3de08208e71eb0117f1c970c27e9120c9", - "26046559ed4cbe962cd86813516dfb143449b145", - ], - "8.3" => [ - "4b00c7916e62f2599d06965251e206532c3368c9", - "59bc2bb7061a9803294c5b1e0190d45ce6577151", - ], - "8.4" => [ - "d553fa36940639b0889ec4358fa3bbb92f123b69", - "dfcbb8664db0a8f2cc8184835702a4c60abd59d8", - ], - "8.5" => [ - "ce397343296708654c8cdac774e23a9ee47ab27d", - "2f8a49e992af23ef26749d7c2cf12157669d7d31", - ], - "8.6" => [ - "99a687b41b2d9578f5c6c6702a76d545feb8ecec", - "a99f4372a97499886afc5ddde0f75cdfdd138e4a", - ], - "8.7" => [ - "master", - "master", - ], - ]; + $allVersions = VersionManager::load(); + $latestVersion = VersionManager::latestVersion($allVersions); + $versions = $allVersions; $version = $input->getArgument("version"); if ($version) { + if (!isset($versions[$version])) { + throw new \InvalidArgumentException("Unknown PHP version: $version"); + } $versions = [$version => $versions[$version]]; } @@ -87,8 +57,11 @@ protected function execute( // in the past, so if we stop needing a safe-wrapper, we can start // generating no-op wrappers instead. $pastFunctionNames = []; + $fileCreator = new FileCreator(); - foreach ($versions as $version => [$commit, $docBaseCommit]) { + foreach ($versions as $version => $commits) { + $commit = $commits['doc-en']; + $docBaseCommit = $commits['doc-base']; $output->writeln('==============================================='); $output->writeln('Generating safe wrappers for PHP ' . $version); $output->writeln('==============================================='); @@ -114,17 +87,16 @@ protected function execute( } $genDir = FileCreator::getSafeRootDir() . "/generated/$version"; - $fileCreator = new FileCreator(); $fileCreator->generatePhpFile($res->methods, "$genDir/"); $fileCreator->generateFunctionsList($res->methods, "$genDir/functionsList.php"); $fileCreator->generateRectorFile($res->methods, "$genDir/rector-migrate.php"); } - // always reset docs to master, even if the most-recently-supported-version + // always reset docs to the latest tracked commits, even if the most-recently-supported-version // in $versions is pinned to a specific commit - this is so that at the end // of a `generate` run, we don't have docs stuck in the past - $this->checkout(DocPage::referenceDir(), "master"); - $this->checkout(PathHelper::docsDirectory() . '/php/doc-base', "master"); + $this->checkout(DocPage::referenceDir(), $allVersions[$latestVersion]['doc-en']); + $this->checkout(PathHelper::docsDirectory() . '/php/doc-base', $allVersions[$latestVersion]['doc-base']); foreach (\array_keys($modules) as $moduleName) { $fileCreator->deduplicateAndGenerateVersionSplitters($moduleName, FileCreator::getSafeRootDir() . "/generated/", \array_keys($versions)); diff --git a/generator/src/Commands/UpdateVersionsCommand.php b/generator/src/Commands/UpdateVersionsCommand.php new file mode 100644 index 00000000..8ab21ce2 --- /dev/null +++ b/generator/src/Commands/UpdateVersionsCommand.php @@ -0,0 +1,65 @@ +setName('update-versions') + ->setDescription('Pull the PHP documentation and update the latest PHP version commits.') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $versions = VersionManager::load(); + $latestVersion = VersionManager::latestVersion($versions); + $repositories = [ + 'doc-en' => PathHelper::docsDirectory().'/php/doc-en', + 'doc-base' => PathHelper::docsDirectory().'/php/doc-base', + ]; + + foreach ($repositories as $repository => $directory) { + $this->pull($directory); + $versions[$latestVersion][$repository] = $this->getHead($directory); + } + + VersionManager::save($versions); + $output->writeln(\sprintf('Updated PHP %s in %s', $latestVersion, VersionManager::path())); + + return self::SUCCESS; + } + + private function pull(string $directory): void + { + $process = new Process(['git', 'pull', '--ff-only'], $directory); + $process->setTimeout(600); + $code = $process->run(); + if (0 !== $code) { + throw new \RuntimeException("Failed to git-pull in $directory (exit $code):\n{$process->getErrorOutput()}"); + } + } + + private function getHead(string $directory): string + { + $process = new Process(['git', 'rev-parse', 'HEAD'], $directory); + $process->setTimeout(10); + $code = $process->run(); + if (0 !== $code) { + throw new \RuntimeException("Failed to get the current commit in $directory (exit $code):\n{$process->getErrorOutput()}"); + } + + return \trim($process->getOutput()); + } +} diff --git a/generator/src/VersionManager.php b/generator/src/VersionManager.php new file mode 100644 index 00000000..5d0edc1a --- /dev/null +++ b/generator/src/VersionManager.php @@ -0,0 +1,82 @@ + + */ + public static function load(): array + { + $contents = \file_get_contents(self::path()); + if (false === $contents) { + throw new \RuntimeException('Unable to read '.self::path()); + } + + try { + $versions = \json_decode($contents, true, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $exception) { + throw new \RuntimeException('Unable to decode '.self::path().': '.$exception->getMessage(), 0, $exception); + } + + if (!\is_array($versions) || [] === $versions) { + throw new \RuntimeException(self::path().' must contain at least one PHP version'); + } + + $normalizedVersions = []; + foreach ($versions as $version => $commits) { + if (!\is_string($version) || !\is_array($commits) + || !isset($commits['doc-en'], $commits['doc-base']) + || !\is_string($commits['doc-en']) || !\is_string($commits['doc-base']) + ) { + throw new \RuntimeException(\sprintf('Invalid version entry %s in %s', (string) $version, self::path())); + } + + $normalizedVersions[$version] = [ + 'doc-en' => $commits['doc-en'], + 'doc-base' => $commits['doc-base'], + ]; + } + + return $normalizedVersions; + } + + /** + * @param array $versions + */ + public static function save(array $versions): void + { + try { + $contents = \json_encode($versions, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)."\n"; + } catch (\JsonException $exception) { + throw new \RuntimeException('Unable to encode versions: '.$exception->getMessage(), 0, $exception); + } + + if (false === \file_put_contents(self::path(), $contents)) { + throw new \RuntimeException('Unable to write '.self::path()); + } + } + + /** + * @param array $versions + */ + public static function latestVersion(array $versions): string + { + $versionNumbers = \array_keys($versions); + \usort($versionNumbers, static fn (string $left, string $right): int => \version_compare($left, $right)); + + return (string) \end($versionNumbers); + } + + public static function path(): string + { + return \dirname(__DIR__).'/versions.json'; + } + + private function __construct() + { + } +} diff --git a/generator/versions.json b/generator/versions.json new file mode 100644 index 00000000..6986bb35 --- /dev/null +++ b/generator/versions.json @@ -0,0 +1,30 @@ +{ + "8.1": { + "doc-en": "9097ea48f608dbbbf795235a31af82b85bd94430", + "doc-base": "03bc5fa4060c36c8af4ef3d64b8e272929d6ac0a" + }, + "8.2": { + "doc-en": "8f4e8cf3de08208e71eb0117f1c970c27e9120c9", + "doc-base": "26046559ed4cbe962cd86813516dfb143449b145" + }, + "8.3": { + "doc-en": "4b00c7916e62f2599d06965251e206532c3368c9", + "doc-base": "59bc2bb7061a9803294c5b1e0190d45ce6577151" + }, + "8.4": { + "doc-en": "d553fa36940639b0889ec4358fa3bbb92f123b69", + "doc-base": "dfcbb8664db0a8f2cc8184835702a4c60abd59d8" + }, + "8.5": { + "doc-en": "ce397343296708654c8cdac774e23a9ee47ab27d", + "doc-base": "2f8a49e992af23ef26749d7c2cf12157669d7d31" + }, + "8.6": { + "doc-en": "99a687b41b2d9578f5c6c6702a76d545feb8ecec", + "doc-base": "a99f4372a97499886afc5ddde0f75cdfdd138e4a" + }, + "8.7": { + "doc-en": "ab8c3fc04d81d25ac11d9426e4118daa62424ee8", + "doc-base": "a331e55094ca170b73357fa461101ffa28e0c617" + } +}