Add wp scaffold package-readme

Generates a readme based on the package's `composer.json`
This commit is contained in:
Daniel Bachhuber 2016-03-08 16:13:20 -08:00
parent e9c982124e
commit 30d524361c
6 changed files with 101 additions and 10 deletions

View file

@ -1,18 +1,20 @@
scaffold-package-command
=============================
wp-cli/wp-scaffold-package-command
==================================

Scaffold WP-CLI commands
Scaffold WP-CLI commands with functional tests

[![Build Status](https://travis-ci.org/wp-cli/scaffold-package-command.svg?branch=master)](https://travis-ci.org/wp-cli/scaffold-package-command)
[![Build Status](https://travis-ci.org/wp-cli/wp-scaffold-package-command.svg?branch=master)](https://travis-ci.org/wp-cli/wp-scaffold-package-command)

Quick links: [Installing](#installing) | [Contributing](#contributing)

### Installing
## Installing

`wp scaffold package` requires the latest nightly version of WP-CLI. Update with `wp cli update --nightly`.
This package requires the latest nightly version of WP-CLI. Update with `wp cli update --nightly`.

Once you've done so, you can install `wp scaffold package` with `wp package install wp-cli/scaffold-package-command`
Once you've done so, you can install this package with `wp package install wp-cli/wp-scaffold-package-command`

### Contributing
## Contributing

Code and ideas are more than welcome. Please [open an issue](https://github.com/wp-cli/scaffold-package-command/issues) with questions, feedback, and violent dissent. Pull requests are expected to include test coverage.
Code and ideas are more than welcome.

Please [open an issue](https://github.com/wp-cli/wp-scaffold-package-command/issues) with questions, feedback, and violent dissent. Pull requests are expected to include test coverage.

View file

@ -7,4 +7,5 @@ if ( ! class_exists( 'WP_CLI' ) ) {
require_once __DIR__ . '/inc/ScaffoldPackageCommand.php';

WP_CLI::add_command( 'scaffold package', array( 'WP_CLI\ScaffoldPackageCommand', 'package' ) );
WP_CLI::add_command( 'scaffold package-readme', array( 'WP_CLI\ScaffoldPackageCommand', 'package_readme' ) );
WP_CLI::add_command( 'scaffold package-tests', array( 'WP_CLI\ScaffoldPackageCommand', 'package_tests' ) );

View file

@ -1,6 +1,6 @@
{
"name": "wp-cli/wp-scaffold-package-command",
"description": "Scaffold WP-CLI packages",
"description": "Scaffold WP-CLI commands with functional tests",
"license": "MIT",
"authors": [],
"minimum-stability": "dev",

View file

@ -0,0 +1,11 @@
Feature: Scaffold a README.md file for an existing package

Scenario: Scaffold a README.md based on the defaults
Given an empty directory

When I run `wp scaffold package wp-cli/foo --dir=foo`
Then STDOUT should contain:
"""
Success: Created package readme.
"""
And the foo/README.md file should exist

View file

@ -26,6 +26,9 @@ class ScaffoldPackageCommand {
* [--skip-tests]
* : Don't generate files for integration testing.
*
* [--skip-readme]
* : Don't generate a README.md for the package.
*
* [--force]
* : Overwrite files that already exist.
*
@ -74,6 +77,58 @@ class ScaffoldPackageCommand {
if ( ! Utils\get_flag_value( $assoc_args, 'skip-tests' ) ) {
WP_CLI::run_command( array( 'scaffold', 'package-tests', $package_dir ), array( 'force' => $force ) );
}

if ( ! Utils\get_flag_value( $assoc_args, 'skip-readme' ) ) {
WP_CLI::run_command( array( 'scaffold', 'package-readme', $package_dir ), array( 'force' => $force ) );
}
}

/**
* Generate a README.md for your command.
*
* <dir>
* : Directory of an existing command.
*
* [--force]
* : Overwrite the readme if it already exists.
*
* @when before_wp_load
* @subcommand package-readme
*/
public function package_readme( $args ) {

list( $package_dir ) = $args;

if ( ! is_dir( $package_dir ) || ! file_exists( $package_dir . '/composer.json' ) ) {
WP_CLI::error( "Invalid package directory. composer.json file must be present." );
}

$composer_obj = json_decode( file_get_contents( $package_dir . '/composer.json' ), true );
if ( ! $composer_obj ) {
WP_CLI::error( 'Invalid composer.json in package directory.' );
}

$force = Utils\get_flag_value( $assoc_args, 'force' );

$package_root = dirname( dirname( __FILE__ ) );
$template_path = $package_root . '/templates/';

$readme_args = array(
'package_name' => $composer_obj['name'],
'package_name_border' => str_pad( '', strlen( $composer_obj['name'] ), '=' ),
'package_description' => $composer_obj['description'],
'has_travis' => file_exists( $package_dir . '/.travis.yml' ),
);

$files_written = $this->create_files( array(
"{$package_dir}/README.md" => Utils\mustache_render( "{$template_path}/readme.mustache", $readme_args ),
), $force );

if ( empty( $files_written ) ) {
WP_CLI::log( 'Package readme generation skipped.' );
} else {
WP_CLI::success( 'Created package readme.' );
}
}

/**

22
templates/readme.mustache Normal file
View file

@ -0,0 +1,22 @@
{{package_name}}
{{package_name_border}}

{{package_description}}

{{#has_travis}}
[![Build Status](https://travis-ci.org/{{package_name}}.svg?branch=master)](https://travis-ci.org/{{package_name}})
{{/has_travis}}

Quick links: [Installing](#installing) | [Contributing](#contributing)

## Installing

This package requires the latest nightly version of WP-CLI. Update with `wp cli update --nightly`.

Once you've done so, you can install this package with `wp package install {{package_name}}`

## Contributing

Code and ideas are more than welcome.

Please [open an issue](https://github.com/{{package_name}}/issues) with questions, feedback, and violent dissent. Pull requests are expected to include test coverage.