|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the contentful/contentful-core package. |
| 5 | + * |
| 6 | + * @copyright 2015-2018 Contentful GmbH |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +class ReleaseUpdater |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + const PENDING_CHANGES_START = '<!-- PENDING-CHANGES -->'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + const PENDING_CHANGES_END = '<!-- /PENDING-CHANGES -->'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + const PENDING_CHANGES_PLACEHOLDER = '> No meaningful changes since last release.'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + const UNRELEASED_CHANGES_HEADER = '## [Unreleased](https://github.com/contentful/contentful-core.php/compare/%s...HEAD)'; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + const NEW_RELEASE_HEADER = '## [%s](https://github.com/contentful/contentful-core.php/tree/%s) (%s)'; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + private $changelogPath; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var string |
| 46 | + */ |
| 47 | + private $composerConfigPath; |
| 48 | + |
| 49 | + public function __construct(string $changelogPath, string $composerConfigPath) |
| 50 | + { |
| 51 | + $this->changelogPath = $changelogPath; |
| 52 | + $this->composerConfigPath = $composerConfigPath; |
| 53 | + } |
| 54 | + |
| 55 | + public function updateChangelog(string $version) |
| 56 | + { |
| 57 | + $content = \file_get_contents($this->changelogPath); |
| 58 | + |
| 59 | + $pendingChangesStart = \mb_strpos($content, self::PENDING_CHANGES_START); |
| 60 | + $pendingChangesEnd = \mb_strpos($content, self::PENDING_CHANGES_END); |
| 61 | + |
| 62 | + if (\false === $pendingChangesStart || \false === $pendingChangesEnd) { |
| 63 | + throw new Exception('ERROR: Cannot reliably determine the changelog section to edit, aborting.'); |
| 64 | + } |
| 65 | + |
| 66 | + $changelog = $this->createUpdatedChangelog($version, $content, $pendingChangesStart, $pendingChangesEnd); |
| 67 | + |
| 68 | + \file_put_contents($this->changelogPath, $changelog); |
| 69 | + } |
| 70 | + |
| 71 | + private function createUpdatedChangelog(string $version, string $content, int $contentStart, int $contentEnd): string |
| 72 | + { |
| 73 | + $extractFrom = $contentStart + \mb_strlen(self::PENDING_CHANGES_START); |
| 74 | + $extractLength = $contentEnd - $contentStart - \mb_strlen(self::PENDING_CHANGES_START); |
| 75 | + |
| 76 | + $newReleaseChanges = \trim(\mb_substr($content, $extractFrom, $extractLength)); |
| 77 | + |
| 78 | + $changelogStart = \trim(\mb_substr($content, 0, $contentStart)); |
| 79 | + $changelogEnd = \trim(\mb_substr($content, $contentEnd + \ mb_strlen(self::PENDING_CHANGES_END))); |
| 80 | + |
| 81 | + $unreleasedLineStart = \mb_strpos($changelogStart, '## [Unreleased]'); |
| 82 | + $changelogStart = \trim(\mb_substr($changelogStart, 0, $unreleasedLineStart)); |
| 83 | + |
| 84 | + $changelogTemplate = '[START] |
| 85 | +
|
| 86 | +[UNRELEASED_HEADER] |
| 87 | +
|
| 88 | +[PENDING_CHANGES_START] |
| 89 | +[PENDING_CHANGES_PLACEHOLDER] |
| 90 | +[PENDING_CHANGES_END] |
| 91 | +
|
| 92 | +[NEW_RELEASE_HEADER] |
| 93 | +
|
| 94 | +[NEW_RELEASE_CHANGES] |
| 95 | +
|
| 96 | +[END] |
| 97 | +'; |
| 98 | + |
| 99 | + $tempPath = \tempnam(\sys_get_temp_dir(), 'changelog-'); |
| 100 | + \file_put_contents($tempPath, $newReleaseChanges); |
| 101 | + \system('pbcopy < '.$tempPath); |
| 102 | + |
| 103 | + return \strtr($changelogTemplate, [ |
| 104 | + '[START]' => $changelogStart, |
| 105 | + '[UNRELEASED_HEADER]' => \sprintf(self::UNRELEASED_CHANGES_HEADER, $version), |
| 106 | + '[PENDING_CHANGES_START]' => self::PENDING_CHANGES_START, |
| 107 | + '[PENDING_CHANGES_PLACEHOLDER]' => self::PENDING_CHANGES_PLACEHOLDER, |
| 108 | + '[PENDING_CHANGES_END]' => self::PENDING_CHANGES_END, |
| 109 | + '[NEW_RELEASE_HEADER]' => \sprintf(self::NEW_RELEASE_HEADER, $version, $version, \date('Y-m-d')), |
| 110 | + '[NEW_RELEASE_CHANGES]' => $newReleaseChanges, |
| 111 | + '[END]' => $changelogEnd, |
| 112 | + ]); |
| 113 | + } |
| 114 | + |
| 115 | + public function updateComposerAliasVersion(string $composerAliasVersion) |
| 116 | + { |
| 117 | + $json = \file_get_contents($this->composerConfigPath); |
| 118 | + $contents = \json_decode($json, \true); |
| 119 | + |
| 120 | + $contents['extra']['branch-alias']['dev-master'] = $composerAliasVersion.'-dev'; |
| 121 | + |
| 122 | + \file_put_contents( |
| 123 | + $this->composerConfigPath, |
| 124 | + \json_encode($contents, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)."\n" |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments