From b43bfb44165ae694d4eba758291db0f5670a45c2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 6 Mar 2021 01:25:00 -0800 Subject: [PATCH] (REF) set-version.php - Prepare to accept more args --- tools/bin/scripts/set-version.php | 62 ++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/tools/bin/scripts/set-version.php b/tools/bin/scripts/set-version.php index 1918e4e4bb..f8f93e34e7 100755 --- a/tools/bin/scripts/set-version.php +++ b/tools/bin/scripts/set-version.php @@ -22,22 +22,14 @@ if (!isVersionValid($oldVersion)) { fatal("failed to read old version from \"xml/version.xml\"\n"); } -$newVersion = @$argv[1]; +/** @var string $newVersion */ +/** @var bool $doCommit */ +extract(parseArgs($argv)); + if (!isVersionValid($newVersion)) { fatal("failed to read new version\n"); } -switch (@$argv[2]) { - case '--commit': - $doCommit = 1; - break; - case '--no-commit': - $doCommit = 0; - break; - default: - fatal("Must specify --commit or --no-commit\n"); -} - /* *********************************************************************** */ /* Main */ @@ -128,7 +120,7 @@ function initFile($file, $callback) { * Ex: 'FiveTen'. */ function makeVerName($version) { - list ($a, $b) = explode('.', $version); + [$a, $b] = explode('.', $version); require_once 'CRM/Utils/EnglishNumber.php'; return CRM_Utils_EnglishNumber::toCamelCase($a) . CRM_Utils_EnglishNumber::toCamelCase($b); } @@ -147,3 +139,47 @@ function fatal($error) { echo " With --no-commit, any changes will be left uncommitted.\n"; exit(1); } + +/** +* @param array $argv + * Ex: ['myscript.php', '--no-commit', '5.6.7'] + * @return array + * Ex: ['scriptFile' => 'myscript.php', 'doCommit' => FALSE, 'newVersion' => '5.6.7'] + */ +function parseArgs($argv) { + $parsed = []; + $positions = ['scriptFile', 'newVersion']; + $positional = []; + + foreach ($argv as $arg) { + switch ($arg) { + case '--commit': + $parsed['doCommit'] = TRUE; + break; + + case '--no-commit': + $parsed['doCommit'] = FALSE; + break; + + default: + if ($arg[0] !== '-') { + $positional[] = $arg; + } + else { + fatal("Unrecognized argument: $arg\n"); + } + break; + } + } + + foreach ($positional as $offset => $value) { + $name = $positions[$offset] ?? "unknown_$offset"; + $parsed[$name] = $value; + } + + if (!isset($parsed['doCommit'])) { + fatal("Must specify --commit or --no-commit\n"); + } + + return $parsed; +} -- 2.25.1