Skip to content

Commit

Permalink
Allow to filter the generated model (include/exclude)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Jan 20, 2024
1 parent e8f3a00 commit 2c5629d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
38 changes: 37 additions & 1 deletion src/Command/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
)
;
}

Expand All @@ -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');
Expand All @@ -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('</comment>');

$this->reconfigureServices(
Expand All @@ -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 ... ');
Expand Down
14 changes: 10 additions & 4 deletions src/PhpGenerator/OdooModelsStructureConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];

Expand All @@ -61,7 +62,7 @@ public function convert(string $modelNamespace): array

$modelList = $this->recordListOperations->search_read(
'ir.model',
null,
$searchDomains,
$searchReadOptions
);

Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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[] = '---';
Expand Down
4 changes: 3 additions & 1 deletion src/PhpGenerator/OdooModelsStructureConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2c5629d

Please sign in to comment.