Release.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace AlibabaCloud\Client;
  3. use Composer\Script\Event;
  4. /**
  5. * Class Release
  6. *
  7. * @codeCoverageIgnore
  8. * @package AlibabaCloud\Client
  9. */
  10. class Release
  11. {
  12. /**
  13. * @param Event $event
  14. */
  15. public static function release(Event $event)
  16. {
  17. $arguments = $event->getArguments();
  18. if (count($arguments) <= 1) {
  19. echo 'Missing ChangeLog';
  20. return;
  21. }
  22. self::updateChangelogFile($arguments[0], $arguments[1]);
  23. self::changeVersionInCode($arguments[0]);
  24. }
  25. /**
  26. * @param $version
  27. * @param $changeLog
  28. */
  29. private static function updateChangelogFile($version, $changeLog)
  30. {
  31. $content = preg_replace(
  32. '/# CHANGELOG/',
  33. '# CHANGELOG'
  34. . "\n"
  35. . "\n"
  36. . "## $version - " . date('Y-m-d')
  37. . self::log($changeLog),
  38. self::getChangeLogContent()
  39. );
  40. file_put_contents(self::getChangeLogFile(), $content);
  41. }
  42. /**
  43. * @param $changeLog
  44. *
  45. * @return string
  46. */
  47. private static function log($changeLog)
  48. {
  49. $logs = explode('|', $changeLog);
  50. $string = "\n";
  51. foreach ($logs as $log) {
  52. if ($log) {
  53. $string .= "- $log." . "\n";
  54. }
  55. }
  56. return $string;
  57. }
  58. /**
  59. * @return string
  60. */
  61. private static function getChangeLogContent()
  62. {
  63. return file_get_contents(self::getChangeLogFile());
  64. }
  65. /**
  66. * @return string
  67. */
  68. private static function getChangeLogFile()
  69. {
  70. return __DIR__ . '/../CHANGELOG.md';
  71. }
  72. /**
  73. * @param $version
  74. */
  75. private static function changeVersionInCode($version)
  76. {
  77. $content = preg_replace(
  78. "/const VERSION = \'(.*)\';/",
  79. "const VERSION = '" . $version . "';",
  80. self::getCodeContent()
  81. );
  82. file_put_contents(self::getCodeFile(), $content);
  83. }
  84. /**
  85. * @return string
  86. */
  87. private static function getCodeContent()
  88. {
  89. return file_get_contents(self::getCodeFile());
  90. }
  91. /**
  92. * @return string
  93. */
  94. private static function getCodeFile()
  95. {
  96. return __DIR__ . '/AlibabaCloud.php';
  97. }
  98. }