Skip to content

Commit

Permalink
Create new custom Robo Drush task. (acquia#1413)
Browse files Browse the repository at this point in the history
* New custom Robo drush task.

* Convert AcsfCommand to use new drush task.

* Use protected instead of private properties, create include property.

* Additional fixups from code review.

* Code sniffer fixups.
  • Loading branch information
malikkotob authored and grasmash committed Apr 24, 2017
1 parent b6930e2 commit ea5d4a1
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Robo/BltTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
use Acquia\Blt\Robo\Inspector\InspectorAwareInterface;
use Acquia\Blt\Robo\Inspector\InspectorAwareTrait;
use Acquia\Blt\Robo\Tasks\LoadTasks;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Psr\Log\LoggerAwareInterface;
Expand All @@ -30,6 +31,7 @@ class BltTasks implements ConfigAwareInterface, InspectorAwareInterface, LoggerA
use InspectorAwareTrait;
use IO;
use LoggerAwareTrait;
use LoadTasks;

/**
* The depth of command invokations, used by invokeCommands().
Expand Down
8 changes: 3 additions & 5 deletions src/Robo/Commands/Acsf/AcsfCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ public function acsfInitialize($options = ['acsf-version' => '^1.33.0']) {
* @command acsf:init:drush
*/
public function acsfDrushInitialize() {
$drushBin = $this->getConfigValue('drush.bin');

$this->say('Executing initialization command for acsf module.');

$this->taskExec("{$drushBin} acsf-init --include={$this->getConfigValue('docroot')}/modules/contrib/acsf/acsf_init --yes")
->printOutput(TRUE)
->dir($this->getConfigValue('docroot'))
$this->taskDrush()
->includePath("{$this->getConfigValue('docroot')}/modules/contrib/acsf/acsf_init")
->drush('acsf-init')
->run();

$this->say('Please add acsf_init as a dependency for your installation profile to ensure that it remains enabled.');
Expand Down
248 changes: 248 additions & 0 deletions src/Robo/Tasks/DrushTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php

namespace Acquia\Blt\Robo\Tasks;

use Robo\Task\CommandStack;
use Robo\Contract\VerbosityThresholdInterface;
use Robo\Common\CommandArguments;

/**
* Runs Drush commands in stack. You can use `stopOnFail()` to point that stack
* should be terminated on first fail.
*
* ``` php
* $this->taskDrush()
* ->drush('updb')
* ->drush('cr')
* ->run();
* ```
*/
class DrushTask extends CommandStack {
use CommandArguments;

/**
* Site alias to prepend to each command.
*
* @var string
*/
protected $alias;

/**
* Directory to execute the command from.
*
* @var string
*
* @see ExecTrait::$workingDirectory
*/
protected $dir;

/**
* Site uri to append uri option to each command.
*
* @var string
*/
protected $uri;

/**
* Assume 'yes' or 'no' to all prompts.
*
* @var string|bool
*/
protected $assume;

/**
* Indicates if the command output should be verbose.
*
* @var bool
*/
protected $verbose;

/**
* @var bool
*
* @todo Figure out how to fetch config from constructor to avoid this.
*/
protected $defaultsInitialized;

/**
* Additional directory paths to search for drush commands.
*
* @var string
*/
protected $include;

/**
* Runs the given drush command.
*
* @param string $command
*
* @return $this
*/
public function drush($command) {
// @todo Figure out how to fetch config from constructor to avoid this.
if (!$this->defaultsInitialized) {
$this->init();
}

if ($this->alias) {
$command = "@{$this->alias} {$command}";
}

if (!empty($this->uri)) {
$this->option("uri={$this->uri}");
}

if (isset($this->assume) && is_bool($this->assume)) {
$assumption = $this->assume ? 'yes' : 'no';
$this->option($assumption);
}

if ($this->verbosityThreshold() >= VerbosityThresholdInterface::VERBOSITY_VERBOSE) {
$this->verbose(TRUE);
}

if ($this->verbose) {
$this->option('verbose');
}

if ($this->include) {
$this->option("include={$this->include}");
}

// Add in arguments set via option method and clear for next invocation.
$command = $command . $this->arguments;
$this->arguments = '';

return $this->exec($command)
->dir($this->dir);
}

/**
* Sets the site alias to be used for each command.
*
* @param string $alias
*
* @return $this
*/
public function alias($alias) {
$this->alias = $alias;
return $this;
}

/**
* Sets the site uri to be used for each command.
*
* @param string $uri
*
* @return $this
*/
public function uri($uri) {
$this->uri = $uri;
return $this;
}

/**
* Sets the working directory for each command.
*
* @param string $dir
*
* @return $this
*
* @see ExecTrait::$workingDirectory
*/
public function dir($dir) {
$this->dir = $dir;
parent::dir($dir);
return $this;
}

/**
* Assume 'yes' or 'no' to all prompts.
*
* @param string|bool $assume
*
* @return $this
*/
public function assume($assume) {
if ($assume === "") {
$this->assume = $assume;
}
else {
$this->assume = $this->mixedToBool($assume);
}
return $this;
}

/**
* Indicates if the command output should be verbose.
*
* @param string|bool $verbose
*
* @return $this
*/
public function verbose($verbose) {
$this->verbose = $this->mixedToBool($verbose);
return $this;
}

/**
* Include additional directory paths to search for drush commands.
*
* @param string $include
*
* @return $this
*/
public function includePath($path) {
$this->include = $path;
return $this;
}

/**
* Sets up drush defaults using config.
*/
protected function init() {
$this->executable = $this->getConfig()->get('drush.bin') ?: 'drush';
if (!$this->dir) {
$this->dir($this->getConfig()->get('drush.dir'));
}
if (!$this->uri) {
$this->uri = $this->getConfig()->get('drush.uri');
}
if (!isset($this->assume)) {
$this->assume($this->getConfig()->get('drush.assume'));
}
if (!isset($this->interactive)) {
$interactive = $this->mixedToBool($this->getConfig()->get('drush.passthru'));
$this->interactive($interactive);
}
if (!isset($this->isPrinted)) {
$isPrinted = $this->mixedToBool($this->getConfig()->get('drush.logoutput'));
$this->printOutput($isPrinted);
}
if (!isset($this->verbose)) {
$this->verbose($this->getConfig()->get('drush.verbose'));
}

$this->defaultsInitialized = TRUE;
}

/**
* Helper function to get the boolean equivalent of a variable.
*
* @param mixed $mixedVar
*
* @return bool
* TRUE/FALSE as per PHP's cast to boolean ruleset, with the exception that
* a string value not equal to 'yes' or 'true' will evaluate to FALSE.
*/
protected function mixedToBool($mixedVar) {
if (is_string($mixedVar)) {
$boolVar = ($mixedVar === 'yes' || $mixedVar === 'true');
}
else {
$boolVar = (bool) $mixedVar;
}
return $boolVar;
}

}
17 changes: 17 additions & 0 deletions src/Robo/Tasks/LoadTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Acquia\Blt\Robo\Tasks;

/**
* Load BLT's custom Robo tasks.
*/
trait LoadTasks {

/**
* @return DrushTask
*/
protected function taskDrush() {
return $this->task(DrushTask::class);
}

}

0 comments on commit ea5d4a1

Please sign in to comment.