Skip to content

Commit

Permalink
Merge pull request #6 from gaambo/wp-cli-tasks
Browse files Browse the repository at this point in the history
WP-CLI Tasks
  • Loading branch information
gaambo authored Jan 21, 2021
2 parents 48bd89e + 5c84c76 commit d83784b
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 26 deletions.
67 changes: 49 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre
- [wp-config.php](#wp-configphp)
- [Rsync filters/excludes/includes](#rsync-filtersexcludesincludes)
- [Tasks](#tasks)
- [Database Tasks (`tasks/database.php`)](#database-tasks-tasksdatabasephp)
- [File Tasks (`tasks/files.php`)](#file-tasks-tasksfilesphp)
- [Theme Tasks (`tasks/theme.php`)](#theme-tasks-tasksthemephp)
- [Uploads Tasks (`tasks/uploads.php`)](#uploads-tasks-tasksuploadsphp)
- [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)
- [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp)
- [Database Tasks (`tasks/database.php`)](#database-tasks-tasksdatabasephp)
- [File Tasks (`tasks/files.php`)](#file-tasks-tasksfilesphp)
- [Theme Tasks (`tasks/theme.php`)](#theme-tasks-tasksthemephp)
- [Uploads Tasks (`tasks/uploads.php`)](#uploads-tasks-tasksuploadsphp)
- [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)
- [Custom Theme](#custom-theme)
Expand All @@ -29,6 +30,7 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre
- [Custom Theme](#custom-theme-1)
- [Custom MU-Plugin](#custom-mu-plugin-1)
- [Contributing](#contributing)
- [Built by](#built-by)

## Installation

Expand Down Expand Up @@ -108,7 +110,7 @@ This prevents any development files/development tools from syncing. I strongly r
All tasks reside in the `src/tasks` directory and are documented well. Here's a summary of all tasks - for details (eg required variables/config) see their source.
You can also run `dep --list` to see all available tasks and their description.

#### Database Tasks (`tasks/database.php`)
### Database Tasks (`tasks/database.php`)

- `db:remote:backup`: Backup remote database and download to localhost
- `db:local:backup`: Backup local database and upload to remote host
Expand All @@ -117,12 +119,12 @@ You can also run `dep --list` to see all available tasks and their description.
- `db:push`: Pushes local database to remote host (combines `db:local:backup` and `db:remote:import`)
- `db:pull`: Pulls remote database to localhost (combines `db:remote:backup` and `db:local:import`)

#### File Tasks (`tasks/files.php`)
### File Tasks (`tasks/files.php`)

- `files:push`: Pushes all files from local to remote host (combines `wp:push`, `uploads:push`, `plugins:push`, `mu-plugins:push`, `themes:push`)
- `files:pull`: Pulls all files from remote to local host (combines `wp:pull`, `uploads:pull`, `plugins:pull`, `mu-plugins:pull`, `themes:pull`)

#### Theme Tasks (`tasks/theme.php`)
### Theme Tasks (`tasks/theme.php`)

- `theme:assets:vendors`: Install theme assets vendors/dependencies (npm), can be run locally or remote
- `theme:assets:build`: Run theme assets (npm) build script, can be run locally or remote
Expand All @@ -135,23 +137,23 @@ You can also run `dep --list` to see all available tasks and their description.
- `themes:backup:remote`: Backup themes on remote host and download zip
- `themes:backup:local`: Backup themes on localhost

#### Uploads Tasks (`tasks/uploads.php`)
### Uploads Tasks (`tasks/uploads.php`)

- `uploads:push`: Push uploads from local to remote
- `uploads:pull`: Pull uploads from remote to local
- `uploads:sync`: Syncs uploads between remote and local
- `uploads:backup:remote`: Backup uploads on remote host and download zip
- `uploads:backup:local`: Backup uploads on localhost

#### Plugin Tasks (`tasks/plugins.php`)
### Plugin Tasks (`tasks/plugins.php`)

- `plugins:push`: Push plugins from local to remote
- `plugins:pull`: Pull plugins from remote to local
- `plugins:sync`: Syncs plugins between remote and local
- `plugins:backup:remote`: Backup plugins on remote host and download zip
- `plugins:backup:local`: Backup plugins on localhost

#### MU Plugin Tasks (`tasks/mu-plugins.php`)
### MU Plugin Tasks (`tasks/mu-plugins.php`)

- `mu-plugin:vendors`: Install mu-plugin vendors (composer), can be run locally or remote
- `mu-plugin`: A combined tasks - at the moment only runs mu-plugin:vendors task
Expand All @@ -161,14 +163,39 @@ You can also run `dep --list` to see all available tasks and their description.
- `mu-plugins:backup:remote`: Backup mu-plugins on remote host and download zip
- `mu-plugins:backup:local`: Backup mu-plugins on localhost

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

- `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

#### Simple Tasks (`tasks/simple.php`)
#### WP-CLI

Handling and installing the WP-CLI binary can be done in one of multiple ways:

1. The default `bin/wp` in `set.php` checks for a usable WP-CLI binary and if none is found it downloads and installs it to `{{deploy_path}}/.dep/wp-cli.phar` (this path is checked in the future as well).
2. If you want this behaviour (check if installed, else install) but in another path, overwrite the `bin/wp` variable with a function:
```php
set('bin/wp', function() {
if($path = getWPCLIBinary()) {
return $path;
}
return installWPCLI('/usr/local/bin', 'wp', true);
}
```
This would install the WP-CLI binary into `/usr/local/bin` with sudo, since this path is probably in $PATH it's found via `getWPCLIBinary` the next time.

3. Set the `bin/wp` variable path on the host configuration, if WP-CLI is already installed.
4. Install the WP-CLI binary manually with the `wp:install-wpcli` task and set the path as `/bin/wp` afterwards.
You can pass the installPath, binaryFile and sudo usage via CLI:
`dep wp:install-wpcli production -o installPath='{{deploy_path}}/.bin -o binaryFile=wp -o sudo=true`

See [original PR](https://github.com/gaambo/deployer-wordpress/pull/5) for more information.

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 Expand Up @@ -223,4 +250,8 @@ Installing PHP/composer vendors/dependencies is done on the server. The `mu-plug
## Contributing

If you have feature requests, find bugs or need help just open an issue on [GitHub](https://github.com/gaambo/deployer-wordpress).
Pull requests are always welcome. PSR2 coding standard are used I try to adhere to Deployer best-practices.
Pull requests are always welcome. PSR2 coding standard are used and I try to adhere to Deployer best-practices.

## Built by

[Gaambo](https://github.com/gaambo) and [Contributors](https://github.com/gaambo/deployer-wordpress/graphs/contributors)
17 changes: 15 additions & 2 deletions src/set.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@

namespace Deployer;

use function Gaambo\DeployerWordpress\Utils\WPCLI\getWPCLIBinary;
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 ($path = getWPCLIBinary()) {
return $path;
}

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

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

installWPCLI($installPath, $binaryFile);
});

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

namespace Deployer;

use function Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI;
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';

/**
* 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 @@ -59,6 +63,18 @@
* - 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");
});

/**
* Installs the WP-CLI binary - for usage via CLI
* Pass installPath, binaryFile and sudo via CLI like so:
* `dep wp:install-wpcli production -o installPath=/usr/local/bin -o binaryFile=wp -o sudo=true`
*/
task('wp:install-wpcli', function () {
$installPath = get('installPath');
$binaryFile = get('binaryFile', 'wp-cli.phar');
$sudo = get('sudo', false);

installWPCLI($installPath, $binaryFile, $sudo);
});
69 changes: 69 additions & 0 deletions src/utils/wp-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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';

/**
* 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");
}

/**
* Gets the path to the WP-CLI binary
*
* Uses locateBinaryPath to get a global binary
* or alternatively check in a given path for a file
*
* Default path/file to check is {{deploy_path}}/.dep/wp-cli.phar
* Which is the same as the default path PHPDeployer installs composer if not installed
*
* @param string $path Path in which the WP-CLI binary is stored
* @param string $binaryFile Name of the WP-CLI binary file
* @return string|boolean Path to binary file or false if not found
*/
function getWPCLIBinary($path = '{{deploy_path}}/.dep', $binaryFile = 'wp-cli.phar')
{
if (\Deployer\commandExist('wp')) {
return \Deployer\locateBinaryPath('wp');
}

if (\Deployer\test("[ -f $path/$binaryFile ]")) {
return "{{bin/php}} $path/$binaryFile";
}
return false;
}

/**
* Installs the WP-CLI Binary to a specified installPath
* Allows passing a name for the binary file and using sudo (eg to move to /usr/local/bin)
*
* @param string $installPath
* @param string $binaryName
* @param boolean $sudo
* @return string Path to installed and moved binary file
*/
function installWPCLI($installPath, $binaryName = 'wp-cli.phar', $sudo = false)
{
$sudoCommand = $sudo ? 'sudo ' : '';

\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("$sudoCommand mv $installPath/wp-cli.phar $installPath/$binaryName");
}

return "$installPath/$binaryName";
}

0 comments on commit d83784b

Please sign in to comment.