2 namespace Robo\Task\Development;
4 use Robo\Task\BaseTask;
6 use Robo\Contract\BuilderAwareInterface;
7 use Robo\Common\BuilderAwareTrait;
10 * Helps to manage changelog file.
11 * Creates or updates `changelog.md` file with recent changes in current version.
16 * $this->taskChangelog()
18 * ->change("released to github")
23 * Changes can be asked from Console
27 * $this->taskChangelog()
34 class Changelog extends BaseTask implements BuilderAwareInterface
36 use BuilderAwareTrait;
51 protected $anchor = "# Changelog";
56 protected $version = "";
66 protected $header = "";
69 * @param string $filename
73 public function filename($filename)
75 $this->filename = $filename;
80 * Sets the changelog body text.
82 * This method permits the raw changelog text to be set directly If this is set, $this->log changes will be ignored.
88 public function setBody($body)
95 * @param string $header
99 public function setHeader($header)
101 $this->header = $header;
106 * @param string $item
110 public function log($item)
112 $this->log[] = $item;
117 * @param string $anchor
121 public function anchor($anchor)
123 $this->anchor = $anchor;
128 * @param string $version
132 public function version($version)
134 $this->version = $version;
139 * @param string $filename
141 public function __construct($filename)
143 $this->filename = $filename;
151 public function changes(array $data)
153 $this->log = array_merge($this->log, $data);
158 * @param string $change
162 public function change($change)
164 $this->log[] = $change;
171 public function getChanges()
179 public function run()
181 if (empty($this->body)) {
182 if (empty($this->log)) {
183 return Result::error($this, "Changelog is empty");
185 $this->body = $this->generateBody();
187 if (empty($this->header)) {
188 $this->header = $this->generateHeader();
191 $text = $this->header . $this->body;
193 if (!file_exists($this->filename)) {
194 $this->printTaskInfo('Creating {filename}', ['filename' => $this->filename]);
195 $res = file_put_contents($this->filename, $this->anchor);
196 if ($res === false) {
197 return Result::error($this, "File {filename} cant be created", ['filename' => $this->filename]);
201 /** @var \Robo\Result $result */
202 // trying to append to changelog for today
203 $result = $this->collectionBuilder()->taskReplaceInFile($this->filename)
204 ->from($this->header)
208 if (!isset($result['replaced']) || !$result['replaced']) {
209 $result = $this->collectionBuilder()->taskReplaceInFile($this->filename)
210 ->from($this->anchor)
211 ->to($this->anchor . "\n\n" . $text)
215 return new Result($this, $result->getExitCode(), $result->getMessage(), $this->log);
219 * @return \Robo\Result|string
221 protected function generateBody()
223 $text = implode("\n", array_map([$this, 'processLogRow'], $this->log));
232 protected function generateHeader()
234 return "#### {$this->version}\n\n";
242 public function processLogRow($i)
244 return "* $i *" . date('Y-m-d') . "*";