Skip to content

Commit d83784b

Browse files
authored
Merge pull request #6 from gaambo/wp-cli-tasks
WP-CLI Tasks
2 parents 48bd89e + 5c84c76 commit d83784b

File tree

4 files changed

+155
-26
lines changed

4 files changed

+155
-26
lines changed

README.md

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre
1313
- [wp-config.php](#wp-configphp)
1414
- [Rsync filters/excludes/includes](#rsync-filtersexcludesincludes)
1515
- [Tasks](#tasks)
16-
- [Database Tasks (`tasks/database.php`)](#database-tasks-tasksdatabasephp)
17-
- [File Tasks (`tasks/files.php`)](#file-tasks-tasksfilesphp)
18-
- [Theme Tasks (`tasks/theme.php`)](#theme-tasks-tasksthemephp)
19-
- [Uploads Tasks (`tasks/uploads.php`)](#uploads-tasks-tasksuploadsphp)
20-
- [Plugin Tasks (`tasks/plugins.php`)](#plugin-tasks-taskspluginsphp)
21-
- [MU Plugin Tasks (`tasks/mu-plugins.php`)](#mu-plugin-tasks-tasksmu-pluginsphp)
22-
- [WordPress Tasks (`tasks/wp.php`)](#wordpress-tasks-taskswpphp)
23-
- [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp)
16+
- [Database Tasks (`tasks/database.php`)](#database-tasks-tasksdatabasephp)
17+
- [File Tasks (`tasks/files.php`)](#file-tasks-tasksfilesphp)
18+
- [Theme Tasks (`tasks/theme.php`)](#theme-tasks-tasksthemephp)
19+
- [Uploads Tasks (`tasks/uploads.php`)](#uploads-tasks-tasksuploadsphp)
20+
- [Plugin Tasks (`tasks/plugins.php`)](#plugin-tasks-taskspluginsphp)
21+
- [MU Plugin Tasks (`tasks/mu-plugins.php`)](#mu-plugin-tasks-tasksmu-pluginsphp)
22+
- [WordPress Tasks (`tasks/wp.php`)](#wordpress-tasks-taskswpphp)
23+
- [WP-CLI](#wp-cli)
24+
- [Simple Tasks (`tasks/simple.php`)](#simple-tasks-taskssimplephp)
2425
- [Recipes](#recipes)
2526
- [Default aka Vanilla - `deploy.php`](#default-aka-vanilla---deployphp)
2627
- [Custom Theme](#custom-theme)
@@ -29,6 +30,7 @@ A collection of [Deployer](https://deployer.org) Tasks/Recipes to deploy WordPre
2930
- [Custom Theme](#custom-theme-1)
3031
- [Custom MU-Plugin](#custom-mu-plugin-1)
3132
- [Contributing](#contributing)
33+
- [Built by](#built-by)
3234

3335
## Installation
3436

@@ -108,7 +110,7 @@ This prevents any development files/development tools from syncing. I strongly r
108110
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.
109111
You can also run `dep --list` to see all available tasks and their description.
110112

111-
#### Database Tasks (`tasks/database.php`)
113+
### Database Tasks (`tasks/database.php`)
112114

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

120-
#### File Tasks (`tasks/files.php`)
122+
### File Tasks (`tasks/files.php`)
121123

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

125-
#### Theme Tasks (`tasks/theme.php`)
127+
### Theme Tasks (`tasks/theme.php`)
126128

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

138-
#### Uploads Tasks (`tasks/uploads.php`)
140+
### Uploads Tasks (`tasks/uploads.php`)
139141

140142
- `uploads:push`: Push uploads from local to remote
141143
- `uploads:pull`: Pull uploads from remote to local
142144
- `uploads:sync`: Syncs uploads between remote and local
143145
- `uploads:backup:remote`: Backup uploads on remote host and download zip
144146
- `uploads:backup:local`: Backup uploads on localhost
145147

146-
#### Plugin Tasks (`tasks/plugins.php`)
148+
### Plugin Tasks (`tasks/plugins.php`)
147149

148150
- `plugins:push`: Push plugins from local to remote
149151
- `plugins:pull`: Pull plugins from remote to local
150152
- `plugins:sync`: Syncs plugins between remote and local
151153
- `plugins:backup:remote`: Backup plugins on remote host and download zip
152154
- `plugins:backup:local`: Backup plugins on localhost
153155

154-
#### MU Plugin Tasks (`tasks/mu-plugins.php`)
156+
### MU Plugin Tasks (`tasks/mu-plugins.php`)
155157

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

164-
#### WordPress Tasks (`tasks/wp.php`)
166+
### WordPress Tasks (`tasks/wp.php`)
165167

166-
- `wp:install`: Installs WordPress core via WP CLI
168+
- `wp:download-core`: Installs WordPress core via WP CLI
167169
- `wp:push`: Pushes WordPress core files via rsync
168170
- `wp:pull`: Pulls WordPress core files via rsync
169171
- `wp:info`: Runs the --info command via WP CLI - just a helper/test task
170172

171-
#### Simple Tasks (`tasks/simple.php`)
173+
#### WP-CLI
174+
175+
Handling and installing the WP-CLI binary can be done in one of multiple ways:
176+
177+
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).
178+
2. If you want this behaviour (check if installed, else install) but in another path, overwrite the `bin/wp` variable with a function:
179+
```php
180+
set('bin/wp', function() {
181+
if($path = getWPCLIBinary()) {
182+
return $path;
183+
}
184+
return installWPCLI('/usr/local/bin', 'wp', true);
185+
}
186+
```
187+
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.
188+
189+
3. Set the `bin/wp` variable path on the host configuration, if WP-CLI is already installed.
190+
4. Install the WP-CLI binary manually with the `wp:install-wpcli` task and set the path as `/bin/wp` afterwards.
191+
You can pass the installPath, binaryFile and sudo usage via CLI:
192+
`dep wp:install-wpcli production -o installPath='{{deploy_path}}/.bin -o binaryFile=wp -o sudo=true`
193+
194+
See [original PR](https://github.com/gaambo/deployer-wordpress/pull/5) for more information.
195+
196+
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`);
197+
198+
### Simple Tasks (`tasks/simple.php`)
172199

173200
- Contains some overwrites of Deployer default `deploy:*` tasks to be used in a "simple" recipe without release paths. See [Simple Recipe](#simple)
174201

@@ -223,4 +250,8 @@ Installing PHP/composer vendors/dependencies is done on the server. The `mu-plug
223250
## Contributing
224251

225252
If you have feature requests, find bugs or need help just open an issue on [GitHub](https://github.com/gaambo/deployer-wordpress).
226-
Pull requests are always welcome. PSR2 coding standard are used I try to adhere to Deployer best-practices.
253+
Pull requests are always welcome. PSR2 coding standard are used and I try to adhere to Deployer best-practices.
254+
255+
## Built by
256+
257+
[Gaambo](https://github.com/gaambo) and [Contributors](https://github.com/gaambo/deployer-wordpress/graphs/contributors)

src/set.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,29 @@
66

77
namespace Deployer;
88

9+
use function Gaambo\DeployerWordpress\Utils\WPCLI\getWPCLIBinary;
10+
use function \Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI;
11+
912
require_once 'utils/localhost.php';
13+
require_once 'utils/wp-cli.php';
1014

1115
// BINARIES
1216
set('bin/npm', function () {
1317
return locateBinaryPath('npm');
1418
});
1519

20+
// can be overwritten if you eg. use wpcli in a docker container
1621
set('bin/wp', function () {
17-
// can be overwritten if you eg. use wpcli in a docker container
18-
return locateBinaryPath('wp');
22+
if ($path = getWPCLIBinary()) {
23+
return $path;
24+
}
25+
26+
$installPath = '{{deploy_path}}/.dep';
27+
$binaryFile = 'wp-cli.phar';
28+
29+
writeln("WP-CLI binary wasn't found. Installing latest wp-cli to \"$installPath/$binaryFile\".");
30+
31+
installWPCLI($installPath, $binaryFile);
1932
});
2033

2134
set('composer_options', 'install --no-dev');

src/tasks/wp.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55

66
namespace Deployer;
77

8+
use function Gaambo\DeployerWordpress\Utils\WPCLI\installWPCLI;
9+
use function Gaambo\DeployerWordpress\Utils\WPCLI\runCommand;
10+
811
require_once 'utils/files.php';
912
require_once 'utils/localhost.php';
1013
require_once 'utils/rsync.php';
14+
require_once 'utils/wp-cli.php';
1115

1216
/**
13-
* Installs WordPress core via WP CLI
17+
* Downloads WordPress core via WP CLI
1418
* Needs the following variables:
1519
* - deploy_path or release_path: to build remote path
1620
* - bin/wp: WP CLI binary/command to use (has a default)
1721
* - wp/version: WordPress verstion to install
1822
*/
19-
task('wp:install', function () {
20-
$remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath();
21-
run("cd $remotePath && {{bin/wp}} core download --version={{wp/version}}");
23+
task('wp:download-core', function () {
24+
$wpVersion = get('wp/version', 'latest');
25+
runCommand("core download --version=$wpVersion");
2226
})->desc('Installs a WordPress version via WP CLI');
2327

2428
/**
@@ -59,6 +63,18 @@
5963
* - bin/wp: WP CLI binary/command to use (has a default)
6064
*/
6165
task('wp:info', function () {
62-
$remotePath = Gaambo\DeployerWordpress\Utils\Files\getRemotePath();
63-
run("cd $remotePath && {{bin/wp}} --info");
66+
runCommand("--info");
67+
});
68+
69+
/**
70+
* Installs the WP-CLI binary - for usage via CLI
71+
* Pass installPath, binaryFile and sudo via CLI like so:
72+
* `dep wp:install-wpcli production -o installPath=/usr/local/bin -o binaryFile=wp -o sudo=true`
73+
*/
74+
task('wp:install-wpcli', function () {
75+
$installPath = get('installPath');
76+
$binaryFile = get('binaryFile', 'wp-cli.phar');
77+
$sudo = get('sudo', false);
78+
79+
installWPCLI($installPath, $binaryFile, $sudo);
6480
});

src/utils/wp-cli.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Provides helper functions for running composer commands
4+
*/
5+
6+
namespace Gaambo\DeployerWordpress\Utils\WPCLI;
7+
8+
const INSTALLER_DOWNLOAD = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar';
9+
10+
/**
11+
* Runs any WP-CLI command
12+
* Passes on the verbosity flags passed to Deployer CLI
13+
*
14+
* @param string $path Path in which to run WP-CLI command
15+
* @param string $command WP-CLI command to run
16+
* @param string $arguments Command-line arguments to be passed to WP-CLI as a string
17+
* @return string Result/Returned output from CLI
18+
*/
19+
function runCommand(string $command, string $path = '{{deploy_path}}', string $arguments = '') : string
20+
{
21+
return \Deployer\run("cd $path && {{bin/wp}} $command $arguments");
22+
}
23+
24+
/**
25+
* Gets the path to the WP-CLI binary
26+
*
27+
* Uses locateBinaryPath to get a global binary
28+
* or alternatively check in a given path for a file
29+
*
30+
* Default path/file to check is {{deploy_path}}/.dep/wp-cli.phar
31+
* Which is the same as the default path PHPDeployer installs composer if not installed
32+
*
33+
* @param string $path Path in which the WP-CLI binary is stored
34+
* @param string $binaryFile Name of the WP-CLI binary file
35+
* @return string|boolean Path to binary file or false if not found
36+
*/
37+
function getWPCLIBinary($path = '{{deploy_path}}/.dep', $binaryFile = 'wp-cli.phar')
38+
{
39+
if (\Deployer\commandExist('wp')) {
40+
return \Deployer\locateBinaryPath('wp');
41+
}
42+
43+
if (\Deployer\test("[ -f $path/$binaryFile ]")) {
44+
return "{{bin/php}} $path/$binaryFile";
45+
}
46+
return false;
47+
}
48+
49+
/**
50+
* Installs the WP-CLI Binary to a specified installPath
51+
* Allows passing a name for the binary file and using sudo (eg to move to /usr/local/bin)
52+
*
53+
* @param string $installPath
54+
* @param string $binaryName
55+
* @param boolean $sudo
56+
* @return string Path to installed and moved binary file
57+
*/
58+
function installWPCLI($installPath, $binaryName = 'wp-cli.phar', $sudo = false)
59+
{
60+
$sudoCommand = $sudo ? 'sudo ' : '';
61+
62+
\Deployer\run("mkdir -p $installPath");
63+
\Deployer\run("cd $installPath && curl -sS -O " . INSTALLER_DOWNLOAD . " && chmod +x wp-cli.phar");
64+
if ($binaryName !== 'wp-cli.phar') {
65+
\Deployer\run("$sudoCommand mv $installPath/wp-cli.phar $installPath/$binaryName");
66+
}
67+
68+
return "$installPath/$binaryName";
69+
}

0 commit comments

Comments
 (0)