Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
5623e8b
Initial CLI command script
mukeshpanchal27 Mar 2, 2023
b342c4f
Add enpty line at bootom of file
mukeshpanchal27 Mar 2, 2023
d2cc34c
Add readme.txt placeholder files to all wpp modules. Content to be ad…
10upsimon Mar 2, 2023
0c4f99c
Draft v1 of webp-uploads module standalone readme
10upsimon Mar 2, 2023
40e2cc0
Added placeholder readme.txt file to sqlite module folder
10upsimon Mar 2, 2023
eb9f5c3
Added placeholder readme.txt file to audit autoload options module fo…
10upsimon Mar 2, 2023
7255a01
Add export-ignore flags for all standalone module readme.txt files
10upsimon Mar 2, 2023
8bc3ff2
Setting version of webp-uploads plugin readme file to 1.0.0
10upsimon Mar 2, 2023
d3d3ad4
Removed readme.txt files from modules that will not be unbundled
10upsimon Mar 2, 2023
b19e57c
Address review feedback
mukeshpanchal27 Mar 3, 2023
a3b2c74
Add can load functions
mukeshpanchal27 Mar 3, 2023
4ae6593
Address review feedback
mukeshpanchal27 Mar 3, 2023
ca59ae6
Address review feedback
mukeshpanchal27 Mar 3, 2023
d541342
Address review feedback
mukeshpanchal27 Mar 6, 2023
0032104
Address review feedback
mukeshpanchal27 Mar 7, 2023
2bc3c6d
Removed all readme.txt files from modules other than webp-uploads
10upsimon Mar 7, 2023
a4d266c
Added glob pattern to match all readme.txt files in module folders in…
10upsimon Mar 7, 2023
456cc20
Address review feedback
mukeshpanchal27 Mar 8, 2023
6630fef
Added exceptions for synchronous i/o operations
mukeshpanchal27 Mar 9, 2023
2b05070
Address review feedback
mukeshpanchal27 Mar 13, 2023
9806fb5
Merge pull request #662 from WordPress/add/cli-command-for-standalone…
mukeshpanchal27 Mar 13, 2023
fe2f547
Initial commit
mukeshpanchal27 Mar 13, 2023
6740a35
Merge branch 'feature/creating-standalone-plugins' of github.com:Word…
10upsimon Mar 13, 2023
5d8867d
Merge branch 'feature/creating-standalone-plugins' into add/issue-636…
10upsimon Mar 13, 2023
1ebcbfb
Remove dangling readme.txt file from SQLite module
10upsimon Mar 13, 2023
c4e2817
Ongoing modifications to WebP Uploads readme.txt following review fee…
10upsimon Mar 13, 2023
f7255de
Ongoing modifications to WebP Uploads readme.txt following review fee…
10upsimon Mar 13, 2023
fc74809
Amended readme.txt plugin description text for webp-uploads plugin
10upsimon Mar 13, 2023
a453083
Track plugin test assets to be copied to each built plugin within ./b…
10upsimon Mar 13, 2023
135fdb5
Added test-plugins command logic
10upsimon Mar 13, 2023
2a16c5a
Added test-plugins command reference and necessary imports to cli.js …
10upsimon Mar 14, 2023
f4f9733
Added test-plugins command to package.json
10upsimon Mar 14, 2023
828265b
Set plugin-tests to be export ignored so as not to be part of any Wor…
10upsimon Mar 14, 2023
387a8af
Removed unnneded bin folder from within plugin-tests folder
10upsimon Mar 14, 2023
2b73a24
Fixed incorrect variable reference within catch condition
10upsimon Mar 14, 2023
e065b71
Better logging and error handling
10upsimon Mar 14, 2023
c525470
Added exit status 1 for all failures so that test pipelines/worklflow…
10upsimon Mar 14, 2023
0257c5e
Returned with exit code 0 at the end of the plugin test process to si…
10upsimon Mar 14, 2023
cb2e4f5
Added standalone plugin integration testing GitHub workflow
10upsimon Mar 14, 2023
3f5f992
Forcing failed standalone plugin integration test to validate test pi…
10upsimon Mar 14, 2023
9ca5131
Removing forced failure for standalone plugin integration test to val…
10upsimon Mar 14, 2023
1ab29d3
Code and comment formatting following initial draft PR review
10upsimon Mar 14, 2023
55252ac
Remove unnecessary extra line.
felixarntz Mar 14, 2023
1cc462f
Merge branch 'feature/creating-standalone-plugins' into fix/666-check…
mukeshpanchal27 Mar 15, 2023
e5776e5
Merge trunk and resolve conflict
mukeshpanchal27 Mar 15, 2023
f93cf0f
Implement a constant approach
mukeshpanchal27 Mar 15, 2023
bf8fbe6
Remove debug print statement
mukeshpanchal27 Mar 15, 2023
978bba2
Update modules/images/webp-uploads/readme.txt
mukeshpanchal27 Mar 15, 2023
9082aa8
Merge branch 'add/issue-636-module-specific-readme-files' of github.c…
10upsimon Mar 15, 2023
afcf6cf
Enhancements to webp-uploads readme.txt file following review
10upsimon Mar 15, 2023
5bfac79
Enhancements to webp-uploads readme.txt file following review
10upsimon Mar 15, 2023
f48d31c
Merge branch 'trunk' into refresh-with-trunk
Mar 15, 2023
7ba9b77
Avoid declaring a function for can-load check.
Mar 15, 2023
803a26a
Update logic
mukeshpanchal27 Mar 16, 2023
a7fd001
Merge pull request #676 from WordPress/refresh-with-trunk
felixarntz Mar 16, 2023
66ebb84
Merge branch 'feature/creating-standalone-plugins' into fix/666-check…
mukeshpanchal27 Mar 16, 2023
ce70b37
Remove duplicate code
mukeshpanchal27 Mar 16, 2023
3e279a6
Merge pull request #664 from WordPress/add/issue-636-module-specific-…
felixarntz Mar 16, 2023
87ba1cb
Update WebP Uploads readme similarly to #679.
Mar 16, 2023
b4d3bf2
Merge pull request #680 from WordPress/update/webp-uploads-readme
mukeshpanchal27 Mar 17, 2023
829631e
Address review feedback
mukeshpanchal27 Mar 17, 2023
626a66a
Update constant
mukeshpanchal27 Mar 17, 2023
b6a3f5b
Add base .wp-env.json file to plugin-tests as opposed to using top le…
10upsimon Mar 17, 2023
692c2e9
Fix module load issue in admin screen
mukeshpanchal27 Mar 17, 2023
804767b
Fix JS lint errors by formatting with Prettier.
Mar 17, 2023
95f0d6f
Merge pull request #681 from WordPress/666-update-constant
felixarntz Mar 17, 2023
b50b2d9
Use escape function
mukeshpanchal27 Mar 18, 2023
97d865e
use str_starts_with function
mukeshpanchal27 Mar 18, 2023
0c5bc48
Introduce perflab_is_module_standalone()
mukeshpanchal27 Mar 20, 2023
d64a6e8
Merge pull request #682 from WordPress/fix/js-format
mukeshpanchal27 Mar 20, 2023
981db8e
Address review feedback
mukeshpanchal27 Mar 20, 2023
6c4244e
Clarify new function doc block.
felixarntz Mar 21, 2023
db7e8a5
Add GHA for deploy standalone plugins
mukeshpanchal27 Mar 22, 2023
aa2907c
Merge pull request #670 from WordPress/fix/666-check-module-loaded
mukeshpanchal27 Mar 23, 2023
76f9fa5
Merge branch feature/creating-standalone-plugins into add/639-standal…
10upsimon Mar 23, 2023
63365eb
Add .wp-env.json file to plugin-tests folder, instead of using the ba…
10upsimon Mar 23, 2023
e282e9a
Only add necessary composer pacakges for plugin tests
10upsimon Mar 23, 2023
2ab2bf6
Defined --sitetype arg, various ongoing review feedback fixes
10upsimon Mar 23, 2023
187c490
Removed composer install command at project root when running built p…
10upsimon Mar 23, 2023
07d46ef
Defined ability to perform standalone plugins tests in multisite envi…
10upsimon Mar 23, 2023
cc12fd1
Removed force inclusion/activation for plugins in tests bootstrap, as…
10upsimon Mar 23, 2023
5c397ce
Added single and multi site specific tests as part of Github workflow…
10upsimon Mar 23, 2023
d79c35f
Fix: Comment typo
10upsimon Mar 23, 2023
794312d
Use already defined variable for build directory
10upsimon Mar 23, 2023
02112e5
Split up code into main test function and reduce complexity of the ha…
10upsimon Mar 23, 2023
d1f6666
Added new test-plugins-multisite npm script for multisite stadnalone …
10upsimon Mar 23, 2023
44d9bfe
Updated GitHub workflow to use new multisite plugin test script
10upsimon Mar 23, 2023
fea7d11
Lint fixes
10upsimon Mar 23, 2023
9595734
Disable phpunit caching for standalone plugin integration tests
10upsimon Mar 23, 2023
dd08849
Removed duplicate plugin unit test call after multi and single site t…
10upsimon Mar 23, 2023
45d3d88
Updated typedefs for WPTestPluginsSettings type suite
10upsimon Mar 23, 2023
7bc5656
Updated typedef for WPTestPluginsCommandOptions
10upsimon Mar 23, 2023
ba55bb2
Updated typedef for WPTestPluginsCommandOptions
10upsimon Mar 23, 2023
4e955e6
Move export ignore line for plugin-tests alongisde other tooling ignores
10upsimon Mar 23, 2023
a2c815b
Refactored logic to obtain plugin list from plugins.json file as oppo…
10upsimon Mar 23, 2023
1a5ce9a
Adding relevant code comments
10upsimon Mar 23, 2023
14e0e95
Added standalone doReplaceWpEnvContent and doRunUnitTests functions t…
10upsimon Mar 23, 2023
2fa1c75
Added standalone doReplaceWpEnvContent and doRunUnitTests functions t…
10upsimon Mar 23, 2023
bc07130
Added logic to filter results from builtPlugins array when said plugi…
10upsimon Mar 23, 2023
9f20b11
Fixed PHPCS indentation issues in phpunit XML files within plugin-tes…
10upsimon Mar 23, 2023
3dd9a4c
Merge branch 'feature/creating-standalone-plugins' of github.com:Word…
10upsimon Mar 23, 2023
fc9403d
Merge branch 'feature/creating-standalone-plugins' into add/639-stand…
10upsimon Mar 23, 2023
95fed30
Remove incorrectly added wp-env json file in server-timing folder
10upsimon Mar 23, 2023
0efd1e6
Altered logic so that copied wp-env json file is an overrides one, an…
10upsimon Mar 23, 2023
4eb793b
Address review feedback
mukeshpanchal27 Mar 24, 2023
fb93494
Changes to GH workflow file for standalone plugin integration tests
10upsimon Mar 24, 2023
a63c123
Update workflow steps
mukeshpanchal27 Mar 24, 2023
5d720c6
Add newline at end of file
mukeshpanchal27 Mar 27, 2023
f485ccf
Tidy up test run logic to not unnecessarily restart wp-env | Deactive…
10upsimon Mar 31, 2023
e36d516
Made main wpp plugin slug dynamic based on process cwd | Deleted wp-e…
10upsimon Mar 31, 2023
555c071
Fixed typo and moved operation that deletes wp-env overrides file to …
10upsimon Mar 31, 2023
8dce595
Merge pull request #671 from WordPress/add/639-standalone-built-plugi…
felixarntz Mar 31, 2023
a05f869
Minor change
mukeshpanchal27 Apr 1, 2023
a2c11d3
uncomment code
mukeshpanchal27 Apr 1, 2023
ff881a0
Merge pull request #694 from WordPress/trunk
mukeshpanchal27 Apr 3, 2023
5d4e5c0
Add ASSETS_DIR for plugin
mukeshpanchal27 Apr 4, 2023
62e88fc
Add generator tag for WebP Uploads
mukeshpanchal27 Apr 4, 2023
91b45e0
TODO comment added
mukeshpanchal27 Apr 4, 2023
9f0a131
Update output for meta tag
mukeshpanchal27 Apr 4, 2023
7e54759
Refer to the constant directly.
felixarntz Apr 4, 2023
d9111ca
Merge pull request #696 from WordPress/fix/692-add-generator-meta-tag
felixarntz Apr 4, 2023
748df3b
Add manual workflow related changes
mukeshpanchal27 Apr 5, 2023
d315bda
Remove comment for generating zip code
mukeshpanchal27 Apr 5, 2023
511eac7
Remove pull_request trigger
mukeshpanchal27 Apr 5, 2023
ce90a8e
Merge pull request #686 from WordPress/fix/638-deploy-standalone-plugin
mukeshpanchal27 Apr 5, 2023
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
11 changes: 11 additions & 0 deletions bin/plugin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const {
handler: translationsHandler,
options: translationsOptions,
} = require( './commands/translations' );
const {
handler: buildPluginsHandler,
options: buildPluginsOptions,
} = require( './commands/build-plugins' );
const {
handler: enabledModulesHandler,
options: enabledModulesOptions,
Expand Down Expand Up @@ -69,6 +73,13 @@ withOptions( program.command( 'module-translations' ), translationsOptions )
)
.action( catchException( translationsHandler ) );

withOptions( program.command( 'build-standalone-plugins' ), buildPluginsOptions )
.alias( 'build-plugins' )
.description(
'Build standalone plugins'
)
.action( catchException( buildPluginsHandler ) );

withOptions(
program.command( 'default-enabled-modules' ),
enabledModulesOptions
Expand Down
214 changes: 214 additions & 0 deletions bin/plugin/commands/build-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/**
* External dependencies
*/
const fs = require( 'fs-extra' );
const path = require( 'path' );
const glob = require( 'fast-glob' );

/**
* Internal dependencies
*/
const { log, formats } = require( '../lib/logger' );
const { getModuleDataFromHeader, getModuleHeader } = require( './common' );

exports.options = [];

/**
* Command for copying the files required for the standalone plugin.
*/
exports.handler = async () => {
const pluginsFile = path.join( '.', 'plugins.json' );
fs.readFile( pluginsFile, 'utf8', ( err, jsonString ) => {
if ( err ) {
log(
formats.error(
`Error reading file from disk: "${ err }"`
)
);
}

try {
const plugins = JSON.parse( jsonString );
for ( const moduleDir in plugins ) {
const pluginVersion = plugins[ moduleDir ]?.version;
const pluginSlug = plugins[ moduleDir ]?.slug;

if ( ! pluginVersion || ! pluginSlug ) {
log(
formats.error(
'The given module configuration is invalid, the module JSON object is missing the "version" and/or "slug" properties, or they are misspelled.'
)
);
return;
}

try {
// Copy module files from the folder.
const modulePath = path.join( '.', 'modules/' + moduleDir );
const buildModulePath = path.join( '.', 'build/' + pluginSlug );
try {
// Clean up build module files directory.
fs.rmSync( buildModulePath, { force: true, recursive: true } );

fs.copySync( modulePath, buildModulePath, { overwrite: true } );
} catch ( copyError ) {
log(
formats.error(
`Error copying files for plugin "${ pluginSlug }": "${ copyError }"`
)
);
continue;
}

// Update file content.
updatePluginHeader( {
modulePath: moduleDir,
slug: pluginSlug,
version: pluginVersion,
pluginPath: buildModulePath,
} );

// Update text domain.
updateModuleDetails( {
pluginPath: buildModulePath,
regex: '[\']performance-lab[\']',
result: `'${ pluginSlug }'`,
} );

// Update `@package`.
updateModuleDetails( {
pluginPath: buildModulePath,
regex: '@package\\s{1,}performance-lab',
result: '@package ' + pluginSlug,
} );
} catch ( error ) {
log(
formats.error(
`${ error }`
)
);
}
}
} catch ( jsonError ) {
log(
formats.error(
`Error parsing JSON string: "${ jsonError }"`
)
);
}
} );
};

/**
* Updates the `load.php` file header information.
*
* @param {Object} settings Plugin settings.
*/
async function updatePluginHeader( settings ) {
const { modulePath, version, slug, pluginPath } = settings;
// Specific module `load.php` file content.
const buildLoadFile = path.join( pluginPath, 'load.php' );
let buildLoadFileContent = '';
try {
buildLoadFileContent = fs.readFileSync( buildLoadFile, 'utf-8' );
} catch ( err ) {
log(
formats.error(
`Error reading the file "${ buildLoadFile }": "${ err }"`
)
);
}

if ( buildLoadFileContent === '' ) {
log(
formats.error(
`Error reading the file "${ buildLoadFile }"`
)
);
return false;
}
const moduleHeader = await getModuleHeader( buildLoadFileContent );

// Get module header data.
const { name, description } = await getModuleDataFromHeader( moduleHeader );

const pluginHeader = `/**
* Plugin Name: ${ name }
* Plugin URI: https://github.com/WordPress/performance/tree/trunk/modules/${ modulePath }
* Description: ${ description }
* Requires at least: 6.1
* Requires PHP: 5.6
* Version: ${ version }
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: ${ slug }
*
* @package ${ slug }
`;
try {
// Replace the module file header.
fs.writeFileSync( buildLoadFile, buildLoadFileContent.replace( moduleHeader, pluginHeader ) );
} catch ( error ) {
log(
formats.error(
`Error replacing module file header: "${ error }"`
)
);
}
}

/**
* Updates the text domain and package details in the build folder files content.
*
* @param {Object} settings Plugin settings.
*/
async function updateModuleDetails( settings ) {
const patterns = [
path.resolve( settings.pluginPath, './**/*.php' ),
];

const files = await glob( patterns, {
ignore: [ __filename ],
} );

const regexp = new RegExp( settings.regex, 'gm' );

files.forEach( ( file ) => {
let content = '';
try {
content = fs.readFileSync( file, 'utf-8' );
} catch ( err ) {
log(
formats.error(
`Error reading the file "${ file }": "${ err }"`
)
);
}

if ( content === '' ) {
log(
formats.error(
`Error reading the file "${ file }"`
)
);
return false;
}

if ( regexp.test( content ) ) {
try {
fs.writeFileSync(
file,
content.replace( regexp, `${ settings.result }` )
);
} catch ( error ) {
log(
formats.error(
`Error replacing content for regex "${ settings.regex }": "${ error }"`
)
);
}
}
} );
}
98 changes: 65 additions & 33 deletions bin/plugin/commands/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const FOCUS_AREAS = {
* Returns a promise resolving to the list of data for all modules.
*
* @param {string} modulesDir Modules directory.
*
* @return {Promise<[]WPModuleData>} Promise resolving to module data list.
*/
exports.getModuleData = async ( modulesDir ) => {
Expand All @@ -41,44 +40,25 @@ exports.getModuleData = async ( modulesDir ) => {

return moduleFiles
.map( ( moduleFile ) => {
let moduleFileContent = '';
try {
moduleFileContent = fs.readFileSync( moduleFile, 'utf-8' );
} catch ( error ) {
log(
formats.error(
`Error reading the file "${ moduleFile }": "${ error }"`
)
);
}
const moduleHeader = exports.getModuleHeader( moduleFileContent );

// Populate slug and focus based on file path.
const moduleDir = path.dirname( moduleFile );
const moduleData = {
slug: path.basename( moduleDir ),
focus: path.basename( path.dirname( moduleDir ) ),
...exports.getModuleDataFromHeader( moduleHeader ),
};

// Map of module header => object property.
const headers = {
'Module Name': 'name',
Description: 'description',
Experimental: 'experimental',
};

// Populate name, description and experimental based on module file headers.
const fileContent = fs.readFileSync( moduleFile, 'utf8' );
const regex = new RegExp(
`^(?:[ \t]*<?php)?[ \t/*#@]*(${ Object.keys( headers ).join(
'|'
) }):(.*)$`,
'gmi'
);
let match = regex.exec( fileContent );
while ( match ) {
const content = match[ 2 ].trim();
const prop = headers[ match[ 1 ] ];
if ( content && prop ) {
moduleData[ prop ] = content;
}
match = regex.exec( fileContent );
}

// Parse experimental field into a boolean.
if ( typeof moduleData.experimental === 'string' ) {
moduleData.experimental =
moduleData.experimental.toLowerCase() === 'yes';
}

return moduleData;
} )
.filter( ( moduleData ) => {
Expand Down Expand Up @@ -133,3 +113,55 @@ exports.getModuleData = async ( modulesDir ) => {
return firstModule.slug.localeCompare( secondModule.slug );
} );
};

/**
* Returns the list of module data for module.
*
* @param {string} moduleHeader Modules file header contetnt.
* @return {WPModuleData} Module data.
*/
exports.getModuleDataFromHeader = ( moduleHeader ) => {
const moduleData = {};
// Map of module header => object property.
const headers = {
'Module Name': 'name',
Description: 'description',
Experimental: 'experimental',
};

const regex = new RegExp(
`^(?:[ \t]*<?php)?[ \t/*#@]*(${ Object.keys( headers ).join(
'|'
) }):(.*)$`,
'gmi'
);
let match = regex.exec( moduleHeader );
while ( match ) {
const content = match[ 2 ].trim();
const prop = headers[ match[ 1 ] ];
if ( content && prop ) {
moduleData[ prop ] = content;
}
match = regex.exec( moduleHeader );
}

// Parse experimental field into a boolean.
if ( typeof moduleData.experimental === 'string' ) {
moduleData.experimental =
moduleData.experimental.toLowerCase() === 'yes';
}

return moduleData;
};

/**
* Returns the file header.
*
* @param {string} moduleFileContent Module file content.
* @return {string} Module file header.
*/
exports.getModuleHeader = ( moduleFileContent ) => {
const regex = /\/\\*\\*[\s\S]+?(?=\*\/)/mi;
const moduleHeader = moduleFileContent.match( regex )?.[ 0 ];
return moduleHeader;
};
31 changes: 31 additions & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@
* @package performance-lab
*/

/**
* Determines whether the feature can be loaded, or whether is already merged into WordPress core.
*
* If it cannot be loaded, it will add an action to display a WordPress admin notice.
*
* @since n.e.x.t
*
* @return bool Whether the feature can be loaded.
*/
function webp_uploads_can_load() {
$can_load = require __DIR__ . '/can-load.php';
if ( $can_load() ) {
return true;
}
add_action(
'admin_notices',
function() {
printf(
'<div class="notice notice-error"><p>%s</p></div>',
esc_html__( 'The WebP Uploads feature cannot be loaded from within the plugin since it is already merged into WordPress core.', 'performance-lab' )
);
}
);
return false;
}

// Do not run the plugin if conditions are not met.
if ( ! webp_uploads_can_load() ) {
return;
}

/**
* Require helper functions and specific integrations.
*/
Expand Down
Loading