Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use a WP CLI command to prepare your plugins for release. For more info see https://philkurth.com.au/tips/use-a-wp-cli-command-to-prepare-your-plugins-for-release/
<?php
namespace WpLandingKitPlugin;
use WP_CLI;
use WP_CLI_Command;
/**
* Class MakeRelease
* @package WpLandingKit
*/
class MakeReleaseCommand extends WP_CLI_Command {
const PLUGIN_SLUG = 'wp-landing-kit';
const PLUGIN_DIR_PATH = WP_CONTENT_DIR . '/plugins/' . self::PLUGIN_SLUG;
const RELEASES_DIR_PATH = WP_CONTENT_DIR . '/releases';
/**
* Files & directories to exclude when building the archive.
*/
const RELEASE_EXCLUSIONS = [
'.*',
'tests',
'bin',
'phpunit.xml.dist',
'node_modules',
'package.json',
'package-lock.json',
'webpack.mix.js'
];
private $positional_args;
private $associative_args;
/**
* Creates a release archive with the version name appended to the archive file.
* e.g; my-plugin-name-v1.2.3.zip
*
* ## OPTIONS
*
* [--suffix=<suffix>]
* : Optional suffix to append to end of archive file name. Leading hyphen is automatically added.
*
* @param $positional_args
* @param $associative_args
*/
public function __invoke( $positional_args, $associative_args ) {
$this->positional_args = $positional_args;
$this->associative_args = $associative_args;
exec( $this->shell_command(), $output );
foreach ( $output as $line ) {
WP_CLI::log( $line );
}
WP_CLI::success( 'Done.' );
}
/**
* Build the shell command sequence.
*
* @return string
*/
private function shell_command() {
// Assign class constants and method outputs to vars so we can interpolate in our shell command string. This
// just keeps the command easier to read as opposed to sprintf'ing the shit out of it.
$slug = self::PLUGIN_SLUG;
$plugin_dir = self::PLUGIN_DIR_PATH;
$releases_dir = self::RELEASES_DIR_PATH;
$suffix = $this->suffix();
$version = $this->version_suffix();
$exclusions = $this->format_exclusions();
return "rsync -a $exclusions $plugin_dir $releases_dir \
&& cd $releases_dir \
&& zip -rm {$slug}{$version}{$suffix}.zip $slug \
&& cd - \
&& open $releases_dir;";
}
/**
* Extract the value of the suffix arg passed to the CLI command and format it ready for use in the release archive
* file name.
*
* @return string
*/
private function suffix() {
return ( $suffix = WP_CLI\Utils\get_flag_value( $this->associative_args, 'suffix' ) )
? "-$suffix"
: '';
}
/**
* Extract the plugin version from the plugin headers and format it ready for use in the release archive file name.
*
* @return string
*/
private function version_suffix() {
$data = get_plugin_data( $this->plugin_file_path(), false, false );
return empty( $data['Version'] ) ? '' : "-v{$data['Version']}";
}
/**
* Build full path to plugin main file.
*
* @return string
*/
private function plugin_file_path() {
return self::PLUGIN_DIR_PATH . DIRECTORY_SEPARATOR . self::PLUGIN_SLUG . ".php";
}
/**
* Build the exlude associative args as expected by the rsync command.
*
* @return string
*/
private function format_exclusions() {
if ( empty( self::RELEASE_EXCLUSIONS ) ) {
return '';
}
return join( ' ', array_map( function ( $pattern ) {
return "--exclude=\"$pattern\"";
}, (array) self::RELEASE_EXCLUSIONS ) );
}
}
# To make a release, just call your command.
# This will produce a my-plugin-v1.0.zip file.
wp makerelease
# If you need to suffix your release to differentiate it without
# changing the version, use the --suffix="whatever" assoc arg.
# This will produce a my-plugin-v1.0-feature-test.zip file.
wp makerelease --suffix="feature-test"
<?php
use WpLandingKitPlugin\MakeReleaseCommand;
if ( defined( 'WP_CLI' ) && WP_CLI ) {
try {
require 'lib/MakeReleaseCommand.php';
WP_CLI::add_command( MakeReleaseCommand::COMMAND, MakeReleaseCommand::class );
} catch ( \Exception $e ) {
// Your command appears to have shit the bed… ¯\_(ツ)_/¯
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.