Skip to content

Commit f174112

Browse files
Merge pull request #96 from gziolo/add/scaffold-block
Add initial scaffold block command
2 parents d6ed1d2 + daf30d6 commit f174112

File tree

7 files changed

+406
-5
lines changed

7 files changed

+406
-5
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,62 @@ wp scaffold taxonomy <slug> [--post_types=<post-types>] [--label=<label>] [--tex
345345

346346

347347

348+
### wp scaffold block
349+
350+
Generate PHP, JS and CSS code for registering a custom block.
351+
352+
353+
~~~
354+
wp scaffold taxonomy <slug> [--title=<title>] [--dashicon=<dashicon>] [--category=<category>] [--textdomain=<textdomain>] [--theme] [--plugin=<plugin>] [--force]
355+
~~~
356+
357+
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.
358+
359+
Learn more from the [Block API documentation](https://wordpress.org/gutenberg/handbook/block-api/).
360+
361+
**OPTIONS**
362+
363+
<slug>
364+
The internal name of the block.
365+
366+
[--title=<title>]
367+
The display title for your block.
368+
369+
[--dashicon=<dashicon>]
370+
The dashicon to make it easier to identify your block.
371+
372+
[--category=<category>]
373+
The category name to help users browse and discover your block.
374+
---
375+
default: widgets
376+
options:
377+
- common
378+
- embed
379+
- formatting
380+
- layout
381+
- reusable-blocks
382+
- widgets
383+
384+
[--textdomain=<textdomain>]
385+
The textdomain to use for the labels.
386+
387+
[--theme]
388+
Create files in the active theme directory. Specify a theme with `--theme=<theme>` to have the file placed in that theme.
389+
390+
[--plugin=<plugin>]
391+
Create files in the given plugin's directory.
392+
393+
[--force]
394+
Overwrite files that already exist.
395+
396+
**EXAMPLES**
397+
398+
# Generate a 'movie' block for the 'simple-life' theme
399+
$ wp scaffold block movie --title="Movie block" --theme=simple-life
400+
Success: Created block 'Movie block'.
401+
402+
Generate PHP code for registering a custom taxonomy.
403+
348404
### wp scaffold theme-tests
349405

350406
Generate files needed for running PHPUnit tests in a theme.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"commands": [
3737
"scaffold",
3838
"scaffold _s",
39+
"scaffold block",
3940
"scaffold child-theme",
4041
"scaffold plugin",
4142
"scaffold plugin-tests",

features/scaffold-block.feature

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Feature: WordPress block code scaffolding
2+
3+
Background:
4+
Given a WP install
5+
Given I run `wp scaffold plugin movies`
6+
And I run `wp plugin path movies --dir`
7+
And save STDOUT as {PLUGIN_DIR}
8+
Given I run `wp theme install p2 --activate`
9+
And I run `wp theme path p2 --dir`
10+
And save STDOUT as {THEME_DIR}
11+
12+
13+
Scenario: Scaffold a block with an invalid slug
14+
When I try `wp scaffold block The_Godfather`
15+
Then STDERR should be:
16+
"""
17+
Error: Invalid block slug specified. Block slugs can contain only lowercase alphanumeric characters or dashes, and start with a letter.
18+
"""
19+
20+
Scenario: Scaffold a block with a missing plugin and theme
21+
When I try `wp scaffold block the-godfather`
22+
Then STDERR should be:
23+
"""
24+
Error: No plugin or theme selected.
25+
"""
26+
27+
Scenario: Scaffold a block for an invalid plugin
28+
When I try `wp scaffold block the-godfather --plugin=unknown`
29+
Then STDERR should be:
30+
"""
31+
Error: Can't find 'unknown' plugin.
32+
"""
33+
34+
Scenario: Scaffold a block for a specific plugin
35+
When I run `wp scaffold block the-green-mile --plugin=movies`
36+
Then the {PLUGIN_DIR}/blocks/the-green-mile.php file should exist
37+
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
38+
"""
39+
function the_green_mile_enqueue_block_editor_assets() {
40+
"""
41+
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
42+
"""
43+
$block_js = 'the-green-mile/block.js';
44+
"""
45+
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
46+
"""
47+
$editor_css = 'the-green-mile/editor.css';
48+
"""
49+
And the {PLUGIN_DIR}/blocks/the-green-mile.php file should contain:
50+
"""
51+
add_action( 'enqueue_block_editor_assets', 'the_green_mile_enqueue_block_editor_assets' );
52+
"""
53+
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should exist
54+
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
55+
"""
56+
wp.blocks.registerBlockType( 'movies/the-green-mile', {
57+
"""
58+
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
59+
"""
60+
title: __( 'The green mile', 'movies' ),
61+
"""
62+
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
63+
"""
64+
category: 'widgets',
65+
"""
66+
And the {PLUGIN_DIR}/blocks/the-green-mile/block.js file should contain:
67+
"""
68+
__( 'Replace with your content!', 'movies' )
69+
"""
70+
And the {PLUGIN_DIR}/blocks/the-green-mile/editor.css file should exist
71+
And the {PLUGIN_DIR}/blocks/the-green-mile/editor.css file should contain:
72+
"""
73+
.wp-block-movies-the-green-mile {
74+
"""
75+
And STDOUT should be:
76+
"""
77+
Success: Created block 'The green mile'.
78+
"""
79+
80+
Scenario: Scaffold a block with a specific title provided
81+
When I run `wp scaffold block shawshank-redemption --plugin=movies --title="The Shawshank Redemption"`
82+
Then the {PLUGIN_DIR}/blocks/shawshank-redemption/block.js file should contain:
83+
"""
84+
title: __( 'The Shawshank Redemption', 'movies' ),
85+
"""
86+
And STDOUT should be:
87+
"""
88+
Success: Created block 'The Shawshank Redemption'.
89+
"""
90+
91+
Scenario: Scaffold a block with a specific dashicon provided
92+
When I run `wp scaffold block forrest-gump --plugin=movies --dashicon=movie`
93+
Then the {PLUGIN_DIR}/blocks/forrest-gump/block.js file should contain:
94+
"""
95+
icon: 'movie',
96+
"""
97+
And STDOUT should be:
98+
"""
99+
Success: Created block 'Forrest gump'.
100+
"""
101+
102+
Scenario: Scaffold a block with a specific category provided
103+
When I run `wp scaffold block pulp-fiction --plugin=movies --category=embed`
104+
Then the {PLUGIN_DIR}/blocks/pulp-fiction/block.js file should contain:
105+
"""
106+
category: 'embed',
107+
"""
108+
And STDOUT should be:
109+
"""
110+
Success: Created block 'Pulp fiction'.
111+
"""
112+
113+
Scenario: Scaffold a block with a specific textdomain provided
114+
When I run `wp scaffold block inception --plugin=movies --textdomain=MY-MOVIES`
115+
Then the {PLUGIN_DIR}/blocks/inception/block.js file should contain:
116+
"""
117+
__( 'Replace with your content!', 'MY-MOVIES' )
118+
"""
119+
And STDOUT should be:
120+
"""
121+
Success: Created block 'Inception'.
122+
"""
123+
124+
Scenario: Scaffold a block for an active theme
125+
When I run `wp scaffold block fight-club --theme`
126+
Then the {THEME_DIR}/blocks/fight-club.php file should exist
127+
And the {THEME_DIR}/blocks/fight-club/block.js file should exist
128+
And the {THEME_DIR}/blocks/fight-club/editor.css file should exist
129+
And STDOUT should be:
130+
"""
131+
Success: Created block 'Fight club'.
132+
"""
133+
134+
Scenario: Scaffold a block for an invalid theme
135+
When I try `wp scaffold block intouchables --theme=unknown`
136+
Then STDERR should be:
137+
"""
138+
Error: Can't find 'unknown' theme.
139+
"""
140+
141+
Scenario: Scaffold a block for a specific theme
142+
When I run `wp scaffold block intouchables --theme=p2`
143+
Then the {THEME_DIR}/blocks/intouchables.php file should exist
144+
And the {THEME_DIR}/blocks/intouchables/block.js file should exist
145+
And the {THEME_DIR}/blocks/intouchables/editor.css file should exist
146+
And STDOUT should be:
147+
"""
148+
Success: Created block 'Intouchables'.
149+
"""

0 commit comments

Comments
 (0)