Skip to content
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

[Create-block] Add --variant flag to allow creation of different block type variants #41289

Merged
merged 66 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
6438856
Add npmDevDependencies as a template variable. Update messaging to sh…
ryanwelcher Mar 24, 2022
6f24eb8
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Mar 25, 2022
0b691cc
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Apr 4, 2022
c847911
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Apr 9, 2022
b0b836e
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Apr 22, 2022
cdb77bc
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher May 19, 2022
e8d6cc3
Remove extra code block.
ryanwelcher May 24, 2022
2b4e90c
Add --is-dynamic flag and some conditional logic as a first pass at m…
ryanwelcher May 24, 2022
df813af
Update templates and tutorial packages to use new templates.
ryanwelcher May 24, 2022
8a59dff
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher May 24, 2022
b52fd0b
Merge branch 'trunk' into try/adding-is-dynamic-flag
ryanwelcher May 24, 2022
505db7d
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 1, 2022
85af639
Merge branch 'trunk' of github.com:ryanwelcher/gutenberg into trunk
ryanwelcher Jun 1, 2022
c69e955
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 8, 2022
5363d2c
Merge branch 'trunk' into try/adding-is-dynamic-flag
ryanwelcher Jun 8, 2022
7a2fd82
Revert code to check the slug of a file.
ryanwelcher Jun 8, 2022
a353949
Check for empty template files before writing them into the filesystem.
ryanwelcher Jun 8, 2022
4bae818
Remove files with block type in the name.
ryanwelcher Jun 8, 2022
211ae27
Add the {{isDynamic}} placeholders to the templates bundled with the …
ryanwelcher Jun 8, 2022
2a1b2bf
Add readme and changelog entries.
ryanwelcher Jun 8, 2022
9ab2889
Render the output files will whitespace maintained.
ryanwelcher Jun 9, 2022
567a22c
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 9, 2022
898d8e8
Updating the create-block-tutorial-template to use the mustache condi…
ryanwelcher Jun 14, 2022
430a9f0
Add the --webpack-copy-php to the scripts based on the the --is-dynam…
ryanwelcher Jun 14, 2022
1e7a1d5
Move the .trim call into the context of the if statement. Proof that …
ryanwelcher Jun 14, 2022
c3de411
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 14, 2022
969eac8
Update packages/create-block/CHANGELOG.md
gziolo Jun 15, 2022
b31ab37
Update packages/create-block/lib/index.js
gziolo Jun 15, 2022
9d61058
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 22, 2022
ffc4a63
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 23, 2022
ce4613d
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 28, 2022
614bb81
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 29, 2022
7d39306
Merge branch 'trunk' of github.com:ryanwelcher/gutenberg into trunk
ryanwelcher Jun 29, 2022
5b079b5
Remove extra code block added in error.
ryanwelcher Jun 29, 2022
c809657
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 30, 2022
7f680ab
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jul 22, 2022
a729ae0
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 8, 2022
08488a0
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 11, 2022
682510d
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 16, 2022
dc4dbfc
Merge branch 'trunk' into try/adding-is-dynamic-flag
ryanwelcher Aug 16, 2022
2c52e09
Merge branch 'try/adding-is-dynamic-flag' of github.com:ryanwelcher/g…
ryanwelcher Aug 16, 2022
ba0c060
Adding variants array to the template and generating template variabl…
ryanwelcher Aug 16, 2022
d4388d7
Add isStatic check to save.js
ryanwelcher Aug 16, 2022
6cdfed1
Add logic to return empty object if no variants are defined in the te…
ryanwelcher Aug 16, 2022
903b0c9
Update the template variable names to be is{variantName}Variant.
ryanwelcher Aug 16, 2022
c841b26
Update templates to use new variable name convention and look for the…
ryanwelcher Aug 16, 2022
987d7f8
If no variant is select via CLI, use the first one in the array of de…
ryanwelcher Aug 16, 2022
689041b
Check that the passed variant is a valid variant and exit if not.
ryanwelcher Aug 16, 2022
352869e
Define a single variant for the create-block-tutorial-template. Remov…
ryanwelcher Aug 16, 2022
2db5457
Add template selection to interactive mode.
ryanwelcher Aug 16, 2022
8c74ad2
Update changelog.
ryanwelcher Aug 16, 2022
7a3dbab
Update isDynamic to isDynamicVariant
ryanwelcher Aug 18, 2022
ae65dee
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 18, 2022
2ebd8f8
Merge branch 'trunk' into try/adding-is-dynamic-flag
ryanwelcher Aug 18, 2022
ca0b38b
Replace variantTypes with variantVars
ryanwelcher Aug 18, 2022
5349b5f
Update isDynamic to isDynamicVariant... again.
ryanwelcher Aug 18, 2022
fb27be1
Update readme to use --variant flag.
ryanwelcher Aug 18, 2022
6aae5e6
Add logic to conditionally show the variant prompt only if there are …
ryanwelcher Aug 18, 2022
d406438
Adds variants to the es5 block and removes "static" from various prop…
ryanwelcher Aug 18, 2022
4149c35
Change "static" template to "standard".
ryanwelcher Aug 18, 2022
4e304ad
Convert variants to an object where the property is the variant name …
ryanwelcher Aug 18, 2022
50bb648
Define a dynamic variant for the create-block-tutorial and override t…
ryanwelcher Aug 18, 2022
07d0614
Get the variant first and then retrieve the defaults based on that va…
ryanwelcher Aug 19, 2022
1c0b85c
Update templates.js
gziolo Aug 22, 2022
e700c55
Update templates.js
gziolo Aug 22, 2022
11a27c0
Update packages/create-block/lib/prompts.js
gziolo Aug 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import './editor.scss';
* Internal dependencies
*/
import Edit from './edit';
{{#isStaticVariant}}
import save from './save';
{{/isStaticVariant}}
import metadata from './block.json';

/**
Expand All @@ -41,8 +43,10 @@ registerBlockType( metadata.name, {
* @see ./edit.js
*/
edit: Edit,
{{#isStaticVariant}}
/**
* @see ./save.js
*/
save,
{{/isStaticVariant}}
} );
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#isStaticVariant}}
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
Expand All @@ -21,3 +22,4 @@ export default function save( { attributes } ) {
const blockProps = useBlockProps.save();
return <div { ...blockProps }>{ attributes.message }</div>;
}
{{/isStaticVariant}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#isDynamicVariant}}
<p <?php echo get_block_wrapper_attributes(); ?>>
<?php echo esc_html( $atts['message']) ?>
</p>
{{/isDynamicVariant}}
10 changes: 10 additions & 0 deletions packages/create-block-tutorial-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ module.exports = {
html: false,
},
},
variants: {
static: {},
dynamic: {
attributes: {
message: {
type: 'string',
},
},
},
},
pluginTemplatesPath: join( __dirname, 'plugin-templates' ),
blockTemplatesPath: join( __dirname, 'block-templates' ),
assetsPath: join( __dirname, 'assets' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,35 @@
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
{{#isStaticVariant}}
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
{{/isStaticVariant}}
{{#isDynamicVariant}}
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
)
);
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );

/**
* Render callback function
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $block Block instance.
*
* @return string The rendered output.
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $atts, $content, $block) {
ob_start();
require plugin_dir_path( __FILE__ ) . 'build/template.php';
return ob_get_clean();
}
{{/isDynamicVariant}}
1 change: 1 addition & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### New Feature

- Add `--no-plugin` flag to allow scaffolding of a block in an existing plugin ([#41642](https://github.com/WordPress/gutenberg/pull/41642))
- Introduce the `--variant` flag to allow selection of a variant as defined in the template ([#41289](https://github.com/WordPress/gutenberg/pull/41289)).

## 3.6.0 (2022-07-13)

Expand Down
9 changes: 8 additions & 1 deletion packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Options:
--no-wp-scripts disable integration with `@wordpress/scripts` package
--wp-env enable integration with `@wordpress/env` package
-h, --help output usage information
--variant choose a block variant as defined by the template
```

More examples:
Expand All @@ -72,7 +73,13 @@ $ npx @wordpress/create-block --template my-template-package
$ npx @wordpress/create-block --template ./path/to/template-directory
```

4. Help – you need to use `npx` to output usage information.
4. Generating a dynamic block based on the built-in template.

```bash
$ npx @wordpress/create-block --variant dynamic
```

5. Help – you need to use `npx` to output usage information.

```bash
$ npx @wordpress/create-block --help
Expand Down
45 changes: 34 additions & 11 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ program
.arguments( '[slug]' )
.option(
'-t, --template <name>',
'project template type name; allowed values: "static", "es5", the name of an external npm package, or the path to a local directory',
'static'
'project template type name; allowed values: "standard", "es5", the name of an external npm package, or the path to a local directory',
'standard'
)
.option( '--namespace <value>', 'internal namespace for the block name' )
.option(
Expand All @@ -58,6 +58,7 @@ program
)
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
.option( '--no-plugin', 'scaffold only block files' )
.option( '--variant <variant>', 'the variant of the template to use' )
.action(
async (
slug,
Expand All @@ -70,12 +71,16 @@ program
title,
wpScripts,
wpEnv,
variant,
}
) => {
await checkSystemRequirements( engines );
try {
const pluginTemplate = await getPluginTemplate( templateName );
const defaultValues = getDefaultValues( pluginTemplate );
const defaultValues = getDefaultValues(
pluginTemplate,
variant
);
const optionsValues = Object.fromEntries(
Object.entries( {
plugin,
Expand All @@ -85,6 +90,7 @@ program
title,
wpScripts,
wpEnv,
variant,
} ).filter( ( [ , value ] ) => value !== undefined )
);

Expand All @@ -107,14 +113,30 @@ program

const filterOptionsProvided = ( { name } ) =>
! Object.keys( optionsValues ).includes( name );
const blockPrompts = getPrompts( pluginTemplate, [
'slug',
'namespace',
'title',
'description',
'dashicon',
'category',
] ).filter( filterOptionsProvided );

gziolo marked this conversation as resolved.
Show resolved Hide resolved
// Get the variant prompt first. This will help get the default values
const variantPrompt =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I provide the variant from the CLI, it asks again for the variant. We can address it seperately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #43481.

Object.keys( pluginTemplate.variants )?.length > 1
? getPrompts( pluginTemplate, [ 'variant' ] )
: false;

const variantSelection = variantPrompt
? await inquirer.prompt( variantPrompt )
: false;

const blockPrompts = getPrompts(
pluginTemplate,
[
'slug',
'namespace',
'title',
'description',
'dashicon',
'category',
],
variantSelection.variant
).filter( filterOptionsProvided );

const blockAnswers = await inquirer.prompt( blockPrompts );

const pluginAnswers = plugin
Expand Down Expand Up @@ -154,6 +176,7 @@ program
...defaultValues,
...optionsValues,
...blockAnswers,
...variantSelection,
...pluginAnswers,
} );
}
Expand Down
9 changes: 7 additions & 2 deletions packages/create-block/lib/init-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = async ( {
npmDependencies,
npmDevDependencies,
customScripts,
isDynamicVariant,
} ) => {
const cwd = join( process.cwd(), slug );

Expand All @@ -42,13 +43,17 @@ module.exports = async ( {
main: wpScripts && 'build/index.js',
scripts: {
...( wpScripts && {
build: 'wp-scripts build',
build: isDynamicVariant
? 'wp-scripts build --webpack-copy-php'
: 'wp-scripts build',
format: 'wp-scripts format',
'lint:css': 'wp-scripts lint-style',
'lint:js': 'wp-scripts lint-js',
'packages-update': 'wp-scripts packages-update',
'plugin-zip': 'wp-scripts plugin-zip',
start: 'wp-scripts start',
start: isDynamicVariant
? 'wp-scripts start --webpack-copy-php'
: 'wp-scripts start',
} ),
...( wpEnv && { env: 'wp-env' } ),
...customScripts,
Expand Down
6 changes: 5 additions & 1 deletion packages/create-block/lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const writeOutputTemplate = async ( inputFile, outputFile, view ) => {
? join( view.slug, outputFile.replace( /\$slug/g, view.slug ) )
: outputFile;
await makeDir( dirname( outputFilePath ) );
writeFile( outputFilePath, render( inputFile, view ) );
// If the rendered template is empty, don't write it. This is how we can conditionally add template files.
const renderedFile = render( inputFile, view );
if ( renderedFile.trim().length ) {
writeFile( outputFilePath, renderedFile );
}
};

module.exports = {
Expand Down
8 changes: 8 additions & 0 deletions packages/create-block/lib/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,16 @@ const updateURI = {
message: 'A custom update URI for the plugin (optional):',
};

const variant = {
type: 'list',
name: 'variant',
message: 'The template variant to use for this block:',
choices: [],
};

module.exports = {
slug,
variant,
namespace,
title,
description,
Expand Down
7 changes: 5 additions & 2 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const initWPScripts = require( './init-wp-scripts' );
const initWPEnv = require( './init-wp-env' );
const { code, info, success, error } = require( './log' );
const { writeOutputAsset, writeOutputTemplate } = require( './output' );
const { getTemplateVariantVars } = require( './templates' );

module.exports = async (
{ blockOutputTemplates, pluginOutputTemplates, outputAssets },
{ blockOutputTemplates, pluginOutputTemplates, outputAssets, variants },
{
$schema,
apiVersion,
Expand Down Expand Up @@ -43,11 +44,11 @@ module.exports = async (
editorScript,
editorStyle,
style,
variant,
}
) => {
slug = slug.toLowerCase();
namespace = namespace.toLowerCase();

/**
* --no-plugin relies on the used template supporting the [blockTemplatesPath property](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#blocktemplatespath).
* If the blockOutputTemplates object has no properties, we can assume that there was a custom --template passed that
Expand Down Expand Up @@ -99,6 +100,8 @@ module.exports = async (
editorScript,
editorStyle,
style,
...getTemplateVariantVars( variants, variant ),
...variants[ variant ],
gziolo marked this conversation as resolved.
Show resolved Hide resolved
};

if ( plugin ) {
Expand Down
Loading