8000 [AssetMapper] Add command to download missing downloaded packages by jmsche · Pull Request #51351 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Add command to download missing downloaded packages #51351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\AssetMapper\Command\AssetMapperCompileCommand;
use Symfony\Component\AssetMapper\Command\DebugAssetMapperCommand;
use Symfony\Component\AssetMapper\Command\ImportMapExportCommand;
use Symfony\Component\AssetMapper\Command\ImportMapInstallCommand;
use Symfony\Component\AssetMapper\Command\ImportMapRemoveCommand;
use Symfony\Component\AssetMapper\Command\ImportMapRequireCommand;
use Symfony\Component\AssetMapper\Command\ImportMapUpdateCommand;
Expand Down Expand Up @@ -202,5 +203,9 @@
->set('asset_mapper.importmap.command.export', ImportMapExportCommand::class)
->args([service('asset_mapper.importmap.manager')])
->tag('console.command')

->set('asset_mapper.importmap.command.install', ImportMapInstallCommand::class)
->args([service('asset_mapper.importmap.manager')])
->tag('console.command')
;
};
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"doctrine/persistence": "^1.3|^2|^3",
"seld/jsonlint": "^1.10",
"symfony/asset": "^5.4|^6.0|^7.0",
"symfony/asset-mapper": "^6.3|^7.0",
"symfony/asset-mapper": "^6.4|^7.0",
"symfony/browser-kit": "^5.4|^6.0|^7.0",
"symfony/console": "^5.4.9|^6.0.9|^7.0",
"symfony/clock": "^6.2|^7.0",
Expand Down Expand Up @@ -79,6 +79,7 @@
"phpdocumentor/reflection-docblock": "<3.2.2",
"phpdocumentor/type-resolver": "<1.4.0",
"symfony/asset": "<5.4",
"symfony/asset-mapper": "<6.4",
"symfony/clock": "<6.3",
"symfony/console": "<5.4",
"symfony/dotenv": "<5.4",
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/AssetMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Mark the component as non experimental
* Add a `importmap:install` command to download all missing downloaded packages

6.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AssetMapper\Command;

use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Downloads all assets that should be downloaded.
*
* @author Jonathan Scheiber <contact@jmsche.fr>
*/
#[AsCommand(name: 'importmap:install', description: 'Downloads all assets that should be downloaded.')]
final class ImportMapInstallCommand extends Command
{
public function __construct(
private readonly ImportMapManager $importMapManager,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$downloadedPackages = $this->importMapManager->downloadMissingPackages();
$io->success(sprintf('Downloaded %d assets.', \count($downloadedPackages)));

return Command::SUCCESS;
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\AssetMapper\AssetDependency;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\AssetMapper\ImportMap\Resolver\PackageResolverInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\VarExporter\VarExporter;
Expand Down Expand Up @@ -108,6 +109,36 @@ public function update(): array
return $this->updateImportMapConfig(true, [], []);
}

/**
* Downloads all missing downloaded packages.
*
* @return ImportMapEntry[] The downloaded packages
*/
public function downloadMissingPackages(): array
{
$entries = $this->loadImportMapEntries();
$packagesToDownload = [];

foreach ($entries as $entry) {
if (!$entry->isDownloaded || $this->assetMapper->getAsset($entry->path)) {
continue;
}

$parts = self::parsePackageName($entry->url);

$packagesToDownload[] = new PackageRequireOptions(
$parts['package'],
$parts['version'] ?? throw new RuntimeException(sprintf('Cannot get a version for the "%s" package.', $parts['package'])),
true,
$entry->preload,
$parts['alias'] ?? $parts['package'],
isset($parts['registry']) && $parts['registry'] ? $parts['registry'] : null,
);
}

return $this->require($packagesToDownload);
}

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,43 @@ public function testUpdate()
$this->assertSame('contents of cowsay.js', $actualContents);
}

public function testDownloadMissingPackages()
{
$rootDir = __DIR__.'/../fixtures/download';
$manager = $this->createImportMapManager(['assets' => ''], $rootDir);

$this->packageResolver->expects($this->once())
->method('resolvePackages')
->willReturn([
self::resolvedPackage('@hotwired/stimulus', 'https://cdn.jsdelivr.net/npm/stimulus@3.2.1/+esm', true, content: 'contents of stimulus.js'),
])
;

$downloadedPackages = $manager->downloadMissingPackages();
$actualImportMap = require $rootDir.'/importmap.php';
$expectedImportMap = [
'@hotwired/stimulus' => [
'downloaded_to' => 'vendor/@hotwired/stimulus.js',
'url' => 'https://cdn.jsdelivr.net/npm/stimulus@3.2.1/+esm',
],
'lodash' => [
'downloaded_to' => 'vendor/lodash.js',
'url' => 'https://ga.jspm.io/npm:lodash@4.17.21/lodash.js',
],
];
$this->assertEquals($expectedImportMap, $actualImportMap);

$expectedDownloadedFiles = [
'assets/vendor/@hotwired/stimulus.js' => 'contents of stimulus.js',
];
foreach ($expectedDownloadedFiles as $file => $expectedContents) {
$this->assertFileExists($rootDir.'/'.$file);
$actualContents = file_get_contents($rootDir.'/'.$file);
$this->assertSame($expectedContents, $actualContents);
unlink($rootDir.'/'.$file);
}
}

/**
* @dataProvider getPackageNameTests
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('fake downloaded lodash.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'@hotwired/stimulus' => [
'downloaded_to' => 'vendor/@hotwired/stimulus.js',
'url' => 'https://cdn.jsdelivr.net/npm/stimulus@3.2.1/+esm',
],
'lodash' => [
'downloaded_to' => 'vendor/lodash.js',
'url' => 'https://ga.jspm.io/npm:lodash@4.17.21/lodash.js',
],
];
0