Skip to content

Commit c5bd2b3

Browse files
Copilotswissspidy
andcommitted
Add optional package name argument to wp package update command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent f16787c commit c5bd2b3

2 files changed

Lines changed: 124 additions & 2 deletions

File tree

‎features/package-update.feature‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,80 @@ Feature: Update WP-CLI packages
9898
"""
9999
Success: Packages updated.
100100
"""
101+
102+
Scenario: Update a specific package by name
103+
Given an empty directory
104+
105+
When I run `wp package install wp-cli-test/updateable-package:v1.0.0`
106+
Then STDOUT should contain:
107+
"""
108+
Success: Package installed.
109+
"""
110+
111+
When I run `wp package install danielbachhuber/wp-cli-reset-post-date-command`
112+
Then STDOUT should contain:
113+
"""
114+
Success: Package installed.
115+
"""
116+
117+
When I run `sed -i.bak s/v1.0.0/\>=1.0.0/g {PACKAGE_PATH}/composer.json`
118+
Then the return code should be 0
119+
120+
When I run `wp package update wp-cli-test/updateable-package`
121+
Then STDOUT should contain:
122+
"""
123+
Using Composer to update packages...
124+
"""
125+
And STDOUT should contain:
126+
"""
127+
Success: Package updated successfully.
128+
"""
129+
130+
When I run `wp package list --fields=name,update`
131+
Then STDOUT should be a table containing rows:
132+
| name | update |
133+
| wp-cli-test/updateable-package | none |
134+
135+
Scenario: Update multiple specific packages by name
136+
Given an empty directory
137+
138+
When I run `wp package install wp-cli-test/updateable-package:v1.0.0`
139+
Then STDOUT should contain:
140+
"""
141+
Success: Package installed.
142+
"""
143+
144+
When I run `wp package install danielbachhuber/wp-cli-reset-post-date-command`
145+
Then STDOUT should contain:
146+
"""
147+
Success: Package installed.
148+
"""
149+
150+
When I run `sed -i.bak s/v1.0.0/\>=1.0.0/g {PACKAGE_PATH}/composer.json`
151+
Then the return code should be 0
152+
153+
When I run `wp package update wp-cli-test/updateable-package danielbachhuber/wp-cli-reset-post-date-command`
154+
Then STDOUT should contain:
155+
"""
156+
Using Composer to update packages...
157+
"""
158+
And STDOUT should contain:
159+
"""
160+
Success: 2 packages updated successfully.
161+
"""
162+
163+
Scenario: Error when trying to update a non-existent package
164+
Given an empty directory
165+
166+
When I run `wp package install danielbachhuber/wp-cli-reset-post-date-command`
167+
Then STDOUT should contain:
168+
"""
169+
Success: Package installed.
170+
"""
171+
172+
When I try `wp package update non-existent/package`
173+
Then STDERR should contain:
174+
"""
175+
Error: Package 'non-existent/package' is not installed.
176+
"""
177+
And the return code should be 1

‎src/Package_Command.php‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,14 @@ public function path( $args ) {
503503
/**
504504
* Updates all installed WP-CLI packages to their latest version.
505505
*
506+
* ## OPTIONS
507+
*
508+
* [<package-name>...]
509+
* : One or more package names to update. If not specified, all packages will be updated.
510+
*
506511
* ## EXAMPLES
507512
*
513+
* # Update all packages.
508514
* $ wp package update
509515
* Using Composer to update packages...
510516
* ---
@@ -518,9 +524,34 @@ public function path( $args ) {
518524
* Generating autoload files
519525
* ---
520526
* Success: Packages updated.
527+
*
528+
* # Update a specific package.
529+
* $ wp package update wp-cli/server-command
530+
* Using Composer to update packages...
531+
* ---
532+
* Loading composer repositories with package information
533+
* Updating dependencies
534+
* Writing lock file
535+
* Generating autoload files
536+
* ---
537+
* Success: Package updated successfully.
521538
*/
522-
public function update() {
539+
public function update( $args = [] ) {
523540
$this->set_composer_auth_env_var();
541+
542+
// Validate package names if provided
543+
$packages_to_update = [];
544+
if ( ! empty( $args ) ) {
545+
foreach ( $args as $package_name ) {
546+
$package = $this->get_installed_package_by_name( $package_name );
547+
if ( false === $package ) {
548+
WP_CLI::error( sprintf( "Package '%s' is not installed.", $package_name ) );
549+
}
550+
// Use the package's pretty name (case-sensitive) from composer
551+
$packages_to_update[] = $package->getPrettyName();
552+
}
553+
}
554+
524555
$composer = $this->get_composer();
525556

526557
// Set up the EventSubscriber
@@ -531,6 +562,12 @@ public function update() {
531562
$install = Installer::create( new ComposerIO(), $composer );
532563
$install->setUpdate( true ); // Installer class will only override composer.lock with this flag
533564
$install->setPreferSource( true ); // Use VCS when VCS for easier contributions.
565+
566+
// If specific packages are provided, use the allow list
567+
if ( ! empty( $packages_to_update ) ) {
568+
$install->setUpdateAllowList( $packages_to_update );
569+
}
570+
534571
WP_CLI::log( 'Using Composer to update packages...' );
535572
WP_CLI::log( '---' );
536573
$res = false;
@@ -544,7 +581,15 @@ public function update() {
544581
// TODO: The --insecure (to be added here) flag should cause another Composer run with verify disabled.
545582

546583
if ( 0 === $res ) {
547-
WP_CLI::success( 'Packages updated.' );
584+
if ( ! empty( $packages_to_update ) ) {
585+
if ( 1 === count( $packages_to_update ) ) {
586+
WP_CLI::success( 'Package updated successfully.' );
587+
} else {
588+
WP_CLI::success( sprintf( '%d packages updated successfully.', count( $packages_to_update ) ) );
589+
}
590+
} else {
591+
WP_CLI::success( 'Packages updated.' );
592+
}
548593
} else {
549594
$res_msg = $res ? " (Composer return code {$res})" : ''; // $res may be null apparently.
550595
WP_CLI::error( "Failed to update packages{$res_msg}." );

0 commit comments

Comments
 (0)