From e21999c84acb0decdc6c5cefa4acbf676dfb4a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Fri, 23 Nov 2018 11:45:17 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'une=20option=20=C3=A0=20ng-model:gene?= =?UTF-8?q?rate=20pour=20limiter=20les=20fichiers=20g=C3=A9n=C3=A9r=C3=A9s?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Command/NgModelGenerateCommand.php | 17 ++++++- src/Writers/FilteringFileWriter.php | 65 ++++++++++++++++++++++++++ src/Writers/NullWriter.php | 45 ++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/Writers/FilteringFileWriter.php create mode 100644 src/Writers/NullWriter.php diff --git a/src/Command/NgModelGenerateCommand.php b/src/Command/NgModelGenerateCommand.php index 8004451..9359fcf 100644 --- a/src/Command/NgModelGenerateCommand.php +++ b/src/Command/NgModelGenerateCommand.php @@ -23,6 +23,7 @@ use ApiPlatform\Core\Documentation\Documentation; use Irstea\NgModelGeneratorBundle\ModelGenerator; use Irstea\NgModelGeneratorBundle\Writers\ConsoleWriter; use Irstea\NgModelGeneratorBundle\Writers\DirectoryWriter; +use Irstea\NgModelGeneratorBundle\Writers\FilteringFileWriter; use Irstea\NgModelGeneratorBundle\Writers\MultiFileWriter; use Irstea\NgModelGeneratorBundle\Writers\PhonyFileWriter; use Irstea\NgModelGeneratorBundle\Writers\ZipWriter; @@ -72,7 +73,8 @@ final class NgModelGenerateCommand extends Command ->setDescription('Dump Typescript representations') ->addOption('zip', 'z', InputOption::VALUE_REQUIRED, 'Archive ZIP à créer') ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Dossier de destination') - ->addOption('force', 'f', InputOption::VALUE_NONE, 'Ecrase la cible'); + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Ecrase la cible') + ->addOption('restrict', 'r', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Limite la sortie aux modèles de nom de fichier'); } /** @@ -85,6 +87,19 @@ final class NgModelGenerateCommand extends Command $output ); + if ($input->getOption('restrict')) { + $globs = $input->getOption('restrict'); + $writer = new FilteringFileWriter($writer, function (string $path) use ($globs): bool { + foreach ($globs as $glob) { + if (fnmatch($glob, $path, FNM_PATHNAME)) { + return true; + } + } + + return false; + }); + } + try { $this->generator->generate($this->documentation, $writer); } finally { diff --git a/src/Writers/FilteringFileWriter.php b/src/Writers/FilteringFileWriter.php new file mode 100644 index 0000000..a617718 --- /dev/null +++ b/src/Writers/FilteringFileWriter.php @@ -0,0 +1,65 @@ +<?php declare(strict_types=1); +/* + * irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata. + * Copyright (C) 2018 IRSTEA + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License and the GNU + * Lesser General Public License along with this program. If not, see + * <https://www.gnu.org/licenses/>. + */ + +namespace Irstea\NgModelGeneratorBundle\Writers; + +/** + * Class FilteringFileWriter. + */ +final class FilteringFileWriter implements MultiFileWriter +{ + /** + * @var MultiFileWriter + */ + private $writer; + + /** + * @var callable + */ + private $accept; + + /** + * FilteringFileWriter constructor. + * + * @param MultiFileWriter $writer + * @param callable $accept + */ + public function __construct(MultiFileWriter $writer, callable $accept) + { + $this->writer = $writer; + $this->accept = $accept; + } + + /** + * {@inheritdoc} + */ + public function newFile(string $path): Writer + { + if (!($this->accept)($path)) { + return new NullWriter(); + } + + return $this->writer->newFile($path); + } + + public function close(): void + { + $this->writer->close(); + } +} diff --git a/src/Writers/NullWriter.php b/src/Writers/NullWriter.php new file mode 100644 index 0000000..9106895 --- /dev/null +++ b/src/Writers/NullWriter.php @@ -0,0 +1,45 @@ +<?php declare(strict_types=1); +/* + * irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata. + * Copyright (C) 2018 IRSTEA + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License and the GNU + * Lesser General Public License along with this program. If not, see + * <https://www.gnu.org/licenses/>. + */ + +namespace Irstea\NgModelGeneratorBundle\Writers; + +/** + * Class NullWriter. + */ +class NullWriter implements MultiFileWriter, Writer +{ + /** + * {@inheritdoc} + */ + public function newFile(string $path): Writer + { + return $this; + } + + public function close(): void + { + } + + /** + * {@inheritdoc} + */ + public function write(string $data): void + { + } +} -- GitLab