-
Notifications
You must be signed in to change notification settings - Fork 304
/
Changelog.php
184 lines (165 loc) · 3.7 KB
/
Changelog.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
<?php
namespace Robo\Task\Development;
use Robo\Task\BaseTask;
use Robo\Result;
use Robo\Contract\BuilderAwareInterface;
use Robo\Common\BuilderAwareTrait;
/**
* Helps to manage changelog file.
* Creates or updates `changelog.md` file with recent changes in current version.
*
* ``` php
* <?php
* $version = "0.1.0";
* $this->taskChangelog()
* ->version($version)
* ->change("released to github")
* ->run();
* ?>
* ```
*
* Changes can be asked from Console
*
* ``` php
* <?php
* $this->taskChangelog()
* ->version($version)
* ->askForChanges()
* ->run();
* ?>
* ```
*/
class Changelog extends BaseTask implements BuilderAwareInterface
{
use BuilderAwareTrait;
/**
* @var string
*/
protected $filename;
/**
* @var array
*/
protected $log = [];
/**
* @var string
*/
protected $anchor = "# Changelog";
/**
* @var string
*/
protected $version = "";
/**
* @param string $filename
*
* @return $this
*/
public function filename($filename)
{
$this->filename = $filename;
return $this;
}
/**
* @param string $item
*
* @return $this
*/
public function log($item)
{
$this->log[] = $item;
return $this;
}
/**
* @param string $anchor
*
* @return $this
*/
public function anchor($anchor)
{
$this->anchor = $anchor;
return $this;
}
/**
* @param string $version
*
* @return $this
*/
public function version($version)
{
$this->version = $version;
return $this;
}
/**
* @param string $filename
*/
public function __construct($filename)
{
$this->filename = $filename;
}
/**
* @param array $data
*
* @return $this
*/
public function changes(array $data)
{
$this->log = array_merge($this->log, $data);
return $this;
}
/**
* @param string $change
*
* @return $this
*/
public function change($change)
{
$this->log[] = $change;
return $this;
}
/**
* @return array
*/
public function getChanges()
{
return $this->log;
}
/**
* {@inheritdoc}
*/
public function run()
{
if (empty($this->log)) {
return Result::error($this, "Changelog is empty");
}
$text = implode(
"\n",
array_map(
function ($i) {
return "* $i *" . date('Y-m-d') . "*";
},
$this->log
)
) . "\n";
$ver = "#### {$this->version}\n\n";
$text = $ver . $text;
if (!file_exists($this->filename)) {
$this->printTaskInfo('Creating {filename}', ['filename' => $this->filename]);
$res = file_put_contents($this->filename, $this->anchor);
if ($res === false) {
return Result::error($this, "File {filename} cant be created", ['filename' => $this->filename]);
}
}
/** @var \Robo\Result $result */
// trying to append to changelog for today
$result = $this->collectionBuilder()->taskReplaceInFile($this->filename)
->from($ver)
->to($text)
->run();
if (!isset($result['replaced']) || !$result['replaced']) {
$result = $this->collectionBuilder()->taskReplaceInFile($this->filename)
->from($this->anchor)
->to($this->anchor . "\n\n" . $text)
->run();
}
return new Result($this, $result->getExitCode(), $result->getMessage(), $this->log);
}
}