Skip to content

Commit

Permalink
added WP-CLI util functions and tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
gaambo committed Jan 8, 2021
1 parent 718880d commit ba2334b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre
- [Plugin Tasks (`tasks/plugins.php`)](#plugin-tasks-taskspluginsphp)
- [MU Plugin Tasks (`tasks/mu-plugins.php`)](#mu-plugin-tasks-tasksmu-pluginsphp)
- [WordPress Tasks (`tasks/wp.php`)](#wordpress-tasks-taskswpphp)
- [WP-CLI](#wp-cli)
- [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp)
- [Recipes](#recipes)
- [Default aka Vanilla - `deploy.php`](#default-aka-vanilla---deployphp)
Expand Down Expand Up @@ -163,12 +164,15 @@ You can also run `dep --list` to see all available tasks and their description.

#### WordPress Tasks (`tasks/wp.php`)

- `wp:install-cli`: Installs WP CLI on remote machine
- `wp:install`: Installs WordPress core via WP CLI
- `wp:download-core`: Installs WordPress core via WP CLI
- `wp:push`: Pushes WordPress core files via rsync
- `wp:pull`: Pulls WordPress core files via rsync
- `wp:info`: Runs the --info command via WP CLI - just a helper/test task

##### WP-CLI

You can set the `bin/wp` variable to the existing path of your WP-CLI binary or let the default `bin/wp` function download it to `{{deploy_path}}/.dep/wp-cli.phar`. So without setting the variable the binary will be downloaded on the first usage of WP-CLI. There's a task for downloading core and `--info`. You can generate your own tasks to handle other WP-CLI commands, there's a util function `Gaambo\DeployerWordpress\Utils\WPCLI\runCommand` (`src/utils/wp-cli.php`);

#### Simple Tasks (`tasks/simple.php`)

- Contains some overwrites of Deployer default `deploy:*` tasks to be used in a "simple" recipe without release paths. See [Simple Recipe](#simple)
Expand Down
21 changes: 19 additions & 2 deletions src/set.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@

namespace Deployer;

use function \Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI;

require_once 'utils/localhost.php';
require_once 'utils/wp-cli.php';

// BINARIES
set('bin/npm', function () {
return locateBinaryPath('npm');
});

// can be overwritten if you eg. use wpcli in a docker container
set('bin/wp', function () {
// can be overwritten if you eg. use wpcli in a docker container
return locateBinaryPath('wp');
if (commandExist('wp')) {
return locateBinaryPath('wp');
}

$installPath = '{{deploy_path}}/.dep';
$binaryFile = 'wp-cli.phar';

if (test("[ -f $installPath/$binaryFile ]")) {
return "{{bin/php}} $installPath/$binaryFile";
}


writeln("WP-CLI binary wasn't found. Installing latest wp-cli to \"$installPath/$binaryFile\".");
installWPCLI($installPath, $binaryFile);
return "$installPath/$binaryFile";
});

set('composer_options', 'install --no-dev');
Expand Down
24 changes: 8 additions & 16 deletions src/tasks/wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,23 @@

namespace Deployer;

use function Gaambo\DeployerWordpress\Utils\WPCLI\runCommand;

require_once 'utils/files.php';
require_once 'utils/localhost.php';
require_once 'utils/rsync.php';
require_once 'utils/wp-cli.php';

/**
* Install WP Cli
*/
task('wp:install-cli', function () {
$remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath();
run("cd $remotePath && curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar");
run("chmod +x wp-cli.phar");
run("{{sudo_cmd}} mv wp-cli.phar /usr/local/bin/wp", ['tty' => true] );
})->desc('Installs WP Cli on remote server');

/**
* Installs WordPress core via WP CLI
* Downloads WordPress core via WP CLI
* Needs the following variables:
* - deploy_path or release_path: to build remote path
* - bin/wp: WP CLI binary/command to use (has a default)
* - wp/version: WordPress verstion to install
*/
task('wp:install', function () {
$remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath();
run("cd $remotePath && {{bin/wp}} core download --version={{wp/version}}");
task('wp:download-core', function () {
$wpVersion = get('wp/version', 'latest');
runCommand("core download --version=$wpVersion");
})->desc('Installs a WordPress version via WP CLI');

/**
Expand Down Expand Up @@ -69,6 +62,5 @@
* - bin/wp: WP CLI binary/command to use (has a default)
*/
task('wp:info', function () {
$remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath();
run("cd $remotePath && {{bin/wp}} --info");
runCommand("--info");
});
31 changes: 31 additions & 0 deletions src/utils/wp-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Provides helper functions for running composer commands
*/

namespace Gaambo\DeployerWordpress\Utils\WPCLI;

const INSTALLER_DOWNLOAD = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar';

function installWPCLI($installPath, $binaryName = 'wp-cli.phar')
{
\Deployer\run("mkdir -p $installPath");
\Deployer\run("cd $installPath && curl -sS -O " . INSTALLER_DOWNLOAD . " && chmod +x wp-cli.phar");
if ($binaryName !== 'wp-cli.phar') {
\Deployer\run("mv $installPath/wp-cli.phar $installPath/$binaryName");
}
}

/**
* Runs any WP-CLI command
* Passes on the verbosity flags passed to Deployer CLI
*
* @param string $path Path in which to run WP-CLI command
* @param string $command WP-CLI command to run
* @param string $arguments Command-line arguments to be passed to WP-CLI as a string
* @return string Result/Returned output from CLI
*/
function runCommand(string $command, string $path = '{{deploy_path}}', string $arguments = '') : string
{
return \Deployer\run("cd $path && {{bin/wp}} $command $arguments");
}

0 comments on commit ba2334b

Please sign in to comment.