Skip to content

Commit

Permalink
Merge pull request #96 from gziolo/add/scaffold-block
Browse files Browse the repository at this point in the history
Add initial scaffold block command
  • Loading branch information
danielbachhuber committed Nov 28, 2017
2 parents d6ed1d2 + daf30d6 commit f174112
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 5 deletions.
56 changes: 56 additions & 0 deletions README.md
Expand Up @@ -345,6 +345,62 @@ wp scaffold taxonomy <slug> [--post_types=<post-types>] [--label=<label>] [--tex



### wp scaffold block

Generate PHP, JS and CSS code for registering a custom block.


~~~
wp scaffold taxonomy <slug> [--title=<title>] [--dashicon=<dashicon>] [--category=<category>] [--textdomain=<textdomain>] [--theme] [--plugin=<plugin>] [--force]
~~~

Blocks are the fundamental element of the Gutenberg editor. They are the primary way in which plugins and themes can register their own functionality and extend the capabilities of the editor.

Learn more from the [Block API documentation](https://wordpress.org/gutenberg/handbook/block-api/).

**OPTIONS**

<slug>
The internal name of the block.

[--title=<title>]
The display title for your block.

[--dashicon=<dashicon>]
The dashicon to make it easier to identify your block.

[--category=<category>]
The category name to help users browse and discover your block.
---
default: widgets
options:
- common
- embed
- formatting
- layout
- reusable-blocks
- widgets

[--textdomain=<textdomain>]
The textdomain to use for the labels.

[--theme]
Create files in the active theme directory. Specify a theme with `--theme=<theme>` to have the file placed in that theme.

[--plugin=<plugin>]
Create files in the given plugin's directory.

[--force]
Overwrite files that already exist.

**EXAMPLES**

# Generate a 'movie' block for the 'simple-life' theme
$ wp scaffold block movie --title="Movie block" --theme=simple-life
Success: Created block 'Movie block'.

Generate PHP code for registering a custom taxonomy.

### wp scaffold theme-tests

Generate files needed for running PHPUnit tests in a theme.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -36,6 +36,7 @@
"commands": [
"scaffold",
"scaffold _s",
"scaffold block",
"scaffold child-theme",
"scaffold plugin",
"scaffold plugin-tests",
Expand Down
149 changes: 149 additions & 0 deletions features/scaffold-block.feature
@@ -0,0 +1,149 @@
Feature: WordPress block code scaffolding

Background:
Given a WP install
Given I run `wp scaffold plugin movies`
And I run `wp plugin path movies --dir`
And save STDOUT as {PLUGIN_DIR}
Given I run `wp theme install p2 --activate`
And I run `wp theme path p2 --dir`
And save STDOUT as {THEME_DIR}


Scenario: Scaffold a block with an invalid slug
When I try `wp scaffold block The_Godfather`
Then STDERR should be:
"""
Error: Invalid block slug specified. Block slugs can contain only lowercase alphanumeric characters or dashes, and start with a letter.
"""

Scenario: Scaffold a block with a missing plugin and theme
When I try `wp scaffold block the-godfather`
Then STDERR should be:
"""
Error: No plugin or theme selected.
"""

Scenario: Scaffold a block for an invalid plugin
When I try `wp scaffold block the-godfather --plugin=unknown`
Then STDERR should be:
"""
Error: Can't find 'unknown' plugin.
"""

Scenario: Scaffold a block for a specific plugin
When I run `wp scaffold block the-green-mile --plugin=movies`
Then the {PLUGIN_DIR}/blocks/the-green-mile.php file should exist
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
"""
function the_green_mile_enqueue_block_editor_assets() {
"""
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
"""
$block_js = 'the-green-mile/block.js';
"""
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
"""
$editor_css = 'the-green-mile/editor.css';
"""
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
"""
add_action( 'enqueue_block_editor_assets', 'the_green_mile_enqueue_block_editor_assets' );
"""
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should exist
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
"""
wp.blocks.registerBlockType( 'movies/the-green-mile', {
"""
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
"""
title: __( 'The green mile', 'movies' ),
"""
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
"""
category: 'widgets',
"""
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
"""
__( 'Replace with your content!', 'movies' )
"""
And the {PLUGIN_DIR}/blocks/the-green-mile/editor.css file should exist
And the {PLUGIN_DIR}/blocks/the-green-mile/editor.css file should contain:
"""
.wp-block-movies-the-green-mile {
"""
And STDOUT should be:
"""
Success: Created block 'The green mile'.
"""

Scenario: Scaffold a block with a specific title provided
When I run `wp scaffold block shawshank-redemption --plugin=movies --title="The Shawshank Redemption"`
Then the {PLUGIN_DIR}/blocks/shawshank-redemption/block.js file should contain:
"""
title: __( 'The Shawshank Redemption', 'movies' ),
"""
And STDOUT should be:
"""
Success: Created block 'The Shawshank Redemption'.
"""

Scenario: Scaffold a block with a specific dashicon provided
When I run `wp scaffold block forrest-gump --plugin=movies --dashicon=movie`
Then the {PLUGIN_DIR}/blocks/forrest-gump/block.js file should contain:
"""
icon: 'movie',
"""
And STDOUT should be:
"""
Success: Created block 'Forrest gump'.
"""

Scenario: Scaffold a block with a specific category provided
When I run `wp scaffold block pulp-fiction --plugin=movies --category=embed`
Then the {PLUGIN_DIR}/blocks/pulp-fiction/block.js file should contain:
"""
category: 'embed',
"""
And STDOUT should be:
"""
Success: Created block 'Pulp fiction'.
"""

Scenario: Scaffold a block with a specific textdomain provided
When I run `wp scaffold block inception --plugin=movies --textdomain=MY-MOVIES`
Then the {PLUGIN_DIR}/blocks/inception/block.js file should contain:
"""
__( 'Replace with your content!', 'MY-MOVIES' )
"""
And STDOUT should be:
"""
Success: Created block 'Inception'.
"""

Scenario: Scaffold a block for an active theme
When I run `wp scaffold block fight-club --theme`
Then the {THEME_DIR}/blocks/fight-club.php file should exist
And the {THEME_DIR}/blocks/fight-club/block.js file should exist
And the {THEME_DIR}/blocks/fight-club/editor.css file should exist
And STDOUT should be:
"""
Success: Created block 'Fight club'.
"""

Scenario: Scaffold a block for an invalid theme
When I try `wp scaffold block intouchables --theme=unknown`
Then STDERR should be:
"""
Error: Can't find 'unknown' theme.
"""

Scenario: Scaffold a block for a specific theme
When I run `wp scaffold block intouchables --theme=p2`
Then the {THEME_DIR}/blocks/intouchables.php file should exist
And the {THEME_DIR}/blocks/intouchables/block.js file should exist
And the {THEME_DIR}/blocks/intouchables/editor.css file should exist
And STDOUT should be:
"""
Success: Created block 'Intouchables'.
"""

0 comments on commit f174112

Please sign in to comment.