|
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\Support\Changelog; |
|
9
|
|
|
|
|
10
|
|
|
class Parser |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private $tokens; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private $span; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
private $lineNumber = 0; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
private $strict; |
|
31
|
|
|
|
|
32
|
1 |
|
public function __construct(string $changelog, bool $strict = true) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->tokens = array_map('trim', explode("\n", $changelog)); |
|
35
|
1 |
|
$this->strict = $strict; |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
private function current(): string |
|
39
|
|
|
{ |
|
40
|
1 |
|
if (count($this->tokens) === 0) { |
|
41
|
1 |
|
return ''; |
|
42
|
|
|
} |
|
43
|
1 |
|
return $this->tokens[0]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
private function next(): string |
|
47
|
|
|
{ |
|
48
|
1 |
|
if (count($this->tokens) === 0) { |
|
49
|
|
|
throw $this->error('Unexpected end of file'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$n = ++$this->lineNumber; |
|
53
|
1 |
|
$line = array_shift($this->tokens); |
|
54
|
|
|
|
|
55
|
1 |
|
$this->span[] = " {$n}: $line"; |
|
56
|
|
|
|
|
57
|
1 |
|
if (count($this->span) > 4) { |
|
58
|
1 |
|
array_shift($this->span); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return $line; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
private function acceptEmptyLine() |
|
65
|
|
|
{ |
|
66
|
1 |
|
if ($this->strict) { |
|
67
|
|
|
if ('' !== $this->next()) { |
|
68
|
|
|
throw $this->error('Expected an empty line'); |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
1 |
|
while (preg_match('/^\s*$/', $this->current()) && count($this->tokens) > 0) { |
|
72
|
1 |
|
$this->next(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
private function acceptEof() |
|
78
|
|
|
{ |
|
79
|
1 |
|
if (count($this->tokens) !== 0) { |
|
80
|
|
|
$this->next(); |
|
81
|
|
|
throw $this->error('Expected EOF'); |
|
82
|
|
|
} |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
private function matchVersion($line, &$m = null) |
|
86
|
|
|
{ |
|
87
|
1 |
|
return preg_match('/^\#\# \s ( v\d+\.\d+\.\d+(-[\w\.]+)? | master )$/x', $line, $m); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function error($message): ParseException |
|
91
|
|
|
{ |
|
92
|
|
|
$c = count($this->span) - 1; |
|
93
|
|
|
$this->span[$c] = preg_replace('/^\s{4}/', ' -> ', $this->span[$c]); |
|
94
|
|
|
|
|
95
|
|
|
if (count($this->tokens) > 0) { |
|
96
|
|
|
$this->next(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return new ParseException($message, implode("\n", $this->span)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function parse(): Changelog |
|
103
|
|
|
{ |
|
104
|
1 |
|
$changelog = $this->parseTitle(); |
|
105
|
|
|
|
|
106
|
1 |
|
$this->acceptEmptyLine(); |
|
107
|
1 |
|
$this->acceptEmptyLine(); |
|
108
|
|
|
|
|
109
|
1 |
|
while ($this->matchVersion($this->current())) { |
|
110
|
1 |
|
$version = $this->parseVersion(); |
|
111
|
1 |
|
$changelog->addVersion($version); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$refs = $this->parseReferences(); |
|
115
|
1 |
|
$changelog->setReferences($refs); |
|
116
|
|
|
|
|
117
|
1 |
|
$this->acceptEmptyLine(); |
|
118
|
1 |
|
$this->acceptEof(); |
|
119
|
|
|
|
|
120
|
1 |
|
return $changelog; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
private function parseTitle(): Changelog |
|
124
|
|
|
{ |
|
125
|
1 |
|
if (preg_match('/# (.+)/', $this->next(), $m)) { |
|
126
|
1 |
|
$c = new Changelog(); |
|
127
|
1 |
|
$c->setTitle($m[1]); |
|
128
|
1 |
|
return $c; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
throw $this->error('Expected title'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
private function parseVersion(): Version |
|
135
|
|
|
{ |
|
136
|
1 |
|
if ($this->matchVersion($this->next(), $m)) { |
|
137
|
1 |
|
$version = new Version(); |
|
138
|
1 |
|
$version->setVersion($curr = $m[1]); |
|
139
|
|
|
|
|
140
|
1 |
|
$compareLink = $this->next(); |
|
141
|
1 |
|
if (!preg_match('/^\[/', $compareLink)) { |
|
142
|
|
|
throw $this->error('Expected link to compare page with previous version'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
$prev = 'v\d+\.\d+\.\d+(-[\d\w\.]+)?'; |
|
146
|
|
|
$regexp = "/ |
|
147
|
1 |
|
^ \[($prev)\.\.\.$curr\] |
|
148
|
1 |
|
\(https\:\/\/github\.com\/deployphp\/deployer\/compare\/$prev\.\.\.$curr\) $ |
|
149
|
|
|
/x"; |
|
150
|
|
|
|
|
151
|
1 |
|
if (preg_match($regexp, $compareLink, $m)) { |
|
152
|
1 |
|
$version->setPrevious($m[1]); |
|
153
|
|
|
} else { |
|
154
|
|
|
throw $this->error('Error in compare link syntax'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
$this->acceptEmptyLine(); |
|
158
|
|
|
|
|
159
|
1 |
|
$sections = ['Added', 'Changed', 'Fixed', 'Removed']; |
|
160
|
1 |
|
$sectionsCount = count($sections); |
|
161
|
|
|
|
|
162
|
1 |
|
for ($i = 0; $i < $sectionsCount; $i++) { |
|
163
|
1 |
|
foreach ($sections as $key => $section) { |
|
164
|
1 |
|
if (preg_match('/^\#\#\# \s ' . $section . ' $/x', $this->current())) { |
|
165
|
1 |
|
$this->next(); |
|
166
|
|
|
|
|
167
|
1 |
|
$version->{"set$section"}($this->parseItems()); |
|
168
|
1 |
|
unset($sections[$key]); |
|
169
|
|
|
|
|
170
|
1 |
|
$this->acceptEmptyLine(); |
|
171
|
|
|
|
|
172
|
1 |
|
break; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
$this->acceptEmptyLine(); |
|
178
|
|
|
|
|
179
|
1 |
|
return $version; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
throw $this->error('Expected version'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
private function parseItems(): array |
|
186
|
|
|
{ |
|
187
|
1 |
|
$items = []; |
|
188
|
1 |
|
while (preg_match('/^\- (.+) $/x', $this->current(), $m)) { |
|
189
|
1 |
|
$this->next(); |
|
190
|
|
|
|
|
191
|
1 |
|
$item = new Item(); |
|
192
|
1 |
|
$message = $m[1]; |
|
193
|
1 |
|
$ref = '/\[ \#(\d+) \]/x'; |
|
194
|
|
|
|
|
195
|
1 |
|
preg_match_all($ref, $message, $matches); |
|
196
|
1 |
|
foreach ($matches[1] as $m) { |
|
197
|
1 |
|
$item->addReference($m); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
$message = trim(preg_replace($ref, '', $message)); |
|
201
|
1 |
|
$item->setMessage($message); |
|
202
|
1 |
|
$items[] = $item; |
|
203
|
|
|
} |
|
204
|
1 |
|
return $items; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
private function parseReferences(): array |
|
208
|
|
|
{ |
|
209
|
1 |
|
$refs = []; |
|
210
|
1 |
|
while (preg_match('/^\[/', $this->current())) { |
|
211
|
1 |
|
if (preg_match('/^ \[\#(\d+)\]\: \s (https\:\/\/github\.com\/deployphp\/deployer\/(issues|pull)\/\d+)$/x', $this->next(), $m)) { |
|
212
|
1 |
|
$refs[$m[1]] = $m[2]; |
|
213
|
|
|
} else { |
|
214
|
|
|
throw $this->error('Error parsing reference'); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
1 |
|
return $refs; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|