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
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Command\Developer;
use OC\Core\Command\Base;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\Server;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateDocs extends Base {
public function __construct(
private IConfig $config,
private IAppManager $appManager,
) {
parent::__construct();
}
public function isEnabled(): bool {
return $this->config->getSystemValue('debug', false) === true;
}
protected function configure(): void {
$this
->setName('talk:developer:update-docs')
->setDescription('Update documentation of commands')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$info = $this->appManager->getAppInfo('spreed');
$documentation = "# Talk occ commands\n\n";
foreach ($info['commands'] as $namespace) {
if ($namespace === self::class
|| $namespace === AgeChatMessages::class) {
continue;
}
$command = $this->getCommand($namespace);
$documentation .= $this->getDocumentation($command) . "\n";
}
$handle = fopen(__DIR__ . '/../../../docs/occ.md', 'w');
fwrite($handle, $documentation);
fclose($handle);
return 0;
}
protected function getCommand(string $namespace): Command {
$command = Server::get($namespace);
// Clean full definition of command that have the default Symfony options
$command->setApplication($this->getApplication());
return $command;
}
protected function getDocumentation(Command $command): string {
$doc = '## ' . $command->getName() . "\n\n";
$doc .= $command->getDescription() . "\n\n";
$doc .=
'### Usage' . "\n\n" .
array_reduce(
array_merge(
[$command->getSynopsis()],
$command->getAliases(),
$command->getUsages()
),
function ($carry, $usage) {
return $carry.'* `'.$usage.'`'."\n";
}
);
$doc .= $this->describeInputDefinition($command);
return $doc;
}
protected function describeInputDefinition(Command $command): string {
$definition = $command->getDefinition();
$text = '';
if (\count($definition->getArguments()) > 0) {
$text .= "\n";
$text .= "| Arguments | Description | Is required | Is array | Default |\n";
$text .= '|---|---|---|---|---|';
foreach ($definition->getArguments() as $argument) {
$describeInputArgument = $this->describeInputArgument($argument);
if ($describeInputArgument) {
$text .= "\n" . $describeInputArgument;
}
}
$text .= "\n";
}
if (\count($definition->getOptions()) > 0) {
$text .= "\n";
$text .= "| Options | Description | Accept value | Is value required | Is multiple | Default |\n";
$text .= '|---|---|---|---|---|---|';
foreach ($definition->getOptions() as $option) {
$describeInputOption = $this->describeInputOption($option);
if ($describeInputOption) {
$text .= "\n" . $describeInputOption;
}
}
$text .= "\n";
}
return $text;
}
protected function describeInputArgument(InputArgument $argument): string {
$description = $argument->getDescription();
return
'| `'.($argument->getName() ?: '<none>') . '` | ' .
($description ? preg_replace('/\s*[\r\n]\s*/', ' ', $description) : '') . ' | ' .
($argument->isRequired() ? 'yes' : 'no') . ' | ' .
($argument->isArray() ? 'yes' : 'no') . ' | ' .
($argument->isRequired() ? '*Required*' : '`' . str_replace("\n", '', var_export($argument->getDefault(), true)) . '`') . ' |';
}
protected function describeInputOption(InputOption $option): string {
$name = '--'.$option->getName();
if ($option->getShortcut()) {
$name .= '\|-'.str_replace('|', '\|-', $option->getShortcut());
}
$description = $option->getDescription();
return
'| `' . $name . '` | ' .
($description ? preg_replace('/\s*[\r\n]\s*/', " ", $description) : '') . ' | '.
($option->acceptValue() ? 'yes' : 'no') . ' | ' .
($option->isValueRequired() ? 'yes' : 'no') . ' | ' .
($option->isArray() ? 'yes' : 'no') . ' | ' .
($option->isValueRequired() ? '*Required*' : '`' . str_replace("\n", '', var_export($option->getDefault(), true)) . '`') . ' |';
}
}
|