-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_docs.php
executable file
·122 lines (110 loc) · 3.73 KB
/
copy_docs.php
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
#!/usr/bin/env php
<?php
/**
* Copy relevant documentation from Ar.php to ArFluent.php and README.md
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('./vendor/autoload.php');
use FriendlyPixel\Ar\Ar;
$arString = file_get_contents('./src/Ar.php');
$matches = [];
$re = '/(\/\*\*(?:.|\n)*\*\/)\s+public static function ([a-zA-Z]+)\(/U';
preg_match_all($re, $arString, $matches);
$madeChanges = false;
/* Update ArFluent.php */
$arFluent = file_get_contents('./src/ArFluent.php');
foreach ($matches[1] as $i => $comment) {
$funcName = $matches[2][$i];
$re = '/(\/\*\*(\n|[^*]|\*[^\/])*+\*\/\s+)?public function ' . $funcName . '\(/';
// dump($re);
$changedComment = $comment;
$changedComment = str_replace('@return mixed[]', '@return ArFluent', $changedComment);
$changedComment = str_replace('@return A[]', '@return ArFluent<A>', $changedComment);
$changedComment = str_replace('@return B[]', '@return ArFluent<B>', $changedComment);
$changedComment = str_replace('@return array<int, A>', '@return ArFluent<A>', $changedComment);
$changedComment = str_replace('@return array<K, A>', '@return ArFluent<A>', $changedComment);
$changedComment = str_replace('
* @template A', '', $changedComment);
$changedComment = str_replace('
* @param A[] $array', '', $changedComment);
$changedComment = str_replace('@return A[]', '@return ArFluent<A>', $changedComment);
$arFluent = preg_replace($re, $changedComment . "\n " . 'public function ' . $funcName . '(', $arFluent);
if (preg_last_error()) {
dump(array_flip(get_defined_constants(true)['pcre'])[preg_last_error()]);
}
// dump($arFluentString);
// dump('---------------');
// dump('');
}
if ($arFluent != file_get_contents('src/ArFluent.php')) {
$madeChanges = true;
file_put_contents('src/ArFluent.php', $arFluent);
}
/* Generate README.md */
$readme = '<!--
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!! Never modify this file, `README.md`
!!!!!!!!
!!!!!!!! Modify `README_template.md` instead then run `copy_docs.php`
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
!!!!!!!!
-->
';
$readme .= file_get_contents('./README.template.md');
$rows = Ar::wrap($matches[1])
->filter(function ($comment) {
return stripos($comment, '@deprecated') === false;
})
->map(function ($value, $i) use ($matches) {
return [
'comment' => $matches[1][$i],
'funcName' => $matches[2][$i],
];
})
->filter(function ($row) {
// Don't include `wrap` docs from Ar.php.
// It belongs to the fluent section of the readme.
return $row['funcName'] != 'wrap';
})
->sort(function ($a, $b) {
return strnatcmp($a['funcName'], $b['funcName']);
});
$toc = Ar::wrap($rows)
->map(function ($row) {
$funcName = $row['funcName'];
return "- [$funcName()](#$funcName)";
})
->implode("\n");
$readme = str_replace('<!-- METHOD_TOC_HERE -->', $toc, $readme);
$methodDocs = Ar::wrap($rows)
->map(function ($row) {
$comment = $row['comment'];
$comment = str_replace(" * @", " * \n * @", $comment);
$comment = str_replace("/**\n", '', $comment);
$comment = str_replace(' * ', '', $comment);
$comment = str_replace("\n */", '', $comment);
$funcName = $row['funcName'];
return "<a name=\"$funcName\"></a>\n### $funcName\n\n$comment\n\n\n";
})
->implode("\n");
$readme = str_replace('<!-- METHODS_HERE -->', $methodDocs, $readme);
if ($readme != file_get_contents('./README.md')) {
$madeChanges = true;
file_put_contents('./README.md', $readme);
}
if ($madeChanges) {
echo ('Updated docs' . PHP_EOL);
exit(1);
}