Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contr
## Using

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

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

Indicates depth at which the WordPress install was found, and its alias,
if it has one.

```
$ wp find ./
+---------------------------------------------------------------------+---------------------+
| version_path | version |
+---------------------------------------------------------------------+---------------------+
| /Users/wpcli/projects/wordpress-develop/src/wp-includes/version.php | 4.8-alpha-39357-src |
+---------------------------------------------------------------------+---------------------+
+--------------------------------------+---------------------+-------+--------+
| version_path | version | depth | alias |
+--------------------------------------+---------------------+-------+--------+
| /Users/wpcli/wp-includes/version.php | 4.8-alpha-39357-src | 2 | @wpcli |
+--------------------------------------+---------------------+-------+--------+
```

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

[--max_depth=<max-depth>]
Only recurse to a specified depth, inclusive.

[--fields=<fields>]
Limit the output to specific row fields.

Expand Down
16 changes: 16 additions & 0 deletions features/find.feature
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,19 @@ Feature: Find WordPress installs on the filesystem
"""
Error: Invalid path specified.
"""

Scenario: List aliases for directories if they exist
Given a WP install in 'subdir1'
And a WP install in 'subdir2'

When I run `wp eval --skip-wordpress 'echo realpath( getenv( "RUN_DIR" ) );'`
Then save STDOUT as {TEST_DIR}

When I run `echo "@test1:\n path: {TEST_DIR}/subdir2" > wp-cli.yml`
Then the return code should be 0

When I run `wp find {TEST_DIR} --fields=version_path,alias`
Then STDOUT should be a table containing rows:
| version_path | alias |
| {TEST_DIR}/subdir1/wp-includes/version.php | |
| {TEST_DIR}/subdir2/wp-includes/version.php | @test1 |
34 changes: 28 additions & 6 deletions src/Find_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ class Find_Command {
*/
private $start_time = false;

/**
* Resolved alias paths
*
* @var array
*/
private $resolved_aliases = array();

/**
* Found WordPress installs.
*
Expand All @@ -95,13 +102,16 @@ class Find_Command {
* Avoids recursing some known paths (e.g. node_modules) to significantly
* improve performance.
*
* Indicates depth at which the WordPress install was found, and its alias,
* if it has one.
*
* ```
* $ wp find ./
* +---------------------------------------------------------------------+---------------------+
* | version_path | version |
* +---------------------------------------------------------------------+---------------------+
* | /Users/wpcli/projects/wordpress-develop/src/wp-includes/version.php | 4.8-alpha-39357-src |
* +---------------------------------------------------------------------+---------------------+
* +--------------------------------------+---------------------+-------+--------+
* | version_path | version | depth | alias |
* +--------------------------------------+---------------------+-------+--------+
* | /Users/wpcli/wp-includes/version.php | 4.8-alpha-39357-src | 2 | @wpcli |
* +--------------------------------------+---------------------+-------+--------+
* ```
*
* ## OPTIONS
Expand Down Expand Up @@ -147,10 +157,19 @@ public function __invoke( $args, $assoc_args ) {
$this->skip_ignored_paths = Utils\get_flag_value( $assoc_args, 'skip-ignored-paths' );
$this->max_depth = Utils\get_flag_value( $assoc_args, 'max_depth', false );
$this->verbose = Utils\get_flag_value( $assoc_args, 'verbose' );

$aliases = WP_CLI::get_runner()->aliases;
foreach( $aliases as $alias => $target ) {
if ( empty( $target['path'] ) ) {
continue;
}
$this->resolved_aliases[ rtrim( $target['path'], '/' ) ] = $alias;
}

$this->start_time = microtime( true );
$this->log( "Searching for WordPress installs in '{$path}'" );
$this->recurse_directory( $this->base_path );
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version_path', 'version', 'depth' ) );
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'version_path', 'version', 'depth', 'alias' ) );
$formatter->display_items( $this->found_wp );
}

Expand Down Expand Up @@ -181,10 +200,13 @@ private function recurse_directory( $path ) {
if ( '/wp-includes/' === substr( $path, -13 )
&& file_exists( $path . 'version.php' ) ) {
$version_path = $path . 'version.php';
$wp_path = substr( $path, 0, -13 );
$alias = isset( $this->resolved_aliases[ $wp_path ] ) ? $this->resolved_aliases[ $wp_path ] : '';
$this->found_wp[ $version_path ] = array(
'version_path' => $version_path,
'version' => self::get_wp_version( $version_path ),
'depth' => $this->current_depth - 1,
'alias' => $alias,
);
$this->log( "Found WordPress install at '{$version_path}'" );
return;
Expand Down