Skip to content

Commit

Permalink
Merge branch '1.x' into 2.x
Browse files Browse the repository at this point in the history
* 1.x:
  AssetMapper: upgrade packages when needed
  • Loading branch information
fabpot committed Oct 22, 2023
2 parents 84de0f9 + 884a881 commit 84d8824
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private function synchronizePackageJson(string $rootDir)
$vendorDir = trim((new Filesystem())->makePathRelative($this->config->get('vendor-dir'), $rootDir), '/');

$executor = new ScriptExecutor($this->composer, $this->io, $this->options);
$synchronizer = new PackageJsonSynchronizer($rootDir, $vendorDir, $executor);
$synchronizer = new PackageJsonSynchronizer($rootDir, $vendorDir, $executor, $this->io);

if ($synchronizer->shouldSynchronize()) {
$lockData = $this->composer->getLocker()->getLockData();
Expand Down
33 changes: 20 additions & 13 deletions src/PackageJsonSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Flex;

use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Semver\Semver;
use Composer\Semver\VersionParser;
use Seld\JsonLint\ParsingException;

Expand All @@ -25,13 +27,15 @@ class PackageJsonSynchronizer
private $rootDir;
private $vendorDir;
private $scriptExecutor;
private $io;
private $versionParser;

public function __construct(string $rootDir, string $vendorDir, ScriptExecutor $scriptExecutor)
public function __construct(string $rootDir, string $vendorDir, ScriptExecutor $scriptExecutor, IOInterface $io)
{
$this->rootDir = $rootDir;
$this->vendorDir = $vendorDir;
$this->scriptExecutor = $scriptExecutor;
$this->io = $io;
$this->versionParser = new VersionParser();
}

Expand Down Expand Up @@ -147,11 +151,9 @@ private function resolveImportMapPackages($phpPackage): array
foreach ($packageJson->read()['symfony']['importmap'] ?? [] as $importMapName => $constraintConfig) {
if (\is_array($constraintConfig)) {
$constraint = $constraintConfig['version'] ?? [];
$preload = $constraintConfig['preload'] ?? false;
$package = $constraintConfig['package'] ?? $importMapName;
} else {
$constraint = $constraintConfig;
$preload = false;
$package = $importMapName;
}

Expand All @@ -161,7 +163,6 @@ private function resolveImportMapPackages($phpPackage): array

$dependencies[$importMapName] = [
'path' => $path,
'preload' => $preload,
];

continue;
Expand All @@ -170,7 +171,6 @@ private function resolveImportMapPackages($phpPackage): array
$dependencies[$importMapName] = [
'version' => $constraint,
'package' => $package,
'preload' => $preload,
];
}

Expand Down Expand Up @@ -239,7 +239,7 @@ private function shouldUpdateConstraint(string $existingConstraint, string $cons
}

/**
* @param array<string, array{path?: string, preload: bool, package?: string, version?: string}> $importMapEntries
* @param array<string, array{path?: string, package?: string, version?: string}> $importMapEntries
*/
private function updateImportMap(array $importMapEntries): void
{
Expand All @@ -251,14 +251,24 @@ private function updateImportMap(array $importMapEntries): void

foreach ($importMapEntries as $name => $importMapEntry) {
if (isset($importMapData[$name])) {
continue;
if (!isset($importMapData[$name]['version'])) {
// AssetMapper 6.3
continue;
}

$version = $importMapData[$name]['version'];
$versionConstraint = $importMapEntry['version'] ?? null;

// if the version constraint is satisfied, skip - else, update the package
if (Semver::satisfies($version, $versionConstraint)) {
continue;
}

$this->io->writeError(sprintf('Updating package <comment>%s</> from <info>%s</> to <info>%s</>.', $name, $version, $versionConstraint));
}

if (isset($importMapEntry['path'])) {
$arguments = [$name, '--path='.$importMapEntry['path']];
if ($importMapEntry['preload']) {
$arguments[] = '--preload';
}
$this->scriptExecutor->execute(
'symfony-cmd',
'importmap:require',
Expand All @@ -274,9 +284,6 @@ private function updateImportMap(array $importMapEntries): void
$packageName .= '='.$name;
}
$arguments = [$packageName];
if ($importMapEntry['preload']) {
$arguments[] = '--preload';
}
$this->scriptExecutor->execute(
'symfony-cmd',
'importmap:require',
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/packageJson/elevated_dependencies_package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "symfony/fixture",
"dependencies": {
"@hotcookies": "^1.1|^2",
"@hotdogs": "^2",
"@hotcookies/bar": "^1.1|^2",
"@hotdogs/bun": "^2",
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/packageJson/stricter_constraints_package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "symfony/fixture",
"devDependencies": {
"@hotcookies": "^2",
"@hotdogs": "^1.9",
"@hotcookies/bar": "^2",
"@hotdogs/bun": "^1.9",
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets"
},
"browserslist": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
},
"peerDependencies": {
"@hotcookies": "^1.1|^2",
"@hotdogs": "^2"
"@hotcookies/bar": "^1.1|^2",
"@hotdogs/bun": "^2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
},
"entrypoints": ["admin.js"],
"importmap": {
"@hotcake": "^1.9.0",
"@hotcake/foo": "^1.9.0",
"@symfony/new-package": {
"version": "path:%PACKAGE%/dist/loader.js",
"preload": true
"version": "path:%PACKAGE%/dist/loader.js"
}
}
},
"peerDependencies": {
"@hotcookies": "^1.1"
"@hotcookies/bar": "^1.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"needsPackageAsADependency": false
},
"peerDependencies": {
"@hotcookies": "^1.1"
"@hotcookies/bar": "^1.1"
}
}
80 changes: 67 additions & 13 deletions tests/PackageJsonSynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Flex\Tests;

use Composer\IO\IOInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Flex\PackageJsonSynchronizer;
Expand All @@ -31,7 +32,8 @@ protected function setUp(): void
$this->synchronizer = new PackageJsonSynchronizer(
$this->tempDir,
'vendor',
$this->scriptExecutor
$this->scriptExecutor,
$this->createMock(IOInterface::class)
);
}

Expand Down Expand Up @@ -99,8 +101,8 @@ public function testSynchronizeExistingPackage()
[
'name' => 'symfony/fixture',
'devDependencies' => [
'@hotcookies' => '^1.1|^2',
'@hotdogs' => '^2',
'@hotcookies/bar' => '^1.1|^2',
'@hotdogs/bun' => '^2',
'@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
'@symfony/stimulus-bridge' => '^1.0.0',
'stimulus' => '^1.1.1',
Expand Down Expand Up @@ -151,7 +153,7 @@ public function testSynchronizeNewPackage()
'{
"name": "symfony/fixture",
"devDependencies": {
"@hotdogs": "^2",
"@hotdogs/bun": "^2",
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets",
"@symfony/new-package": "file:vendor/symfony/new-package/assets",
"@symfony/stimulus-bridge": "^1.0.0",
Expand Down Expand Up @@ -207,8 +209,8 @@ public function testArrayFormattingHasNotChanged()
'{
"name": "symfony/fixture",
"devDependencies": {
"@hotcookies": "^1.1|^2",
"@hotdogs": "^2",
"@hotcookies/bar": "^1.1|^2",
"@hotdogs/bun": "^2",
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets",
"@symfony/stimulus-bridge": "^1.0.0",
"stimulus": "^1.1.1"
Expand Down Expand Up @@ -237,8 +239,8 @@ public function testExistingElevatedPackage()
[
'name' => 'symfony/fixture',
'dependencies' => [
'@hotcookies' => '^1.1|^2',
'@hotdogs' => '^2',
'@hotcookies/bar' => '^1.1|^2',
'@hotdogs/bun' => '^2',
'@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
],
'devDependencies' => [
Expand Down Expand Up @@ -272,9 +274,9 @@ public function testStricterConstraintsAreKeptNonMatchingAreReplaced()
'name' => 'symfony/fixture',
'devDependencies' => [
// this satisfies the constraint, so it's kept
'@hotcookies' => '^2',
'@hotcookies/bar' => '^2',
// this was too low, so it's replaced
'@hotdogs' => '^2',
'@hotdogs/bun' => '^2',
'@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
],
'browserslist' => [
Expand Down Expand Up @@ -303,7 +305,7 @@ public function testSynchronizePackageWithoutNeedingFilePackage()
'{
"name": "symfony/fixture",
"devDependencies": {
"@hotdogs": "^2",
"@hotdogs/bun": "^2",
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets",
"@symfony/stimulus-bridge": "^1.0.0",
"stimulus": "^1.1.1"
Expand All @@ -324,12 +326,13 @@ public function testSynchronizeAssetMapperNewPackage()
$this->scriptExecutor->expects($this->exactly(2))
->method('execute')
->withConsecutive(
['symfony-cmd', 'importmap:require', ['@hotcake@^1.9.0']],
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath, '--preload']]
['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']],
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]]
);

$this->synchronizer->synchronize([
[
// no "importmap" specific config, but still registered as a controller
'name' => 'symfony/existing-package',
'keywords' => ['symfony-ux'],
],
Expand Down Expand Up @@ -384,4 +387,55 @@ public function testSynchronizeAssetMapperNewPackage()
json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true)
);
}

public function testSynchronizeAssetMapperUpgradesPackageIfNeeded()
{
$importMap = [
'@hotcake/foo' => [
// constraint in package.json is ^1.9.0
'version' => '1.8.0',
],
];
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));

$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
$this->scriptExecutor->expects($this->exactly(2))
->method('execute')
->withConsecutive(
['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']],
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]]
);

$this->synchronizer->synchronize([
[
'name' => 'symfony/new-package',
'keywords' => ['symfony-ux'],
],
]);
}

public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
{
$importMap = [
'@hotcake/foo' => [
// constraint in package.json is ^1.9.0
'version' => '1.9.1',
],
];
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));

$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
$this->scriptExecutor->expects($this->once())
->method('execute')
->withConsecutive(
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]]
);

$this->synchronizer->synchronize([
[
'name' => 'symfony/new-package',
'keywords' => ['symfony-ux'],
],
]);
}
}

0 comments on commit 84d8824

Please sign in to comment.