From 3155419a73f742d05e2b111a0f9cdd8f451d931f Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 10 Mar 2017 13:34:50 -0800 Subject: [PATCH 1/2] List aliases for found WP directories when they exist --- features/find.feature | 16 ++++++++++++++++ src/Find_Command.php | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/features/find.feature b/features/find.feature index c42c592..2be5ec3 100644 --- a/features/find.feature +++ b/features/find.feature @@ -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 | diff --git a/src/Find_Command.php b/src/Find_Command.php index a032b46..988a843 100644 --- a/src/Find_Command.php +++ b/src/Find_Command.php @@ -78,6 +78,13 @@ class Find_Command { */ private $start_time = false; + /** + * Resolved alias paths + * + * @var array + */ + private $resolved_aliases = array(); + /** * Found WordPress installs. * @@ -147,10 +154,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 ); } @@ -181,10 +197,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; From 79423e4d4ae1e922a465f8a689946d2c8747c8d2 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 10 Mar 2017 13:38:13 -0800 Subject: [PATCH 2/2] Update docs to mention depth and alias --- README.md | 18 ++++++++++++------ src/Find_Command.php | 13 ++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 01583ec..687bd11 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contr ## Using ~~~ -wp find [--skip-ignored-paths] [--fields=] [--field=] [--format=] [--verbose] +wp find [--skip-ignored-paths] [--max_depth=] [--fields=] [--field=] [--format=] [--verbose] ~~~ Recursively iterates subdirectories of provided `` to find and @@ -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** @@ -37,6 +40,9 @@ $ wp find ./ [--skip-ignored-paths] Skip the paths that are ignored by default. + [--max_depth=] + Only recurse to a specified depth, inclusive. + [--fields=] Limit the output to specific row fields. diff --git a/src/Find_Command.php b/src/Find_Command.php index 988a843..719c54e 100644 --- a/src/Find_Command.php +++ b/src/Find_Command.php @@ -102,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