Skip to content

Commit 6089f4f

Browse files
authored
Merge branch 'main' into copilot/add-package-version-command
2 parents 803dd30 + 1446020 commit 6089f4f

16 files changed

Lines changed: 194 additions & 163 deletions

.github/workflows/code-quality.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
branches:
77
- main
88
- master
9+
schedule:
10+
- cron: '17 2 * * *' # Run every day on a seemly random time.
911

1012
jobs:
1113
code-quality:

.github/workflows/copilot-setup-steps.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
steps:
1919
- name: Checkout code
20-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
20+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
2121

2222
- name: Check existence of composer.json file
2323
id: check_composer_file

.typos.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[default]
2+
extend-ignore-re = [
3+
"(?Rm)^.*(#|//)\\s*spellchecker:disable-line$",
4+
"(?s)(#|//)\\s*spellchecker:off.*?\\n\\s*(#|//)\\s*spellchecker:on",
5+
"(#|//)\\s*spellchecker:ignore-next-line\\n.*"
6+
]
7+
8+
[files]
9+
extend-exclude = [
10+
"tests/phpunit/JsonManipulatorTest.php"
11+
]

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,20 @@ These fields are optionally available:
312312

313313
### wp package update
314314

315-
Updates all installed WP-CLI packages to their latest version.
315+
Updates installed WP-CLI packages to their latest version.
316316

317317
~~~
318-
wp package update
318+
wp package update [<package-name>...]
319319
~~~
320320

321+
**OPTIONS**
322+
323+
[<package-name>...]
324+
One or more package names to update. If not specified, all packages will be updated.
325+
321326
**EXAMPLES**
322327

328+
# Update all packages.
323329
$ wp package update
324330
Using Composer to update packages...
325331
---
@@ -334,6 +340,17 @@ wp package update
334340
---
335341
Success: Packages updated.
336342

343+
# Update a specific package.
344+
$ wp package update wp-cli/server-command
345+
Using Composer to update packages...
346+
---
347+
Loading composer repositories with package information
348+
Updating dependencies
349+
Writing lock file
350+
Generating autoload files
351+
---
352+
Success: Package updated successfully.
353+
337354

338355

339356
### wp package uninstall

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
],
1414
"require": {
1515
"ext-json": "*",
16-
"composer/composer": "^2.2.25",
17-
"wp-cli/wp-cli": "^2.12"
16+
"composer/composer": "^2.9.5",
17+
"wp-cli/wp-cli": "^2.13"
1818
},
1919
"require-dev": {
2020
"wp-cli/scaffold-command": "^1 || ^2",

phpcs.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<exclude-pattern>*/src/WP_CLI/JsonManipulator\.php$</exclude-pattern>
1919
<exclude-pattern>*/src/WP_CLI/Package/Compat/Min_Composer_1_10/NullIOMethodsTrait\.php$</exclude-pattern>
2020
<exclude-pattern>*/src/WP_CLI/Package/Compat/Min_Composer_2_3/NullIOMethodsTrait\.php$</exclude-pattern>
21-
<exclude-pattern>*/tests/JsonManipulatorTest\.php$</exclude-pattern>
21+
<exclude-pattern>*/tests/phpunit/JsonManipulatorTest\.php$</exclude-pattern>
2222

2323
<!-- Show progress. -->
2424
<arg value="p"/>
@@ -63,4 +63,5 @@
6363
<exclude-pattern>*/src/Package_Command\.php$</exclude-pattern>
6464
</rule>
6565

66+
<exclude-pattern>*/tests/phpstan/scan-files\.php$</exclude-pattern>
6667
</ruleset>

phpstan.neon.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
parameters:
2-
level: 1
2+
level: 5
33
paths:
44
- src
55
- package-command.php
66
scanDirectories:
77
- vendor/wp-cli/wp-cli/php
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- tests/phpstan/scan-files.php
1011
treatPhpDocTypesAsCertain: false
1112
ignoreErrors:
1213
# WP_CLI_VERSION is defined in wp-cli core at runtime

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
colors="true"
1414
verbose="true">
1515
<testsuite name="wp-cli/package-command tests">
16-
<directory suffix="Test.php">tests</directory>
16+
<directory suffix="Test.php">tests/phpunit</directory>
1717
</testsuite>
1818

1919
<filter>

src/Package_Command.php

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use Composer\Composer;
44
use Composer\Config;
55
use Composer\Config\JsonConfigSource;
6-
use Composer\DependencyResolver\Pool;
76
use Composer\Factory;
87
use Composer\IO\NullIO;
98
use Composer\Installer;
@@ -223,9 +222,7 @@ public function install( $args, $assoc_args ) {
223222
$dir_package = false;
224223
$version = '';
225224
if ( $this->is_git_repository( $package_name ) ) {
226-
if ( '' === $version ) {
227-
$version = "dev-{$this->get_github_default_branch( $package_name, $insecure )}";
228-
}
225+
$version = "dev-{$this->get_github_default_branch( $package_name, $insecure )}";
229226
$git_package = $package_name;
230227
$matches = [];
231228
if ( preg_match( '#([^:\/]+\/[^\/]+)\.git#', $package_name, $matches ) ) {
@@ -247,7 +244,7 @@ public function install( $args, $assoc_args ) {
247244
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
248245
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
249246
$response = Utils\http_request( 'GET', $package_name, null, $headers, $options );
250-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
247+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
251248
@unlink( $temp ); // @codingStandardsIgnoreLine
252249
WP_CLI::error( sprintf( "Couldn't download package from '%s' (HTTP code %d).", $package_name, $response->status_code ) );
253250
}
@@ -316,11 +313,9 @@ public function install( $args, $assoc_args ) {
316313
}
317314
}
318315

319-
if ( $this->is_composer_v2() ) {
320-
$package_name = function_exists( 'mb_strtolower' )
321-
? mb_strtolower( $package_name )
322-
: strtolower( $package_name );
323-
}
316+
$package_name = function_exists( 'mb_strtolower' )
317+
? mb_strtolower( $package_name )
318+
: strtolower( $package_name );
324319

325320
if ( '' === $version ) {
326321
$version = self::DEFAULT_DEV_BRANCH_CONSTRAINTS;
@@ -932,8 +927,8 @@ private function package_index() {
932927
* Displays a set of packages
933928
*
934929
* @param string $context
935-
* @param array
936-
* @param array
930+
* @param array $packages
931+
* @param array $assoc_args
937932
*/
938933
private function show_packages( $context, $packages, $assoc_args ) {
939934
$default_fields = [];
@@ -976,7 +971,7 @@ private function show_packages( $context, $packages, $assoc_args ) {
976971
$update_version = '';
977972
if ( 'list' === $context && ! $skip_update_check ) {
978973
try {
979-
$latest = $this->find_latest_package( $package, $composer, null );
974+
$latest = $this->find_latest_package( $package, $composer );
980975
if ( $latest && $latest->getFullPrettyVersion() !== $package->getFullPrettyVersion() ) {
981976
$update = 'available';
982977
$update_version = $latest->getPrettyVersion();
@@ -1039,7 +1034,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
10391034
// Check if the package exists on Packagist.
10401035
$url = "https://repo.packagist.org/p2/{$package_name}.json";
10411036
$response = Utils\http_request( 'GET', $url, null, [], $options );
1042-
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
1037+
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
10431038
return $package_name;
10441039
}
10451040

@@ -1048,7 +1043,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
10481043
$github_token = getenv( 'GITHUB_TOKEN' ); // Use GITHUB_TOKEN if available to avoid authorization failures or rate-limiting.
10491044
$headers = $github_token ? [ 'Authorization' => 'token ' . $github_token ] : [];
10501045
$response = Utils\http_request( 'GET', $url, null /*data*/, $headers, $options );
1051-
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
1046+
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
10521047
return $url;
10531048
}
10541049

@@ -1058,7 +1053,7 @@ private function get_package_by_shortened_identifier( $package_name, $insecure =
10581053
$headers = $github_token ? [ 'Authorization' => 'token ' . $github_token ] : [];
10591054
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
10601055
$response = Utils\http_request( 'GET', $url, null /*data*/, $headers, $options );
1061-
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) {
1056+
if ( 20 === (int) substr( (string) $response->status_code, 0, 2 ) ) {
10621057
return $url;
10631058
}
10641059

@@ -1086,9 +1081,6 @@ private function get_installed_packages() {
10861081
if ( in_array( $package->getPrettyName(), $installed_package_keys, true ) ) {
10871082
$installed_packages[] = $package;
10881083
} elseif ( false !== $idx ) { // Legacy incorrect name check.
1089-
if ( ! $this->is_composer_v2() ) {
1090-
WP_CLI::warning( sprintf( "Found package '%s' misnamed '%s' in '%s'.", $package->getPrettyName(), $installed_package_keys[ $idx ], $this->get_composer_json_path() ) );
1091-
}
10921084
$installed_packages[] = $package;
10931085
}
10941086
}
@@ -1240,12 +1232,11 @@ private function create_default_composer_json( $composer_path ) {
12401232
*
12411233
* @param PackageInterface $package
12421234
* @param Composer $composer
1243-
* @param string $phpVersion
1244-
* @param bool $minorOnly
1235+
* @param bool $minor_only
12451236
*
1246-
* @return PackageInterface|null
1237+
* @return PackageInterface|false
12471238
*/
1248-
private function find_latest_package( PackageInterface $package, Composer $composer, $php_version, $minor_only = false ) {
1239+
private function find_latest_package( PackageInterface $package, Composer $composer, $minor_only = false ) {
12491240
// Find the latest version allowed in this pool/repository set.
12501241
$name = $package->getPrettyName();
12511242
$version_selector = $this->get_version_selector( $composer );
@@ -1266,30 +1257,20 @@ private function find_latest_package( PackageInterface $package, Composer $compo
12661257
$target_version = '^' . $package->getVersion();
12671258
}
12681259

1269-
if ( $this->is_composer_v2() ) {
1270-
return $version_selector->findBestCandidate( $name, $target_version, $best_stability );
1271-
}
1272-
1273-
return $version_selector->findBestCandidate( $name, $target_version, $php_version, $best_stability );
1260+
return $version_selector->findBestCandidate( $name, $target_version, $best_stability );
12741261
}
12751262

12761263
/**
12771264
* @return VersionSelector
12781265
*/
12791266
private function get_version_selector( Composer $composer ) {
12801267
if ( ! $this->version_selector ) {
1281-
if ( $this->is_composer_v2() ) {
1282-
$repository_set = new Repository\RepositorySet(
1283-
$composer->getPackage()->getMinimumStability(),
1284-
$composer->getPackage()->getStabilityFlags()
1285-
);
1286-
$repository_set->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
1287-
$this->version_selector = new VersionSelector( $repository_set );
1288-
} else {
1289-
$pool = new Pool( $composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags() );
1290-
$pool->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
1291-
$this->version_selector = new VersionSelector( $pool );
1292-
}
1268+
$repository_set = new Repository\RepositorySet(
1269+
$composer->getPackage()->getMinimumStability(),
1270+
$composer->getPackage()->getStabilityFlags()
1271+
);
1272+
$repository_set->addRepository( new CompositeRepository( $composer->getRepositoryManager()->getRepositories() ) );
1273+
$this->version_selector = new VersionSelector( $repository_set );
12931274
}
12941275

12951276
return $this->version_selector;
@@ -1323,7 +1304,7 @@ private function check_github_package_name( $package_name, $version = '', $insec
13231304
$raw_content_url = "https://raw.githubusercontent.com/{$package_name}/{$this->get_raw_git_version( $version )}/composer.json";
13241305

13251306
$response = Utils\http_request( 'GET', $raw_content_url, null /*data*/, $headers, $options );
1326-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
1307+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
13271308
// Could not get composer.json. Possibly private so warn and return best guess from input (always xxx/xxx).
13281309
WP_CLI::warning(
13291310
sprintf(
@@ -1470,7 +1451,7 @@ private function get_github_latest_release_tag( $package_name, $insecure ) {
14701451
$url = "https://api.github.com/repos/{$package_name}/releases/latest";
14711452
$options = [ 'insecure' => $insecure ];
14721453
$response = Utils\http_request( 'GET', $url, null, [], $options );
1473-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
1454+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
14741455
WP_CLI::warning( 'Could not guess stable version from GitHub repository, falling back to master branch' );
14751456
return 'master';
14761457
}
@@ -1610,15 +1591,6 @@ static function () use (
16101591
);
16111592
}
16121593

1613-
/**
1614-
* Check whether we are dealing with Composer version 2.0.0+.
1615-
*
1616-
* @return bool
1617-
*/
1618-
private function is_composer_v2() {
1619-
return version_compare( Composer::getVersion(), '2.0.0', '>=' );
1620-
}
1621-
16221594
/**
16231595
* Try to retrieve default branch via GitHub API.
16241596
*
@@ -1639,7 +1611,7 @@ private function get_github_default_branch( $package_name, $insecure = false ) {
16391611

16401612
$github_api_repo_url = "https://api.github.com/repos/{$package_name}";
16411613
$response = Utils\http_request( 'GET', $github_api_repo_url, null /*data*/, $headers, $options );
1642-
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
1614+
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
16431615
WP_CLI::warning(
16441616
sprintf(
16451617
"Couldn't fetch default branch for package '%s' (HTTP code %d). Presuming default branch is 'master'.",

src/WP_CLI/Package/Compat/Min_Composer_1_10/NullIOMethodsTrait.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)