Skip to content

Commit 0a2eae5

Browse files
Copilotswissspidy
andcommitted
Exclude command namespaces and missing commands from README generation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 5e19c2f commit 0a2eae5

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

features/scaffold-package-readme.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,46 @@ Feature: Scaffold a README.md file for an existing package
386386
"""
387387
**Alias:** `cpt`
388388
"""
389+
390+
Scenario: README excludes command namespaces
391+
Given an empty directory
392+
And a foo/composer.json file:
393+
"""
394+
{
395+
"name": "wp-cli/namespace-test",
396+
"description": "Test package for excluding command namespaces",
397+
"license": "MIT",
398+
"authors": [],
399+
"minimum-stability": "dev",
400+
"autoload": {
401+
"files": [ "command.php" ]
402+
},
403+
"require": {
404+
"wp-cli/wp-cli": "^2.12"
405+
},
406+
"require-dev": {
407+
"wp-cli/wp-cli-tests": "^5.0.0"
408+
},
409+
"extra": {
410+
"commands": [
411+
"i18n",
412+
"i18n make-pot"
413+
]
414+
}
415+
}
416+
"""
417+
418+
When I run `wp scaffold package-readme foo`
419+
Then the foo/README.md file should exist
420+
And the foo/README.md file should not contain:
421+
"""
422+
### wp i18n
423+
"""
424+
And the foo/README.md file should not contain:
425+
"""
426+
### wp i18n make-pot
427+
"""
428+
And the foo/README.md file should contain:
429+
"""
430+
## Using
431+
"""

src/ScaffoldPackageCommand.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,33 @@ public function package_readme( $args, $assoc_args ) {
339339
}
340340
*/
341341

342+
// Skip commands that were not found or are namespaces with no meaningful content.
343+
if ( false === $parent_command ) {
344+
continue;
345+
}
346+
347+
// Skip command namespaces (commands with subcommands but no meaningful content).
348+
// This needs to be done before processing longdesc to accurately detect empty content.
349+
$has_subcommands = ! empty( $parent_command['subcommands'] );
350+
$shortdesc = isset( $parent_command['description'] ) ? $parent_command['description'] : '';
351+
$raw_longdesc = isset( $parent_command['longdesc'] ) ? $parent_command['longdesc'] : '';
352+
$is_namespace = $has_subcommands && empty( trim( $shortdesc ) ) && empty( trim( $raw_longdesc ) );
353+
354+
if ( $is_namespace ) {
355+
continue;
356+
}
357+
342358
$longdesc = isset( $parent_command['longdesc'] ) ? $parent_command['longdesc'] : '';
343359
$longdesc = (string) preg_replace( '/## GLOBAL PARAMETERS(.+)/s', '', $longdesc );
344360
$longdesc = (string) preg_replace( '/##\s(.+)/', '**$1**', $longdesc );
345361

346362
// definition lists
347363
$longdesc = preg_replace_callback( '/([^\n]+)\n: (.+?)(\n\n|$)/s', [ __CLASS__, 'rewrap_param_desc' ], $longdesc );
348364

365+
$shortdesc = isset( $parent_command['description'] ) ? $parent_command['description'] : '';
349366
$command_data = [
350367
'name' => "wp {$command}",
351-
'shortdesc' => isset( $parent_command['description'] ) ? $parent_command['description'] : '',
368+
'shortdesc' => $shortdesc,
352369
'synopsis' => "wp {$command}" . ( empty( $parent_command['subcommands'] ) ? ( isset( $parent_command['synopsis'] ) ? " {$parent_command['synopsis']}" : '' ) : '' ),
353370
'longdesc' => $longdesc,
354371
];

0 commit comments

Comments
 (0)