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