|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Web\Documentation; |
| 4 | + |
| 5 | +use RuntimeException; |
| 6 | +use Symfony\Component\Process\Process; |
| 7 | +use Tempest\Console\Console; |
| 8 | +use Tempest\Console\ConsoleCommand; |
| 9 | +use Tempest\Support\Filesystem; |
| 10 | + |
| 11 | +use function Tempest\root_path; |
| 12 | +use function Tempest\Support\path; |
| 13 | + |
| 14 | +final class PullDocumentationCommand |
| 15 | +{ |
| 16 | + private const CLONE_DIRECTORY = 'clone'; |
| 17 | + private const DOCS_DIRECTORY = 'docs'; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private readonly Console $console, |
| 21 | + ) {} |
| 22 | + |
| 23 | + #[ConsoleCommand('docs:pull')] |
| 24 | + public function __invoke(?Version $version = null) |
| 25 | + { |
| 26 | + $versions = $version ? [$version] : Version::cases(); |
| 27 | + |
| 28 | + $this->console->header('Cloning documentation'); |
| 29 | + |
| 30 | + foreach ($versions as $version) { |
| 31 | + $this->console->task( |
| 32 | + label: "Fetching {$version->getBranch()}", |
| 33 | + handler: fn () => $this->fetchVersionDocumentation($version), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + $this->console->writeln(); |
| 38 | + $this->console->success('Documentation fetched successfully.'); |
| 39 | + } |
| 40 | + |
| 41 | + private function fetchVersionDocumentation(Version $version): void |
| 42 | + { |
| 43 | + $versionedDocsDirectory = root_path('src/Web/Documentation/content/', $version->getUrlSegment()); |
| 44 | + $temporaryDirectory = root_path('docs-clone'); |
| 45 | + |
| 46 | + Filesystem\ensure_directory_empty($versionedDocsDirectory); |
| 47 | + Filesystem\ensure_directory_empty($temporaryDirectory); |
| 48 | + |
| 49 | + $this->run( |
| 50 | + command: sprintf( |
| 51 | + 'git clone --no-checkout --depth=1 --filter=tree:0 -b %s https://github.com/tempestphp/tempest-framework %s', |
| 52 | + $version->getBranch(), |
| 53 | + self::CLONE_DIRECTORY, |
| 54 | + ), |
| 55 | + cwd: $temporaryDirectory, |
| 56 | + ); |
| 57 | + |
| 58 | + $this->run( |
| 59 | + command: 'git sparse-checkout set --no-cone /' . self::DOCS_DIRECTORY, |
| 60 | + cwd: path($temporaryDirectory, self::CLONE_DIRECTORY), |
| 61 | + ); |
| 62 | + |
| 63 | + $this->run( |
| 64 | + command: 'git checkout', |
| 65 | + cwd: path($temporaryDirectory, self::CLONE_DIRECTORY), |
| 66 | + ); |
| 67 | + |
| 68 | + Filesystem\move(path($temporaryDirectory, self::CLONE_DIRECTORY, self::DOCS_DIRECTORY), $versionedDocsDirectory, overwrite: true); |
| 69 | + Filesystem\delete_directory($temporaryDirectory); |
| 70 | + } |
| 71 | + |
| 72 | + private function run(string $command, string $cwd): string |
| 73 | + { |
| 74 | + $process = Process::fromShellCommandline($command, $cwd); |
| 75 | + $process->run(); |
| 76 | + |
| 77 | + if (! $process->isSuccessful()) { |
| 78 | + throw new RuntimeException($process->getErrorOutput()); |
| 79 | + } |
| 80 | + |
| 81 | + return $process->getOutput(); |
| 82 | + } |
| 83 | +} |
0 commit comments