From 2c5629d076741b11a61211db0bc33659028f9dd2 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Sat, 20 Jan 2024 15:59:13 +0100 Subject: [PATCH] Allow to filter the generated model (include/exclude) --- src/Command/GeneratorCommand.php | 38 ++++++++++++++++++- .../OdooModelsStructureConverter.php | 14 +++++-- .../OdooModelsStructureConverterInterface.php | 4 +- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Command/GeneratorCommand.php b/src/Command/GeneratorCommand.php index 2e49f80..5cc7ed4 100644 --- a/src/Command/GeneratorCommand.php +++ b/src/Command/GeneratorCommand.php @@ -5,6 +5,8 @@ namespace FluxSE\OdooApiClient\Command; use FluxSE\OdooApiClient\Api\OdooApiRequestMakerInterface; +use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\Arguments\Criterion; +use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\Arguments\SearchDomains; use FluxSE\OdooApiClient\Operations\ObjectOperationsInterface; use FluxSE\OdooApiClient\PhpGenerator\OdooModelsStructureConverterInterface; use Http\Discovery\Psr17FactoryDiscovery; @@ -72,6 +74,25 @@ protected function configure(): void sprintf('Your Odoo account password or API key (since Odoo v14, default: %s)', $defaultPassword), $defaultPassword ) + ->addOption( + 'only-model', + null, + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'Filter the model list with the model you will set in this option.' + ) + ->addOption( + 'exclude-model', + null, + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'Filter the model list excluding the model you will set in this option.' + ) + ->addOption( + 'password', + null, + InputOption::VALUE_OPTIONAL, + sprintf('Your Odoo account password or API key (since Odoo v14, default: %s)', $defaultPassword), + $defaultPassword + ) ; } @@ -85,6 +106,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $username = $input->getOption('username'); /** @var string $password */ $password = $input->getOption('password'); + /** @var string[] $onlyModels */ + $onlyModels = $input->getOption('only-model'); + /** @var string[] $excludeModels */ + $excludeModels = $input->getOption('exclude-model'); /** @var string $path */ $path = $input->getArgument('path'); @@ -99,6 +124,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln(sprintf('Password : %s', $password)); $output->writeln(sprintf('Base path : %s', $path)); $output->writeln(sprintf('Base namespace : %s', $namespace)); + + $searchDomains = new SearchDomains(); + if ([] !== $onlyModels) { + $output->writeln(sprintf('List only those models : %s', implode(', ', $onlyModels))); + $onlyModels[] = 'base'; + $searchDomains->addCriterion(Criterion::in('model', $onlyModels)); + } + if ([] !== $excludeModels) { + $output->writeln(sprintf('Exclude those models : %s', implode(', ', $excludeModels))); + $searchDomains->addCriterion(Criterion::not_in('model', $excludeModels)); + } $output->writeln(''); $this->reconfigureServices( @@ -109,7 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); $output->write('Converting model structure to a class generator config array ... '); - $config = $this->odooModelsStructureConverter->convert($namespace); + $config = $this->odooModelsStructureConverter->convert($namespace, $searchDomains); $output->writeln('DONE'); $output->write('Generating model classes base on the generated config ... '); diff --git a/src/PhpGenerator/OdooModelsStructureConverter.php b/src/PhpGenerator/OdooModelsStructureConverter.php index 3ad249c..9a37817 100644 --- a/src/PhpGenerator/OdooModelsStructureConverter.php +++ b/src/PhpGenerator/OdooModelsStructureConverter.php @@ -8,6 +8,7 @@ use FluxSE\OdooApiClient\Model\BaseInterface; use FluxSE\OdooApiClient\Model\Object\AbstractBase; use FluxSE\OdooApiClient\Model\OdooRelation; +use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\Arguments\SearchDomainsInterface; use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\InspectionOperationsInterface; use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\Options\FieldsGetOptions; use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\Options\SearchReadOptions; @@ -43,7 +44,7 @@ public function __construct( $this->modelFixers = $modelFixers; } - public function convert(string $modelNamespace): array + public function convert(string $modelNamespace, SearchDomainsInterface $searchDomains = null): array { $config = []; @@ -61,7 +62,7 @@ public function convert(string $modelNamespace): array $modelList = $this->recordListOperations->search_read( 'ir.model', - null, + $searchDomains, $searchReadOptions ); @@ -107,7 +108,7 @@ public function getClassNameFormModelName(string $modelName): string return $this->modelNameToClass[$modelName]; } - throw new LogicException('The model name has not been found !'); + throw new LogicException(sprintf('The model name "%s" has not been found !', $modelName)); } /** @@ -341,7 +342,12 @@ private function buildModelPropertyDescription( : null ; $description[] = sprintf('Relation : %s (%s%s)', $fieldInfo['type'], $fieldInfo['relation'], $relationField); - $description[] = sprintf('@see \\%s\\%s', $baseModelNamespace, $this->getClassNameFormModelName($fieldInfo['relation'])); + try { + $relationClassName = $this->getClassNameFormModelName($fieldInfo['relation']); + $description[] = sprintf('@see \\%s\\%s', $baseModelNamespace, $relationClassName); + } catch (LogicException $e) { + $description[] = sprintf('@see %s (not generated)', $fieldInfo['relation']); + } } $description[] = '---'; diff --git a/src/PhpGenerator/OdooModelsStructureConverterInterface.php b/src/PhpGenerator/OdooModelsStructureConverterInterface.php index 9638222..ec40fe8 100644 --- a/src/PhpGenerator/OdooModelsStructureConverterInterface.php +++ b/src/PhpGenerator/OdooModelsStructureConverterInterface.php @@ -4,9 +4,11 @@ namespace FluxSE\OdooApiClient\PhpGenerator; +use FluxSE\OdooApiClient\Operations\Object\ExecuteKw\Arguments\SearchDomainsInterface; + interface OdooModelsStructureConverterInterface { public const BASE_MODEL_NAME = 'base'; - public function convert(string $modelNamespace): array; + public function convert(string $modelNamespace, SearchDomainsInterface $searchDomains = null): array; }