forked from transilvlad/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.php
318 lines (265 loc) · 9.5 KB
/
notes.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env php
<?php
/**
* Config
*/
class Config {
/**
* App info
*/
public static $name = "Maven Release Notes";
public static $version = "1.0";
public static $author = "Vlad Marian";
public static $email = "[email protected]";
/**
* Options
* http://php.net/manual/en/function.getopt.php
*/
public static $short = [
// Built in
"h" => "Show help",
"v" => "Show version",
// Custom
"p:" => "Project directory path",
"l:" => "Logfile name",
];
public static $long = [
// Built in
"help" => "Show help",
// Custom
"list" => "List latest releases",
"show:" => "Show selected version changes",
"from:" => "Show changes from this version onwards",
];
/**
* List options that are always required (without colons)
*/
public static $required = [];
/**
* Get description
*/
public static function desc($param = "") {
$opts = strlen($param) == 1 ? Config::$short : Config::$long;
foreach([$param, $param . ":", $param . "::"] as $entry) {
if(array_key_exists($entry, $opts)) {
return $opts[$entry];
}
}
return "";
}
}
/**
* Display
*/
class Display {
/**
* Show about
*/
public static function about() {
echo Config::$name . " " . Config::$version . " by " . Config::$author . " <" . Config::$email . ">\r\n";
echo "\r\n";
}
/**
* Show help
*/
public static function help() {
$short = array_map(function($v) { return str_replace(":", "", $v); }, array_keys(Config::$short));
$long = array_map(function($v) { return str_replace(":", "", $v); }, array_keys(Config::$long));
Display::about();
echo "Usage: " . basename(__FILE__) . " ";
if(count($short) > 0) echo "[-" . implode("][-", $short) . "]";
if(count($long) > 0) echo "[--" . implode("][--", $long) . "]";
echo "\r\n";
echo "\r\n";
if(count(Config::$required) > 0) {
echo "Mandatory:\r\n";
foreach(Config::$required as $param) {
echo " " . (strlen($param) == 1 ? " -" : "--") . str_pad($param, 12) . Config::desc($param) . "\r\n";
}
echo "\r\n";
}
echo "Optional:\r\n";
foreach($short as $param) {
echo " " . (strlen($param) == 1 ? " -" : "--") . str_pad($param, 12) . Config::desc($param) . "\r\n";
}
foreach($long as $param) {
echo " " . (strlen($param) == 1 ? " -" : "--") . str_pad($param, 12) . Config::desc($param) . "\r\n";
}
echo "\r\n";
Display::stop();
}
/**
* Show version
*/
public static function version() {
Display::about();
Display::stop();
}
/**
* Show error message and exit
*/
public static function error($str = "") {
Display::about();
echo $str . "\r\n\r\n";
Display::stop();
}
/**
* Exit app
*/
public static function stop() {
exit(1);
}
}
/**
* Usage
*/
class Usage {
public static $opts;
public function __construct() {
Usage::$opts = getopt(implode(array_keys(Config::$short)), array_keys(Config::$long));
$this->checkBuiltIn();
$this->checkOptions();
}
/**
* Check built-in options called
*/
private function checkBuiltIn() {
if (in_array("h", array_keys(Usage::$opts)) || in_array("help", array_keys(Usage::$opts)) || empty(Usage::$opts)) Display::help();
if (in_array("v", array_keys(Usage::$opts))) Display::version();
}
/**
* Check required options present
*/
private function checkOptions() {
foreach(Config::$required as $param) {
if (!array_key_exists($param, Usage::$opts)) {
Display::error("Missing required argument: " . $param . " - " . Config::desc($param));
}
}
}
}
/**
* App
*/
class App {
private $project;
private $logfile;
private $tmpfile;
private $versions;
private $list;
private $show;
private $from;
public function __construct() {
$this->project = array_key_exists("p", Usage::$opts) ? Usage::$opts["p"] : "./";
$this->logfile = array_key_exists("l", Usage::$opts) ? Usage::$opts["l"] : str_replace(".php", ".log", basename(__FILE__));
$this->tmpfile = $tmp = tempnam("/tmp", "notes");
$this->list = array_key_exists("list", Usage::$opts);
$this->show = array_key_exists("show", Usage::$opts) ? Usage::$opts["show"] : "";
$this->from = array_key_exists("from", Usage::$opts) ? Usage::$opts["from"] : "";
file_put_contents($this->logfile, ""); // clear logfile
$this->versions();
if($this->list || ($this->project != null && count(Usage::$opts) == 1)) $this->list();
if(!empty($this->show)) $this->show($this->show, $this->from);
}
/**
* Versions
*/
private function versions() {
// execute git log grep for maven releases
shell_exec("cd " . $this->project . " && git log --pretty=format:\"[%h] %s\" -500 | grep \"maven-release-plugin\" | grep \"prepare release\" > " . $this->tmpfile);
// read git releases
$file = file($this->tmpfile);
// list of versions
$this->versions = [];
foreach($file as $k => $line) {
// parse line and extract hash/version
preg_match("/\[([a-z0-9]+)\]\s\[maven\-release\-plugin\].*?\(.*\)prepare\srelease\s(.*?)(\s|$)/", $line, $matches);
// record versions and hashes except last
if (count($matches) > 1 && $k < count($file)) {
$this->versions[] = [$matches[2], $matches[1]];
// stop if 10 reached
if (count($this->versions) == 11) break;
}
}
}
/**
* List
*/
private function list() {
$this->out("Versions:");
foreach($this->versions as $k => $array) {
if($k < 10) $this->out($array[0]);
}
$this->out("");
}
/**
* Show
*/
private function show($show = "", $from = "") {
// pick start/end hashes for selected version
$start = "";
$end = "";
foreach($this->versions as $k => $array) {
if (count($array) == 2 && $array[0] == $show) {
$end = $array[1];
if (empty($from)) {
$start = $this->versions[$k+1][1];
}
}
if (!empty($from)) {
if (count($array) == 2 && $array[0] == $from) {
$start = $this->versions[$k+1][1];
}
}
}
// validate
if(empty($start) || empty($end)) {
Display::error("Cannot find version: " . $show);
}
// execute git log grep for release commits
shell_exec("cd " . $this->project . " && git log --pretty=format:\"[%h] %s\" " . $start . ".." . $end . " | grep -v \"maven-release-plugin\" | grep -Ev \"Merge( remote-tracking)? branch \'\" > " . $this->tmpfile);
// read git commits
$file = file($this->tmpfile);
// separate
$tickets = [];
$changes = [];
foreach($file as $k => $line) {
// parse line and extract hash/version
preg_match("/\[([a-z0-9]+)\] (.*)/", $line, $matches);
// output comments
if (count($matches) > 1) {
if (preg_match('/([a-z]+-[0-9]+)/i', $matches[2], $ticket)) {
$tickets[] = $ticket[1];
} else {
$changes[] = $matches[2];
}
}
}
// tickets
if(count($tickets) > 0) $this->out("Tickets:");
$unique = array_unique($tickets);
sort($unique);
foreach($unique AS $line) {
$this->out($line);
}
// separate if both
if(count($tickets) > 0 && count($changes) > 0) $this->out("");
// changes
if(count($changes) > 0) $this->out("Changes:");
foreach($changes AS $line) {
$this->out($line);
}
$this->out("");
}
/**
* Print to screen and logfile
*/
private function out($str = "") {
echo $str . "\r\n";
file_put_contents($this->logfile, $str . "\r\n", FILE_APPEND);
}
}
// Run
new Usage();
new App();
exit(0);