|
| 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\Console\ExitCode; |
| 10 | +use Tempest\Support\Filesystem; |
| 11 | + |
| 12 | +use function Tempest\root_path; |
| 13 | + |
| 14 | +final readonly class SymlinkDocumentationCommand |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + private Console $console, |
| 18 | + ) {} |
| 19 | + |
| 20 | + #[ConsoleCommand('docs:symlink')] |
| 21 | + public function __invoke(string $path = '../tempest-framework'): ExitCode |
| 22 | + { |
| 23 | + $from = root_path($path, '/docs'); |
| 24 | + $to = root_path('src/Web/Documentation/content/1.x'); |
| 25 | + |
| 26 | + $this->console->header('Symlinking documentation'); |
| 27 | + |
| 28 | + if (! Filesystem\exists($from)) { |
| 29 | + $this->console->error("The source path does not exist (tried {$from})."); |
| 30 | + |
| 31 | + return ExitCode::ERROR; |
| 32 | + } |
| 33 | + |
| 34 | + if (Filesystem\is_symbolic_link($to) || Filesystem\is_file($to)) { |
| 35 | + $this->console->task( |
| 36 | + label: "Removing existing symlink at {$to}.", |
| 37 | + handler: fn () => $this->run('rm -rf ' . escapeshellarg($to)), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + $this->console->task( |
| 42 | + label: "Creating symlink from {$from} to {$to}.", |
| 43 | + handler: fn () => $this->run('ln -s ' . escapeshellarg($from) . ' ' . escapeshellarg($to)), |
| 44 | + ); |
| 45 | + |
| 46 | + $this->console->writeln(); |
| 47 | + $this->console->success('Symlink created successfully.'); |
| 48 | + |
| 49 | + return ExitCode::SUCCESS; |
| 50 | + } |
| 51 | + |
| 52 | + private function run(string $command): string |
| 53 | + { |
| 54 | + $process = Process::fromShellCommandline($command); |
| 55 | + $process->run(); |
| 56 | + |
| 57 | + if (! $process->isSuccessful()) { |
| 58 | + throw new RuntimeException($process->getErrorOutput()); |
| 59 | + } |
| 60 | + |
| 61 | + return $process->getOutput(); |
| 62 | + } |
| 63 | +} |
0 commit comments