Skip to content
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

Add support for Composer v2 #189

Merged
merged 20 commits into from
Feb 12, 2021
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
36 changes: 25 additions & 11 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,45 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions:
php-version:
- "7.2"
- "7.3"
- "7.4"
- "8.0"
composer-dependencies:
- 'highest'
env:
COMPOSER_DISABLE_XDEBUG_WARN: 1

name: PHP ${{ matrix.php-versions }} test
name: Test PHP ${{ matrix.php-version }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP version
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: ${{ matrix.php-version }}
coverage: xdebug
tools: composer:v1
tools: composer:v2

- name: Install composer dependencies
run: composer install --prefer-dist --no-progress
- name: Test Composer v1
run: composer require --dev --no-update composer/composer:^1.1

- uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.composer-dependencies }}"

- run: composer test

- name: Test Composer v2
run: composer require --dev --no-update composer/composer:^2.0

- uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.composer-dependencies }}"

- name: Run tests
run: composer test
- run: composer test

ocular-push:
runs-on: ubuntu-latest
Expand All @@ -57,7 +71,7 @@ jobs:
- name: Run coverage
run: composer coverage

- name: Get ocular
- name: Get Ocular
run: wget https://scrutinizer-ci.com/ocular.phar

- name: Upload code coverage
Expand All @@ -68,7 +82,7 @@ jobs:
if: ${{ always() }} && github.repository == 'wikimedia/composer-merge-plugin'
needs: [run]
steps:
- name: irc push notification
- name: IRC push notification
uses: rectalogic/notify-irc@v1
if: github.event_name == 'push'
with:
Expand Down
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ extensions which may be managed via Composer.
Installation
------------

Composer Merge Plugin requires [Composer 1.1.0](https://getcomposer.org/) or
newer, but doesn't currently support Composer 2.0. See
[PR #189](https://github.com/wikimedia/composer-merge-plugin/pull/189) for
current progress.
Composer Merge Plugin 1.4.x (and older) requires Composer 1.x.

Composer Merge Plugin 1.5.x (and newer) is compatible with both Composer 2.x and 1.x.

```
$ composer require wikimedia/composer-merge-plugin
```

### Upgrading from Composer 1 to 2

If you are already using Composer Merge Plugin 1.4 (or older) and you are updating the plugin to 1.5 (or newer), it is recommended that you update the plugin first using Composer 1.

If you update the incompatible plugin using Composer 2, the plugin will be ignored:

> The "wikimedia/composer-merge-plugin" plugin was skipped because it requires a Plugin API version ("^1.0") that does not match your Composer installation ("2.0.0"). You may need to run composer update with the "--no-plugins" option.

Consequently, Composer will be unaware of the merged dependencies and will remove them requiring you to run `composer update` again to reinstall merged dependencies.


Usage
-----
Expand Down Expand Up @@ -66,13 +75,13 @@ Usage
### Updating sub-levels `composer.json` files


In order for composer-merge-plugin to install dependencies from updated or newly created sub-level `composer.json` files in your project you need to run the command:
In order for Composer Merge Plugin to install dependencies from updated or newly created sub-level `composer.json` files in your project you need to run the command:

```
$ composer update --lock
$ composer update
```

This will [instruct Composer to recalculate the file hash](https://getcomposer.org/doc/03-cli.md#update) for the top-level `composer.json` thus triggering composer-merge-plugin to look for the sub-level configuration files and update your dependencies.
This will [instruct Composer to recalculate the file hash](https://getcomposer.org/doc/03-cli.md#update) for the top-level `composer.json` thus triggering Composer Merge Plugin to look for the sub-level configuration files and update your dependencies.


Plugin configuration
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"composer-plugin-api": "^1.1",
"php": ">=7.2.0"
"php": ">=7.2.0",
"composer-plugin-api": "^1.1||^2.0"
},
"require-dev": {
"composer/composer": "^1.1",
"composer/composer": "^1.1||^2.0",
"php-parallel-lint/php-parallel-lint": "~1.1.0",
"phpunit/phpunit": "^8.5||^9.0",
"squizlabs/php_codesniffer": "~3.5.4"
Expand All @@ -33,7 +33,8 @@
"class": "Wikimedia\\Composer\\MergePlugin"
},
"config": {
"optimize-autoloader": true
"optimize-autoloader": true,
"sort-packages": true
},
"scripts": {
"coverage": [
Expand Down
7 changes: 5 additions & 2 deletions example/composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"repositories": [
{
"type": "vcs",
"url": "./.."
"type": "path",
"url": "./..",
"only": [
"wikimedia/composer-merge-plugin"
]
}
],
"require": {
Expand Down
2 changes: 1 addition & 1 deletion src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function warning($message)
*
* @param string $message
*/
protected function log($message)
public function log($message)
{
if (method_exists($this->inputOutput, 'writeError')) {
$this->inputOutput->writeError($message);
Expand Down
102 changes: 88 additions & 14 deletions src/Merge/ExtraPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Composer\Package\RootPackage;
use Composer\Package\RootPackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Plugin\PluginInterface;
use Composer\Semver\Constraint\MultiConstraint as SemverMultiConstraint;
use Composer\Semver\Intervals;
use UnexpectedValueException;

/**
Expand Down Expand Up @@ -58,6 +61,11 @@ class ExtraPackage
*/
protected $package;

/**
* @var array<string, bool> $mergedRequirements
*/
protected $mergedRequirements = array();

/**
* @var VersionParser $versionParser
*/
Expand Down Expand Up @@ -100,6 +108,16 @@ public function getRequires()
$this->json['extra']['merge-plugin']['require'] : array();
}

/**
* Get list of merged requirements from this package.
*
* @return string[]
*/
public function getMergedRequirements()
{
return array_keys($this->mergedRequirements);
}

/**
* Read the contents of a composer.json style file into an array.
*
Expand Down Expand Up @@ -273,33 +291,89 @@ protected function mergeOrDefer(
$type,
array $origin,
array $merge,
$state
PluginState $state
) {
if ($state->ignoreDuplicateLinks() && $state->replaceDuplicateLinks()) {
$this->logger->warning("Both replace and ignore-duplicates are true. These are mutually exclusive.");
$this->logger->warning("Duplicate packages will be ignored.");
}

$dups = array();
foreach ($merge as $name => $link) {
if (isset($origin[$name]) && $state->ignoreDuplicateLinks()) {
$this->logger->info("Ignoring duplicate <comment>{$name}</comment>");
continue;
} elseif (!isset($origin[$name]) || $state->replaceDuplicateLinks()) {
$this->logger->info("Merging <comment>{$name}</comment>");
$origin[$name] = $link;
if (isset($origin[$name])) {
if ($state->ignoreDuplicateLinks()) {
$this->logger->info("Ignoring duplicate <comment>{$name}</comment>");
continue;
}

if ($state->replaceDuplicateLinks()) {
$this->logger->info("Replacing <comment>{$name}</comment>");
$this->mergedRequirements[$name] = true;
$origin[$name] = $link;
} else {
$this->logger->info("Merging <comment>{$name}</comment>");
$this->mergedRequirements[$name] = true;
$origin[$name] = $this->mergeConstraints($origin[$name], $link, $state);
}
} else {
// Defer to solver.
$this->logger->info(
"Deferring duplicate <comment>{$name}</comment>"
);
$dups[] = $link;
$this->logger->info("Adding <comment>{$name}</comment>");
$this->mergedRequirements[$name] = true;
$origin[$name] = $link;
}
}
$state->addDuplicateLinks($type, $dups);

if (!$state->isComposer1()) {
Intervals::clear();
}

return $origin;
}

/**
* Merge package constraints.
*
* Adapted from Composer's UpdateCommand::appendConstraintToLink
*
* @param Link $origin The base package link.
* @param Link $merge The related package link to merge.
* @param PluginState $state
* @return Link Merged link.
*/
protected function mergeConstraints(Link $origin, Link $merge, PluginState $state)
{
$parser = $this->versionParser;

$oldPrettyString = $origin->getConstraint()->getPrettyString();
$newPrettyString = $merge->getConstraint()->getPrettyString();

if ($state->isComposer1()) {
$constraintClass = 'Wikimedia\\Composer\\Merge\\MultiConstraint';
} else {
$constraintClass = 'Composer\\Semver\\Constraint\\MultiConstraint';

if (Intervals::isSubsetOf($origin->getConstraint(), $merge->getConstraint())) {
return $origin;
}

if (Intervals::isSubsetOf($merge->getConstraint(), $origin->getConstraint())) {
return $merge;
}
}

$newConstraint = $constraintClass::create(array(
$origin->getConstraint(),
$merge->getConstraint()
), true);
$newConstraint->setPrettyString($oldPrettyString.', '.$newPrettyString);
mcaskill marked this conversation as resolved.
Show resolved Hide resolved

return new Link(
$origin->getSource(),
$origin->getTarget(),
$newConstraint,
$origin->getDescription(),
$origin->getPrettyConstraint() . ', ' . $newPrettyString
);
}

/**
* Merge autoload or autoload-dev into a RootPackageInterface
*
Expand Down
Loading