8
8
namespace yii \build \controllers ;
9
9
10
10
use Yii ;
11
+ use yii \base \Exception ;
11
12
use yii \console \Controller ;
12
13
13
14
/**
@@ -30,6 +31,8 @@ class ReleaseController extends Controller
30
31
*/
31
32
public function actionPrepare ($ version )
32
33
{
34
+ $ this ->resortChangelogs ($ version );
35
+ $ this ->mergeChangelogs ($ version );
33
36
$ this ->closeChangelogs ($ version );
34
37
$ this ->composerSetStability ($ version );
35
38
$ this ->updateYiiVersion ($ version );
@@ -77,9 +80,95 @@ protected function openChangelogs($version)
77
80
}
78
81
}
79
82
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
+
80
159
protected function getChangelogs ()
81
160
{
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 ' );
83
172
}
84
173
85
174
protected function composerSetStability ($ version )
0 commit comments