Skip to content

Commit 5834256

Browse files
committed
added changelog merge to release command
fixes #5975
1 parent 8a3d8c9 commit 5834256

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

Diff for: build/controllers/ReleaseController.php

+90-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace yii\build\controllers;
99

1010
use Yii;
11+
use yii\base\Exception;
1112
use yii\console\Controller;
1213

1314
/**
@@ -30,6 +31,8 @@ class ReleaseController extends Controller
3031
*/
3132
public function actionPrepare($version)
3233
{
34+
$this->resortChangelogs($version);
35+
$this->mergeChangelogs($version);
3336
$this->closeChangelogs($version);
3437
$this->composerSetStability($version);
3538
$this->updateYiiVersion($version);
@@ -77,9 +80,95 @@ protected function openChangelogs($version)
7780
}
7881
}
7982

83+
protected function resortChangelogs($version)
84+
{
85+
foreach($this->getChangelogs() as $file) {
86+
// split the file into relevant parts
87+
list($start, $changelog, $end) = $this->splitChangelog($file, $version);
88+
$changelog = $this->resortChangelog($changelog);
89+
file_put_contents($file, implode("\n", array_merge($start, $changelog, $end)));
90+
}
91+
}
92+
93+
protected function mergeChangelogs($version)
94+
{
95+
$file = $this->getFrameworkChangelog();
96+
// split the file into relevant parts
97+
list($start, $changelog, $end) = $this->splitChangelog($file, $version);
98+
99+
$changelog = $this->resortChangelog($changelog);
100+
101+
$changelog[] = '';
102+
$extensions = $this->getExtensionChangelogs();
103+
asort($extensions);
104+
foreach($extensions as $changelogFile) {
105+
if (!preg_match('~extensions/([a-z]+)/CHANGELOG\\.md~', $changelogFile, $m)) {
106+
throw new Exception("Illegal extension changelog file: " . $changelogFile);
107+
}
108+
list( , $extensionChangelog, ) = $this->splitChangelog($changelogFile, $version);
109+
$name = $m[1];
110+
$ucname = ucfirst($name);
111+
$changelog[] = "### $ucname Extension (yii2-$name)";
112+
$changelog = array_merge($changelog, $extensionChangelog);
113+
}
114+
115+
file_put_contents($file, implode("\n", array_merge($start, $changelog, $end)));
116+
}
117+
118+
/**
119+
* Extract changelog content for a specific version
120+
*/
121+
protected function splitChangelog($file, $version)
122+
{
123+
$lines = explode("\n", file_get_contents($file));
124+
125+
// split the file into relevant parts
126+
$start = [];
127+
$changelog = [];
128+
$end = [];
129+
130+
$state = 'start';
131+
foreach($lines as $l => $line) {
132+
// starting from the changelogs headline
133+
if (isset($lines[$l-2]) && strpos($lines[$l-2], $version) !== false &&
134+
isset($lines[$l-1]) && strncmp($lines[$l-1], '---', 3) === 0) {
135+
$state = 'changelog';
136+
}
137+
if ($state === 'changelog' && isset($lines[$l+1]) && strncmp($lines[$l+1], '---', 3) === 0) {
138+
$state = 'end';
139+
}
140+
${$state}[] = $line;
141+
}
142+
return [$start, $changelog, $end];
143+
}
144+
145+
/**
146+
* Ensure sorting of the changelog lines
147+
*/
148+
protected function resortChangelog($changelog)
149+
{
150+
// cleanup whitespace
151+
foreach($changelog as $i => $line) {
152+
$changelog[$i] = rtrim($line);
153+
}
154+
155+
// TODO sorting
156+
return $changelog;
157+
}
158+
80159
protected function getChangelogs()
81160
{
82-
return array_merge([YII2_PATH . '/CHANGELOG.md'], glob(dirname(YII2_PATH) . '/extensions/*/CHANGELOG.md'));
161+
return array_merge([$this->getFrameworkChangelog()], $this->getExtensionChangelogs());
162+
}
163+
164+
protected function getFrameworkChangelog()
165+
{
166+
return YII2_PATH . '/CHANGELOG.md';
167+
}
168+
169+
protected function getExtensionChangelogs()
170+
{
171+
return glob(dirname(YII2_PATH) . '/extensions/*/CHANGELOG.md');
83172
}
84173

85174
protected function composerSetStability($version)

0 commit comments

Comments
 (0)