From 6704359adb472389495317bd7cf2f4ae00eec962 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Sun, 29 Oct 2023 10:15:59 +0100 Subject: [PATCH] Add check-newer CLI command, fix returned code (#13) --- .github/workflows/tests.yml | 2 +- bin/unipoints | 60 +++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 45574b3..c35585f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: uses: actions/checkout@v4 - name: Check PHP coding style if: startsWith(matrix.os, 'ubuntu') && matrix.php-version == '8.2' - run: composer run-script phpcs -- --ansi --no-interaction --dry-run + run: composer run-script phpcs -- --ansi --no-interaction --dry-run --diff - name: Install Composer dependencies run: composer update --optimize-autoloader --no-progress --ansi --no-interaction - name: Build diff --git a/bin/unipoints b/bin/unipoints index ca86ba2..1dd85f4 100755 --- a/bin/unipoints +++ b/bin/unipoints @@ -43,47 +43,62 @@ function showSyntax(string $name): void { $defaultVersion = DEFAULT_VERSION; echo << +Syntax {$name} <-h|--help|installed|latest|check-newer|build [version]> Where: - installed: print the Unicode version used to build the data in this project - latest: print the latest version of Unicode +- check-newer: check if the latest Unicode version is greater than the installed one - build [version]: (re) build the data in this project. If [version] is not specified, we'll install version {$defaultVersion} + EOT ; } -function showInstalledUnicodeVersion(): int +function getInstalledUnicodeVersion(): string { if (!enum_exists(Block::class)) { - printf("The %s enum has not been created yet.\n", Block::class); - return 1; + throw new UserMessageException(sprintf("The %s enum has not been created yet.\n", Block::class)); } $versionFromBlocks = UnicodeInfo::from(Block::class)->unicodeVersion; if (!enum_exists(Codepoint::class)) { - printf("The %s enum has not been created yet.\n", Codepoint::class); - return 1; + throw new UserMessageException(sprintf("The %s enum has not been created yet.\n", Codepoint::class)); } $versionFromCodepoints = UnicodeInfo::from(Codepoint::class)->unicodeVersion; - if (!enum_exists(Codepoint::class)) { - printf("The %s enum has not been created yet.\n", Codepoint::class); - return 1; - } if ($versionFromBlocks !== $versionFromCodepoints) { - printf("The version extracted from %s (%s) is different from the one extracted from %s (%s).\n", Block::class, $versionFromBlocks, Codepoint::class, $versionFromCodepoints); - return 1; + throw new UserMessageException(sprintf("The version extracted from %s (%s) is different from the one extracted from %s (%s).\n", Block::class, $versionFromBlocks, Codepoint::class, $versionFromCodepoints)); } - echo $versionFromBlocks, "\n"; + return $versionFromBlocks; +} +function showInstalledUnicodeVersion(): int +{ + echo getInstalledUnicodeVersion(), "\n"; return 0; } -function getLatestUnicodeVersion(): int +function getLatestUnicodeVersion(): string { $dataStorage = DataStorage::getInstance(); - echo $dataStorage->getLatestVersion(), "\n"; + return $dataStorage->getLatestVersion(); +} + +function showLatestUnicodeVersion(): int +{ + echo getLatestUnicodeVersion(), "\n"; + return 0; +} +function checkNewerUnicodeVersion(): int +{ + $installed = getInstalledUnicodeVersion(); + $latest = getLatestUnicodeVersion(); + if (version_compare($installed, $latest) >= 0) { + echo "The latest Unicode version ({$latest}) is not newer than the installed one ({$installed}).\n"; + return 1; + } + echo "The latest Unicode version ({$latest}) is newer than the installed one ({$installed}).\n"; return 0; } @@ -98,7 +113,6 @@ function build(string $unicodeVersion): int $codepointsBuilder = new CodepointsBuilder(unicodeVersion: $unicodeVersion, blocksBuilder: $blocksBuilder); $codepointsBuilder->saveAllCodepoints(); echo "done.\n"; - return 0; } @@ -121,7 +135,13 @@ function main(array $args): int showSyntax($args[0]); return 1; } - return getLatestUnicodeVersion(); + return showLatestUnicodeVersion(); + case 'check-newer': + if ($numArgs !== 2) { + showSyntax($args[0]); + return 1; + } + return checkNewerUnicodeVersion(); case 'build': switch ($numArgs) { case 2: @@ -137,8 +157,10 @@ function main(array $args): int } try { - return main($argv); + $rc = main($argv); } catch (UserMessageException $x) { echo trim($x->getMessage()), "\n"; - return 1; + $rc = 1; } + +exit($rc);