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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\build\controllers;
use Yii;
use yii\base\Exception;
use yii\console\Controller;
/**
* ReleaseController is there to help preparing releases
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ReleaseController extends Controller
{
public $defaultAction = 'help';
/**
* Usage:
*
* ```
* ./build/build release/prepare 2.0.0-beta
* ```
*
*/
public function actionPrepare($version)
{
$this->resortChangelogs($version);
$this->mergeChangelogs($version);
$this->closeChangelogs($version);
$this->composerSetStability($version);
$this->updateYiiVersion($version);
}
/**
* Usage:
*
* ```
* ./build/build release/done 2.0.0-dev 2.0.0-rc
* ```
*/
public function actionDone($devVersion, $nextVersion)
{
$this->openChangelogs($nextVersion);
$this->composerSetStability('dev');
$this->updateYiiVersion($devVersion);
}
protected function closeChangelogs($version)
{
$v = str_replace('\\-', '[\\- ]', preg_quote($version, '/'));
$headline = $version . ' ' . date('F d, Y');
$this->sed(
'/'.$v.' under development\n(-+?)\n/',
$headline . "\n" . str_repeat('-', strlen($headline)) . "\n",
$this->getChangelogs()
);
}
protected function openChangelogs($version)
{
$headline = "\n$version under development\n";
$headline .= str_repeat('-', strlen($headline) - 2) . "\n\n";
$headline .= "- no changes in this release.\n";
foreach($this->getChangelogs() as $file) {
$lines = explode("\n", file_get_contents($file));
$hl = [
array_shift($lines),
array_shift($lines),
];
array_unshift($lines, $headline);
file_put_contents($file, implode("\n", array_merge($hl, $lines)));
}
}
protected function resortChangelogs($version)
{
foreach($this->getChangelogs() as $file) {
// split the file into relevant parts
list($start, $changelog, $end) = $this->splitChangelog($file, $version);
$changelog = $this->resortChangelog($changelog);
file_put_contents($file, implode("\n", array_merge($start, $changelog, $end)));
}
}
protected function mergeChangelogs($version)
{
$file = $this->getFrameworkChangelog();
// split the file into relevant parts
list($start, $changelog, $end) = $this->splitChangelog($file, $version);
$changelog = $this->resortChangelog($changelog);
$changelog[] = '';
$extensions = $this->getExtensionChangelogs();
asort($extensions);
foreach($extensions as $changelogFile) {
if (!preg_match('~extensions/([a-z]+)/CHANGELOG\\.md~', $changelogFile, $m)) {
throw new Exception("Illegal extension changelog file: " . $changelogFile);
}
list( , $extensionChangelog, ) = $this->splitChangelog($changelogFile, $version);
$name = $m[1];
$ucname = ucfirst($name);
$changelog[] = "### $ucname Extension (yii2-$name)";
$changelog = array_merge($changelog, $extensionChangelog);
}
file_put_contents($file, implode("\n", array_merge($start, $changelog, $end)));
}
/**
* Extract changelog content for a specific version
*/
protected function splitChangelog($file, $version)
{
$lines = explode("\n", file_get_contents($file));
// split the file into relevant parts
$start = [];
$changelog = [];
$end = [];
$state = 'start';
foreach($lines as $l => $line) {
// starting from the changelogs headline
if (isset($lines[$l-2]) && strpos($lines[$l-2], $version) !== false &&
isset($lines[$l-1]) && strncmp($lines[$l-1], '---', 3) === 0) {
$state = 'changelog';
}
if ($state === 'changelog' && isset($lines[$l+1]) && strncmp($lines[$l+1], '---', 3) === 0) {
$state = 'end';
}
${$state}[] = $line;
}
return [$start, $changelog, $end];
}
/**
* Ensure sorting of the changelog lines
*/
protected function resortChangelog($changelog)
{
// cleanup whitespace
foreach($changelog as $i => $line) {
$changelog[$i] = rtrim($line);
}
// TODO sorting
return $changelog;
}
protected function getChangelogs()
{
return array_merge([$this->getFrameworkChangelog()], $this->getExtensionChangelogs());
}
protected function getFrameworkChangelog()
{
return YII2_PATH . '/CHANGELOG.md';
}
protected function getExtensionChangelogs()
{
return glob(dirname(YII2_PATH) . '/extensions/*/CHANGELOG.md');
}
protected function composerSetStability($version)
{
$stability = 'stable';
if (strpos($version, 'alpha') !== false) {
$stability = 'alpha';
} elseif (strpos($version, 'beta') !== false) {
$stability = 'beta';
} elseif (strpos($version, 'rc') !== false) {
$stability = 'RC';
} elseif (strpos($version, 'dev') !== false) {
$stability = 'dev';
}
$this->sed(
'/"minimum-stability": "(.+?)",/',
'"minimum-stability": "' . $stability . '",',
[
dirname(YII2_PATH) . '/apps/advanced/composer.json',
dirname(YII2_PATH) . '/apps/basic/composer.json',
dirname(YII2_PATH) . '/apps/benchmark/composer.json',
]
);
}
protected function updateYiiVersion($version)
{
$this->sed(
'/function getVersion\(\)\n \{\n return \'(.+?)\';/',
"function getVersion()\n {\n return '$version';",
YII2_PATH . '/BaseYii.php');
}
protected function sed($pattern, $replace, $files)
{
foreach((array) $files as $file) {
file_put_contents($file, preg_replace($pattern, $replace, file_get_contents($file)));
}
}
}