From 8ec6f42ef34c09002f4834add2108ed519e22c92 Mon Sep 17 00:00:00 2001 From: harshadyeola Date: Tue, 3 Nov 2015 12:13:33 +0530 Subject: [PATCH] made work help command --- php/EE_CLI/Dispatcher/CompositeCommand.php | 2 +- php/EE_CLI/Runner.php | 94 +----------- php/commands/help.php | 161 +++++++++++++++++++++ templates/man.mustache | 20 +++ 4 files changed, 187 insertions(+), 90 deletions(-) create mode 100644 php/commands/help.php create mode 100644 templates/man.mustache diff --git a/php/EE_CLI/Dispatcher/CompositeCommand.php b/php/EE_CLI/Dispatcher/CompositeCommand.php index 18f1b881..f56d1ff2 100644 --- a/php/EE_CLI/Dispatcher/CompositeCommand.php +++ b/php/EE_CLI/Dispatcher/CompositeCommand.php @@ -156,7 +156,7 @@ class CompositeCommand { $cmd_name = implode( ' ', array_slice( get_path( $this ), 1 ) ); \EE_CLI::line(); - \EE_CLI::line( "See 'wp help $cmd_name ' for more information on a specific command." ); + \EE_CLI::line( "See 'ee help $cmd_name ' for more information on a specific command." ); } /** diff --git a/php/EE_CLI/Runner.php b/php/EE_CLI/Runner.php index 7a226822..47c36f8e 100644 --- a/php/EE_CLI/Runner.php +++ b/php/EE_CLI/Runner.php @@ -73,7 +73,7 @@ class Runner { $config_path = getenv( 'EE_CLI_CONFIG_PATH' ); $this->_global_config_path_debug = 'Using global config from EE_CLI_CONFIG_PATH env var: ' . $config_path; } else { - $config_path = getenv( 'HOME' ) . '/.wp-cli/config.yml'; + $config_path = getenv( 'HOME' ) . '/.ee-cli/config.yml'; $this->_global_config_path_debug = 'Using default global config: ' . $config_path; } @@ -88,14 +88,14 @@ class Runner { /** * Get the path to the project-specific configuration * YAML file. - * wp-cli.local.yml takes priority over wp-cli.yml. + * ee-cli.local.yml takes priority over ee-cli.yml. * * @return string|false */ private function get_project_config_path() { $config_files = array( - 'wp-cli.local.yml', - 'wp-cli.yml' + 'ee-cli.local.yml', + 'ee-cli.yml' ); // Stop looking upward when we find we have emerged from a subdirectory @@ -116,56 +116,6 @@ class Runner { return $project_config_path; } - /** - * Attempts to find the path to the WP install inside index.php - * - * @param string $index_path - * @return string|false - */ - private static function extract_subdir_path( $index_path ) { - $index_code = file_get_contents( $index_path ); - - if ( !preg_match( '|^\s*require\s*\(?\s*(.+?)/wp-blog-header\.php([\'"])|m', $index_code, $matches ) ) { - return false; - } - - $wp_path_src = $matches[1] . $matches[2]; - $wp_path_src = Utils\replace_path_consts( $wp_path_src, $index_path ); - $wp_path = eval( "return $wp_path_src;" ); - - if ( !Utils\is_path_absolute( $wp_path ) ) { - $wp_path = dirname( $index_path ) . "/$wp_path"; - } - - return $wp_path; - } - - - /** - * Guess which URL context WP-CLI has been invoked under. - * - * @param array $assoc_args - * @return string|false - */ - private static function guess_url( $assoc_args ) { - if ( isset( $assoc_args['blog'] ) ) { - $assoc_args['url'] = $assoc_args['blog']; - } - - if ( isset( $assoc_args['url'] ) ) { - $url = $assoc_args['url']; - if ( true === $url ) { - EE_CLI::warning( 'The --url parameter expects a value.' ); - } - } - - if ( isset( $url ) ) { - return $url; - } - - return false; - } - private function cmd_starts_with( $prefix ) { return $prefix == array_slice( $this->arguments, 0, count( $prefix ) ); } @@ -264,40 +214,6 @@ class Runner { return in_array( $path, $this->config['disabled_commands'] ); }*/ - /** - * Returns wp-config.php code, skipping the loading of wp-settings.php - * - * @return string - */ - - - public function get_wp_config_code() { - $wp_config_path = Utils\locate_wp_config(); - - // $wp_config_code = explode( "\n", file_get_contents( $wp_config_path ) ); - - $found_wp_settings = false; - - // $lines_to_run = array(); - - // foreach ( $wp_config_code as $line ) { - // if ( preg_match( '/^\s*require.+wp-settings\.php/', $line ) ) { - // $found_wp_settings = true; - // continue; - // } - - $lines_to_run[] = $line; - } - - // if ( !$found_wp_settings ) { - // EE_CLI::error( 'Strange wp-config.php file: wp-settings.php is not loaded directly.' ); - // } - - // $source = implode( "\n", $lines_to_run ); - // $source = Utils\replace_path_consts( $source, $wp_config_path ); - // return preg_replace( '|^\s*\<\?php\s*|', '', $source ); - //} - /** * Transparently convert deprecated syntaxes * @@ -438,7 +354,7 @@ class Runner { } // First try at showing man page - if ( 'help' === $this->arguments[0] && ! $this->wp_exists() ) { + if ( 'help' === $this->arguments[0] ) { $this->_run_command(); } diff --git a/php/commands/help.php b/php/commands/help.php new file mode 100644 index 00000000..08235ed9 --- /dev/null +++ b/php/commands/help.php @@ -0,0 +1,161 @@ +...] + * : Get help on a specific command. + * + * ## EXAMPLES + * + * # get help for `core` command + * wp help core + * + * # get help for `core download` subcommand + * wp help core download + */ + function __invoke( $args, $assoc_args ) { + $command = self::find_subcommand( $args ); + + if ( $command ) { + + // if ( EE_CLI::get_runner()->is_command_disabled( $command ) ) { + // $path = implode( ' ', array_slice( \EE_CLI\Dispatcher\get_path( $command ), 1 ) ); + // EE_CLI::error( sprintf( + // "The '%s' command has been disabled from the config file.", + // $path + // ) ); + // } + + self::show_help( $command ); + exit; + } + + // WordPress is already loaded, so there's no chance we'll find the command + if ( function_exists( 'add_filter' ) ) { + $command_string = implode( ' ', $args ); + \EE_CLI::error( sprintf( "'%s' is not a registered wp command.", $command_string ) ); + } + } + + private static function find_subcommand( $args ) { + $command = \EE_CLI::get_root_command(); + + while ( !empty( $args ) && $command && $command->can_have_subcommands() ) { + $command = $command->find_subcommand( $args ); + } + + return $command; + } + + private static function show_help( $command ) { + $out = self::get_initial_markdown( $command ); + + $longdesc = $command->get_longdesc(); + if ( $longdesc ) { + $out .= wordwrap( $longdesc, 90 ) . "\n"; + } + + // definition lists + $out = preg_replace_callback( '/([^\n]+)\n: (.+?)(\n\n|$)/s', array( __CLASS__, 'rewrap_param_desc' ), $out ); + + // Ensure all non-section headers are indented + $out = preg_replace( '#^([^\s^\#])#m', "\t$1", $out ); + + // section headers + $out = preg_replace( '/^## ([A-Z ]+)/m', EE_CLI::colorize( '%9\1%n' ), $out ); + + $out = str_replace( "\t", ' ', $out ); + + self::pass_through_pager( $out ); + } + + private static function rewrap_param_desc( $matches ) { + $param = $matches[1]; + $desc = self::indent( "\t\t", wordwrap( $matches[2] ) ); + return "\t$param\n$desc\n\n"; + } + + private static function indent( $whitespace, $text ) { + $lines = explode( "\n", $text ); + foreach ( $lines as &$line ) { + $line = $whitespace . $line; + } + return implode( $lines, "\n" ); + } + + private static function pass_through_pager( $out ) { + + $pager = Utils\is_windows() ? 'more' : 'less -r'; + + // convert string to file handle + $fd = fopen( "php://temp", "r+" ); + fputs( $fd, $out ); + rewind( $fd ); + + $descriptorspec = array( + 0 => $fd, + 1 => STDOUT, + 2 => STDERR + ); + + return proc_close( proc_open( $pager, $descriptorspec, $pipes ) ); + } + + private static function get_initial_markdown( $command ) { + $name = implode( ' ', Dispatcher\get_path( $command ) ); + + $binding = array( + 'name' => $name, + 'shortdesc' => $command->get_shortdesc(), + ); + + $binding['synopsis'] = wordwrap( "$name " . $command->get_synopsis(), 79 ); + + if ( $command->can_have_subcommands() ) { + $binding['has-subcommands']['subcommands'] = self::render_subcommands( $command ); + } + + return Utils\mustache_render( 'man.mustache', $binding ); + } + + private static function render_subcommands( $command ) { + $subcommands = array(); + foreach ( $command->get_subcommands() as $subcommand ) { + + // if ( EE_CLI::get_runner()->is_command_disabled( $subcommand ) ) { + // continue; + // } + + $subcommands[ $subcommand->get_name() ] = $subcommand->get_shortdesc(); + } + + $max_len = self::get_max_len( array_keys( $subcommands ) ); + + $lines = array(); + foreach ( $subcommands as $name => $desc ) { + $lines[] = str_pad( $name, $max_len ) . "\t\t\t" . $desc; + } + + return $lines; + } + + private static function get_max_len( $strings ) { + $max_len = 0; + foreach ( $strings as $str ) { + $len = strlen( $str ); + if ( $len > $max_len ) + $max_len = $len; + } + + return $max_len; + } + +} + +EE_CLI::add_command( 'help', 'Help_Command' ); diff --git a/templates/man.mustache b/templates/man.mustache new file mode 100644 index 00000000..a42acbcc --- /dev/null +++ b/templates/man.mustache @@ -0,0 +1,20 @@ +## NAME + + {{name}} + +## DESCRIPTION + + {{shortdesc}} + +## SYNOPSIS + + {{synopsis}} + +{{#has-subcommands}} +## SUBCOMMANDS + +{{#subcommands}} + {{.}} +{{/subcommands}} + +{{/has-subcommands}}