-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikiCommand.php
More file actions
234 lines (206 loc) · 8.6 KB
/
WikiCommand.php
File metadata and controls
234 lines (206 loc) · 8.6 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console\Command;
use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
use Composer\Command\BaseCommand;
use FastForward\DevTools\Composer\Json\ComposerJsonInterface;
use FastForward\DevTools\Console\Input\HasCacheOption;
use FastForward\DevTools\Console\Input\HasJsonOption;
use FastForward\DevTools\Filesystem\FilesystemInterface;
use FastForward\DevTools\Git\GitClientInterface;
use FastForward\DevTools\Process\ProcessBuilderInterface;
use FastForward\DevTools\Process\ProcessQueueInterface;
use FastForward\DevTools\Path\ManagedWorkspace;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
use function Safe\getcwd;
/**
* Handles the generation of API documentation for the project.
* This class MUST NOT be extended and SHALL utilize phpDocumentor to accomplish its task.
*/
#[AsCommand(name: 'wiki', description: 'Generates API documentation in Markdown format.')]
final class WikiCommand extends BaseCommand implements LoggerAwareCommandInterface
{
use HasCacheOption;
use HasJsonOption;
use LogsCommandResults;
/**
* Creates a new WikiCommand instance.
*
* @param ComposerJsonInterface $composer the composer.json accessor
* @param ProcessBuilderInterface $processBuilder
* @param ProcessQueueInterface $processQueue
* @param FilesystemInterface $filesystem the filesystem used to inspect the wiki target
* @param GitClientInterface $gitClient
* @param LoggerInterface $logger the output-aware logger
*/
public function __construct(
private readonly ProcessBuilderInterface $processBuilder,
private readonly ProcessQueueInterface $processQueue,
private readonly ComposerJsonInterface $composer,
private readonly FilesystemInterface $filesystem,
private readonly GitClientInterface $gitClient,
private readonly LoggerInterface $logger,
) {
return parent::__construct();
}
/**
* Configures the command instance.
*
* The method MUST set up the name and description. It MAY accept an optional `--target` option
* pointing to an alternative configuration target path.
*
* @return void
*/
protected function configure(): void
{
$this->setHelp('This command generates API documentation in Markdown format using phpDocumentor. ');
$this
->addJsonOption()
->addCacheOption('Whether to enable phpDocumentor caching.')
->addCacheDirOption(
description: 'Path to the cache directory for phpDocumentor.',
default: ManagedWorkspace::getCacheDirectory(ManagedWorkspace::PHPDOC),
)
->addOption(
name: 'target',
shortcut: 't',
mode: InputOption::VALUE_OPTIONAL,
description: 'Path to the output directory for the generated Markdown documentation.',
default: '.github/wiki'
)
->addOption(
name: 'init',
mode: InputOption::VALUE_NONE,
description: 'Initialize the configured wiki target as a Git submodule.',
);
}
/**
* Executes the generation of the documentation files in Markdown format.
*
* This method MUST compile arguments based on PSR-4 namespaces to feed into phpDocumentor.
* It SHOULD provide feedback on generation progress, and SHALL return `self::SUCCESS` on success.
*
* @param InputInterface $input the input details for the command
* @param OutputInterface $output the output mechanism for logging
*
* @return int the final execution status code
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$jsonOutput = $this->isJsonOutput($input);
$processOutput = $jsonOutput ? new BufferedOutput() : $output;
$target = (string) $input->getOption('target');
$cacheEnabled = $this->isCacheEnabled($input);
if ($input->getOption('init')) {
return $this->initializeWikiSubmodule($input, $target, $processOutput);
}
if (! $jsonOutput) {
$this->logger->info('Generating wiki documentation...', [
'input' => $input,
]);
}
$processBuilder = $this->processBuilder
->withArgument('--visibility', 'public,protected')
->withArgument('--template', 'vendor/saggre/phpdocumentor-markdown/themes/markdown')
->withArgument('--title', $this->composer->getDescription())
->withArgument('--target', $target);
if ($cacheEnabled) {
$processBuilder = $processBuilder->withArgument('--cache-folder', $input->getOption('cache-dir'));
}
$psr4Namespaces = $this->composer->getAutoload('psr-4');
foreach ($psr4Namespaces as $path) {
$processBuilder = $processBuilder->withArgument('--directory', $path);
}
if ($defaultPackageName = array_key_first($psr4Namespaces)) {
$processBuilder = $processBuilder->withArgument('--defaultpackagename', $defaultPackageName);
}
$this->processQueue->add($processBuilder->build('vendor/bin/phpdoc'));
$result = $this->processQueue->run($processOutput);
if (self::SUCCESS === $result) {
return $this->success('Wiki documentation generated successfully.', $input, [
'output' => $processOutput,
]);
}
return $this->failure(
'Wiki documentation generation failed.',
$input,
[
'output' => $processOutput,
],
(string) $input->getOption('target'),
);
}
/**
* Adds the repository wiki as a Git submodule when the target path is missing.
*
* @param string $target the configured wiki target path
* @param OutputInterface $output the output used for process feedback
* @param InputInterface $input
*
* @return int the command status code
*/
private function initializeWikiSubmodule(InputInterface $input, string $target, OutputInterface $output): int
{
$wikiSubmodulePath = (string) $this->filesystem->getAbsolutePath($target);
if ($this->filesystem->exists($wikiSubmodulePath)) {
return $this->success(
'Wiki submodule already exists at {wiki_submodule_path}.',
$input,
[
'input' => $input,
'wiki_submodule_path' => $wikiSubmodulePath,
],
);
}
$repositoryUrl = $this->getGitRepositoryUrl();
$wikiRepoUrl = str_replace('.git', '.wiki.git', $repositoryUrl);
$this->processQueue->add(
$this->processBuilder
->withArgument('submodule')
->withArgument('add')
->withArgument($wikiRepoUrl)
->withArgument(Path::makeRelative($wikiSubmodulePath, getcwd()))
->build('git')
);
$result = $this->processQueue->run($output);
if (self::SUCCESS === $result) {
return $this->success('Wiki submodule initialized successfully.', $input, [
'wiki_submodule_path' => $wikiSubmodulePath,
'wiki_repository_url' => $wikiRepoUrl,
]);
}
return $this->failure('Wiki submodule initialization failed.', $input, [
'wiki_submodule_path' => $wikiSubmodulePath,
'wiki_repository_url' => $wikiRepoUrl,
], $target);
}
/**
* Resolves the current repository remote origin URL.
*
* @return string the Git remote origin URL
*/
private function getGitRepositoryUrl(): string
{
return $this->gitClient->getConfig('remote.origin.url', getcwd());
}
}