1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Task runner, code generator and build tool for easier continuos integration |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hidev |
7
|
|
|
* @package hidev |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hidev\handlers; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Handler for commits.md file. |
18
|
|
|
*/ |
19
|
|
|
class CommitsHandler extends BaseHandler |
20
|
|
|
{ |
21
|
|
|
public $type = 'commits'; |
22
|
|
|
|
23
|
|
|
protected $_tag; |
24
|
|
|
protected $_note; |
25
|
|
|
protected $_hash; |
26
|
|
|
protected $_history = []; |
27
|
|
|
protected $_commits = []; |
28
|
|
|
|
29
|
|
|
public function getTag() |
30
|
|
|
{ |
31
|
|
|
return $this->_tag; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getNote() |
35
|
|
|
{ |
36
|
|
|
return $this->_note; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getHash() |
40
|
|
|
{ |
41
|
|
|
return $this->_hash; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setTag($tag) |
45
|
|
|
{ |
46
|
|
|
$this->_tag = $tag; |
47
|
|
|
$this->_note = ''; |
48
|
|
|
$this->_hash = ''; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setNote($note) |
52
|
|
|
{ |
53
|
|
|
$this->_note = $note; |
54
|
|
|
$this->_hash = ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setHash($hash) |
58
|
|
|
{ |
59
|
|
|
$this->_hash = $hash; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addTag($tag, $label = null) |
63
|
|
|
{ |
64
|
|
|
$this->tag = $tag; |
|
|
|
|
65
|
|
|
$ref = &$this->_history[$tag]['tag']; |
66
|
|
|
$ref = $label ?: $ref ?: $tag; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function addNote($note, $label = null) |
70
|
|
|
{ |
71
|
|
|
$this->note = $note; |
|
|
|
|
72
|
|
|
$ref = &$this->_history[$this->tag][$note]['note']; |
|
|
|
|
73
|
|
|
$ref = $label ?: $ref ?: $note; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function addHash($hash, $label) |
77
|
|
|
{ |
78
|
|
|
$this->_hash = $hash; |
79
|
|
|
$this->_commits[(string) $hash] = $label; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function hasCommit($hash) |
83
|
|
|
{ |
84
|
|
|
return array_key_exists((string) $hash, $this->_commits); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function parsePath($path, $minimal = null) |
88
|
|
|
{ |
89
|
|
|
$this->tag = static::getVcs()->lastTag; |
|
|
|
|
90
|
|
|
$this->_history = [ |
91
|
|
|
$this->tag => [], |
|
|
|
|
92
|
|
|
]; |
93
|
|
|
$lines = is_file($path) ? $this->readArray($path) : []; |
94
|
|
|
foreach ($lines as $str) { |
|
|
|
|
95
|
|
|
$str = rtrim($str); |
96
|
|
|
++$no; |
|
|
|
|
97
|
|
|
if (!$str || $no < 3) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
if (preg_match('/^# /', $str)) { |
101
|
|
|
continue; |
102
|
|
|
}; |
103
|
|
|
if (preg_match('/^## (.*)$/', $str, $m)) { |
104
|
|
|
$label = $m[1]; |
105
|
|
|
foreach ([static::getVcs()->lastTag, static::getVcs()->initTag] as $z) { |
106
|
|
|
if (stripos($label, $z) !== false) { |
107
|
|
|
$this->addTag($z, $label); |
108
|
|
|
continue 2; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
preg_match('/^(\S+)\s*(.*)$/', $label, $m); |
112
|
|
|
$this->addTag($m[1], $label); |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
if (preg_match('/^- (.*)$/', $str, $m)) { |
116
|
|
|
$this->addNote($m[1]); |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
if (preg_match('/^\s+- ([0-9a-fA-F]{7})/', $str, $m)) { |
120
|
|
|
$this->addHash($m[1], $str); |
121
|
|
|
} |
122
|
|
|
$this->_history[$this->tag][$this->note][$this->hash][] = $str; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->_history; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function addHistory($commit, $front = false) |
129
|
|
|
{ |
130
|
|
|
$tag = $commit['tag']; |
131
|
|
|
$note = $commit['note']; |
132
|
|
|
$hash = $commit['hash']; |
133
|
|
|
$render = static::renderCommit($commit); |
134
|
|
|
$hashes = &$this->_history[$tag][$note]; |
135
|
|
|
$hashes = (array) $hashes; |
136
|
|
|
if ($front) { |
137
|
|
|
$hashes = [$hash => [$render]] + $hashes; |
138
|
|
|
} else { |
139
|
|
|
$hashes[$hash][] = $render; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function hasHistory($tag) |
144
|
|
|
{ |
145
|
|
|
return array_key_exists($tag, $this->_history); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setHistory($value) |
149
|
|
|
{ |
150
|
|
|
$this->_history = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getHistory() |
154
|
|
|
{ |
155
|
|
|
return $this->_history; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function render($data) |
159
|
|
|
{ |
160
|
|
|
$res = static::renderHeader('commits history'); |
161
|
|
|
|
162
|
|
|
foreach (array_reverse(static::getVcs()->commits, true) as $hash => $commit) { |
163
|
|
|
if ($this->hasCommit($hash)) { |
164
|
|
|
continue; |
165
|
|
|
} |
166
|
|
|
$this->addHistory($commit, true); |
167
|
|
|
} |
168
|
|
|
if (!$this->hasHistory(static::getVcs()->initTag)) { |
169
|
|
|
$this->addHistory(['tag' => static::getVcs()->initTag]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/// TODO |
173
|
|
|
/// $this->cleanupHistory(); |
|
|
|
|
174
|
|
|
|
175
|
|
|
foreach ($this->_history as $tag => $notes) { |
176
|
|
|
$prev = $res; |
177
|
|
|
$tag = static::arrayPop($notes, 'tag') ?: $tag; |
178
|
|
|
$new = static::arrayPop($notes, '') ?: []; |
179
|
|
|
$res .= static::renderTag($tag); |
180
|
|
|
$save = $res; |
181
|
|
|
foreach ($new as $hash => $lines) { |
182
|
|
|
$res .= static::renderLines($lines); |
183
|
|
|
} |
184
|
|
|
foreach ($notes as $note => $cs) { |
185
|
|
|
$note = static::arrayPop($cs, 'note'); |
186
|
|
|
$res .= static::renderNote($note); |
187
|
|
|
foreach ($cs as $hash => $lines) { |
188
|
|
|
$res .= static::renderLines($lines); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
/// TODO redo with cleanupHistory |
192
|
|
|
/// skip empty Under development section |
193
|
|
|
if ($save === $res && stripos($tag, static::getVcs()->lastTag) !== false) { |
194
|
|
|
$res = $prev; |
195
|
|
|
unset($this->_history[$tag]); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $res; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public static function arrayPop(&$array, $key) |
203
|
|
|
{ |
204
|
|
|
$res = $array[$key]; |
205
|
|
|
unset($array[$key]); |
206
|
|
|
|
207
|
|
|
return $res; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public static function renderNote($note) |
211
|
|
|
{ |
212
|
|
|
return "- $note\n"; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public static function renderLines(array $lines) |
216
|
|
|
{ |
217
|
|
|
$res = implode("\n", array_filter($lines)); |
218
|
|
|
|
219
|
|
|
return $res ? $res . "\n" : ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public static function renderCommit($commit) |
223
|
|
|
{ |
224
|
|
|
return static::skipCommit($commit) ? '' : " - $commit[hash] $commit[date] $commit[comment] ($commit[email])"; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public static function skipCommit($commit) |
228
|
|
|
{ |
229
|
|
|
$comment = $commit['comment']; |
230
|
|
|
|
231
|
|
|
static $equals = [ |
232
|
|
|
'' => 1, |
233
|
|
|
'minor' => 1, |
234
|
|
|
]; |
235
|
|
|
if ($equals[$comment]) { |
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
static $starts = [ |
240
|
|
|
'version bump', |
241
|
|
|
'bumped version', |
242
|
|
|
"merge branch 'master'", |
243
|
|
|
]; |
244
|
|
|
foreach ($starts as $start) { |
245
|
|
|
if (strtolower(substr($comment, 0, strlen($start))) === $start) { |
246
|
|
|
return true; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return false; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public static function renderTag($tag) |
254
|
|
|
{ |
255
|
|
|
if (strpos($tag, ' ') === false || $tag === static::getVcs()->initTag) { |
256
|
|
|
$tag .= static::renderDate(static::getVcs()->tags[$tag]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return "\n## $tag\n\n"; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public static function renderDate($date) |
263
|
|
|
{ |
264
|
|
|
return $date ? date(' Y-m-d', strtotime($date)) : ''; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public static function renderHeader($string) |
268
|
|
|
{ |
269
|
|
|
$header = Yii::$app->config->package->fullName . ' ' . $string; |
270
|
|
|
|
271
|
|
|
return $header . "\n" . str_repeat('-', mb_strlen($header, Yii::$app->charset)) . "\n"; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public static function getVcs() |
275
|
|
|
{ |
276
|
|
|
return Yii::$app->get('config')->getVcs(); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.