Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Add the ability to create custom plugin builds (#3044)
Browse files Browse the repository at this point in the history
* Add the ability to create custom plugin builds

* Use $_ENV instead of $_SERVER inside the generate-feature-config CLI script.
  • Loading branch information
justinshreve authored Oct 17, 2019
1 parent 9d0de7c commit 38c6e92
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
14 changes: 13 additions & 1 deletion bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#!/bin/bash

ZIP_FILE='woocommerce-admin.zip';

while [ $# -gt 0 ]; do
if [[ $1 == '-f' || $1 == '--features' ]]; then
export WC_ADMIN_ADDITIONAL_FEATURES="$2"
fi
if [[ $1 == '-s' || $1 == '--slug' ]]; then
ZIP_FILE="woocommerce-admin-$2.zip";
fi
shift
done

# Exit if any command fails.
set -e

Expand Down Expand Up @@ -74,7 +86,7 @@ build_files=$(ls dist/*/*.{js,css})

# Generate the plugin zip file.
status "Creating archive... 🎁"
zip -r woocommerce-admin.zip \
zip -r ${ZIP_FILE} \
woocommerce-admin.php \
uninstall.php \
includes/ \
Expand Down
11 changes: 10 additions & 1 deletion bin/generate-feature-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
* - plugin: For the standalone feature plugin, for GitHub and WordPress.org.
* - core: Stable features for WooCommerce core merge.
*/
$phase = isset( $_SERVER['WC_ADMIN_PHASE'] ) ? $_SERVER['WC_ADMIN_PHASE'] : ''; // WPCS: sanitization ok.
$phase = isset( $_ENV['WC_ADMIN_PHASE'] ) ? $_ENV['WC_ADMIN_PHASE'] : ''; // WPCS: sanitization ok.

if ( ! in_array( $phase, array( 'development', 'plugin', 'core' ), true ) ) {
$phase = 'plugin'; // Default to plugin when running `npm run build`.
}
$config_json = file_get_contents( 'config/' . $phase . '.json' );
$config = json_decode( $config_json );

if ( ! empty( $_ENV['WC_ADMIN_ADDITIONAL_FEATURES'] ) ) {
$additional_features = json_decode( $_ENV['WC_ADMIN_ADDITIONAL_FEATURES'], true );
if ( is_array( $additional_features ) ) {
foreach ( $additional_features as $feature => $enabled ) {
$config->features->$feature = $enabled;
}
}
}

if ( 'core' !== $phase ) {
$write = "<?php\n";
Expand Down
10 changes: 10 additions & 0 deletions docs/feature-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ We currently support the following environments:
Flags can be added to the files located in the `config/` directory. Make sure to add a flag for each environment and explicitly set the flag to false.
Please add new feature flags alphabetically so they are easy to find.

## Building custom plugin builds

Sometimes it is useful to create a test zip of a plugin, separate from the released WordPress.org version. This makes internal testing easier for non developers, removing the requirment of using Git and NPM commands. These releases are usually uploaded to GitHub releases as a pre release.

You can use the `build:release` command with the `--slug` and `--features` arguments to create a custom build. Base feature flags will be pulled from `config/plugin.json` and your additional changes are overlaid on top. When the build is complete, a `woocommerce-admin-$slug.zip` file will be generated.

For example, to create a `woocommerce-admin-onboarding.zip` build by enabling onboarding in addition to the feature flags defined in `config/plugin.json`, you would run:

`npm run build:release -- --slug onboarding --features '{"onboarding":true}'`.

## Basic Use - Client

The `window.wcAdminFeatures` constant is a global variable containing the feature flags.
Expand Down
11 changes: 3 additions & 8 deletions src/API/OnboardingPlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,9 @@ public function connect_jetpack( $request ) {
$redirect_url = apply_filters( 'woocommerce_onboarding_jetpack_connect_redirect_url', esc_url_raw( $request['redirect_url'] ) );
$connect_url = \Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' );

if ( defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ) {
$connect_url = add_query_arg(
array(
'calypso_env' => WOOCOMMERCE_CALYPSO_ENVIRONMENT,
),
$connect_url
);
}
// @todo When implementing user-facing split testing, this should be abled to a default of 'production'.
$calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'wpcalypso';
$connect_url = add_query_arg( array( 'calypso_env' => $calypso_env ), $connect_url );

return( array(
'slug' => 'jetpack',
Expand Down
3 changes: 2 additions & 1 deletion src/Features/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ public static function update_help_tab() {
* Allows quick access to testing the calypso parts of onboarding.
*/
public static function calypso_tests() {
$calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production';
// @todo When implementing user-facing split testing, this should be abled to a default of 'production'.
$calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'wpcalypso';

if ( Loader::is_admin_page() && class_exists( 'Jetpack' ) && isset( $_GET['test_wc_jetpack_connect'] ) && 1 === absint( $_GET['test_wc_jetpack_connect'] ) ) { // WPCS: CSRF ok.
$redirect_url = esc_url_raw(
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if ( [ 'development', 'plugin', 'core' ].indexOf( WC_ADMIN_PHASE ) === -1 ) {
WC_ADMIN_PHASE = 'core';
}
const WC_ADMIN_CONFIG = require( path.join( __dirname, 'config', WC_ADMIN_PHASE + '.json' ) );
const WC_ADMIN_ADDITIONAL_FEATURES = process.env.WC_ADMIN_ADDITIONAL_FEATURES &&
JSON.parse( process.env.WC_ADMIN_ADDITIONAL_FEATURES ) || {};

const externals = {
'@wordpress/api-fetch': { this: [ 'wp', 'apiFetch' ] },
Expand Down Expand Up @@ -161,7 +163,7 @@ const webpackConfig = {
new FixStyleOnlyEntriesPlugin(),
// Inject the current feature flags.
new DefinePlugin( {
'window.wcAdminFeatures': { ...WC_ADMIN_CONFIG.features },
'window.wcAdminFeatures': { ...WC_ADMIN_CONFIG.features, ...WC_ADMIN_ADDITIONAL_FEATURES },
} ),
new CustomTemplatedPathPlugin( {
modulename( outputPath, data ) {
Expand Down

0 comments on commit 38c6e92

Please sign in to comment.