1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Documentation; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use RecursiveDirectoryIterator; |
12
|
|
|
use RecursiveIteratorIterator; |
13
|
|
|
use RecursiveRegexIterator; |
14
|
|
|
use RegexIterator; |
15
|
|
|
|
16
|
|
|
class DocGen |
17
|
|
|
{ |
18
|
|
|
public $root; |
19
|
|
|
/** |
20
|
|
|
* @var DocRecipe[] |
21
|
|
|
*/ |
22
|
|
|
public $recipes = []; |
23
|
|
|
|
24
|
|
|
public function __construct(string $root) |
25
|
|
|
{ |
26
|
|
|
$this->root = realpath($root); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function parse(string $source) |
30
|
|
|
{ |
31
|
|
|
$directory = new RecursiveDirectoryIterator($source); |
32
|
|
|
$iterator = new RegexIterator(new RecursiveIteratorIterator($directory), '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH); |
33
|
|
|
foreach ($iterator as [$path]) { |
34
|
|
|
$recipePath = str_replace($this->root . '/', '', realpath($path)); |
35
|
|
|
$recipeName = preg_replace('/\.php$/i', '', basename($recipePath)); |
36
|
|
|
$recipe = new DocRecipe($recipeName, $recipePath); |
37
|
|
|
$recipe->parse(file_get_contents($path)); |
38
|
|
|
$this->recipes[$recipePath] = $recipe; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function gen(string $destination) |
43
|
|
|
{ |
44
|
|
|
foreach ($this->recipes as $recipe) { |
45
|
|
|
// $find will try to return DocConfig for a given config $name. |
46
|
|
|
$findConfig = function (string $name) use ($recipe): ?DocConfig { |
47
|
|
|
if (array_key_exists($name, $recipe->config)) { |
48
|
|
|
return $recipe->config[$name]; |
49
|
|
|
} |
50
|
|
|
foreach ($recipe->require as $r) { |
51
|
|
|
if (array_key_exists($r, $this->recipes)) { |
52
|
|
|
if (array_key_exists($name, $this->recipes[$r]->config)) { |
53
|
|
|
return $this->recipes[$r]->config[$name]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
foreach ($this->recipes as $r) { |
58
|
|
|
if (array_key_exists($name, $r->config)) { |
59
|
|
|
return $r->config[$name]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
return null; |
63
|
|
|
}; |
64
|
|
|
$findConfigOverride = function (DocRecipe $recipe, string $name) use (&$findConfigOverride): ?DocConfig { |
65
|
|
|
foreach ($recipe->require as $r) { |
66
|
|
|
if (array_key_exists($r, $this->recipes)) { |
67
|
|
|
if (array_key_exists($name, $this->recipes[$r]->config)) { |
68
|
|
|
return $this->recipes[$r]->config[$name]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
foreach ($recipe->require as $r) { |
73
|
|
|
if (array_key_exists($r, $this->recipes)) { |
74
|
|
|
return $findConfigOverride($this->recipes[$r], $name); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return null; |
78
|
|
|
}; |
79
|
|
|
// Replace all {{name}} with link to correct config declaration. |
80
|
|
|
$replaceLinks = function (string $comment) use ($findConfig): string { |
81
|
|
|
return preg_replace_callback('#(\{\{(?<name>[\w_:]+)\}\})#', function ($m) use ($findConfig) { |
82
|
|
|
$name = $m['name']; |
83
|
|
|
$config = $findConfig($name); |
84
|
|
|
if ($config !== null) { |
85
|
|
|
$md = php_to_md($config->recipePath); |
86
|
|
|
$anchor = anchor($name); |
87
|
|
|
return "[$name](/docs/$md#$anchor)"; |
88
|
|
|
} |
89
|
|
|
return "{{" . $name . "}}"; |
90
|
|
|
}, $comment); |
91
|
|
|
}; |
92
|
|
|
$findTask = function (string $name) use ($recipe): ?DocTask { |
93
|
|
|
if (array_key_exists($name, $recipe->tasks)) { |
94
|
|
|
return $recipe->tasks[$name]; |
95
|
|
|
} |
96
|
|
|
foreach ($recipe->require as $r) { |
97
|
|
|
if (array_key_exists($r, $this->recipes)) { |
98
|
|
|
if (array_key_exists($name, $this->recipes[$r]->tasks)) { |
99
|
|
|
return $this->recipes[$r]->tasks[$name]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
foreach ($this->recipes as $r) { |
104
|
|
|
if (array_key_exists($name, $r->tasks)) { |
105
|
|
|
return $r->tasks[$name]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return null; |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
$filePath = "$destination/" . php_to_md($recipe->recipePath); |
113
|
|
|
|
114
|
|
|
$toc = ''; |
115
|
|
|
$config = ''; |
116
|
|
|
$tasks = ''; |
117
|
|
|
if (count($recipe->require) > 0) { |
118
|
|
|
$toc .= "* Require\n"; |
119
|
|
|
foreach ($recipe->require as $r) { |
120
|
|
|
$md = php_to_md($r); |
121
|
|
|
$toc .= " * [`{$r}`](/docs/{$md})\n"; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
if (count($recipe->config) > 0) { |
125
|
|
|
$toc .= "* Config\n"; |
126
|
|
|
$config .= "## Config\n"; |
127
|
|
|
foreach ($recipe->config as $c) { |
128
|
|
|
$anchor = anchor($c->name); |
129
|
|
|
$toc .= " * [`{$c->name}`](#{$anchor})\n"; |
130
|
|
|
$config .= "### {$c->name}\n"; |
131
|
|
|
$config .= "[Source](/{$c->recipePath}#L{$c->lineNumber})\n\n"; |
132
|
|
|
$o = $findConfigOverride($recipe, $c->name); |
133
|
|
|
if ($o !== null) { |
134
|
|
|
$md = php_to_md($o->recipePath); |
135
|
|
|
$anchor = anchor($c->name); |
136
|
|
|
$config .= "* Overrides [`{$c->name}`](/docs/$md#$anchor) from `$o->recipePath`\n\n"; |
137
|
|
|
} |
138
|
|
|
$config .= $replaceLinks($c->comment); |
139
|
|
|
$config .= "\n\n"; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
if (count($recipe->tasks) > 0) { |
143
|
|
|
$toc .= "* Tasks\n"; |
144
|
|
|
$tasks .= "## Tasks\n"; |
145
|
|
|
foreach ($recipe->tasks as $t) { |
146
|
|
|
$anchor = anchor($t->name); |
147
|
|
|
$desc = ""; |
148
|
|
|
if ($t->desc !== "") { |
149
|
|
|
$desc = " — {$t->desc}"; |
150
|
|
|
} |
151
|
|
|
$toc .= " * [`{$t->name}`](#{$anchor}){$desc}\n"; |
152
|
|
|
$tasks .= "### {$t->name}\n"; |
153
|
|
|
$tasks .= "[Source](/{$t->recipePath}#L{$t->lineNumber})\n\n"; |
154
|
|
|
$tasks .= $replaceLinks($t->comment); |
155
|
|
|
if (is_array($t->group)) { |
156
|
|
|
$tasks .= "\n\n"; |
157
|
|
|
$tasks .= "This task is group task which contains next tasks:\n"; |
158
|
|
|
foreach ($t->group as $taskName) { |
159
|
|
|
$t = $findTask($taskName); |
160
|
|
|
if ($t !== null) { |
161
|
|
|
$md = php_to_md($t->recipePath); |
162
|
|
|
$anchor = anchor($t->name); |
163
|
|
|
$tasks .= "* [`$taskName`](/docs/$md#$anchor)\n"; |
164
|
|
|
} else { |
165
|
|
|
$tasks .= "* `$taskName`\n"; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
$tasks .= "\n\n"; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$output = <<<MD |
174
|
|
|
<!-- DO NOT EDIT THIS FILE! --> |
175
|
|
|
<!-- Instead edit {$recipe->recipePath} --> |
176
|
|
|
<!-- Then run bin/docgen --> |
177
|
|
|
|
178
|
|
|
# {$recipe->recipeName} |
179
|
|
|
|
180
|
|
|
[Source](/{$recipe->recipePath}) |
181
|
|
|
|
182
|
|
|
{$recipe->comment} |
183
|
|
|
|
184
|
|
|
{$toc} |
185
|
|
|
{$config} |
186
|
|
|
{$tasks} |
187
|
|
|
MD; |
188
|
|
|
|
189
|
|
|
if (!file_exists(dirname($filePath))) { |
190
|
|
|
mkdir(dirname($filePath), 0755, true); |
191
|
|
|
} |
192
|
|
|
file_put_contents($filePath, $output); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
function trimComment($line) |
198
|
|
|
{ |
199
|
|
|
return preg_replace('#^(/\*\*?\s?|\s\*\s?|//\s?)#', '', $line); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
function indent($text) |
203
|
|
|
{ |
204
|
|
|
return implode("\n", array_map(function ($line) { |
205
|
|
|
return " " . $line; |
206
|
|
|
}, explode("\n", $text))); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
function php_to_md($file) |
210
|
|
|
{ |
211
|
|
|
return preg_replace('#\.php$#', '.md', $file); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
function anchor($s) |
215
|
|
|
{ |
216
|
|
|
return str_replace(':', '', $s); |
217
|
|
|
} |
218
|
|
|
|