|
2 | 2 |
|
3 | 3 | namespace Wikimedia\UpdateHistory; |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Update the HISTORY.md file just before/after a release. |
| 7 | + * Run this with `composer update-history`. |
| 8 | + */ |
5 | 9 | class UpdateHistory { |
6 | | - |
| 10 | + /** |
| 11 | + * Main entry point. |
| 12 | + * @param string $which One of 'patch', 'minor', or 'major'. |
| 13 | + * @return int Exit code: zero on success, non-zero on failure. |
| 14 | + */ |
| 15 | + public static function main( string $which = 'patch' ): int { |
| 16 | + $changeLogPath = __DIR__ . '/../HISTORY.md'; |
| 17 | + $changeLog = file_get_contents( $changeLogPath ); |
| 18 | + $changeLog = preg_replace_callback( |
| 19 | + '/^(#+)( \S+)? (x\.x\.x|\d+\.\d+\.\d+)(.*)$/m', |
| 20 | + static function ( $matches ) use ( $changeLog, $which ) { |
| 21 | + $line = $matches[1] . ( $matches[2] ?? '' ); |
| 22 | + if ( $matches[3] === 'x.x.x' ) { |
| 23 | + // Find the previous version |
| 24 | + if ( preg_match( |
| 25 | + '/^#+' . preg_quote( $matches[2] ?? '', '/' ) . |
| 26 | + ' (\d+)\.(\d+)\.(\d+)/m', $changeLog, $m2 |
| 27 | + ) !== 1 ) { |
| 28 | + throw new \Exception( "Last version not found!" ); |
| 29 | + } |
| 30 | + // Do a release! |
| 31 | + list( $ignore,$major,$minor,$patch ) = $m2; |
| 32 | + switch ( $which ) { |
| 33 | + case 'patch': |
| 34 | + case 'minor': |
| 35 | + case 'major': |
| 36 | + $$which = intval( $$which ) + 1; |
| 37 | + break; |
| 38 | + default: |
| 39 | + throw new \Exception( "Unknown version bump type: $which" ); |
| 40 | + } |
| 41 | + $nextVersion = "$major.$minor.$patch"; |
| 42 | + $date = date( 'Y-m-d' ); |
| 43 | + return "$line $nextVersion ($date)"; |
| 44 | + } else { |
| 45 | + // Bump after a release |
| 46 | + return "$line x.x.x (not yet released)\n\n" . $matches[0]; |
| 47 | + } |
| 48 | + }, |
| 49 | + $changeLog, 1, $count ); |
| 50 | + if ( $count != 1 ) { |
| 51 | + throw new \Exception( "Changelog entry not found!" ); |
| 52 | + } |
| 53 | + file_put_contents( $changeLogPath, $changeLog ); |
| 54 | + return 0; |
| 55 | + } |
7 | 56 | } |
0 commit comments