Skip to content

Commit eb5c87d

Browse files
committed
Initial implementation
1 parent dbe7e4a commit eb5c87d

File tree

6 files changed

+105
-23
lines changed

6 files changed

+105
-23
lines changed

.phan/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

33
$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
4+
$cfg['directory_list'] = [
5+
'src',
6+
'tests',
7+
'.phan/stubs',
8+
];
49

510
return $cfg;

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Release History
22

3-
## 1.0.0
3+
## update-history 1.0.0
44

55
* Initial release.

README.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
11
[![Latest Stable Version]](https://packagist.org/packages/wikimedia/update-history) [![License]](https://packagist.org/packages/wikimedia/update-history)
22

3-
UpdateHistory
3+
wikimedia/update-history
44
=====================
55

6-
A simple tool to update HISTORY.md files.
7-
8-
FILL THIS IN
6+
A simple tool to update HISTORY.md files when making a library release.
97

108
Additional documentation about this library can be found on
119
[mediawiki.org](https://www.mediawiki.org/wiki/UpdateHistory).
1210

13-
1411
Usage
1512
-----
1613

17-
```php
18-
// FILL THIS IN
19-
```
14+
To make a release:
2015

16+
## Step 1
17+
```bash
18+
bin/update-history [patch|minor|major]
19+
```
2120

22-
Running tests
23-
-------------
21+
This increments the version number for a patch release (or, if you
22+
specify, for a minor or major release instead) and updates the
23+
HISTORY.md with the new version number and the current date.
2424

25+
## Step 2
26+
```bash
27+
git add HISTORY.md
28+
git commit -m "Release <My Package> <My Version>"
2529
```
26-
composer install
27-
composer test
28-
```
30+
This step will be automated in the future.
2931

30-
History
31-
-------
32+
## Step 3
33+
```bash
34+
bin/update-history
35+
```
36+
This adds a new placeholder "x.x.x (not yet released)" section to
37+
the HISTORY.md.
3238

33-
UPDATE THIS SECTION
34-
This library was first introduced in MediaWiki 1.27 ([eb46307b00](https://gerrit.wikimedia.org/r/c/mediawiki/core/+/264403/)). It was
35-
split out of the MediaWiki codebase and published as an independent library
36-
during the MediaWiki 1.37 development cycle.
39+
## Step 4
40+
```bash
41+
git add HISTORY.md
42+
git commit -m "Bump HISTORY.md after release"
43+
```
44+
This step will be automated in the future.
3745

3846

3947
---

bin/update-history

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env php
2+
<?php declare(strict_types=1);
3+
4+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
5+
require __DIR__ . '/../vendor/autoload.php';
6+
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
7+
require __DIR__ . '/../../../autoload.php';
8+
} else {
9+
echo 'Project dependencies need to be installed using composer.';
10+
exit(1);
11+
}
12+
13+
global $argv;
14+
15+
$which = $argv[1] ?? 'patch';
16+
\Wikimedia\UpdateHistory\UpdateHistory::main( $which );

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
"php": ">=7.2.9"
2424
},
2525
"require-dev": {
26-
"mediawiki/mediawiki-codesniffer": "36.0.0",
26+
"mediawiki/mediawiki-codesniffer": "37.0.0",
2727
"mediawiki/mediawiki-phan-config": "0.10.6",
2828
"mediawiki/minus-x": "1.1.1",
2929
"ockcyp/covers-validator": "1.3.3",
3030
"php-parallel-lint/php-console-highlighter": "0.5.0",
3131
"php-parallel-lint/php-parallel-lint": "1.3.0",
3232
"phpunit/phpunit": "^8.5|^9.5"
3333
},
34+
"bin": [
35+
"bin/update-history"
36+
],
3437
"scripts": {
3538
"test": [
3639
"parallel-lint . --exclude vendor",
@@ -45,6 +48,7 @@
4548
"fix": [
4649
"phpcbf",
4750
"minus-x fix ."
48-
]
51+
],
52+
"update-history": "bin/update-history patch"
4953
}
5054
}

src/UpdateHistory.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,55 @@
22

33
namespace Wikimedia\UpdateHistory;
44

5+
/**
6+
* Update the HISTORY.md file just before/after a release.
7+
* Run this with `composer update-history`.
8+
*/
59
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+
}
756
}

0 commit comments

Comments
 (0)