Skip to content

Add wp handbook gen-custom for custom commands #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
96 changes: 93 additions & 3 deletions bin/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

/**
* @when before_wp_load
*
* @phpstan-type Command_Array array{name:string, description:string, longdesc:string, subcommands?:array<Command_Array>, synopsis?:string, hook?:string}
*/
class Command {

Expand Down Expand Up @@ -382,7 +384,17 @@
echo json_encode( $apis );
}

private static function gen_cmd_pages( $cmd, $parent = [], $verbose = false ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.parentFound
/**
* Generate and save markdown documentation for the provided command and its sub-commands.
*
* @param Command_Array $cmd Command details as array.
* @param string[] $parent The breadcrumb of parent commands.
* @param bool $verbose Whether to output the command page as it is generated.
* @param ?string $output_dir The directory to output the generated command pages, null for default `../commands`.
* @return void
*/
private static function gen_cmd_pages( $cmd, $parent = [], $verbose = false, ?string $output_dir = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.parentFound

Check failure on line 396 in bin/command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Nullable type declarations are not supported in PHP 7.0 or earlier. Found: ?string

Check failure on line 396 in bin/command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

'string' type declaration is not present in PHP version 5.6 or earlier

$parent[] = $cmd['name'];

static $params;
Expand Down Expand Up @@ -516,7 +528,7 @@
$binding['docs'] = $docs;
}

$path = dirname( __DIR__ ) . '/commands/' . $binding['path'];
$path = ( $output_dir ?? dirname( __DIR__ ) . '/commands' ) . '/' . $binding['path'];

Check failure on line 531 in bin/command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

null coalescing operator (??) is not present in PHP version 5.6 or earlier
if ( ! is_dir( dirname( $path ) ) ) {
mkdir( dirname( $path ) );
}
Expand All @@ -530,7 +542,7 @@
}

foreach ( $cmd['subcommands'] as $subcmd ) {
self::gen_cmd_pages( $subcmd, $parent, $verbose );
self::gen_cmd_pages( $subcmd, $parent, $verbose, $output_dir );
}
}

Expand Down Expand Up @@ -674,6 +686,84 @@
}
WP_CLI::log( sprintf( "Removed existing contents of '%s'", $dir ) );
}

/**
* Generate the documentation page for a custom command using the same templates as the official WP-CLI commands.
*
* ## OPTIONS
*
* <command>
* : The custom WP CLI command to generate the documentation for.
*
* [--output_dir=<output_dir>]
* : Directory in which to save the documentation.
* ---
* default: ./docs/wp-cli
* ---
*
* [--verbose]
* : If set will list command pages as they are generated.
*
* @subcommand gen-custom
*
* @when after_wp_load
*
* @param string[] $args
* @param array<string,string> $assoc_args
* @return void
*/
public function gen_custom_cmd_pages( $args, $assoc_args ) {

$command_name = $args[0];

$output_dir = Utils\is_path_absolute( $assoc_args['output_dir'] )
? $assoc_args['output_dir']
: getcwd() . '/' . $assoc_args['output_dir'];

if ( ! is_dir( $output_dir ) ) {
mkdir( $output_dir, 0777, true );
}

$verbose = Utils\get_flag_value( $assoc_args, 'verbose', false );

/**
* Get all registered WP-CLI commands.
*
* @see \CLI_Command::cmd_dump()
*
* @var array{name:string,description:string,longdesc:string,subcommands:array<Command_Array>} $all_commands
*/
$all_commands = WP_CLI::runcommand(
'cli cmd-dump',
[
'launch' => false,
'return' => 'stdout',
'parse' => 'json',
]
);

/**
* Filter the list of WP-CLI commands to find one matching the name passed as an argument to this command.
*
* @var array<Command_Array> $my_commands
*/
$my_commands = array_values(
array_filter(
$all_commands['subcommands'],
function ( array $command ) use ( $command_name ): bool {

Check failure on line 753 in bin/command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

bool return type is not present in PHP version 5.6 or earlier
return $command['name'] === $command_name;
}
)
);

if ( empty( $my_commands ) ) {
WP_CLI::error( "Failed to find command '{$command_name}'." );
}

self::gen_cmd_pages( $my_commands[0], [], $verbose, $output_dir );

WP_CLI::success( "Generated command pages for '{$command_name}'." );
}
}

WP_CLI::add_command( 'handbook', '\WP_CLI\Handbook\Command' );
Loading