<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[53311] trunk: Tools: Further automate backporting from Gutenberg to Core</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/53311">53311</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/53311","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>gziolo</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-04-29 13:59:49 +0000 (Fri, 29 Apr 2022)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Tools: Further automate backporting from Gutenberg to Core
Follow-up for <a href="https://core.trac.wordpress.org/ticket/51491">#51491</a>.
Updating WordPress packages is currently a manual process that takes some reading and trial & error to figure out. This PR adds a single npm task called `sync-gutenberg-packages` that automates this entire process.
Props zieladam.
Fixes <a href="https://core.trac.wordpress.org/ticket/55642">#55642</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkGruntfilejs">trunk/Gruntfile.js</a></li>
<li><a href="#trunkpackagejson">trunk/package.json</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>trunk/tools/release/</li>
<li><a href="#trunktoolsreleasesyncgutenbergpackagesjs">trunk/tools/release/sync-gutenberg-packages.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkGruntfilejs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/Gruntfile.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/Gruntfile.js 2022-04-29 13:31:48 UTC (rev 53310)
+++ trunk/Gruntfile.js 2022-04-29 13:59:49 UTC (rev 53311)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -13,7 +13,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> SOURCE_DIR = 'src/',
</span><span class="cx" style="display: block; padding: 0 10px"> BUILD_DIR = 'build/',
</span><span class="cx" style="display: block; padding: 0 10px"> WORKING_DIR = grunt.option( 'dev' ) ? SOURCE_DIR : BUILD_DIR,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- BANNER_TEXT = '/*! This file is auto-generated */',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ BANNER_TEXT = '/*! This file is auto-generated */',
</ins><span class="cx" style="display: block; padding: 0 10px"> autoprefixer = require( 'autoprefixer' ),
</span><span class="cx" style="display: block; padding: 0 10px"> sass = require( 'sass' ),
</span><span class="cx" style="display: block; padding: 0 10px"> phpUnitWatchGroup = grunt.option( 'group' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -80,7 +80,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- usebanner: {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ usebanner: {
</ins><span class="cx" style="display: block; padding: 0 10px"> options: {
</span><span class="cx" style="display: block; padding: 0 10px"> position: 'top',
</span><span class="cx" style="display: block; padding: 0 10px"> banner: BANNER_TEXT,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1216,6 +1216,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'qunit:compiled'
</span><span class="cx" style="display: block; padding: 0 10px"> ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ grunt.registerTask( 'sync-gutenberg-packages', function() {
+ if ( grunt.option( 'update-browserlist' ) ) {
+ // Updating the browserlist database is opt-in and up to the release lead.
+ //
+ // Browserlist database should be updated:
+ // * In each release cycle up until RC1
+ // * If Webpack throws a warning about an outdated database
+ //
+ // It should not be updated:
+ // * After the RC1
+ // * When backporting fixes to older WordPress releases.
+ //
+ // For more context, see:
+ // https://github.com/WordPress/wordpress-develop/pull/2621#discussion_r859840515
+ // https://core.trac.wordpress.org/ticket/55559
+ grunt.task.run( 'browserslist:update' );
+ }
+
+ // Install the latest version of the packages already listed in package.json.
+ grunt.task.run( 'wp-packages:update' );
+
+ // Install any new @wordpress packages that are now required.
+ // Update any non-@wordpress deps to the same version as required in the @wordpress packages (e.g. react 16 -> 17).
+ grunt.task.run( 'wp-packages:refresh-deps' );
+
+ // Build the files stored in the src/ directory.
+ grunt.task.run( 'build:dev' );
+ } );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> grunt.renameTask( 'watch', '_watch' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> grunt.registerTask( 'watch', function() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1637,6 +1666,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } );
</span><span class="cx" style="display: block; padding: 0 10px"> } );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ grunt.registerTask( 'wp-packages:update', 'Update WordPress packages', function() {
+ const distTag = grunt.option('dist-tag') || 'latest';
+ grunt.log.writeln( `Updating WordPress packages (--dist-tag=${distTag})` );
+ spawn( 'npx', [ 'wp-scripts', 'packages-update', '--', `--dist-tag=${distTag}` ], {
+ cwd: __dirname,
+ stdio: 'inherit',
+ } );
+ } );
+
+ grunt.registerTask( 'browserslist:update', 'Update the local database of browser supports', function() {
+ grunt.log.writeln( `Updating browsers list` );
+ spawn( 'npx', [ 'browserslist@latest', '--update-db' ], {
+ cwd: __dirname,
+ stdio: 'inherit',
+ } );
+ } );
+
+ grunt.registerTask( 'wp-packages:refresh-deps', 'Update version of dependencies in package.json to match the ones listed in the latest WordPress packages', function() {
+ const distTag = grunt.option('dist-tag') || 'latest';
+ grunt.log.writeln( `Updating versions of dependencies listed in package.json (--dist-tag=${distTag})` );
+ spawn( 'node', [ 'tools/release/sync-gutenberg-packages.js', `--dist-tag=${distTag}` ], {
+ cwd: __dirname,
+ stdio: 'inherit',
+ } );
+ } );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Patch task.
</span><span class="cx" style="display: block; padding: 0 10px"> grunt.renameTask('patch_wordpress', 'patch');
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunkpackagejson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/package.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/package.json 2022-04-29 13:31:48 UTC (rev 53310)
+++ trunk/package.json 2022-04-29 13:59:49 UTC (rev 53311)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -174,6 +174,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "test:php": "node ./tools/local-env/scripts/docker.js run -T php composer update -W && node ./tools/local-env/scripts/docker.js run php ./vendor/bin/phpunit",
</span><span class="cx" style="display: block; padding: 0 10px"> "test:e2e": "node ./tests/e2e/run-tests.js",
</span><span class="cx" style="display: block; padding: 0 10px"> "test:visual": "node ./tests/visual-regression/run-tests.js",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "wp-packages-update": "wp-scripts packages-update"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "sync-gutenberg-packages": "grunt sync-gutenberg-packages"
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktoolsreleasesyncgutenbergpackagesjs"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tools/release/sync-gutenberg-packages.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/release/sync-gutenberg-packages.js (rev 0)
+++ trunk/tools/release/sync-gutenberg-packages.js 2022-04-29 13:59:49 UTC (rev 53311)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,227 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* eslint-disable no-console */
+/**
+ * External dependencies
+ */
+const fs = require( 'fs' );
+const spawn = require( 'cross-spawn' );
+const { zip, uniq, identity, groupBy } = require( 'lodash' );
+
+/**
+ * Constants
+ */
+const WORDPRESS_PACKAGES_PREFIX = '@wordpress/';
+const { getArgFromCLI } = require( `../../node_modules/@wordpress/scripts/utils` );
+const distTag = getArgFromCLI( '--dist-tag' ) || 'latest';
+
+/**
+ * The main function of this task.
+ *
+ * It installs any missing WordPress packages, and updates the
+ * mismatched dependencies versions, e.g. it would detect that Gutenberg
+ * updated react from 16.0.4 to 17.0.2 and install the latter.
+ */
+function main() {
+ const initialPackageJSON = readJSONFile( `package.json` );
+
+ // Install any missing WordPress packages:
+ const missingWordPressPackages = getMissingWordPressPackages();
+ if ( missingWordPressPackages.length ) {
+ console.log( "The following @wordpress dependencies are missing: " );
+ console.log( missingWordPressPackages );
+ console.log( "Installing via npm..." );
+ installPackages( missingWordPressPackages.map( name => [name, distTag] ) );
+ }
+
+ // Update any outdated non-WordPress packages:
+ const versionMismatches = getMismatchedNonWordPressDependencies();
+ if ( versionMismatches.length ) {
+ console.log( "The following dependencies are outdated: " );
+ console.log( versionMismatches );
+ console.log( "Updating via npm..." );
+ const requiredPackages = versionMismatches.map( ( { name, required } ) => [name, required] );
+ installPackages( requiredPackages );
+ }
+
+ const finalPackageJSON = readJSONFile( "package.json" );
+ outputPackageDiffReport(
+ getPackageVersionDiff( initialPackageJSON, finalPackageJSON ),
+ );
+ process.exit( 0 );
+}
+
+/**
+ * @param {string} fileName File to read.
+ * @return {Object} Parsed data.
+ */
+function readJSONFile( fileName ) {
+ const data = fs.readFileSync( fileName, 'utf8' );
+ return JSON.parse( data );
+}
+
+/**
+ * Spawns npm install --save.
+ *
+ * @param {Array} packages List of tuples [packageName, version] to install.
+ * @return {string} CLI output.
+ */
+function installPackages( packages ) {
+ const packagesWithVersion = packages.map(
+ ( [packageName, version] ) => `${ packageName }@${ version }`,
+ );
+ return spawn.sync( 'npm', ['install', ...packagesWithVersion, '--save'], {
+ stdio: 'inherit',
+ } );
+}
+
+/**
+ * Computes which @wordpress packages are required by the Gutenberg
+ * dependencies that are missing from WordPress package.json.
+ *
+ * @return {Array} List of tuples [packageName, version].
+ */
+function getMissingWordPressPackages() {
+ const perPackageDeps = getPerPackageDeps();
+ const currentPackages = perPackageDeps.map( ( [name] ) => name );
+
+ const requiredWpPackages = uniq( perPackageDeps
+ // Capture the @wordpress dependencies of our dependencies into a flat list.
+ .flatMap( ( [, dependencies] ) => getWordPressPackages( { dependencies } ) )
+ .sort(),
+ );
+
+ return requiredWpPackages.filter(
+ packageName => !currentPackages.includes( packageName ) );
+}
+
+/**
+ * Computes which third party packages are required by the @wordpress
+ * packages, but not by the WordPress repo itself. This includes
+ * both packages that are missing from package.json and any version
+ * mismatches.
+ *
+ * @return {Array} List of objects {name, required, actual} describing version mismatches.
+ */
+function getMismatchedNonWordPressDependencies() {
+ // Get the installed dependencies from package-lock.json
+ const currentPackageJSON = readJSONFile( "package.json" );
+ const currentPackages = getWordPressPackages( currentPackageJSON );
+
+ const packageLock = readJSONFile( "package-lock.json" );
+ const versionConflicts = Object.entries( packageLock.dependencies )
+ .filter( ( [packageName] ) => currentPackages.includes( packageName ) )
+ .flatMap( ( [, { dependencies }] ) => Object.entries( dependencies || {} ) )
+ .filter( identity )
+ .map( ( [name, { version }] ) => ( {
+ name,
+ required: version,
+ actual: packageLock.dependencies[ name ].version,
+ } ) )
+ .filter( ( { required, actual } ) => required !== actual )
+ ;
+
+ // Ensure that all the conflicts can be resolved with the same version
+ const unresolvableConflicts = Object.entries( groupBy( versionConflicts, ( [name] ) => name ) )
+ .map( ( [name, group] ) => [name, group.map( ( [, { required }] ) => required )] )
+ .filter( ( [, group] ) => group.length > 1 );
+ if ( unresolvableConflicts.length > 0 ) {
+ console.error( "Can't resolve some conflicts automatically." );
+ console.error( "Multiple required versions of the following packages were detected:" );
+ console.error( unresolvableConflicts );
+ process.exit( 1 );
+ }
+ return versionConflicts;
+}
+
+/**
+ * Returns a list of dependencies of each @wordpress dependency.
+ *
+ * @return {Object} An object of shape {packageName: [[packageName, version]]}.
+ */
+function getPerPackageDeps() {
+ // Get the dependencies currently listed in the wordpress-develop package.json
+ const currentPackageJSON = readJSONFile( "package.json" );
+ const currentPackages = getWordPressPackages( currentPackageJSON );
+
+ // Get the dependencies that the above dependencies list in their package.json.
+ const deps = currentPackages
+ .map( ( packageName ) => `node_modules/${ packageName }/package.json` )
+ .map( ( jsonPath ) => readJSONFile( jsonPath ).dependencies );
+ return zip( currentPackages, deps );
+}
+
+/**
+ * Takes unserialized package.json data and returns a list of @wordpress dependencies.
+ *
+ * @param {Object} dependencies unserialized package.json data.
+ * @return {string[]} a list of @wordpress dependencies.
+ */
+function getWordPressPackages( { dependencies = {} } ) {
+ return Object.keys( dependencies )
+ .filter( isWordPressPackage );
+}
+
+/**
+ * Returns true if packageName represents a @wordpress package.
+ *
+ * @param {string} packageName Package name to test.
+ * @return {boolean} Is it a @wodpress package?
+ */
+function isWordPressPackage( packageName ) {
+ return packageName.startsWith( WORDPRESS_PACKAGES_PREFIX );
+}
+
+/**
+ * Computes the dependencies difference between two unserialized
+ * package JSON objects. Needed only for the final reporting.
+ *
+ * @param {Object} initialPackageJSON Initial package JSON data.
+ * @param {Object} finalPackageJSON Final package JSON data.
+ * @return {Object} Delta.
+ */
+function getPackageVersionDiff( initialPackageJSON, finalPackageJSON ) {
+ const diff = ['dependencies', 'devDependencies'].reduce(
+ ( result, keyPackageJSON ) => {
+ return Object.keys(
+ finalPackageJSON[ keyPackageJSON ] || {},
+ ).reduce( ( _result, dependency ) => {
+ const initial =
+ initialPackageJSON[ keyPackageJSON ][ dependency ];
+ const final = finalPackageJSON[ keyPackageJSON ][ dependency ];
+ if ( initial !== final ) {
+ _result.push( { dependency, initial, final } );
+ }
+ return _result;
+ }, result );
+ },
+ [],
+ );
+ return diff.sort( ( a, b ) => a.dependency.localeCompare( b.dependency ) );
+}
+
+/**
+ * Prints the delta between two package.json files.
+ *
+ * @param {Object} packageDiff Delta.
+ */
+function outputPackageDiffReport( packageDiff ) {
+ const readableDiff =
+ packageDiff
+ .map( ( { dependency, initial, final } ) => {
+ return `${ dependency }: ${ initial } -> ${ final }`;
+ } )
+ .filter( identity );
+ if ( !readableDiff.length ) {
+ console.log( 'No changes detected' );
+ return;
+ }
+ console.log(
+ [
+ 'The following package versions were changed:',
+ ...readableDiff,
+ ].join( '\n' ),
+ );
+}
+
+main();
+
+/* eslint-enable no-console */
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tools/release/sync-gutenberg-packages.js
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>