From 92965c69451f18b07348232f38d28d3df6ec2ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 6 Feb 2021 14:34:51 -0300 Subject: [PATCH] Add the PhpMyAdmin\Version class generator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Creates the PhpMyAdmin\Command\SetVersionCommand class. Usage: ./scripts/console set-version 5.1.0-dev Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../classes/Command/SetVersionCommand.php | 101 ++++++++++++++++++ libraries/classes/Config.php | 4 +- libraries/classes/Version.php | 66 ++---------- scripts/console | 2 + 4 files changed, 116 insertions(+), 57 deletions(-) create mode 100644 libraries/classes/Command/SetVersionCommand.php diff --git a/libraries/classes/Command/SetVersionCommand.php b/libraries/classes/Command/SetVersionCommand.php new file mode 100644 index 0000000000..437905a1c8 --- /dev/null +++ b/libraries/classes/Command/SetVersionCommand.php @@ -0,0 +1,101 @@ +setDescription('Sets the version number'); + $this->setHelp('This command generates the PhpMyAdmin\Version class based on the version number provided.'); + $this->addArgument('version', InputArgument::REQUIRED, 'The version number'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + /** @var string $version */ + $version = $input->getArgument('version'); + + $generatedClass = $this->getGeneratedClass($version); + + if (! $this->writeGeneratedClassFile($generatedClass)) { + // failure + return 1; + } + + $output->writeln('PhpMyAdmin\Version class successfully generated!'); + + // success + return 0; + } + + private function getGeneratedClass(string $version): string + { + $return = preg_match('/^(\d+)\.(\d{1,2})\.(\d{1,2})(-(\w+))?$/', $version, $matches); + if ($return === false || $return === 0) { + throw new RangeException('The version number is in the wrong format: ' . $version); + } + + return sprintf( + self::$generatedClassTemplate, + $matches[1], + $matches[2], + $matches[3], + $matches[4] ?? '', + $matches[5] ?? '', + ($matches[5] ?? '') === 'dev' ? 'true' : 'false' + ); + } + + private function writeGeneratedClassFile(string $generatedClass): bool + { + $result = file_put_contents( + ROOT_PATH . 'libraries/classes/Version.php', + $generatedClass + ); + + return $result !== false; + } +} diff --git a/libraries/classes/Config.php b/libraries/classes/Config.php index ff7aefdb98..e442a8cf0e 100644 --- a/libraries/classes/Config.php +++ b/libraries/classes/Config.php @@ -137,8 +137,8 @@ class Config public function checkSystem(): void { // All the version handling is now done in the Version class - $this->set('PMA_VERSION', Version::phpMyAdminVersion()); - $this->set('PMA_MAJOR_VERSION', Version::phpMyAdminSeriesVersion()); + $this->set('PMA_VERSION', Version::VERSION); + $this->set('PMA_MAJOR_VERSION', Version::SERIES); $this->checkWebServerOs(); $this->checkWebServer(); diff --git a/libraries/classes/Version.php b/libraries/classes/Version.php index e5761b6f50..76c2b3c007 100644 --- a/libraries/classes/Version.php +++ b/libraries/classes/Version.php @@ -4,64 +4,20 @@ declare(strict_types=1); namespace PhpMyAdmin; -use function defined; - /** - * Class to handle the phpMyAdmin version + * This class is generated by scripts/console. + * + * @see \PhpMyAdmin\Command\SetVersionCommand */ final class Version { - /* - * Packaging people can add a version suffix using the VERSION_SUFFIX constant at vendor_config.php - */ - - public const VERSION_MAJOR = 5; - public const VERSION_MINOR = 1; - public const VERSION_PATCH = 0; - - // The version will be {major}.{minor}.{patch}-dev + // The VERSION_SUFFIX constant is defined at libraries/vendor_config.php + public const VERSION = '5.1.0-dev' . VERSION_SUFFIX; + public const SERIES = '5.1'; + public const MAJOR = 5; + public const MINOR = 1; + public const PATCH = 0; + public const ID = 50100; + public const PRE_RELEASE_NAME = 'dev'; public const IS_DEV = true; - - // The version will be {major}.{minor}.{patch}-{PRE_RELEASE_NAME} if not empty - public const PRE_RELEASE_NAME = ''; - - /** - * Get the current phpMyAdmin series version - * - * @example 5.1 - */ - public static function phpMyAdminSeriesVersion(): string - { - return self::VERSION_MAJOR . '.' . self::VERSION_MINOR; - } - - /** - * Get the current phpMyAdmin version - */ - public static function phpMyAdminVersion(): string - { - $versionRaw = self::VERSION_MAJOR . '.' . self::VERSION_MINOR . '.' . self::VERSION_PATCH; - - if (self::IS_DEV) { - return $versionRaw . '-dev'; - } - - if (self::PRE_RELEASE_NAME !== '') { - return $versionRaw . '-' . self::PRE_RELEASE_NAME; - } - - if (defined('VERSION_SUFFIX')) { - return $versionRaw . VERSION_SUFFIX; - } - - return $versionRaw; - } - - /** - * If the current version is a dev version - */ - public static function isDev(): bool - { - return self::IS_DEV; - } } diff --git a/scripts/console b/scripts/console index 65c8082ec7..36e24d8415 100755 --- a/scripts/console +++ b/scripts/console @@ -2,6 +2,7 @@ add(new CacheWarmupCommand()); $application->add(new LintCommand($twig)); +$application->add(new SetVersionCommand()); $application->run();