From 51751804229fdd0b09c570d30056171e21c344c0 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Fri, 22 Dec 2023 16:20:10 +0100 Subject: [PATCH] Allow screenshots to be loaded from a folder --- composer.json | 1 + src/Command/GenerateManifestCommand.php | 36 ++++++++++++++++++++++ tests/config.php | 23 +------------- tests/images/{ => screenshots}/360x800.svg | 0 tests/images/{ => screenshots}/390x844.svg | 0 tests/images/{ => screenshots}/600x400.svg | 0 tests/images/{ => screenshots}/780x360.svg | 0 tests/images/{ => screenshots}/915x412.svg | 0 8 files changed, 38 insertions(+), 22 deletions(-) rename tests/images/{ => screenshots}/360x800.svg (100%) rename tests/images/{ => screenshots}/390x844.svg (100%) rename tests/images/{ => screenshots}/600x400.svg (100%) rename tests/images/{ => screenshots}/780x360.svg (100%) rename tests/images/{ => screenshots}/915x412.svg (100%) diff --git a/composer.json b/composer.json index d7e7a56..376e73c 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/config": "^6.4|^7.0", "symfony/dependency-injection": "^6.4|^7.0", "symfony/filesystem": "^6.4|^7.0", + "symfony/finder": "^6.4", "symfony/http-kernel": "^6.4|^7.0", "symfony/mime": "^6.4|^7.0", "symfony/routing": "^6.4|^7.0" diff --git a/src/Command/GenerateManifestCommand.php b/src/Command/GenerateManifestCommand.php index 54beb15..fd67a09 100644 --- a/src/Command/GenerateManifestCommand.php +++ b/src/Command/GenerateManifestCommand.php @@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Routing\RouterInterface; use function count; @@ -243,7 +244,20 @@ private function processScreenshots(SymfonyStyle $io, array $manifest): array|in $progressBar = $io->createProgressBar(count($this->config['screenshots'])); $progressBar->start(); $io->info('Processing screenshots'); + $config = []; foreach ($this->config['screenshots'] as $screenshot) { + $src = $screenshot['src']; + if (! $this->filesystem->exists($src)) { + continue; + } + foreach ($this->findImages($src) as $image) { + $data = $screenshot; + $data['src'] = $image; + $config[] = $data; + } + } + + foreach ($config as $screenshot) { $this->processProgressBar($progressBar, 'screenshot', $screenshot['src']); $data = $this->loadFileAndConvert($screenshot['src'], null, $screenshot['format'] ?? null); if ($data === null) { @@ -391,4 +405,26 @@ private function processActions(SymfonyStyle $io, array $manifest): array|int return $manifest; } + + /** + * @return iterable + */ + private function findImages(string $src): iterable + { + $finder = new Finder(); + if (is_file($src)) { + yield $src; + return; + } + $files = $finder->in($src) + ->files() + ->name('/\.(png|jpg|jpeg|gif|webp|svg)$/i'); + foreach ($files as $file) { + if ($file->isFile()) { + yield $file->getRealPath(); + } else { + yield from $this->findImages($file->getRealPath()); + } + } + } } diff --git a/tests/config.php b/tests/config.php index 8c68ea1..42247aa 100644 --- a/tests/config.php +++ b/tests/config.php @@ -106,31 +106,10 @@ 'theme_color' => 'red', 'screenshots' => [ [ - 'src' => sprintf('%s/images/360x800.svg', __DIR__), - 'label' => 'Homescreen of Awesome App', + 'src' => sprintf('%s/images/screenshots', __DIR__), 'platform' => 'android', 'format' => 'png', ], - [ - 'src' => sprintf('%s/images/390x844.svg', __DIR__), - 'label' => 'List of Awesome Resources available in Awesome App', - 'platform' => 'windows', - 'format' => 'jpeg', - ], - [ - 'src' => sprintf('%s/images/600x400.svg', __DIR__), - 'label' => 'Awesome App in action (1)', - 'format' => 'webp', - ], - [ - 'src' => sprintf('%s/images/780x360.svg', __DIR__), - 'label' => 'Awesome App in action (2)', - 'format' => 'webp', - ], - [ - 'src' => sprintf('%s/images/915x412.svg', __DIR__), - 'label' => 'Awesome App in action (3)', - ], ], 'share_target' => [ 'action' => '/shared-content-receiver/', diff --git a/tests/images/360x800.svg b/tests/images/screenshots/360x800.svg similarity index 100% rename from tests/images/360x800.svg rename to tests/images/screenshots/360x800.svg diff --git a/tests/images/390x844.svg b/tests/images/screenshots/390x844.svg similarity index 100% rename from tests/images/390x844.svg rename to tests/images/screenshots/390x844.svg diff --git a/tests/images/600x400.svg b/tests/images/screenshots/600x400.svg similarity index 100% rename from tests/images/600x400.svg rename to tests/images/screenshots/600x400.svg diff --git a/tests/images/780x360.svg b/tests/images/screenshots/780x360.svg similarity index 100% rename from tests/images/780x360.svg rename to tests/images/screenshots/780x360.svg diff --git a/tests/images/915x412.svg b/tests/images/screenshots/915x412.svg similarity index 100% rename from tests/images/915x412.svg rename to tests/images/screenshots/915x412.svg