Skip to content

Commit deeca52

Browse files
Merge pull request #27 from wp-cli/list-aliases
List aliases for found WP directories when they exist
2 parents 5b31c88 + 79423e4 commit deeca52

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contr
1010
## Using
1111

1212
~~~
13-
wp find <path> [--skip-ignored-paths] [--fields=<fields>] [--field=<field>] [--format=<format>] [--verbose]
13+
wp find <path> [--skip-ignored-paths] [--max_depth=<max-depth>] [--fields=<fields>] [--field=<field>] [--format=<format>] [--verbose]
1414
~~~
1515

1616
Recursively iterates subdirectories of provided `<path>` to find and
@@ -20,13 +20,16 @@ with a version.php file.
2020
Avoids recursing some known paths (e.g. node_modules) to significantly
2121
improve performance.
2222

23+
Indicates depth at which the WordPress install was found, and its alias,
24+
if it has one.
25+
2326
```
2427
$ wp find ./
25-
+---------------------------------------------------------------------+---------------------+
26-
| version_path | version |
27-
+---------------------------------------------------------------------+---------------------+
28-
| /Users/wpcli/projects/wordpress-develop/src/wp-includes/version.php | 4.8-alpha-39357-src |
29-
+---------------------------------------------------------------------+---------------------+
28+
+--------------------------------------+---------------------+-------+--------+
29+
| version_path | version | depth | alias |
30+
+--------------------------------------+---------------------+-------+--------+
31+
| /Users/wpcli/wp-includes/version.php | 4.8-alpha-39357-src | 2 | @wpcli |
32+
+--------------------------------------+---------------------+-------+--------+
3033
```
3134

3235
**OPTIONS**
@@ -37,6 +40,9 @@ $ wp find ./
3740
[--skip-ignored-paths]
3841
Skip the paths that are ignored by default.
3942

43+
[--max_depth=<max-depth>]
44+
Only recurse to a specified depth, inclusive.
45+
4046
[--fields=<fields>]
4147
Limit the output to specific row fields.
4248

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: 28 additions & 6 deletions
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
*
@@ -95,13 +102,16 @@ class Find_Command {
95102
* Avoids recursing some known paths (e.g. node_modules) to significantly
96103
* improve performance.
97104
*
105+
* Indicates depth at which the WordPress install was found, and its alias,
106+
* if it has one.
107+
*
98108
* ```
99109
* $ wp find ./
100-
* +---------------------------------------------------------------------+---------------------+
101-
* | version_path | version |
102-
* +---------------------------------------------------------------------+---------------------+
103-
* | /Users/wpcli/projects/wordpress-develop/src/wp-includes/version.php | 4.8-alpha-39357-src |
104-
* +---------------------------------------------------------------------+---------------------+
110+
* +--------------------------------------+---------------------+-------+--------+
111+
* | version_path | version | depth | alias |
112+
* +--------------------------------------+---------------------+-------+--------+
113+
* | /Users/wpcli/wp-includes/version.php | 4.8-alpha-39357-src | 2 | @wpcli |
114+
* +--------------------------------------+---------------------+-------+--------+
105115
* ```
106116
*
107117
* ## OPTIONS
@@ -147,10 +157,19 @@ public function __invoke( $args, $assoc_args ) {
147157
$this->skip_ignored_paths = Utils\get_flag_value( $assoc_args, 'skip-ignored-paths' );
148158
$this->max_depth = Utils\get_flag_value( $assoc_args, 'max_depth', false );
149159
$this->verbose = Utils\get_flag_value( $assoc_args, 'verbose' );
160+
161+
$aliases = WP_CLI::get_runner()->aliases;
162+
foreach( $aliases as $alias => $target ) {
163+
if ( empty( $target['path'] ) ) {
164+
continue;
165+
}
166+
$this->resolved_aliases[ rtrim( $target['path'], '/' ) ] = $alias;
167+
}
168+
150169
$this->start_time = microtime( true );
151170
$this->log( "Searching for WordPress installs in '{$path}'" );
152171
$this->recurse_directory( $this->base_path );
153-
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version_path', 'version', 'depth' ) );
172+
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version_path', 'version', 'depth', 'alias' ) );
154173
$formatter->display_items( $this->found_wp );
155174
}
156175

@@ -181,10 +200,13 @@ private function recurse_directory( $path ) {
181200
if ( '/wp-includes/' === substr( $path, -13 )
182201
&& file_exists( $path . 'version.php' ) ) {
183202
$version_path = $path . 'version.php';
203+
$wp_path = substr( $path, 0, -13 );
204+
$alias = isset( $this->resolved_aliases[ $wp_path ] ) ? $this->resolved_aliases[ $wp_path ] : '';
184205
$this->found_wp[ $version_path ] = array(
185206
'version_path' => $version_path,
186207
'version' => self::get_wp_version( $version_path ),
187208
'depth' => $this->current_depth - 1,
209+
'alias' => $alias,
188210
);
189211
$this->log( "Found WordPress install at '{$version_path}'" );
190212
return;

0 commit comments

Comments
 (0)