build/make-release: make arg parsing use getopt

This commit is contained in:
Dylan K. Taylor 2021-10-28 16:25:18 +01:00
parent 48f77abe7e
commit dba148cfaa
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -25,18 +25,26 @@ namespace pocketmine\build\make_release;
use pocketmine\utils\VersionString; use pocketmine\utils\VersionString;
use pocketmine\VersionInfo; use pocketmine\VersionInfo;
use function count; use function array_keys;
use function array_map;
use function dirname; use function dirname;
use function fgets; use function fgets;
use function file_get_contents; use function file_get_contents;
use function file_put_contents; use function file_put_contents;
use function fwrite; use function fwrite;
use function getopt;
use function is_string;
use function max;
use function preg_replace; use function preg_replace;
use function sleep; use function sleep;
use function sprintf; use function sprintf;
use function str_pad;
use function strlen;
use function system; use function system;
use const STDERR; use const STDERR;
use const STDIN; use const STDIN;
use const STDOUT;
use const STR_PAD_LEFT;
require_once dirname(__DIR__) . '/vendor/autoload.php'; require_once dirname(__DIR__) . '/vendor/autoload.php';
@ -60,22 +68,38 @@ function replaceVersion(string $versionInfoPath, string $newVersion, bool $isDev
file_put_contents($versionInfoPath, $versionInfo); file_put_contents($versionInfoPath, $versionInfo);
} }
/** const ACCEPTED_OPTS = [
* @param string[] $argv "current" => "Version to insert and tag",
* @phpstan-param list<string> $argv "next" => "Version to put in the file after tagging",
*/ "channel" => "Release channel to post this build into"
function main(array $argv) : void{ ];
if(count($argv) < 2){
fwrite(STDERR, "Arguments: <channel> [release version] [next version]\n"); function main() : void{
$filteredOpts = [];
foreach(getopt("", ["current:", "next:", "channel:", "help"]) as $optName => $optValue){
if($optName === "help"){
fwrite(STDOUT, "Options:\n");
$maxLength = max(array_map(fn(string $str) => strlen($str), array_keys(ACCEPTED_OPTS)));
foreach(ACCEPTED_OPTS as $acceptedName => $description){
fwrite(STDOUT, str_pad("--$acceptedName", $maxLength + 4, " ", STR_PAD_LEFT) . ": $description\n");
}
exit(0);
}
if(!is_string($optValue)){
fwrite(STDERR, "--$optName expects exactly 1 value\n");
exit(1); exit(1);
} }
if(isset($argv[2])){ $filteredOpts[$optName] = $optValue;
$currentVer = new VersionString($argv[2]); }
if(isset($filteredOpts["current"])){
$currentVer = new VersionString($filteredOpts["current"]);
}else{ }else{
$currentVer = VersionInfo::VERSION(); $currentVer = VersionInfo::VERSION();
} }
if(isset($argv[3])){ if(isset($filteredOpts["next"])){
$nextVer = new VersionString($argv[3]); $nextVer = new VersionString($filteredOpts["next"]);
}else{ }else{
$nextVer = new VersionString(sprintf( $nextVer = new VersionString(sprintf(
"%u.%u.%u", "%u.%u.%u",
@ -84,6 +108,7 @@ function main(array $argv) : void{
$currentVer->getPatch() + 1 $currentVer->getPatch() + 1
)); ));
} }
$channel = $filteredOpts["channel"] ?? VersionInfo::BUILD_CHANNEL;
echo "About to tag version $currentVer. Next version will be $nextVer.\n"; echo "About to tag version $currentVer. Next version will be $nextVer.\n";
echo "please add appropriate notes to the changelog and press enter..."; echo "please add appropriate notes to the changelog and press enter...";
@ -95,10 +120,10 @@ function main(array $argv) : void{
exit(1); exit(1);
} }
$versionInfoPath = dirname(__DIR__) . '/src/VersionInfo.php'; $versionInfoPath = dirname(__DIR__) . '/src/VersionInfo.php';
replaceVersion($versionInfoPath, $currentVer->getBaseVersion(), false, $argv[1]); replaceVersion($versionInfoPath, $currentVer->getBaseVersion(), false, $channel);
system('git commit -m "Release ' . $currentVer->getBaseVersion() . '" --include "' . $versionInfoPath . '"'); system('git commit -m "Release ' . $currentVer->getBaseVersion() . '" --include "' . $versionInfoPath . '"');
system('git tag ' . $currentVer->getBaseVersion()); system('git tag ' . $currentVer->getBaseVersion());
replaceVersion($versionInfoPath, $nextVer->getBaseVersion(), true, ""); replaceVersion($versionInfoPath, $nextVer->getBaseVersion(), true, $channel);
system('git add "' . $versionInfoPath . '"'); system('git add "' . $versionInfoPath . '"');
system('git commit -m "' . $nextVer->getBaseVersion() . ' is next" --include "' . $versionInfoPath . '"'); system('git commit -m "' . $nextVer->getBaseVersion() . ' is next" --include "' . $versionInfoPath . '"');
echo "pushing changes in 5 seconds\n"; echo "pushing changes in 5 seconds\n";
@ -106,4 +131,4 @@ function main(array $argv) : void{
system('git push origin HEAD ' . $currentVer->getBaseVersion()); system('git push origin HEAD ' . $currentVer->getBaseVersion());
} }
main($argv); main();