Skip to content

Commit

Permalink
COMOPS-1503: Updating to support Composer 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pdohogne-magento committed Dec 15, 2020
1 parent 44fd2e8 commit 243946b
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 61 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"AFL-3.0"
],
"require": {
"composer/composer": "<=1.10.15",
"composer-plugin-api": "^1.0"
"composer/composer": "<=1.10.19 || >=2.0.0 <=2.0.8",
"composer-plugin-api": "^1.0 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "~6.5.0"
Expand Down
2 changes: 2 additions & 0 deletions docs/class_descriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ This class manages the plugin's self-installation inside the `var` directory to
- **`packageEvent()`**
- When Composer installs or updates a required package, this method checks whether it was the plugin package that changed and calls `updateSetupWizardPlugin()` with the new version if so
- Triggered by the events defined in [PluginDefinition::getSubscribedEvents()](#plugindefinition)
- **`processEvent()`**
- Helper method used by `packageEvent()` to run `updateSetupWizardPlugin()` when an appropriate [PackageEvent](https://getcomposer.org/apidoc/master/Composer/Installer/PackageEvent.html) is fired
- **`doVarInstall()`**
- Checks the `composer.lock` file the plugin and calls `updateSetupWizardPlugin()` with the version found there
- Called by `composer magento-update-plugin install` and the Magento module setup classes ([InstallData](#installdatarecurringdataupgradedata), [RecurringData](#installdatarecurringdataupgradedata), [UpgradeData](#installdatarecurringdataupgradedata))
Expand Down
16 changes: 16 additions & 0 deletions src/Magento/ComposerRootUpdatePlugin/Plugin/PluginDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function activate(Composer $composer, IOInterface $io)
// Method must exist
}

/**
* @inheritdoc
*/
public function deactivate(Composer $composer, IOInterface $io)
{
// Method must exist
}

/**
* @inheritdoc
*/
public function uninstall(Composer $composer, IOInterface $io)
{
// Method must exist
}

/**
* @inheritdoc
*/
Expand Down
24 changes: 13 additions & 11 deletions src/Magento/ComposerRootUpdatePlugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

The `magento/composer-root-update-plugin` Composer plugin resolves changes that need to be made to the root project `composer.json` file before updating to a new Magento product requirement.

This is accomplished by comparing the root `composer.json` file for the Magento project corresponding to the Magento version and edition in the current installation with the Magento project `composer.json` file for the target Magento product package when the `composer require` command runs and applying any deltas found between the two files if they do not conflict with the existing `composer.json` file in the Magento root directory.
This is accomplished by comparing the root `composer.json` file for the Magento project corresponding to the Magento version and edition in the current installation with the Magento project `composer.json` file for the target Magento product or cloud metapackage when the `composer require` command runs and applying any deltas found between the two files if they do not conflict with the existing `composer.json` file in the Magento root directory.

# Getting Started

## System requirements

The `magento/composer-root-update-plugin` package requires Composer version 1.8.0 or earlier. Compatibility with newer Composer versions will be tested and added in future plugin versions.
The `magento/composer-root-update-plugin` package requires Composer version 1.10.19 or earlier, or version 2.0.0 - 2.0.8. Compatibility with newer Composer versions will be tested and added in future plugin versions.

## Installation

To install the plugin, run the following commands in the Magento root directory.

composer require magento/composer-root-update-plugin ~0.1 --no-update
composer require magento/composer-root-update-plugin ~1.1 --no-update
composer update

# Usage

The plugin adds functionality to the `composer require` command when a new Magento product package is required, and in most cases will not need additional options or commands run to function.
The plugin adds functionality to the `composer require` command when a new Magento product or cloud metapackage is required, and in most cases will not need additional options or commands run to function.

If the `composer require` command for the target Magento package fails, one of the following may be necessary.

Expand All @@ -33,9 +33,11 @@ In this case, run the following command with the appropriate values to correct t

composer require <current_Magento_package> <current_version> --base-magento-edition '<Open Source|Commerce>' --base-magento-version <original_Magento_version>

These options are not valid for Magento Cloud installations.

## Conflicting custom values

If the `composer.json` file has custom changes that do not match the values the plugin expects according to the installed Magento product, the entries may need to be corrected to values compatible with the target Magento package.
If the `composer.json` file has custom changes that do not match the values the plugin expects according to the installed Magento metapackage, the entries may need to be corrected to values compatible with the target Magento version.

To resolve these conflicts interactively, re-run the `composer require` command with the `--interactive-magento-conflicts` option.

Expand Down Expand Up @@ -128,23 +130,23 @@ For reference, these are the `"require"` and `"require-dev"` sections for defaul

### With `magento/composer-root-update-plugin`:

In the project directory for a Magento Open Source 2.2.8 installation, a user runs `composer require magento/composer-root-update-plugin ~0.1 --no-update` and `composer update` before the Magento Open Source 2.3.1 upgrade commands.
In the project directory for a Magento Open Source 2.2.8 installation, a user runs `composer require magento/composer-root-update-plugin ~1.1 --no-update` and `composer update` before the Magento Open Source 2.3.1 upgrade commands.

```
$ composer require magento/composer-root-update-plugin ~0.1 --no-update
$ composer require magento/composer-root-update-plugin ~1.1 --no-update
./composer.json has been updated
$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing magento/composer-root-update-plugin (0.1.0): Downloading (100%)
Installing "magento/composer-root-update-plugin: 0.1.0" for the Web Setup Wizard
- Installing magento/composer-root-update-plugin (1.1.0): Downloading (100%)
Installing "magento/composer-root-update-plugin: 1.1.0" for the Web Setup Wizard
Loading composer repositories with package information
Updating dependencies
Package operations: 18 installs, 0 updates, 0 removals
- Installing ...
...
- Installing magento/composer-root-update-plugin (0.1.0): Downloading (100%)
- Installing magento/composer-root-update-plugin (1.1.0): Downloading (100%)
Writing lock file
Generating autoload files
Writing lock file
Expand Down Expand Up @@ -191,7 +193,7 @@ For reference, these are the `"require"` and `"require-dev"` sections from the `
```
"require": {
"magento/product-community-edition": "2.3.1",
"magento/composer-root-update-plugin": "~0.1"
"magento/composer-root-update-plugin": "~1.1"
},
"require-dev": {
"allure-framework/allure-phpunit": "~1.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Composer\Installer;
use Composer\Installer\PackageEvent;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Exception;
use Magento\ComposerRootUpdatePlugin\Utils\Console;
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
Expand Down Expand Up @@ -52,26 +54,52 @@ public function __construct($console)
*/
public function packageEvent($event)
{
$jobs = $event->getRequest()->getJobs();
$packageName = PluginDefinition::PACKAGE_NAME;
foreach ($jobs as $job) {
if (key_exists('packageName', $job) && $job['packageName'] === $packageName) {
$pkg = $event->getInstalledRepo()->findPackage($packageName, '*');
if ($pkg !== null) {
$version = $pkg->getPrettyVersion();
try {
$composer = $event->getComposer();
$this->updateSetupWizardPlugin(
$composer,
$composer->getConfig()->getConfigSource()->getName(),
$version
);
} catch (Exception $e) {
$this->console->error("Web Setup Wizard installation of \"$packageName: $version\" failed", $e);
$apiMajorVersion = explode('.', PluginInterface::PLUGIN_API_VERSION)[0];
if ($apiMajorVersion == '1') {
$jobs = $event->getRequest()->getJobs();
foreach ($jobs as $job) {
if (key_exists('packageName', $job) && $job['packageName'] === $packageName) {
$pkg = $event->getInstalledRepo()->findPackage($packageName, '*');
if ($pkg !== null) {
$this->processEvent($pkg, $event);
break;
}
break;
}
}
} elseif ($apiMajorVersion == '2') {
if (strpos($event->getOperation()->show(false), $packageName) !== false) {
$pkg = $event->getLocalRepo()->findPackage($packageName, '*');
if ($pkg !== null) {
$this->processEvent($pkg, $event);
}
}
} else {
$this->console->error(
"Web Setup Wizard installation of \"$packageName\" failed; unrecognized composer plugin API version"
);
}
}

/**
* Helper function to attempt to run updateSetupWizardPlugin when an appropriate PackageEvent is fired
*
* @param $pkg PackageInterface
* @param $event PackageEvent
*/
public function processEvent($pkg, $event)
{
$packageName = PluginDefinition::PACKAGE_NAME;
$version = $pkg->getPrettyVersion();
try {
$composer = $event->getComposer();
$this->updateSetupWizardPlugin(
$composer,
$composer->getConfig()->getConfigSource()->getName(),
$version
);
} catch (Exception $e) {
$this->console->error("Web Setup Wizard installation of \"$packageName: $version\" failed", $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Composer\Package\RootPackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Package\Version\VersionSelector;
use Composer\Plugin\PluginInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\RepositorySet;
use Composer\Repository\VcsRepository;
use Magento\ComposerRootUpdatePlugin\ComposerReimplementation\AccessibleRootPackageLoader;
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
Expand Down Expand Up @@ -249,6 +251,7 @@ protected function fetchMageRootFromRepo(
$phpVersion = null,
$preferredStability = 'stable'
) {
$apiMajorVersion = explode('.', PluginInterface::PLUGIN_API_VERSION)[0];
$packageName = $this->pkgUtils->getProjectPackageName($edition);
$parsedConstraint = (new VersionParser())->parseConstraints($constraint);

Expand All @@ -263,26 +266,6 @@ protected function fetchMageRootFromRepo(
: $minStability;
$this->console->comment("Minimum stability for \"$packageName: $constraint\": $stability", IOInterface::DEBUG);

$pool = new Pool(
$stability,
$stabilityFlags,
[$packageName => $parsedConstraint]
);
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$pool->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig()
));
} else {
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
}

$metapackageName = $this->pkgUtils->getMetapackageName($edition);
if ($edition != PackageUtils::CLOUD_PKG_EDITION && !$this->pkgUtils->isConstraintStrict($constraint)) {
$this->console->warning(
Expand All @@ -293,13 +276,65 @@ protected function fetchMageRootFromRepo(
}

$phpVersion = $ignorePlatformReqs ? null : $phpVersion;
$versionSelector = null;
$result = null;
if ($apiMajorVersion == '1') {
$pool = new Pool(
$stability,
$stabilityFlags,
[$packageName => $parsedConstraint]
);
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$pool->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig()
));
} else {
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
}

$versionSelector = new VersionSelector($pool);
$result = ($versionSelector)->findBestCandidate(
$packageName,
$constraint,
$phpVersion,
$preferredStability
);
} elseif ($apiMajorVersion == '2') {
$repositorySet = new RepositorySet($minStability, $stabilityFlags);
if ($edition == PackageUtils::CLOUD_PKG_EDITION) {
// magento/magento-cloud-template exists on github, not the composer repo
$repoConfig = [
'url' => 'https://github.com/magento/magento-cloud',
'type' => 'vcs'
];
$repositorySet->addRepository(new VcsRepository(
$repoConfig,
$this->console->getIO(),
$this->composer->getConfig(),
$this->composer->getLoop()->getHttpDownloader()
));
} else {
$repositorySet->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
}

$result = (new VersionSelector($pool))->findBestCandidate(
$packageName,
$constraint,
$phpVersion,
$preferredStability
);
$versionSelector = new VersionSelector($repositorySet);
$result = ($versionSelector)->findBestCandidate(
$packageName,
$constraint,
$preferredStability
);
} else {
$this->console->error(
"Fetching Magento root composer failed; unrecognized composer plugin API version"
);
}

if (!$result) {
$err = "Could not find a Magento project package matching \"$metapackageName $constraint\"";
Expand Down
4 changes: 2 additions & 2 deletions src/Magento/ComposerRootUpdatePlugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"AFL-3.0"
],
"require": {
"composer/composer": "<=1.10.15",
"composer-plugin-api": "^1.0"
"composer/composer": "<=1.10.19 || >=2.0.0 <=2.0.8",
"composer-plugin-api": "^1.0 || ^2.0"
},
"suggest": {
"magento/framework": "Enables the Magento Composer Root Update Plugin's functionality for the Web Setup Wizard"
Expand Down
Loading

0 comments on commit 243946b

Please sign in to comment.