OSDN Git Service

output help markdown directly, instead of going through man
authorscribu <mail@scribu.net>
Sat, 22 Jun 2013 20:31:14 +0000 (23:31 +0300)
committerscribu <mail@scribu.net>
Sun, 23 Jun 2013 11:50:48 +0000 (14:50 +0300)
.travis.yml
CONTRIBUTING.md
bin/ci/install_dependencies.sh
bin/ci/run_build.sh
features/help.feature
man-src/help.txt
php/WP_CLI/Dispatcher/CompositeCommand.php
php/WP_CLI/Dispatcher/Subcommand.php
php/commands/help.php
php/dispatcher.php
templates/man.mustache

index 138b44b..cff1fea 100644 (file)
@@ -6,12 +6,12 @@ php:
 
 env:
     - WP_VERSION=latest 
-    - WP_VERSION=3.4.2 WITH_RONN=1
+    - WP_VERSION=3.4.2
 
 matrix:
   exclude:
     - php: 5.4
-      env: WP_VERSION=3.4.2 WITH_RONN=1
+      env: WP_VERSION=3.4.2
 
 before_script: ./bin/ci/install_dependencies.sh
 
index 52ce37f..3580119 100644 (file)
@@ -15,17 +15,6 @@ Also, please create or update the appropriate `.txt` file in the `man-src` direc
 
 Lastly, please follow the [WordPress Coding Standards](http://make.wordpress.org/core/handbook/coding-standards/).
 
-Generating man pages
---------------------
-
-To generate a man page, WP-CLI looks for `.txt` files in the `man-src` directory. It also gathers information from the inline comments and the `@synopsis` annotations.
-
-The compiled man page is placed in the `man` directory.
-
-To (re)generate one or more man pages, you first need to have the [ronn](https://rubygems.org/gems/ronn) ruby gem installed.
-
-Then, you can use the `wp help --gen` command.
-
 Running the tests
 -----------------
 
index 02b33c1..c756510 100755 (executable)
@@ -8,10 +8,6 @@ set -ex
 composer install --dev --no-interaction --prefer-source
 composer require d11wtq/boris=dev-master --no-interaction --prefer-source
 
-if [ -n "$WITH_RONN" ]; then
-       gem install ronn
-fi
-
 # set up WP install
 ./bin/wp core download --version=$WP_VERSION --path=/tmp/wp-cli-test-core-download-cache/
 
index aed4ad0..3ed2e35 100755 (executable)
@@ -4,8 +4,4 @@ set -ex
 
 vendor/bin/phpunit
 
-if [ -z "$WITH_RONN" ]; then
-       BEHAT_OPTS="--tags ~@ronn"
-fi
-
-vendor/bin/behat --format progress $BEHAT_OPTS
+vendor/bin/behat --format progress
index 5b8b421..2f21935 100644 (file)
@@ -4,52 +4,21 @@ Feature: Get help about WP-CLI commands
     Given an empty directory
 
     When I run `wp help`
-    Then STDOUT should contain:
-      """
-      Available commands:
-      """
+    Then STDOUT should not be empty
 
     When I run `wp help core`
-    Then STDOUT should contain:
-      """
-      usage: wp core
-      """
+    Then STDOUT should not be empty
 
     When I run `wp help core download`
-    Then STDOUT should contain:
-      """
-      WP-CORE-DOWNLOAD(1)
-      """
+    Then STDOUT should not be empty
 
-    When I run `wp help --help`
-    Then STDOUT should contain:
-      """
-      WP-HELP(1)
-      """
+    When I run `wp help help`
+    Then STDOUT should not be empty
 
     When I try `wp help non-existent-command`
     Then the return code should be 1
     And STDERR should not be empty
 
-  @ronn
-  Scenario: Generating help for subcommands
-    Given an empty directory
-    When I run `wp help --gen option`
-    Then STDOUT should be:
-      """
-      generated option.1
-      """
-
-  @ronn
-  Scenario: Generating help for multisite-only subcommands
-    Given an empty directory
-    When I run `wp help --gen site create`
-    Then STDOUT should be:
-      """
-      generated site-create.1
-      """
-
-  @ronn
   Scenario: Help for third-party commands
     Given a WP install
     And a wp-content/plugins/test-cli/test-help.txt file:
@@ -76,17 +45,5 @@ Feature: Get help about WP-CLI commands
     When I run `wp help test-help`
     Then STDOUT should contain:
       """
-      usage: wp test-help
-      """
-
-    When I run `wp help --gen test-help`
-    Then STDOUT should contain:
-      """
-      generated test-help.1
-      """
-
-    When I run `wp help test-help`
-    Then STDOUT should contain:
-      """
-      WP-TEST-HELP(1)
+      wp test-help
       """
index 69ac8e6..19e660f 100644 (file)
@@ -1,10 +1,7 @@
 ## EXAMPLES
 
-       # (re)generates all man pages
-       wp help --gen
+       # get help for `core` command
+       wp help core
 
-       # (re)generate man pages for the `core` command
-       wp help --gen core
-
-       # (re)generate man page only for the `core download` subcommand
-       wp help --gen core download
+       # get help for `core download` subcommand
+       wp help core download
index 1417919..8a527fa 100644 (file)
@@ -17,7 +17,6 @@ class CompositeCommand {
                $this->name = $name;
 
                $this->shortdesc = $docparser->get_shortdesc();
-               $this->synopsis = $docparser->get_synopsis();
 
                $when_to_invoke = $docparser->get_tag( 'when' );
                if ( $when_to_invoke ) {
@@ -52,7 +51,7 @@ class CompositeCommand {
        }
 
        function get_synopsis() {
-               return $this->synopsis;
+               return '<subcommand>';
        }
 
        function invoke( $args, $assoc_args ) {
index 55d27f9..3203ef9 100644 (file)
@@ -14,17 +14,26 @@ class Subcommand extends CompositeCommand {
        function __construct( $parent, $name, $docparser, $when_invoked ) {
                $this->when_invoked = $when_invoked;
 
+               $this->synopsis = $docparser->get_synopsis();
                $this->alias = $docparser->get_tag( 'alias' );
 
                parent::__construct( $parent, $name, $docparser );
        }
 
+       function get_synopsis() {
+               return $this->synopsis;
+       }
+
        function get_alias() {
                return $this->alias;
        }
 
        function show_usage( $prefix = 'usage: ' ) {
-               \WP_CLI::line( $prefix . get_full_synopsis( $this ) );
+               \WP_CLI::line( sprintf( "%s%s %s",
+                       $prefix,
+                       implode( ' ', get_path( $this ) ),
+                       $this->get_synopsis()
+               ) );
        }
 
        private function validate_args( $args, &$assoc_args ) {
index a15ecc2..03ac390 100644 (file)
@@ -8,13 +8,21 @@ class Help_Command extends WP_CLI_Command {
        /**
         * Get help on a certain topic.
         *
-        * @synopsis [<command>] [--gen]
+        * @synopsis [<command>]
         */
        function __invoke( $args, $assoc_args ) {
-               if ( isset( $assoc_args['gen'] ) )
-                       $this->generate( $args );
-               else
-                       $this->show( $args );
+               $command = self::find_subcommand( $args );
+
+               if ( $command ) {
+                       self::add_initial_markdown( $command );
+
+                       $extra_markdown_path = self::find_extra_markdown( $command );
+                       if ( $extra_markdown_path ) {
+                               echo file_get_contents( $extra_markdown_path );
+                       }
+
+                       exit;
+               }
 
                // WordPress is already loaded, so there's no chance we'll find the command
                if ( function_exists( 'add_filter' ) ) {
@@ -32,103 +40,29 @@ class Help_Command extends WP_CLI_Command {
                return $command;
        }
 
-       private function show( $args ) {
-               if ( self::maybe_show_manpage( $args ) ) {
-                       exit;
-               }
-
-               $command = self::find_subcommand( $args );
-
-               if ( $command ) {
-                       $command->show_usage();
-                       exit;
-               }
-       }
-
-       private function generate( $args ) {
-               if ( '' === exec( 'which ronn' ) ) {
-                       WP_CLI::error( '`ronn` executable not found.' );
-               }
-
-               $command = self::find_subcommand( $args );
-
-               if ( $command ) {
-                       foreach ( WP_CLI::get_man_dirs() as $dest_dir => $src_dir ) {
-                               self::_generate( $src_dir, $dest_dir, $command );
-                       }
-                       exit;
-               }
-       }
-
-       private static function maybe_show_manpage( $args ) {
-               $man_file = self::get_file_name( $args );
-
-               foreach ( \WP_CLI::get_man_dirs() as $dest_dir => $_ ) {
-                       $man_path = "$dest_dir/" . $man_file;
-
-                       if ( is_readable( $man_path ) ) {
-                               \WP_CLI::launch( "man $man_path" );
-                               return true;
-                       }
-               }
-
-               return false;
-       }
-
-       private static function _generate( $src_dir, $dest_dir, $command ) {
+       private static function find_extra_markdown( $command ) {
                $cmd_path = Dispatcher\get_path( $command );
                array_shift( $cmd_path ); // discard 'wp'
+               $cmd_path = implode( '-', $cmd_path );
 
-               $src_path = "$src_dir/" . self::get_src_file_name( $cmd_path );
-               $dest_path = "$dest_dir/" . self::get_file_name( $cmd_path );
-
-               self::call_ronn( self::get_markdown( $src_path, $command ), $dest_path );
-
-               if ( $command->has_subcommands() ) {
-                       foreach ( $command->get_subcommands() as $subcommand ) {
-                               self::_generate( $src_dir, $dest_dir, $subcommand );
-                       }
+               foreach ( WP_CLI::get_man_dirs() as $src_dir ) {
+                       $src_path = "$src_dir/$cmd_path.txt";
+                       if ( is_readable( $src_path ) )
+                               return $src_path;
                }
-       }
-
-       // returns a file descriptor or false
-       private static function get_markdown( $doc_path, $command ) {
-               if ( !file_exists( $doc_path ) )
-                       return false;
-
-               $fd = fopen( "php://temp", "rw" );
-
-               self::add_initial_markdown( $fd, $command );
 
-               fwrite( $fd, file_get_contents( $doc_path ) );
-
-               if ( 0 === ftell( $fd ) )
-                       return false;
-
-               fseek( $fd, 0 );
-
-               return $fd;
+               return false;
        }
 
-       private static function add_initial_markdown( $fd, $command ) {
-               $path = Dispatcher\get_path( $command );
+       private static function add_initial_markdown( $command ) {
+               $name = implode( ' ', Dispatcher\get_path( $command ) );
 
                $binding = array(
-                       'name_m' => implode( '-', $path ),
+                       'name' => $name,
                        'shortdesc' => $command->get_shortdesc(),
                );
 
-               $synopsis = Dispatcher\get_full_synopsis( $command, true );
-
-               $synopsis = str_replace( '_', '\_', $synopsis );
-               $synopsis = str_replace( array( '<', '>' ), '_', $synopsis );
-
-               $binding['synopsis'] = $synopsis;
-
-               if ( !$binding['shortdesc'] ) {
-                       $name_s = implode( ' ', $path );
-                       \WP_CLI::warning( "No shortdesc for $name_s" );
-               }
+               $binding['synopsis'] = "$name " . $command->get_synopsis();
 
                if ( $command->has_subcommands() ) {
                        foreach ( $command->get_subcommands() as $subcommand ) {
@@ -139,36 +73,7 @@ class Help_Command extends WP_CLI_Command {
                        }
                }
 
-               fwrite( $fd, Utils\mustache_render( 'man.mustache', $binding ) );
-       }
-
-       private static function call_ronn( $markdown, $dest ) {
-               if ( !$markdown )
-                       return;
-
-               $descriptorspec = array(
-                       0 => $markdown,
-                       1 => array( 'file', $dest, 'w' ),
-                       2 => STDERR
-               );
-
-               $cmd = "ronn --date=2012-01-01 --roff --manual='WP-CLI'";
-
-               $r = proc_close( proc_open( $cmd, $descriptorspec, $pipes ) );
-
-               $roff = file_get_contents( $dest );
-               $roff = str_replace( ' "January 2012"', '', $roff );
-               file_put_contents( $dest, $roff );
-
-               \WP_CLI::log( "generated " . basename( $dest ) );
-       }
-
-       private static function get_file_name( $args ) {
-               return implode( '-', $args ) . '.1';
-       }
-
-       private static function get_src_file_name( $args ) {
-               return implode( '-', $args ) . '.txt';
+               echo Utils\mustache_render( 'man.mustache', $binding );
        }
 }
 
index 4590383..8157d21 100644 (file)
@@ -12,31 +12,3 @@ function get_path( $command ) {
        return $path;
 }
 
-function get_full_synopsis( $command, $validate = false ) {
-       $subcommands = $command->get_subcommands();
-
-       if ( empty( $subcommands ) ) {
-               $synopsis = $command->get_synopsis();
-
-               if ( $validate ) {
-                       $tokens = \WP_CLI\SynopsisParser::parse( $synopsis );
-
-                       foreach ( $tokens as $token ) {
-                               if ( 'unknown' == $token['type'] ) {
-                                       \WP_CLI::warning( sprintf(
-                                               "Invalid token '%s' in synopsis for '%s'",
-                                               $token['token'], $full_name
-                                       ) );
-                               }
-                       }
-               }
-
-               $full_name = implode( ' ', get_path( $command ) );
-
-               return "$full_name $synopsis";
-       } else {
-               return implode( "\n\n", array_map( __FUNCTION__,
-                       $subcommands ) );
-       }
-}
-
index 61a99bc..0c9aafb 100644 (file)
@@ -1,17 +1,20 @@
-{{name_m}}(1) -- {{shortdesc}}
-====
+## NAME
+
+       {{{name}}}
+
+## DESCRIPTION
+
+       {{{shortdesc}}}
 
 ## SYNOPSIS
 
-{{synopsis}}
+       {{{synopsis}}}
 
 {{#has-subcommands}}
 ## SUBCOMMANDS
 
 {{#subcommands}}
-* `{{name}}`:
-
-       {{desc}}
+       * `{{name}}`: {{desc}}
 
 {{/subcommands}}
 {{/has-subcommands}}