Skip to content

Commit

Permalink
Merge pull request #2 from magento-borg/COMOPS-1503-composer-2
Browse files Browse the repository at this point in the history
COMOPS-1503: Adding support for Composer 2
  • Loading branch information
pdohogne-magento authored Dec 16, 2020
2 parents 5807d6e + a453f06 commit 6216caa
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 58 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
8 changes: 8 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 Expand Up @@ -208,6 +210,12 @@ This class contains methods to retrieve Composer [Package](https://getcomposer.o
- Returns the existing root project package, including all user customizations
- **`fetchMageRootFromRepo()`**
- Given a Magento edition and version constraint, fetch the best-fit Magento root project package from the Composer repository or GitHub (in the case of cloud)
- **`findBestCandidate()`**
- Wrapper function around different versions of [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html)
- **`findBestCandidateComposer1()`**
- Helper function to run [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html) on Composer version 1.x.x
- **`findBestCandidateComposer2()`**
- Helper function to run [VersionSelector::findBestCandidate()](https://getcomposer.org/apidoc/master/Composer/Package/Version/VersionSelector.html) on Composer version 2.x.x
- **`parseVersionAndEditionFromLock()`**
- Inspect the `composer.lock` file for the currently-installed Magento product or cloud metapackage and parse out the edition and version for use by `getOriginalRootPackage()`
- **`getTargetLabel()`**
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,7 @@
use Composer\Installer;
use Composer\Installer\PackageEvent;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Exception;
use Magento\ComposerRootUpdatePlugin\Utils\Console;
use Magento\ComposerRootUpdatePlugin\Utils\PackageUtils;
Expand Down Expand Up @@ -52,26 +53,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);
$composerMajorVersion = explode('.', Composer::VERSION)[0];
if ($composerMajorVersion == '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 ($composerMajorVersion == '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
Loading

0 comments on commit 6216caa

Please sign in to comment.