Skip to content

Commit

Permalink
Ensuring that all commands throw exceptions and return status. (#1625)
Browse files Browse the repository at this point in the history
* Ensuring that all commands throw exceptions and return status.

* Fixing PHPCS issues.
  • Loading branch information
grasmash authored Jun 8, 2017
1 parent 676e6e0 commit 05f81b9
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 58 deletions.
8 changes: 6 additions & 2 deletions src/Robo/BltTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ protected function isCommandDisabled($command) {
* @param string $hook
* The hook name.
*
* @return \Robo\Result|int
* @return int
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function invokeHook($hook) {
if ($this->getConfig()->has("target-hooks.$hook.command")
Expand All @@ -161,7 +163,9 @@ protected function invokeHook($hook) {
->stopOnFail()
->run();

return $result->getExitCode();
if (!$result->wasSuccessful()) {
throw new BltException("Executing target-hook $hook failed.");
}
}
else {
$this->say("Skipped $hook target hook. No hook is defined.");
Expand Down
7 changes: 7 additions & 0 deletions src/Robo/Commands/Acsf/AcsfCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ protected function acsfHooksInitialize() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy ACSF scripts.");
}

$this->say('New "factory-hooks/" directory created in repo root. Please commit this to your project.');

return $result;
Expand All @@ -83,6 +87,9 @@ protected function requireAcsf($acsfVersion) {
->printOutput(TRUE)
->dir($this->getConfigValue('repo.root'))
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to install drupal/acsf package.");
}
}
else {
// @todo revert previous file chanages.
Expand Down
5 changes: 5 additions & 0 deletions src/Robo/Commands/Blt/AliasCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ protected function createNewAlias() {
->append(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

if (!$result->wasSuccessful()) {
throw new BltException("Unable to install BLT alias.");
}

$this->say("<info>Added alias for blt to $config_file.</info>");
$this->say("You may now use the <comment>blt</comment> command from anywhere within a BLT-generated repository.");
$this->say("Restart your terminal session or run <comment>source $config_file</comment> to use the new command.");
Expand Down
5 changes: 5 additions & 0 deletions src/Robo/Commands/Blt/DoctorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Acquia\Blt\Robo\Commands\Blt;

use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -50,6 +51,10 @@ public function doctor() {
$this->executeDoctorOnHost('');
}

if (!$result->wasSuccessful()) {
throw new BltException("Unable to execute the `blt doctor` command.");
}

return $result->getMessage();
}

Expand Down
7 changes: 6 additions & 1 deletion src/Robo/Commands/Deploy/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Acquia\Blt\Robo\Commands\Deploy;

use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -73,13 +74,17 @@ protected function checkDirty($options) {
->printOutput(FALSE)
->interactive(FALSE)
->run();
if (!$options['ignore-dirty'] && !$result->wasSuccessful()) {
throw new BltException("Unable to determine if local git repository is dirty.");
}

$dirty = (bool) $result->getMessage();
if ($dirty) {
if ($options['ignore-dirty']) {
$this->logger->warning("There are uncommitted changes on the source repository.");
}
else {
throw new \Exception("There are uncommitted changes, commit or stash these changes before deploying.");
throw new BltException("There are uncommitted changes, commit or stash these changes before deploying.");
}
}
}
Expand Down
66 changes: 32 additions & 34 deletions src/Robo/Commands/Saml/SimpleSamlPhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;
use Robo\Result;
use Symfony\Component\Console\Helper\FormatterHelper;

/**
Expand Down Expand Up @@ -35,10 +34,6 @@ public function initialize() {
* Initializes SimpleSAMLphp for project.
*
* @command simplesamlphp:init
*
* @return \Robo\Result
*
* @throws \Exception
*/
public function initializeSimpleSamlPhp() {
if (!$this->getInspector()->isSimpleSamlPhpInstalled()) {
Expand All @@ -51,18 +46,12 @@ public function initializeSimpleSamlPhp() {
$this->say('SimpleSAMLphp has already been initialized by BLT.');
}
$this->outputCompleteSetupInstructions();
if (isset($result)) {
return $result;
}
return Result::EXITCODE_OK;
}

/**
* Adds simplesamlphp_auth as a dependency.
*
* @return \Robo\Result
*
* @throws \Exception
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function requireModule() {
$this->say('Adding SimpleSAMLphp Auth module as a dependency...');
Expand All @@ -76,16 +65,14 @@ protected function requireModule() {
->run();

if (!$result->wasSuccessful()) {
throw new \Exception("Unable to install drupal/simplesamlphp_auth package.");
throw new BltException("Unable to install drupal/simplesamlphp_auth package.");
}
}

/**
* Copies configuration templates from SimpleSamlPHP to the repo root.
*
* @command simplesamlphp:config:init
*
* @return \Robo\Result
*/
protected function initializeConfig() {
$destinationDirectory = "{$this->repoRoot}/simplesamlphp/config";
Expand All @@ -97,14 +84,19 @@ protected function initializeConfig() {
->copy("{$this->bltRoot}/scripts/simplesamlphp/acquia_config.php", "${destinationDirectory}/acquia_config.php", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
// @todo Check $result.
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy SimpleSamlPhp config files.");
}

$result = $this->taskWriteToFile("{$this->repoRoot}/simplesamlphp/config/config.php")
$config_file = "{$this->repoRoot}/simplesamlphp/config/config.php";
$result = $this->taskWriteToFile($config_file)
->text("include 'acquia_config.php';")
->append()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
// @todo Check $result.
if (!$result->wasSuccessful()) {
throw new BltException("Unable modify $config_file.");
}

$this->say("Copying config files to {$this->repoRoot}/simplesamlphp/metadata...");
$result = $this->taskFileSystemStack()
Expand All @@ -121,36 +113,37 @@ protected function initializeConfig() {
* Copies custom config files to SimpleSamlPHP in deploy artifact.
*
* @command simplesamlphp:deploy:config
*
* @return \Robo\Result
*/
public function simpleSamlPhpDeployConfig() {
$this->say('Copying config files to the appropriate place in simplesamlphp library in the deploy artifact...');
$result = $this->taskCopyDir(["{$this->repoRoot}/simplesamlphp" => "{$this->repoRoot}/deploy/vendor/simplesamlphp/simplesamlphp"])
->overwrite(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy SimpleSamlPhp files into deployment artifact.");
}

$result = $this->taskFileSystemStack()
->copy("{$this->bltRoot}/scripts/simplesamlphp/gitignore.txt", "{$this->repoRoot}/deploy/vendor/simplesamlphp/simplesamlphp/.gitignore", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy SimpleSamlPhp .gitignore into deployment artifact.");
}
}

/**
* Sets value in project.yml to let targets know simplesamlphp is installed.
*
* @return \Robo\Result
*/
protected function setSimpleSamlPhpInstalled() {
$composerBin = $this->getConfigValue('composer.bin');
$projectConfigFile = $this->getConfigValue('blt.config-files.project');
$this->say("Updating ${projectConfigFile}...");
$project_yml = $this->getConfigValue('blt.config-files.project');
$this->say("Updating ${project_yml}...");

$result = $this->taskExec("{$composerBin}/yaml-cli update:value")
->arg("${projectConfigFile}")
->arg($project_yml)
->arg('simplesamlphp')
->arg('TRUE')
->printOutput(TRUE)
Expand All @@ -159,13 +152,13 @@ protected function setSimpleSamlPhpInstalled() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Unable to update $project_yml.");
}
}

/**
* Creates a symlink from the docroot to the web accessible library dir.
*
* @return \Robo\Result
*/
protected function symlinkDocrootToLibDir() {
$docroot = $this->getConfigValue('docroot');
Expand All @@ -176,28 +169,33 @@ protected function symlinkDocrootToLibDir() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Unable create symlink.");
}
}

/**
* Copies customized config files into vendored SimpleSamlPHP.
*
* @command simplesamlphp:build:config
*
* @return \Robo\Result
*/
public function simpleSamlPhpBuildConfig() {
$this->say('Copying config files to the appropriate place in simplesamlphp library...');
$result = $this->taskCopyDir(["{$this->repoRoot}/simplesamlphp" => "{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp"])
->overwrite(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy configuration into SimpleSamlPhp.");
}

$result = $this->taskFileSystemStack()
->copy("{$this->bltRoot}/scripts/simplesamlphp/gitignore.txt", "{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp/.gitignore", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy .gitignore into SimpleSamlPhp.");
}
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Robo/Commands/Setup/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Acquia\Blt\Robo\Commands\Setup;

use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -80,7 +81,9 @@ protected function setSitePermissions() {
$taskFilesystemStack->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE);
$result = $taskFilesystemStack->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Unable to set permissions for site directories.");
}
}

/**
Expand All @@ -103,15 +106,12 @@ public function build() {
}

if ($this->getConfig()->has('simplesamlphp') && $this->getConfigValue('simplesamlphp')) {
$result = $this->taskExec($this->getConfigValue('composer.bin') . "/blt simplesamlphp:build:config")
->detectInteractive()
->dir($this->getConfigValue('repo.root'))
->run();
$this->invokeCommand('simplesamlphp:build:config');
}

$result = $this->invokeHook("post-setup-build");
$exit_code = $this->invokeHook("post-setup-build");

return $result;
return $exit_code;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Robo/Commands/Setup/CloudHooksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class CloudHooksCommand extends BltTasks {
* @command setup:cloud-hooks
*/
public function copy() {
$destination = $this->getConfigValue('repo.root') . '/hooks';
$this->say("Copying default Acquia cloud hooks into $destination...");
// This WILL overwrite files is source files are newer.
$result = $this->taskCopyDir([
$this->getConfigValue('blt.root') . '/scripts/cloud-hooks/hooks' => $this->getConfigValue('repo.root') . '/hooks',
$this->getConfigValue('blt.root') . '/scripts/cloud-hooks/hooks' => $destination,
])
->run();

Expand Down
7 changes: 6 additions & 1 deletion src/Robo/Commands/Setup/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@ protected function checkFeaturesOverrides() {
->option('bundle', $bundle)
->option('format', 'json');
$result = $task->printOutput(TRUE)->run();

if (!$result->wasSuccessful()) {
throw new BltException("Unable to determine if features in bundle $bundle are overridden.");
}

$output = $result->getMessage();
$features_overridden = preg_match('/(changed|conflicts|added)( *)$/', $output);
if ($features_overridden) {
throw new \Exception("A feature in the $bundle bundle is overridden. You must re-export this feature to incorporate the changes.");
throw new BltException("A feature in the $bundle bundle is overridden. You must re-export this feature to incorporate the changes.");
}
}
}
Expand Down
Loading

0 comments on commit 05f81b9

Please sign in to comment.