From 2b075feebc2608738fd6ccb3c9dfe109b313a1ce Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Tue, 24 Sep 2024 16:48:00 +0100 Subject: [PATCH] feat: add support for `--configuration` --- composer.json | 138 ++++++++++++++++---------------- src/Commands/AnalyseCommand.php | 44 ++++++++-- 2 files changed, 109 insertions(+), 73 deletions(-) diff --git a/composer.json b/composer.json index 592c317..ad001b0 100644 --- a/composer.json +++ b/composer.json @@ -1,70 +1,72 @@ { - "name": "worksome/graphlint", - "description": "A static analysis tool for GraphQl", - "type": "project", - "require": { - "php": "^8.2", - "webonyx/graphql-php": "^15.0.1", - "symfony/console": "^6.1", - "illuminate/support": "^10.0", - "symfony/dependency-injection": "^6.0", - "symfony/http-kernel": "^6.0", - "symfony/config": "^6.0", - "symplify/autowire-array-parameter": "^11.0", - "symplify/package-builder": "^11.0", - "thecodingmachine/safe": "^2.4", - "jawira/case-converter": "^3.5" - }, - "require-dev": { - "pestphp/pest": "^2.34", - "symfony/var-dumper": "^6.4", - "symplify/easy-testing": "^11.0", - "worksome/coding-style": "^2.10.2" - }, - "license": "MIT", - "autoload": { - "psr-4": { - "Worksome\\Graphlint\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Worksome\\Graphlint\\Tests\\": "tests/" - } - }, - "bin": [ - "bin/graphlint" - ], - "authors": [ - { - "name": "Oliver Nybroe", - "email": "oliver@worksome.com" - } - ], - "config": { - "allow-plugins": { - "pestphp/pest-plugin": true, - "dealerdirect/phpcodesniffer-composer-installer": true, - "worksome/coding-style": true - } - }, - "scripts": { - "ecs": "vendor/bin/ecs", - "ecs:fix": "vendor/bin/ecs --fix", - "phpstan": "vendor/bin/phpstan analyse", - "rector": "vendor/bin/rector process --dry-run --ansi", - "rector:fix": "vendor/bin/rector process --ansi", - "pest": "vendor/bin/pest --parallel", - "test": [ - "@ecs", - "@phpstan", - "@rector", - "@pest" - ] - }, - "suggest": { - "ext-dom": "Required for Checkstyle output format." - }, - "minimum-stability": "dev", - "prefer-stable": true + "name": "worksome/graphlint", + "description": "A static analysis tool for GraphQl", + "type": "project", + "require": { + "php": "^8.2", + "illuminate/support": "^10.0", + "jawira/case-converter": "^3.5", + "symfony/config": "^6.0", + "symfony/console": "^6.1", + "symfony/dependency-injection": "^6.0", + "symfony/filesystem": "^6.4", + "symfony/http-kernel": "^6.0", + "symplify/autowire-array-parameter": "^11.0", + "symplify/package-builder": "^11.0", + "thecodingmachine/safe": "^2.4", + "webonyx/graphql-php": "^15.0.1" + }, + "require-dev": { + "pestphp/pest": "^2.34", + "symfony/var-dumper": "^6.4", + "symplify/easy-testing": "^11.0", + "worksome/coding-style": "^2.10.2" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Worksome\\Graphlint\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Worksome\\Graphlint\\Tests\\": "tests/" + } + }, + "bin": [ + "bin/graphlint" + ], + "authors": [ + { + "name": "Oliver Nybroe", + "email": "oliver@worksome.com" + } + ], + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "dealerdirect/phpcodesniffer-composer-installer": true, + "worksome/coding-style": true + } + }, + "scripts": { + "ecs": "vendor/bin/ecs", + "ecs:fix": "vendor/bin/ecs --fix", + "phpstan": "vendor/bin/phpstan analyse", + "rector": "vendor/bin/rector process --dry-run --ansi", + "rector:fix": "vendor/bin/rector process --ansi", + "pest": "vendor/bin/pest --parallel", + "test": [ + "@ecs", + "@phpstan", + "@rector", + "@pest" + ] + }, + "suggest": { + "ext-dom": "Required for Checkstyle output format." + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/Commands/AnalyseCommand.php b/src/Commands/AnalyseCommand.php index c428185..976c951 100644 --- a/src/Commands/AnalyseCommand.php +++ b/src/Commands/AnalyseCommand.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Filesystem\Path; use Symplify\PackageBuilder\Console\Output\ConsoleDiffer; use Worksome\Graphlint\EmptyDocumentNode; use Worksome\Graphlint\Graphlint; @@ -21,6 +22,7 @@ use Worksome\Graphlint\Listeners\CheckstyleListener; use Worksome\Graphlint\Listeners\ConsolePrinterListener; +use Worksome\Graphlint\ShouldNotHappenException; use function Safe\file_get_contents; class AnalyseCommand extends Command @@ -49,17 +51,23 @@ protected function configure(): void $this->addOption( self::INPUT, null, - InputOption::VALUE_OPTIONAL, + InputOption::VALUE_REQUIRED, 'The format for the schema inputs.', InputFormat::FILE->value, ); $this->addOption( self::FORMAT, null, - InputOption::VALUE_OPTIONAL, + InputOption::VALUE_REQUIRED, 'The output format.', OutputFormat::Text->value, ); + $this->addOption( + 'configuration', + 'c', + InputOption::VALUE_REQUIRED, + 'The configuration file.', + ); } /** @throws SyntaxError|FilesystemException|JsonException */ @@ -70,10 +78,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output ); - $configurationFile = getcwd() . DIRECTORY_SEPARATOR . 'graphlint.php'; + $currentWorkingDirectory = getcwd(); + if ($currentWorkingDirectory === false) { + throw new ShouldNotHappenException(); + } + + /** @var string|null $configurationFile */ + $configurationFile = $input->getOption('configuration'); + + if ($configurationFile === null) { + foreach (['graphlint.php'] as $discoverableConfigName) { + $discoverableConfigFile = Path::makeAbsolute($discoverableConfigName, $currentWorkingDirectory); + if (is_file($discoverableConfigFile)) { + $configurationFile = $discoverableConfigFile; + break; + } + } + } else { + $configurationFile = Path::makeAbsolute($configurationFile, $currentWorkingDirectory); + } - if (! is_file($configurationFile) || ! is_readable($configurationFile)) { - $style->error("Unable to find a \"graphlint.php\" configuration file in your current directory."); + if ( + $configurationFile === null + || $configurationFile === '' + || ! is_file($configurationFile) + || ! is_readable($configurationFile) + ) { + $style->error(sprintf( + 'Unable to find a "%s" configuration file.', + $configurationFile ?? 'graphlint.php' + )); return self::FAILURE; }