Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
[AssetMapper] Add command to download missing downloaded packages
  • Loading branch information
jmsche authored and nicolas-grekas committed Aug 23, 2023
commit a82a42914b1f8f8418e4145df8c51e06acd51b31
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 <[email protected]>
*
* 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 <[email protected]>
*/
#[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/[email protected]/+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/[email protected]/+esm',
],
'lodash' => [
'downloaded_to' => 'vendor/lodash.js',
'url' => 'https://ga.jspm.io/npm:[email protected]/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 <[email protected]>
*
* 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/[email protected]/+esm',
],
'lodash' => [
'downloaded_to' => 'vendor/lodash.js',
'url' => 'https://ga.jspm.io/npm:[email protected]/lodash.js',
],
];