Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Build: Generate plugin version and make the build script plugin agnostic
  • Loading branch information
youknowriad committed Oct 27, 2025
commit 692532dab1a1dd8ef8b016432a784fde1dbccee9
9 changes: 0 additions & 9 deletions bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ npm ci
status "Generating build... 👷‍♀️"
npm run build

# Temporarily modify `gutenberg.php` with production constants defined. Use a
# temp file because `bin/generate-gutenberg-php.php` reads from `gutenberg.php`
# so we need to avoid writing to that file at the same time.
php bin/generate-gutenberg-php.php > gutenberg.tmp.php
mv gutenberg.tmp.php gutenberg.php

# Generate the plugin zip file.
status "Creating archive... 🎁"
zip --recurse-paths --no-dir-entries \
Expand All @@ -92,7 +86,4 @@ zip --recurse-paths --no-dir-entries \
changelog.txt \
README.md

# Reset `gutenberg.php`.
git checkout gutenberg.php

success "Done. You've built Gutenberg! 🎉 "
64 changes: 0 additions & 64 deletions bin/generate-gutenberg-php.php

This file was deleted.

51 changes: 47 additions & 4 deletions bin/packages/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,9 @@ async function generateScriptRegistrationPhp( scripts ) {
await readFile( path.join( ROOT_DIR, 'package.json' ), 'utf8' )
);
const prefix = rootPackageJson.wpPlugin?.prefix || 'gutenberg';
const version = rootPackageJson.version;
const versionConstant =
prefix.toUpperCase().replace( /-/g, '_' ) + '_VERSION';

// Read templates
const templatesDir = path.join( __dirname, 'templates' );
Expand Down Expand Up @@ -907,10 +910,10 @@ async function generateScriptRegistrationPhp( scripts ) {
.replace( '{{SCRIPTS}}', scriptsArray );

// Generate registration file (logic only)
const registrationContent = registrationTemplate.replace(
/\{\{PREFIX\}\}/g,
prefix
);
const registrationContent = registrationTemplate
.replace( /\{\{PREFIX\}\}/g, prefix )
.replace( /\{\{VERSION\}\}/g, version )
.replace( /\{\{VERSION_CONSTANT\}\}/g, versionConstant );

// Write files
const buildDir = path.join( ROOT_DIR, 'build' );
Expand All @@ -929,6 +932,44 @@ async function generateScriptRegistrationPhp( scripts ) {
);
}

/**
* Generate PHP file for version constant.
*/
async function generateVersionPhp() {
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of duplication between this and generateScriptRegistrationPhp. Maybe we should generalize a function for writing a file from a template with a given set of variables? (maybe some default variables)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed :) I'm planning to do some kind of cleanup PR to make things more digestible in the build tool.

const ROOT_DIR = path.join( PACKAGES_DIR, '..' );
const rootPackageJson = JSON.parse(
await readFile( path.join( ROOT_DIR, 'package.json' ), 'utf8' )
);
const prefix = rootPackageJson.wpPlugin?.prefix || 'gutenberg';
const version = rootPackageJson.version;

// Generate version constant name: 'gutenberg' → 'GUTENBERG_VERSION'
const versionConstant =
prefix.toUpperCase().replace( /-/g, '_' ) + '_VERSION';

// Read template
const templatesDir = path.join( __dirname, 'templates' );
const versionTemplate = await readFile(
path.join( templatesDir, 'version.php.template' ),
'utf8'
);

// Generate version file
const versionContent = versionTemplate
.replace( /\{\{PREFIX\}\}/g, prefix )
.replace( /\{\{VERSION_CONSTANT\}\}/g, versionConstant )
.replace( /\{\{VERSION\}\}/g, version );

// Write file
const buildDir = path.join( ROOT_DIR, 'build' );
await mkdir( buildDir, { recursive: true } );
await writeFile(
path.join( buildDir, 'version.php' ),
versionContent,
'utf8'
);
}

/**
* Generate main index.php that loads both modules and scripts.
*/
Expand Down Expand Up @@ -1332,11 +1373,13 @@ async function buildAll() {
generateMainIndexPhp(),
generateModuleRegistrationPhp( modules ),
generateScriptRegistrationPhp( scripts ),
generateVersionPhp(),
] );
console.log( ' ✔ Generated build/modules.php' );
console.log( ' ✔ Generated build/modules/index.php' );
console.log( ' ✔ Generated build/scripts.php' );
console.log( ' ✔ Generated build/scripts/index.php' );
console.log( ' ✔ Generated build/version.php' );
console.log( ' ✔ Generated build/index.php' );

const totalTime = Date.now() - startTime;
Expand Down
6 changes: 6 additions & 0 deletions bin/packages/templates/index.php.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* @package {{PREFIX}}
*/

// Load version constant.
$version_file = __DIR__ . '/version.php';
if ( file_exists( $version_file ) ) {
require_once $version_file;
}

// Load script module registration.
$modules_file = __DIR__ . '/modules.php';
if ( file_exists( $modules_file ) ) {
Expand Down
2 changes: 1 addition & 1 deletion bin/packages/templates/script-registration.php.template
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if ( ! function_exists( '{{PREFIX}}_register_package_scripts' ) ) {
* Register all package scripts.
*/
function {{PREFIX}}_register_package_scripts( $scripts ) {
$default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
$default_version = defined( '{{VERSION_CONSTANT}}' ) && ! SCRIPT_DEBUG ? {{VERSION_CONSTANT}} : time();
$extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';

$scripts_dir = __DIR__ . '/scripts';
Expand Down
11 changes: 11 additions & 0 deletions bin/packages/templates/version.php.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Plugin version constant - Auto-generated by build process.
* Do not edit this file manually.
*
* @package {{PREFIX}}
*/

if ( ! defined( '{{VERSION_CONSTANT}}' ) ) {
define( '{{VERSION_CONSTANT}}', '{{VERSION}}' );
}
6 changes: 1 addition & 5 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
* @package gutenberg
*/

### BEGIN AUTO-GENERATED DEFINES
defined( 'GUTENBERG_DEVELOPMENT_MODE' ) or define( 'GUTENBERG_DEVELOPMENT_MODE', true );
### END AUTO-GENERATED DEFINES
defined( 'GUTENBERG_MINIMUM_WP_VERSION' ) or define( 'GUTENBERG_MINIMUM_WP_VERSION', '6.7' );


gutenberg_pre_init();

/**
Expand Down Expand Up @@ -55,7 +51,7 @@ function gutenberg_build_files_notice() {
*/
function gutenberg_pre_init() {
global $wp_version;
if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && ! file_exists( __DIR__ . '/build/scripts/blocks' ) ) {
if ( ! file_exists( __DIR__ . '/build/scripts/blocks' ) ) {
add_action( 'admin_notices', 'gutenberg_build_files_notice' );
return;
}
Expand Down
1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
<rule ref="Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames">
<exclude-pattern>/phpunit/*</exclude-pattern>
<exclude-pattern>/packages/*</exclude-pattern>
<exclude-pattern>/bin/generate-gutenberg-php\.php$</exclude-pattern>
<properties>
<property name="functionsWhiteList" type="array">
<element value="/^_?gutenberg.+/"/>
Expand Down
Loading