Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/auto-regenerate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions generator/safe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Safe\Commands\GenerateCommand;
use Safe\Commands\ScanObjectsCommand;
use Safe\Commands\DownloadDocsCommand;
use Safe\Commands\UpdateVersionsCommand;
use Symfony\Component\Console\Application;

$application = new Application();
$application->addCommands([new GenerateCommand()]);
$application->addCommands([new ScanObjectsCommand()]);
$application->addCommands([new FunctionInfoCommand()]);
$application->addCommands([new DownloadDocsCommand()]);
$application->addCommands([new UpdateVersionsCommand()]);

$application->run();
56 changes: 14 additions & 42 deletions generator/src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]];
}

Expand All @@ -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('===============================================');
Expand All @@ -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));
Expand Down
65 changes: 65 additions & 0 deletions generator/src/Commands/UpdateVersionsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Safe\Commands;

use Safe\Filesystem\PathHelper;
use Safe\VersionManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

final class UpdateVersionsCommand extends Command
{
protected function configure(): void
{
$this
->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());
}
}
82 changes: 82 additions & 0 deletions generator/src/VersionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Safe;

final class VersionManager
{
/**
* @return array<string, array{doc-en: string, doc-base: string}>
*/
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<string, array{doc-en: string, doc-base: string}> $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<string, array{doc-en: string, doc-base: string}> $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()
{
}
}
30 changes: 30 additions & 0 deletions generator/versions.json
Original file line number Diff line number Diff line change
@@ -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"
}
}