From 9e75d5d7218374318a9bc9706a263460f8ebf654 Mon Sep 17 00:00:00 2001 From: John Fitzpatrick Date: Tue, 22 Mar 2022 20:19:30 +1100 Subject: [PATCH] Misc updates * Switched to using Composer\InstalledVersions for yaml component version (fixes #47) * Code tidy (removed redundant IDE hints, breaks, namespace qualifiers) * Clarified comments --- README.md | 3 +-- composer.json | 1 + composer.lock | 6 ++++-- src/yaml-lint.php | 32 ++++++++++++++++---------------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 49b733d..20410c3 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Install as a project component with Composer (executable from the project's composer require j13k/yaml-lint ``` -Typically a binary edition (`yaml-lint.phar`) is also available for download +Typically, a binary edition (`yaml-lint.phar`) is also available for download with [each release](https://github.com/j13k/yaml-lint/releases). This embeds the latest stable version of the Symfony Yaml component that is current at the time of the release. @@ -62,4 +62,3 @@ The MIT License (MIT). Please see [LICENCE](LICENSE) for more information. [link-downloads]: https://packagist.org/packages/j13k/yaml-lint/stats [link-dependencies]: https://www.versioneye.com/user/projects/58324238eaa74b004633a7c1 [link-author]: https://github.com/j13k -[link-contributors]: ../../contributors diff --git a/composer.json b/composer.json index 69d34f6..6ff25d9 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ } ], "require": { + "composer-runtime-api": "^2", "symfony/yaml": "^2|^3|^4|^5|^6" }, "autoload": { diff --git a/composer.lock b/composer.lock index 26f885b..40f12ed 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "16cf10f4331fb36c286bf8dece6cc050", + "content-hash": "872f54f03d7f73ad51523bed0c966203", "packages": [ { "name": "symfony/polyfill-ctype", @@ -169,7 +169,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "composer-runtime-api": "^2" + }, "platform-dev": [], "plugin-api-version": "2.2.0" } diff --git a/src/yaml-lint.php b/src/yaml-lint.php index 16a94ea..aa963ef 100755 --- a/src/yaml-lint.php +++ b/src/yaml-lint.php @@ -9,6 +9,7 @@ * file that was distributed with this source code. */ +use Composer\InstalledVersions; use J13k\YamlLint\UsageException; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; @@ -28,7 +29,7 @@ define('YAML_PARSE_PARAM_NAME_FLAGS', 'flags'); // Init app name and args -$appStr = APP_NAME . ' ' . APP_VERSION; +$appStr = APP_NAME; $argQuiet = false; $argPaths = []; @@ -38,23 +39,24 @@ $pathToTry = null; foreach (array('/../../../', '/../vendor/') as $pathToTry) { if (is_readable(__DIR__ . $pathToTry . 'autoload.php')) { - /** @noinspection PhpIncludeInspection */ require __DIR__ . $pathToTry . 'autoload.php'; break; } } if (!class_exists('\Composer\Autoload\ClassLoader')) { - throw new \Exception(_msg('composer')); + throw new Exception(_msg('composer')); } - // Extract YAML component metadata - $componentsManifest = __DIR__ . $pathToTry . 'composer/installed.json'; - $components = json_decode(file_get_contents($componentsManifest), true); - foreach ($components as $component) { - if (isset($component['name']) && $component['name'] == 'symfony/yaml') { - $appStr .= ', symfony/yaml ' . $component['version']; - break; + // Build app version string + if (class_exists('\Composer\InstalledVersions')) { + if (InstalledVersions::isInstalled('j13k/yaml-lint')) { + $appStr .= ' ' . InstalledVersions::getPrettyVersion('j13k/yaml-lint'); + } + if (InstalledVersions::isInstalled('symfony/yaml')) { + $appStr .= ', symfony/yaml ' . InstalledVersions::getPrettyVersion('symfony/yaml'); } + } else { + $appStr .= ' ' . APP_VERSION; } // Process and check args @@ -78,7 +80,7 @@ } } - // Currently only one input file or STDIN supported + // Currently, only one input file or STDIN supported if (count($argPaths) < 1) { throw new UsageException('no input specified', EXIT_ERROR); } @@ -94,11 +96,11 @@ $yamlParseParams = $yamlParseMethod->getParameters(); switch ($yamlParseParams[1]->name) { case YAML_PARSE_PARAM_NAME_EXCEPTION_ON_INVALID_TYPE: - // Maintains original behaviour in ^2 + // Maintains original behaviour in v2 Yaml::parse($content, true); break; case YAML_PARSE_PARAM_NAME_FLAGS: - // Implements same behaviour in ^3 and ^4 + // Implements same behaviour in v3+ Yaml::parse($content, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE); break; default: @@ -156,7 +158,7 @@ fwrite(STDERR, "\n" . $e->getMessage() . "\n\n"); exit(EXIT_ERROR); -} catch (\Exception $e) { +} catch (Exception $e) { // The rest fwrite(STDERR, $appStr); @@ -196,7 +198,6 @@ function _msg($str) Composer dependencies cannot be loaded; install Composer to remedy: https://getcomposer.org/download/ EOD; - break; case 'usage': return <<