From 337c4655f3ff0ba991a39f16e2f673d4787401f9 Mon Sep 17 00:00:00 2001 From: Florian Cellier Date: Mon, 18 Nov 2024 12:13:09 +0100 Subject: [PATCH] [AssetMapper] Add `--dry-run` option on `importmap:require` command --- UPGRADE-7.3.md | 5 + .../Resources/config/asset_mapper.php | 1 + .../Component/AssetMapper/CHANGELOG.md | 2 + .../Command/ImportMapRequireCommand.php | 52 ++++- .../ImportMap/ImportMapManager.php | 4 +- .../Command/ImportMapRequireCommandTest.php | 218 ++++++++++++++++++ .../Fixtures/AssetMapperTestAppKernel.php | 2 +- .../Tests/Fixtures/ImportMapTestAppKernel.php | 56 +++++ 8 files changed, 329 insertions(+), 11 deletions(-) create mode 100644 src/Symfony/Component/AssetMapper/Tests/Command/ImportMapRequireCommandTest.php create mode 100644 src/Symfony/Component/AssetMapper/Tests/Fixtures/ImportMapTestAppKernel.php diff --git a/UPGRADE-7.3.md b/UPGRADE-7.3.md index dd982a1b392e9..fced14c227469 100644 --- a/UPGRADE-7.3.md +++ b/UPGRADE-7.3.md @@ -191,3 +191,8 @@ VarDumper * Deprecate `ResourceCaster::castCurl()`, `ResourceCaster::castGd()` and `ResourceCaster::castOpensslX509()` * Mark all casters as `@internal` + +AssetMapper +----------- + + * `ImportMapRequireCommand` now takes `projectDir` as a required third constructor argument diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php index c187558641079..eeb1ceb4f8962 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php @@ -232,6 +232,7 @@ ->args([ service('asset_mapper.importmap.manager'), service('asset_mapper.importmap.version_checker'), + param('kernel.project_dir'), ]) ->tag('console.command') diff --git a/src/Symfony/Component/AssetMapper/CHANGELOG.md b/src/Symfony/Component/AssetMapper/CHANGELOG.md index dce7c57aad41e..93d622101c0c8 100644 --- a/src/Symfony/Component/AssetMapper/CHANGELOG.md +++ b/src/Symfony/Component/AssetMapper/CHANGELOG.md @@ -5,6 +5,8 @@ CHANGELOG --- * Add support for pre-compressing assets with Brotli, Zstandard, Zopfli, and gzip + * Add option `--dry-run` to `importmap:require` command + * `ImportMapRequireCommand` now takes `projectDir` as a required third constructor argument 7.2 --- diff --git a/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php b/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php index b3ccb1de2b96a..3a1efabc9cd7b 100644 --- a/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php +++ b/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php @@ -11,6 +11,7 @@ namespace Symfony\Component\AssetMapper\Command; +use Symfony\Component\AssetMapper\ImportMap\ImportMapEntries; use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry; use Symfony\Component\AssetMapper\ImportMap\ImportMapManager; use Symfony\Component\AssetMapper\ImportMap\ImportMapVersionChecker; @@ -22,6 +23,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Filesystem\Path; /** * @author Kévin Dunglas @@ -34,7 +36,12 @@ final class ImportMapRequireCommand extends Command public function __construct( private readonly ImportMapManager $importMapManager, private readonly ImportMapVersionChecker $importMapVersionChecker, + private readonly ?string $projectDir = null, ) { + if (null === $projectDir) { + trigger_deprecation('symfony/asset-mapper', '7.3', 'The "%s()" method will have a new `string $projectDir` argument in version 8.0, not defining it is deprecated.', __METHOD__); + } + parent::__construct(); } @@ -42,8 +49,9 @@ protected function configure(): void { $this ->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The packages to add') - ->addOption('entrypoint', null, InputOption::VALUE_NONE, 'Make the package(s) an entrypoint?') + ->addOption('entrypoint', null, InputOption::VALUE_NONE, 'Make the packages an entrypoint?') ->addOption('path', null, InputOption::VALUE_REQUIRED, 'The local path where the package lives relative to the project root') + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Simulate the installation of the packages') ->setHelp(<<<'EOT' The %command.name% command adds packages to importmap.php usually by finding a CDN URL for the given package and version. @@ -72,6 +80,11 @@ protected function configure(): void php %command.full_name% "any_module_name" --path=./assets/some_file.js +To simulate the installation, use the --dry-run option: + + php %command.full_name% "any_module_name" --dry-run -v + +When this option is enabled, this command does not perform any write operations to the filesystem. EOT ); } @@ -92,6 +105,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $path = $input->getOption('path'); } + if ($input->getOption('dry-run')) { + $io->writeln(['', '[DRY-RUN] No changes will apply to the importmap configuration.', '']); + } + $packages = []; foreach ($packageList as $packageName) { $parts = ImportMapManager::parsePackageName($packageName); @@ -110,21 +127,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); } - $newPackages = $this->importMapManager->require($packages); + if ($input->getOption('dry-run')) { + $newPackages = $this->importMapManager->requirePackages($packages, new ImportMapEntries()); + } else { + $newPackages = $this->importMapManager->require($packages); + } $this->renderVersionProblems($this->importMapVersionChecker, $output); - if (1 === \count($newPackages)) { - $newPackage = $newPackages[0]; - $message = \sprintf('Package "%s" added to importmap.php', $newPackage->importName); + $newPackageNames = array_map(fn (ImportMapEntry $package): string => $package->importName, $newPackages); - $message .= '.'; + if (1 === \count($newPackages)) { + $messages = [\sprintf('Package "%s" added to importmap.php.', $newPackageNames[0])]; } else { - $names = array_map(fn (ImportMapEntry $package) => $package->importName, $newPackages); - $message = \sprintf('%d new items (%s) added to the importmap.php!', \count($newPackages), implode(', ', $names)); + $messages = [\sprintf('%d new items (%s) added to the importmap.php!', \count($newPackages), implode(', ', $newPackageNames))]; } - $messages = [$message]; + if ($io->isVerbose()) { + $io->table( + ['Package', 'Version', 'Path'], + array_map(fn (ImportMapEntry $package): array => [ + $package->importName, + $package->version ?? '-', + // BC layer for AssetMapper < 7.3 + // When `projectDir` is not null, we use the absolute path of the package + null !== $this->projectDir ? Path::makeRelative($package->path, $this->projectDir) : $package->path, + ], $newPackages), + ); + } if (1 === \count($newPackages)) { $messages[] = \sprintf('Use the new package normally by importing "%s".', $newPackages[0]->importName); @@ -132,6 +162,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->success($messages); + if ($input->getOption('dry-run')) { + $io->writeln(['[DRY-RUN] No changes applied to the importmap configuration.', '']); + } + return Command::SUCCESS; } } diff --git a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php index 4a12a6a083728..00c265bc4635d 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php @@ -128,13 +128,15 @@ private function updateImportMapConfig(bool $update, array $packagesToRequire, a } /** + * @internal + * * Gets information about (and optionally downloads) the packages & updates the entries. * * Returns an array of the entries that were added. * * @param PackageRequireOptions[] $packagesToRequire */ - private function requirePackages(array $packagesToRequire, ImportMapEntries $importMapEntries): array + public function requirePackages(array $packagesToRequire, ImportMapEntries $importMapEntries): array { if (!$packagesToRequire) { return []; diff --git a/src/Symfony/Component/AssetMapper/Tests/Command/ImportMapRequireCommandTest.php b/src/Symfony/Component/AssetMapper/Tests/Command/ImportMapRequireCommandTest.php new file mode 100644 index 0000000000000..fb410bafab2a4 --- /dev/null +++ b/src/Symfony/Component/AssetMapper/Tests/Command/ImportMapRequireCommandTest.php @@ -0,0 +1,218 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\AssetMapper\Tests\Command; + +use PHPUnit\Framework\Attributes\DataProvider; +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\AssetMapper\Command\ImportMapRequireCommand; +use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry; +use Symfony\Component\AssetMapper\ImportMap\ImportMapManager; +use Symfony\Component\AssetMapper\ImportMap\ImportMapType; +use Symfony\Component\AssetMapper\ImportMap\ImportMapVersionChecker; +use Symfony\Component\AssetMapper\Tests\Fixtures\ImportMapTestAppKernel; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; + +class ImportMapRequireCommandTest extends KernelTestCase +{ + protected static function getKernelClass(): string + { + return ImportMapTestAppKernel::class; + } + + /** + * @dataProvider getRequirePackageTests + */ + public function testDryRunOptionToShowInformationBeforeApplyInstallation(int $verbosity, array $packageEntries, array $packagesToInstall, string $expected, ?string $path = null) + { + $importMapManager = $this->createMock(ImportMapManager::class); + $importMapManager + ->method('requirePackages') + ->willReturn($packageEntries) + ; + + $command = new ImportMapRequireCommand( + $importMapManager, + $this->createMock(ImportMapVersionChecker::class), + '/path/to/project/dir', + ); + + $args = [ + 'packages' => $packagesToInstall, + '--dry-run' => true, + ]; + if ($path) { + $args['--path'] = $path; + } + + $commandTester = new CommandTester($command); + $commandTester->execute($args, ['verbosity' => $verbosity]); + + $commandTester->assertCommandIsSuccessful(); + + $output = $commandTester->getDisplay(); + $this->assertEquals($this->trimBeginEndOfEachLine($expected), $this->trimBeginEndOfEachLine($output)); + } + + public static function getRequirePackageTests(): iterable + { + yield 'require package with dry run and normal verbosity options' => [ + OutputInterface::VERBOSITY_NORMAL, + [self::createRemoteEntry('bootstrap', '4.2.3', 'assets/vendor/bootstrap/bootstrap.js')], + ['bootstrap'], << [ + OutputInterface::VERBOSITY_VERBOSE, + [self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.js')], + ['bootstrap'], << [ + OutputInterface::VERBOSITY_VERBOSE, + [ImportMapEntry::createLocal('alice.js', ImportMapType::JS, 'assets/js/alice.js', false)], + ['alice.js'], << [ + OutputInterface::VERBOSITY_NORMAL, [ + self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.index.js'), + self::createRemoteEntry('lodash', '4.17.20', 'assets/vendor/lodash/lodash.index.js'), + ], + ['bootstrap lodash@4.17.21'], << [ + OutputInterface::VERBOSITY_VERBOSE, [ + self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.js'), + self::createRemoteEntry('lodash', '4.17.20', 'assets/vendor/lodash/lodash.index.js'), + ], + ['bootstrap lodash@4.17.21'], <<getProjectDir(); + + $fs = new Filesystem(); + $fs->mkdir($projectDir.'/public'); + + $fs->dumpFile($projectDir.'/public/assets/manifest.json', '{}'); + $fs->dumpFile($projectDir.'/public/assets/importmap.json', '{}'); + + $importMapManager = $this->createMock(ImportMapManager::class); + $importMapManager + ->expects($this->once()) + ->method('requirePackages') + ->willReturn([self::createRemoteEntry('bootstrap', '5.3.3', 'assets/vendor/bootstrap/bootstrap.index.js')]); + + self::getContainer()->set(ImportMapManager::class, $importMapManager); + + $application = new Application(self::$kernel); + $command = $application->find('importmap:require'); + + $importMapContentBefore = $fs->readFile($projectDir.'/importmap.php'); + $installedVendorBefore = $fs->readFile($projectDir.'/assets/vendor/installed.php'); + + $tester = new CommandTester($command); + $tester->execute(['packages' => ['bootstrap'], '--dry-run' => true]); + + $tester->assertCommandIsSuccessful(); + + $this->assertSame($importMapContentBefore, $fs->readFile($projectDir.'/importmap.php')); + $this->assertSame($installedVendorBefore, $fs->readFile($projectDir.'/assets/vendor/installed.php')); + + $this->assertSame('{}', $fs->readFile($projectDir.'/public/assets/manifest.json')); + $this->assertSame('{}', $fs->readFile($projectDir.'/public/assets/importmap.json')); + + $finder = new Finder(); + $finder->in($projectDir.'/public/assets')->files()->depth(0); + + $this->assertCount(2, $finder); // manifest.json + importmap.json + + $fs->remove($projectDir.'/public'); + $fs->remove($projectDir.'/var'); + + static::$kernel->shutdown(); + } + + private static function createRemoteEntry(string $importName, string $version, ?string $path = null): ImportMapEntry + { + return ImportMapEntry::createRemote($importName, ImportMapType::JS, path: $path, version: $version, packageModuleSpecifier: $importName, isEntrypoint: false); + } + + private function trimBeginEndOfEachLine(string $lines): string + { + return trim(implode("\n", array_map('trim', explode("\n", $lines)))); + } +} diff --git a/src/Symfony/Component/AssetMapper/Tests/Fixtures/AssetMapperTestAppKernel.php b/src/Symfony/Component/AssetMapper/Tests/Fixtures/AssetMapperTestAppKernel.php index 48958274572d3..426e97b810cfd 100644 --- a/src/Symfony/Component/AssetMapper/Tests/Fixtures/AssetMapperTestAppKernel.php +++ b/src/Symfony/Component/AssetMapper/Tests/Fixtures/AssetMapperTestAppKernel.php @@ -44,7 +44,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void 'assets' => null, 'asset_mapper' => [ 'paths' => ['dir1', 'dir2', 'non_ascii', 'assets'], - 'public_prefix' => 'assets' + 'public_prefix' => 'assets', ], 'test' => true, ]); diff --git a/src/Symfony/Component/AssetMapper/Tests/Fixtures/ImportMapTestAppKernel.php b/src/Symfony/Component/AssetMapper/Tests/Fixtures/ImportMapTestAppKernel.php new file mode 100644 index 0000000000000..42d4b65d2af6a --- /dev/null +++ b/src/Symfony/Component/AssetMapper/Tests/Fixtures/ImportMapTestAppKernel.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\AssetMapper\Tests\Fixtures; + +use Psr\Log\NullLogger; +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Kernel; + +class ImportMapTestAppKernel extends Kernel +{ + public function registerBundles(): iterable + { + return [ + new FrameworkBundle(), + ]; + } + + public function getProjectDir(): string + { + return __DIR__; + } + + public function registerContainerConfiguration(LoaderInterface $loader): void + { + $loader->load(static function (ContainerBuilder $container) { + $container->loadFromExtension('framework', [ + 'annotations' => false, + 'http_method_override' => false, + 'handle_all_throwables' => true, + 'php_errors' => ['log' => true], + 'http_client' => true, + 'assets' => null, + 'asset_mapper' => [ + 'paths' => ['assets'], + ], + 'test' => true, + ]); + }); + } + + protected function build(ContainerBuilder $container): void + { + $container->register('logger', NullLogger::class); + } +}