PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/build/controllers/ReleaseController.php

https://gitlab.com/brucealdridge/yii2
PHP | 211 lines | 148 code | 25 blank | 38 comment | 8 complexity | 9e1ba1f5d7316ecb3de62e7297d2b7f5 MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\build\controllers;
  8. use Yii;
  9. use yii\base\Exception;
  10. use yii\console\Controller;
  11. /**
  12. * ReleaseController is there to help preparing releases
  13. *
  14. * @author Carsten Brandt <mail@cebe.cc>
  15. * @since 2.0
  16. */
  17. class ReleaseController extends Controller
  18. {
  19. public $defaultAction = 'help';
  20. /**
  21. * Usage:
  22. *
  23. * ```
  24. * ./build/build release/prepare 2.0.0-beta
  25. * ```
  26. *
  27. */
  28. public function actionPrepare($version)
  29. {
  30. $this->resortChangelogs($version);
  31. $this->mergeChangelogs($version);
  32. $this->closeChangelogs($version);
  33. $this->composerSetStability($version);
  34. $this->updateYiiVersion($version);
  35. }
  36. /**
  37. * Usage:
  38. *
  39. * ```
  40. * ./build/build release/done 2.0.0-dev 2.0.0-rc
  41. * ```
  42. */
  43. public function actionDone($devVersion, $nextVersion)
  44. {
  45. $this->openChangelogs($nextVersion);
  46. $this->composerSetStability('dev');
  47. $this->updateYiiVersion($devVersion);
  48. }
  49. protected function closeChangelogs($version)
  50. {
  51. $v = str_replace('\\-', '[\\- ]', preg_quote($version, '/'));
  52. $headline = $version . ' ' . date('F d, Y');
  53. $this->sed(
  54. '/'.$v.' under development\n(-+?)\n/',
  55. $headline . "\n" . str_repeat('-', strlen($headline)) . "\n",
  56. $this->getChangelogs()
  57. );
  58. }
  59. protected function openChangelogs($version)
  60. {
  61. $headline = "\n$version under development\n";
  62. $headline .= str_repeat('-', strlen($headline) - 2) . "\n\n";
  63. foreach($this->getChangelogs() as $file) {
  64. $lines = explode("\n", file_get_contents($file));
  65. $hl = [
  66. array_shift($lines),
  67. array_shift($lines),
  68. ];
  69. array_unshift($lines, $headline);
  70. file_put_contents($file, implode("\n", array_merge($hl, $lines)));
  71. }
  72. }
  73. protected function resortChangelogs($version)
  74. {
  75. foreach($this->getChangelogs() as $file) {
  76. // split the file into relevant parts
  77. list($start, $changelog, $end) = $this->splitChangelog($file, $version);
  78. $changelog = $this->resortChangelog($changelog);
  79. file_put_contents($file, implode("\n", array_merge($start, $changelog, $end)));
  80. }
  81. }
  82. protected function mergeChangelogs($version)
  83. {
  84. $file = $this->getFrameworkChangelog();
  85. // split the file into relevant parts
  86. list($start, $changelog, $end) = $this->splitChangelog($file, $version);
  87. $changelog = $this->resortChangelog($changelog);
  88. $changelog[] = '';
  89. $extensions = $this->getExtensionChangelogs();
  90. asort($extensions);
  91. foreach($extensions as $changelogFile) {
  92. if (!preg_match('~extensions/([a-z]+)/CHANGELOG\\.md~', $changelogFile, $m)) {
  93. throw new Exception("Illegal extension changelog file: " . $changelogFile);
  94. }
  95. list( , $extensionChangelog, ) = $this->splitChangelog($changelogFile, $version);
  96. $name = $m[1];
  97. $ucname = ucfirst($name);
  98. $changelog[] = "### $ucname Extension (yii2-$name)";
  99. $changelog = array_merge($changelog, $extensionChangelog);
  100. }
  101. file_put_contents($file, implode("\n", array_merge($start, $changelog, $end)));
  102. }
  103. /**
  104. * Extract changelog content for a specific version
  105. */
  106. protected function splitChangelog($file, $version)
  107. {
  108. $lines = explode("\n", file_get_contents($file));
  109. // split the file into relevant parts
  110. $start = [];
  111. $changelog = [];
  112. $end = [];
  113. $state = 'start';
  114. foreach($lines as $l => $line) {
  115. // starting from the changelogs headline
  116. if (isset($lines[$l-2]) && strpos($lines[$l-2], $version) !== false &&
  117. isset($lines[$l-1]) && strncmp($lines[$l-1], '---', 3) === 0) {
  118. $state = 'changelog';
  119. }
  120. if ($state === 'changelog' && isset($lines[$l+1]) && strncmp($lines[$l+1], '---', 3) === 0) {
  121. $state = 'end';
  122. }
  123. ${$state}[] = $line;
  124. }
  125. return [$start, $changelog, $end];
  126. }
  127. /**
  128. * Ensure sorting of the changelog lines
  129. */
  130. protected function resortChangelog($changelog)
  131. {
  132. // cleanup whitespace
  133. foreach($changelog as $i => $line) {
  134. $changelog[$i] = rtrim($line);
  135. }
  136. // TODO sorting
  137. return $changelog;
  138. }
  139. protected function getChangelogs()
  140. {
  141. return array_merge([$this->getFrameworkChangelog()], $this->getExtensionChangelogs());
  142. }
  143. protected function getFrameworkChangelog()
  144. {
  145. return YII2_PATH . '/CHANGELOG.md';
  146. }
  147. protected function getExtensionChangelogs()
  148. {
  149. return glob(dirname(YII2_PATH) . '/extensions/*/CHANGELOG.md');
  150. }
  151. protected function composerSetStability($version)
  152. {
  153. $stability = 'stable';
  154. if (strpos($version, 'alpha') !== false) {
  155. $stability = 'alpha';
  156. } elseif (strpos($version, 'beta') !== false) {
  157. $stability = 'beta';
  158. } elseif (strpos($version, 'rc') !== false) {
  159. $stability = 'RC';
  160. } elseif (strpos($version, 'dev') !== false) {
  161. $stability = 'dev';
  162. }
  163. $this->sed(
  164. '/"minimum-stability": "(.+?)",/',
  165. '"minimum-stability": "' . $stability . '",',
  166. [
  167. dirname(YII2_PATH) . '/apps/advanced/composer.json',
  168. dirname(YII2_PATH) . '/apps/basic/composer.json',
  169. dirname(YII2_PATH) . '/apps/benchmark/composer.json',
  170. ]
  171. );
  172. }
  173. protected function updateYiiVersion($version)
  174. {
  175. $this->sed(
  176. '/function getVersion\(\)\n \{\n return \'(.+?)\';/',
  177. "function getVersion()\n {\n return '$version';",
  178. YII2_PATH . '/BaseYii.php');
  179. }
  180. protected function sed($pattern, $replace, $files)
  181. {
  182. foreach((array) $files as $file) {
  183. file_put_contents($file, preg_replace($pattern, $replace, file_get_contents($file)));
  184. }
  185. }
  186. }