Skip to content

Commit

Permalink
Merge pull request #127 from wp-cli/fix/add-composer-v2-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Nov 10, 2020
2 parents f95a660 + ed52ae4 commit ba4631d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 63 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
],
"require": {
"ext-json": "*",
"composer/composer": ">=1.2.0 <1.7.0 || ^1.7.1",
"composer/composer": ">=1.2.0 <1.7.0 || ^1.7.1 || ^2.0.0",
"wp-cli/wp-cli": "dev-master"
},
"require-dev": {
"wp-cli/scaffold-command": "^1 || ^2",
"wp-cli/wp-cli-tests": "^2.1"
"wp-cli/wp-cli-tests": "^2.1.16"
},
"config": {
"process-timeout": 7200,
Expand Down
41 changes: 5 additions & 36 deletions features/package-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ Feature: Install WP-CLI packages
Given an empty directory

When I run `wp package install trendwerk/faker`
Then STDOUT should contain:
"""
Warning: trendwerk/faker dev-master requires nelmio/alice
"""
And STDOUT should contain:
"""
Success: Package installed
Expand Down Expand Up @@ -263,14 +259,7 @@ Feature: Install WP-CLI packages
"""
Success: Package installed.
"""
And the {PACKAGE_PATH}composer.json file should contain:
"""
"gitlost/TestMixedCaseCommand"
"""
And the {PACKAGE_PATH}composer.json file should not contain:
"""
mixed
"""
And the contents of the {PACKAGE_PATH}composer.json file should match /\"gitlost\/(?:TestMixedCaseCommand|testmixedcasecommand)\"/
When I run `wp package list --fields=name`
Then STDOUT should be a table containing rows:
Expand Down Expand Up @@ -552,10 +541,7 @@ Feature: Install WP-CLI packages
# Install and uninstall with case-sensitive name
When I run `wp package install GeekPress/wp-rocket-cli`
Then STDERR should be empty
And STDOUT should contain:
"""
Installing package GeekPress/wp-rocket-cli (dev-master)
"""
And STDOUT should match /Installing package (?:GeekPress|geekpress)\/wp-rocket-cli \(dev-master\)/
# This path is sometimes changed on Macs to prefix with /private
And STDOUT should contain:
"""
Expand All @@ -569,14 +555,7 @@ Feature: Install WP-CLI packages
"""
Success: Package installed.
"""
And the {PACKAGE_PATH}composer.json file should contain:
"""
GeekPress/wp-rocket-cli
"""
And the {PACKAGE_PATH}composer.json file should not contain:
"""
geek
"""
And the contents of the {PACKAGE_PATH}composer.json file should match /("?:GeekPress|geekpress)\/wp-rocket-cli"/
When I run `wp package list --fields=name`
Then STDOUT should be a table containing rows:
Expand Down Expand Up @@ -606,10 +585,7 @@ Feature: Install WP-CLI packages
# Install with lowercase name (for BC - no warning) and uninstall with lowercase name (for BC and convenience)
When I run `wp package install geekpress/wp-rocket-cli`
Then STDERR should be empty
And STDOUT should contain:
"""
Installing package GeekPress/wp-rocket-cli (dev-master)
"""
And STDOUT should match /Installing package (?:GeekPress|geekpress)\/wp-rocket-cli \(dev-master\)/
# This path is sometimes changed on Macs to prefix with /private
And STDOUT should contain:
"""
Expand All @@ -623,14 +599,7 @@ Feature: Install WP-CLI packages
"""
Success: Package installed.
"""
And the {PACKAGE_PATH}composer.json file should contain:
"""
GeekPress/wp-rocket-cli
"""
And the {PACKAGE_PATH}composer.json file should not contain:
"""
geek
"""
And the contents of the {PACKAGE_PATH}composer.json file should match /("?:GeekPress|geekpress)\/wp-rocket-cli"/
When I run `wp package list --fields=name`
Then STDOUT should be a table containing rows:
Expand Down
15 changes: 3 additions & 12 deletions features/package-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ Feature: Update WP-CLI packages
"""

When I run `wp package update`
Then STDOUT should contain:
"""
Nothing to install or update
"""
Then STDOUT should match /Nothing to install(?: or update|, update or remove)/
And STDOUT should contain:
"""
Success: Packages updated.
Expand Down Expand Up @@ -88,21 +85,15 @@ Feature: Update WP-CLI packages
"""
Success: Packages updated.
"""
And STDOUT should not contain:
"""
Nothing to install or update
"""
And STDOUT should not match /Nothing to install(?: or update|, update or remove)/

When I run `wp package list --fields=name,update`
Then STDOUT should be a table containing rows:
| name | update |
| wp-cli-test/updateable-package | none |

When I run `wp package update`
Then STDOUT should contain:
"""
Nothing to install or update
"""
Then STDOUT should match /Nothing to install(?: or update|, update or remove)/
And STDOUT should contain:
"""
Success: Packages updated.
Expand Down
46 changes: 43 additions & 3 deletions features/package.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ Feature: Manage WP-CLI packages
When I run `wp --require=bad-command.php package list`
Then STDERR should be empty

@require-php-7.0
Scenario: Revert the WP-CLI packages composer.json when fail to install/uninstall a package due to memory limit
Given an empty directory
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=32M -ddisable_functions=ini_set} package install danielbachhuber/wp-cli-reset-post-date-command`
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=10M -ddisable_functions=ini_set} package install danielbachhuber/wp-cli-reset-post-date-command`
Then the return code should not be 0
And STDERR should contain:
"""
Expand All @@ -72,7 +73,7 @@ Feature: Manage WP-CLI packages
Success: Package installed.
"""

When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=32M -ddisable_functions=ini_set} package uninstall danielbachhuber/wp-cli-reset-post-date-command`
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=10M -ddisable_functions=ini_set} package uninstall danielbachhuber/wp-cli-reset-post-date-command`
Then the return code should not be 0
And STDERR should contain:
"""
Expand All @@ -84,7 +85,46 @@ Feature: Manage WP-CLI packages
Then the {RUN_DIR}/mypackages/composer.json file should exist
And save the {RUN_DIR}/mypackages/composer.json file as {MYPACKAGES_COMPOSER_JSON}

When I try `WP_CLI_PACKAGES_DIR={RUN_DIR}/mypackages {INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=32M -ddisable_functions=ini_set} package install danielbachhuber/wp-cli-reset-post-date-command`
When I try `WP_CLI_PACKAGES_DIR={RUN_DIR}/mypackages {INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=10M -ddisable_functions=ini_set} package install danielbachhuber/wp-cli-reset-post-date-command`
Then the return code should not be 0
And STDERR should contain:
"""
Reverted composer.json.
"""
And the mypackages/composer.json file should be:
"""
{MYPACKAGES_COMPOSER_JSON}
"""

@require-php-7.0
Scenario: Revert the WP-CLI packages composer.json when fail to install/uninstall a package due to memory limit
Given an empty directory
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=10M -ddisable_functions=ini_set} package install danielbachhuber/wp-cli-reset-post-date-command`
Then the return code should not be 0
And STDERR should contain:
"""
Reverted composer.json.
"""

When I run `wp package install danielbachhuber/wp-cli-reset-post-date-command`
Then STDOUT should contain:
"""
Success: Package installed.
"""

When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=10M -ddisable_functions=ini_set} package uninstall danielbachhuber/wp-cli-reset-post-date-command`
Then the return code should not be 0
And STDERR should contain:
"""
Reverted composer.json.
"""

# Create a default composer.json first to compare.
When I run `WP_CLI_PACKAGES_DIR={RUN_DIR}/mypackages wp package list`
Then the {RUN_DIR}/mypackages/composer.json file should exist
And save the {RUN_DIR}/mypackages/composer.json file as {MYPACKAGES_COMPOSER_JSON}

When I try `WP_CLI_PACKAGES_DIR={RUN_DIR}/mypackages {INVOKE_WP_CLI_WITH_PHP_ARGS--dmemory_limit=10M -ddisable_functions=ini_set} package install danielbachhuber/wp-cli-reset-post-date-command`
Then the return code should not be 0
And STDERR should contain:
"""
Expand Down
61 changes: 51 additions & 10 deletions src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Composer\Config;
use Composer\Config\JsonConfigSource;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\EventDispatcher\Event;
use Composer\Factory;
use Composer\IO\NullIO;
Expand All @@ -19,6 +20,7 @@
use Composer\Repository\ComposerRepository;
use Composer\Repository\RepositoryManager;
use Composer\Util\Filesystem;
use Composer\Util\HttpDownloader;
use WP_CLI\ComposerIO;
use WP_CLI\Extractor;
use WP_CLI\Utils;
Expand Down Expand Up @@ -81,7 +83,7 @@ class Package_Command extends WP_CLI_Command {
const PACKAGE_INDEX_URL = 'https://wp-cli.org/package-index/';
const SSL_CERTIFICATE = '/rmccue/requests/library/Requests/Transport/cacert.pem';

private $pool = false;
private $version_selector = false;

/**
* Default author data used while creating default WP-CLI packages composer.json.
Expand Down Expand Up @@ -316,6 +318,12 @@ public function install( $args, $assoc_args ) {
}
}

if ( $this->is_composer_v2() ) {
$package_name = function_exists( 'mb_strtolower' )
? mb_strtolower( $package_name )
: strtolower( $package_name );
}

WP_CLI::log( sprintf( 'Installing package %s (%s)', $package_name, $version ) );

// Read the WP-CLI packages composer.json and do some initial error checking.
Expand Down Expand Up @@ -676,8 +684,14 @@ private function package_index() {
$config->merge( $config_args );
$config->setConfigSource( new JsonConfigSource( $this->get_composer_json() ) );

$io = new NullIO();
try {
$package_index = new ComposerRepository( [ 'url' => self::PACKAGE_INDEX_URL ], new NullIO(), $config );
if ( $this->is_composer_v2() ) {
$http_downloader = new HttpDownloader( $io, $config );
$package_index = new ComposerRepository( [ 'url' => self::PACKAGE_INDEX_URL ], $io, $config, $http_downloader );
} else {
$package_index = new ComposerRepository( [ 'url' => self::PACKAGE_INDEX_URL ], $io, $config );
}
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
Expand Down Expand Up @@ -826,7 +840,9 @@ private function get_installed_packages() {
if ( in_array( $package->getPrettyName(), $installed_package_keys, true ) ) {
$installed_packages[] = $package;
} elseif ( false !== $idx ) { // Legacy incorrect name check.
WP_CLI::warning( sprintf( "Found package '%s' misnamed '%s' in '%s'.", $package->getPrettyName(), $installed_package_keys[ $idx ], $this->get_composer_json_path() ) );
if ( ! $this->is_composer_v2() ) {
WP_CLI::warning( sprintf( "Found package '%s' misnamed '%s' in '%s'.", $package->getPrettyName(), $installed_package_keys[ $idx ], $this->get_composer_json_path() ) );
}
$installed_packages[] = $package;
}
}
Expand Down Expand Up @@ -995,9 +1011,9 @@ private function create_default_composer_json( $composer_path ) {
* @return PackageInterface|null
*/
private function find_latest_package( PackageInterface $package, Composer $composer, $php_version, $minor_only = false ) {
// find the latest version allowed in this pool
// Find the latest version allowed in this pool/repository set.
$name = $package->getPrettyName();
$version_selector = new VersionSelector( $this->get_pool( $composer ) );
$version_selector = $this->get_version_selector( $composer );
$stability = $composer->getPackage()->getMinimumStability();
$flags = $composer->getPackage()->getStabilityFlags();
if ( isset( $flags[ $name ] ) ) {
Expand All @@ -1014,15 +1030,31 @@ private function find_latest_package( PackageInterface $package, Composer $compo
if ( null === $target_version && $minor_only ) {
$target_version = '^' . $package->getVersion();
}

if ( $this->is_composer_v2() ) {
return $version_selector->findBestCandidate( $name, $target_version, $best_stability );
}

return $version_selector->findBestCandidate( $name, $target_version, $php_version, $best_stability );
}

private function get_pool( Composer $composer ) {
if ( ! $this->pool ) {
$this->pool = new Pool( $composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags() );
$this->pool->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
private function get_version_selector( Composer $composer ) {
if ( ! $this->version_selector ) {
if ( $this->is_composer_v2() ) {
$repository_set = new Repository\RepositorySet(
$composer->getPackage()->getMinimumStability(),
$composer->getPackage()->getStabilityFlags()
);
$repository_set->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
$this->version_selector = new VersionSelector( $repository_set );
} else {
$pool = new Pool( $composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags() );
$pool->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
$this->version_selector = new VersionSelector( $pool );
}
}
return $this->pool;

return $this->version_selector;
}

/**
Expand Down Expand Up @@ -1317,4 +1349,13 @@ static function () use (
}
);
}

/**
* Check whether we are dealing with Composer version 2.0.0+.
*
* @return bool
*/
private function is_composer_v2() {
return version_compare( Composer::getVersion(), '2.0.0', '>=' );
}
}

0 comments on commit ba4631d

Please sign in to comment.