Files
mongo-php-driver/bin/update-release-version.php
T
Andreas Braun 7142daf6f4 PHPC-1674: Automate Driver Releases (#1538)
* Add script to update version number in phongo_version.h

* Add support for release notes in prep_release.php

* Add workflow to create PECL package for tags

* Use PECL-compatible stability suffixes in version

* Add dummy changelog for branch packages

* Use package version from phongo_version.h for install

* Disable install step as it currently fails

* Upload release artifact for tags

* Extract linux build to action

* Simplify Windows builds

* Create packages for Windows in package workflow
* Extract and reuse Windows build workflow
* Don't create artifacts when not necessary

* Remove branch build trigger

* Fix errors in packaging workflow

* Add release workflow

* Update release message

* Write changelog file for PECL package

* Add more output to build step

* Update release documentation

* Fix reading changelog for PECL packaging

* Remove explicit phpize installation

* Remove unnecessary fetch-depth usage

* Use script parameter instead of hardcoded changelog file name

* Improve update-release-version command names

* Remove obsolete comment

* Update JIRA version field description

* Update release documentation

* Fix wrong version number format
* Clarify manual release process steps
* Add section on announcing releases
* Fix wrong heading level

* Install PECL package to ensure it's functional

* Only install required extensions

* Use GFM note syntax

* Extract release documentation
2024-05-03 10:20:52 +02:00

174 lines
4.8 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
const VERSION_FILENAME = __DIR__ . '/../phongo_version.h';
function usage()
{
global $argv;
echo <<<EOT
Usage:
{$argv[0]} <command>
Commands:
to-stable: Mark the current version as stable
to-next-patch-dev: Update to the next patch development version
to-next-minor-dev: Update to the next minor development version
get-version: Print the current version number
EOT;
exit(1);
}
function read_release_version(string $filename): array
{
if (! is_file($filename)) {
throw new Exception(sprintf('File not found: "%s"', $filename));
}
$contents = file_get_contents($filename);
$versions = [];
if (! preg_match('/^#define PHP_MONGODB_VERSION "(.*)"$/m', $contents, $matches)) {
throw new Exception('Could not match PHP_MONGODB_VERSION');
}
$versions['version'] = $matches[1];
if (! preg_match('/^#define PHP_MONGODB_STABILITY "(.*)"$/m', $contents, $matches)) {
throw new Exception('Could not match PHP_MONGODB_STABILITY');
}
$versions['stability'] = $matches[1];
if (! preg_match('/^#define PHP_MONGODB_VERSION_DESC (\d+),(\d+),(\d+),(\d+)$/m', $contents, $matches)) {
throw new Exception('Could not match PHP_MONGODB_VERSION_DESC');
}
$versions['versionComponents'] = array_slice($matches, 1);
return $versions;
}
function write_release_version(string $filename, array $version): void
{
if (! is_file($filename)) {
throw new Exception(sprintf('File not found: "%s"', $filename));
}
$contents = file_get_contents($filename);
$patterns = [
'/^#define PHP_MONGODB_VERSION "(.*)"$/m',
'/^#define PHP_MONGODB_STABILITY "(.*)"$/m',
'/^#define PHP_MONGODB_VERSION_DESC (\d+),(\d+),(\d+),(\d+)$/m',
];
$replacements = [
sprintf('#define PHP_MONGODB_VERSION "%s"', $version['version']),
sprintf('#define PHP_MONGODB_STABILITY "%s"', $version['stability']),
sprintf('#define PHP_MONGODB_VERSION_DESC %s,%s,%s,%s', ...$version['versionComponents']),
];
$contents = preg_replace($patterns, $replacements, $contents, -1, $count);
if ($count !== 3) {
throw new Exception('Could not properly replace contents in file');
}
file_put_contents($filename, $contents);
}
function get_version_string_from_components(array $versionComponents): string
{
return implode('.', array_slice($versionComponents, 0, 3));
}
function get_stable_version(array $versions): array
{
if (! preg_match('/^(\d+\.\d+\.\d+)/', $versions['version'], $matches)) {
throw new Exception(sprintf('Version "%s" is not in the expected format', $versions['version']));
}
$version = $matches[1];
$expectedVersionString = get_version_string_from_components($versions['versionComponents']);
if ($expectedVersionString !== $version) {
throw new Exception(sprintf('Version "%s" does not match version from components ("%s")', $version, $expectedVersionString));
}
$newVersions = [
'version' => $version,
'stability' => 'stable',
'versionComponents' => $versions['versionComponents'],
];
// Increase build number
$newVersions['versionComponents'][3]++;
return $newVersions;
}
function get_next_patch_version(array $versions): array
{
$versionComponents = $versions['versionComponents'];
// Increase patch version, set build number to 0
$versionComponents[2] += 1;
$versionComponents[3] = 0;
return [
'version' => get_version_string_from_components($versionComponents) . 'dev',
'stability' => 'devel',
'versionComponents' => $versionComponents,
];
}
function get_next_minor_version(array $versions): array
{
$versionComponents = $versions['versionComponents'];
// Increase minor version, set patch and build number to 0
$versionComponents[1] += 1;
$versionComponents[2] = 0;
$versionComponents[3] = 0;
return [
'version' => get_version_string_from_components($versionComponents) . 'dev',
'stability' => 'devel',
'versionComponents' => $versionComponents,
];
}
if ($argc !== 2) {
usage();
}
$currentVersion = read_release_version(VERSION_FILENAME);
switch ($argv[1] ?? null) {
case 'get-version':
echo $currentVersion['version'];
exit(0);
case 'to-stable':
$newVersion = get_stable_version($currentVersion);
break;
case 'to-next-patch-dev':
$newVersion = get_next_patch_version($currentVersion);
break;
case 'to-next-minor-dev':
$newVersion = get_next_minor_version($currentVersion);
break;
default:
usage();
}
write_release_version(VERSION_FILENAME, $newVersion);
printf("Updated version number in version file from %s to %s\n", $currentVersion['version'], $newVersion['version']);