Skip to content

Commit 3155419

Browse files
List aliases for found WP directories when they exist
1 parent 5b31c88 commit 3155419

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

features/find.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,19 @@ Feature: Find WordPress installs on the filesystem
161161
"""
162162
Error: Invalid path specified.
163163
"""
164+
165+
Scenario: List aliases for directories if they exist
166+
Given a WP install in 'subdir1'
167+
And a WP install in 'subdir2'
168+
169+
When I run `wp eval --skip-wordpress 'echo realpath( getenv( "RUN_DIR" ) );'`
170+
Then save STDOUT as {TEST_DIR}
171+
172+
When I run `echo "@test1:\n path: {TEST_DIR}/subdir2" > wp-cli.yml`
173+
Then the return code should be 0
174+
175+
When I run `wp find {TEST_DIR} --fields=version_path,alias`
176+
Then STDOUT should be a table containing rows:
177+
| version_path | alias |
178+
| {TEST_DIR}/subdir1/wp-includes/version.php | |
179+
| {TEST_DIR}/subdir2/wp-includes/version.php | @test1 |

src/Find_Command.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class Find_Command {
7878
*/
7979
private $start_time = false;
8080

81+
/**
82+
* Resolved alias paths
83+
*
84+
* @var array
85+
*/
86+
private $resolved_aliases = array();
87+
8188
/**
8289
* Found WordPress installs.
8390
*
@@ -147,10 +154,19 @@ public function __invoke( $args, $assoc_args ) {
147154
$this->skip_ignored_paths = Utils\get_flag_value( $assoc_args, 'skip-ignored-paths' );
148155
$this->max_depth = Utils\get_flag_value( $assoc_args, 'max_depth', false );
149156
$this->verbose = Utils\get_flag_value( $assoc_args, 'verbose' );
157+
158+
$aliases = WP_CLI::get_runner()->aliases;
159+
foreach( $aliases as $alias => $target ) {
160+
if ( empty( $target['path'] ) ) {
161+
continue;
162+
}
163+
$this->resolved_aliases[ rtrim( $target['path'], '/' ) ] = $alias;
164+
}
165+
150166
$this->start_time = microtime( true );
151167
$this->log( "Searching for WordPress installs in '{$path}'" );
152168
$this->recurse_directory( $this->base_path );
153-
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version_path', 'version', 'depth' ) );
169+
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version_path', 'version', 'depth', 'alias' ) );
154170
$formatter->display_items( $this->found_wp );
155171
}
156172

@@ -181,10 +197,13 @@ private function recurse_directory( $path ) {
181197
if ( '/wp-includes/' === substr( $path, -13 )
182198
&& file_exists( $path . 'version.php' ) ) {
183199
$version_path = $path . 'version.php';
200+
$wp_path = substr( $path, 0, -13 );
201+
$alias = isset( $this->resolved_aliases[ $wp_path ] ) ? $this->resolved_aliases[ $wp_path ] : '';
184202
$this->found_wp[ $version_path ] = array(
185203
'version_path' => $version_path,
186204
'version' => self::get_wp_version( $version_path ),
187205
'depth' => $this->current_depth - 1,
206+
'alias' => $alias,
188207
);
189208
$this->log( "Found WordPress install at '{$version_path}'" );
190209
return;

0 commit comments

Comments
 (0)