Skip to content

Commit

Permalink
Refactoring all commands to throw BltException on failure. (#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
grasmash authored Jun 5, 2017
1 parent a9242f8 commit 519f402
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 73 deletions.
11 changes: 4 additions & 7 deletions src/Robo/BltTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Acquia\Blt\Robo\Common\ArrayManipulator;
use Acquia\Blt\Robo\Common\IO;
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
use Acquia\Blt\Robo\Exceptions\BltException;
use Acquia\Blt\Robo\Inspector\InspectorAwareInterface;
use Acquia\Blt\Robo\Inspector\InspectorAwareTrait;
use Acquia\Blt\Robo\Tasks\LoadTasks;
Expand Down Expand Up @@ -159,7 +160,7 @@ protected function invokeHook($hook) {
->stopOnFail()
->run();

return $result;
return $result->getExitCode();
}
else {
$this->say("Skipped $hook target hook. No hook is defined.");
Expand Down Expand Up @@ -215,26 +216,22 @@ protected function executeCommandInDrupalVm($command) {
* Indicates whether commands should be run in parallel or sequentially.
* Defaults to FALSE.
*
* @return int
* The exit code of the last executed command.
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function executeCommandAgainstFilesets(array $filesets, $command, $parallel = FALSE) {
$exit_code = 0;
foreach ($filesets as $fileset_id => $fileset) {
if (!is_null($fileset) && iterator_count($fileset)) {
$this->say("Iterating over fileset $fileset_id...");
$files = iterator_to_array($fileset);
$result = $this->executeCommandAgainstFiles($files, $command, $parallel);
if (!$result->wasSuccessful()) {
return $result->getExitCode();
throw new BltException("Executing `$command` against $fileset_id returned a non-zero exit code.`");
}
}
else {
$this->logger->info("No files were found in fileset $fileset_id. Skipped.");
}
}

return $exit_code;
}

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

use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function acsfHooksInitialize() {
/**
* Installs drupal/acsf via Composer.
*
* @throws \Exception
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function requireAcsf($acsfVersion) {
$result = $this->taskExec("composer require 'drupal/acsf:{$acsfVersion}'")
Expand All @@ -85,11 +86,9 @@ protected function requireAcsf($acsfVersion) {
}
else {
// @todo revert previous file chanages.
throw new \Exception("Unable to install drupal/acsf package.");
throw new BltException("Unable to install drupal/acsf package.");
}
}

return $result;
}

}
18 changes: 14 additions & 4 deletions src/Robo/Commands/Blt/AliasCommand.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 Tivie\OS\Detector;
use const Tivie\OS\MACOSX;
Expand Down Expand Up @@ -112,7 +113,11 @@ protected function getAliasInfo() {
protected function updateAlias() {
$alias_info = $this->getAliasInfo();
$new_contents = str_replace($alias_info['alias'], $alias_info['canonical_alias'], $alias_info['contents']);
file_put_contents($alias_info['config_file'], $new_contents);
$bytes = file_put_contents($alias_info['config_file'], $new_contents);
if ($bytes) {
throw new BltException("Could not update BLT alias in {$alias_info['config_file']}.");
}

$this->say("<info>The <comment>blt</comment> alias was updated in {$alias_info['config_file']}");
$this->say("Execute <comment>source {$alias_info['config_file']}</comment> to update your terminal session.");
}
Expand Down Expand Up @@ -154,10 +159,15 @@ protected function createOsxBashProfile() {
if ($os_type == MACOSX) {
$user = posix_getpwuid(posix_getuid());
$home_dir = $user['dir'];
if (!file_exists($home_dir . '/.bash_profile')) {
$this->taskFilesystemStack()
->touch($home_dir . '/.bash_profile')
$bash_profile = $home_dir . '/.bash_profile';
if (!file_exists($bash_profile)) {
$result = $this->taskFilesystemStack()
->touch($bash_profile)
->run();

if (!$result->wasSuccessful()) {
throw new BltException("Could not create $bash_profile.");
}
}
}
}
Expand Down
63 changes: 39 additions & 24 deletions src/Robo/Commands/Blt/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Common\ComposerMunge;
use Acquia\Blt\Robo\Common\YamlMunge;
use Acquia\Blt\Robo\Exceptions\BltException;
use Acquia\Blt\Update\Updater;
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -42,19 +43,17 @@ public function initialize() {
* @command internal:create-project
*/
public function createProject() {
$result = $this->cleanUpProjectTemplate();
$result = $this->updateRootProjectFiles();
$result = $this->reInstallComposerPackages();
$result = $this->setProjectName();
$result = $this->initAndCommitRepo();
$this->cleanUpProjectTemplate();
$this->updateRootProjectFiles();
$this->reInstallComposerPackages();
$this->setProjectName();
$this->initAndCommitRepo();
$exit_code = $this->invokeCommand('install-alias');
$this->displayArt();

$this->yell("Your new BLT-based project has been created in {$this->getConfigValue('repo.root')}.");
$this->say("Please continue by following the \"Creating a new project with BLT\" instructions:");
$this->say("<comment>http://blt.readthedocs.io/en/8.x/readme/creating-new-project/</comment>");

return $result->getExitCode();
}

/**
Expand Down Expand Up @@ -82,12 +81,10 @@ public function update() {
* @return \Robo\Result
*/
public function addToProject() {
$result = $this->reInstallComposerPackages();
$this->reInstallComposerPackages();
$this->displayArt();
$this->yell("BLT has been added to your project.");
$this->say("It has added and modified various project files. Please inspect your repository.");

return $result;
}

/**
Expand Down Expand Up @@ -154,8 +151,9 @@ public function initAndCommitRepo() {
->printOutput(FALSE)
->run();

return $result;

if (!$result->wasSuccessful()) {
throw new BltException("Could not initialize new git repository.");
}
}

/**
Expand All @@ -179,7 +177,9 @@ protected function cleanUpProjectTemplate() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Could not remove deprecated files provided by acquia/blt-project.");
}
}

/**
Expand All @@ -196,13 +196,19 @@ protected function reInstallComposerPackages() {
])
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Could not remove Composer files.");
}

$result = $this->taskExecStack()
->dir($this->getConfigValue('repo.root'))
->exec("composer install --no-interaction --prefer-dist --ansi")
->detectInteractive()
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Could not install Composer requirements.");
}
}

/**
Expand All @@ -212,11 +218,9 @@ protected function reInstallComposerPackages() {
*/
protected function updateRootProjectFiles() {
$this->updateSchemaVersionFile();
$result = $this->rsyncTemplate();
$result = $this->mungeComposerJson();
$result = $this->mungeProjectYml();

return $result;
$this->rsyncTemplate();
$this->mungeComposerJson();
$this->mungeProjectYml();
}

/**
Expand Down Expand Up @@ -316,7 +320,9 @@ protected function rsyncTemplate() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Could not rsync files from BLT into your repository.");
}
}

/**
Expand All @@ -329,7 +335,10 @@ protected function mungeComposerJson() {
$project_composer_json = $this->getConfigValue('repo.root') . '/composer.json';
$template_composer_json = $this->getConfigValue('blt.root') . '/template/composer.json';
$munged_json = ComposerMunge::munge($project_composer_json, $template_composer_json);
file_put_contents($project_composer_json, $munged_json);
$bytes = file_put_contents($project_composer_json, $munged_json);
if (!$bytes) {
throw new BltException("Could not update $project_composer_json.");
}
}

/**
Expand All @@ -341,8 +350,12 @@ protected function mungeProjectYml() {
$this->say("Merging BLT's <comment>project.yml</comment> template with your project's <comment>blt/project.yml</comment>...");
// Values in the project's existing project.yml file will be preserved and
// not overridden.
$munged_yaml = YamlMunge::munge($this->getConfigValue('blt.root') . '/template/blt/project.yml', $this->getConfigValue('blt.config-files.project'));
file_put_contents($this->getConfigValue('blt.config-files.project'), $munged_yaml);
$repo_project_yml = $this->getConfigValue('blt.config-files.project');
$munged_yaml = YamlMunge::munge($this->getConfigValue('blt.root') . '/template/blt/project.yml', $repo_project_yml);
$bytes = file_put_contents($this->getConfigValue('blt.config-files.project'), $munged_yaml);
if (!$bytes) {
throw new BltException("Could not update $repo_project_yml.");
}
}

/**
Expand All @@ -356,7 +369,9 @@ protected function setProjectName() {
->exec("{$this->getConfigValue('composer.bin')}/yaml-cli update:value {$this->getConfigValue('blt.config-files.project')} project.machine_name '$project_name'")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
return $result;
if (!$result->wasSuccessful()) {
throw new BltException("Could not set value for project.machine_name in {$this->getConfigValue('blt.config-files.project')}.");
}
}

}
13 changes: 9 additions & 4 deletions src/Robo/Commands/Ci/CiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Acquia\Blt\Robo\Commands\Ci;

use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;

/**
Expand All @@ -22,9 +23,11 @@ public function pipelinesInit() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

$this->say("<info>A pre-configured acquia-pipelines.yml file was copied to your repository root.</info>");
if (!$result->wasSuccessful()) {
throw new BltException("Could not initialize Acquia Pipelines configuration.");
}

return $result;
$this->say("<info>A pre-configured acquia-pipelines.yml file was copied to your repository root.</info>");
}

/**
Expand All @@ -39,9 +42,11 @@ public function travisInit() {
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();

$this->say("<info>A pre-configured .travis.yml file was copied to your repository root.</info>");
if (!$result->wasSuccessful()) {
throw new BltException("Could not initialize Travis CI configuration.");
}

return $result;
$this->say("<info>A pre-configured .travis.yml file was copied to your repository root.</info>");
}

}
28 changes: 28 additions & 0 deletions src/Robo/Commands/Exceptions/BltException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Acquia\Blt\Robo\Exceptions;

/**
* Class BltException
* @package Acquia\Blt\Robo\Exceptions
*/
class BltException extends \Exception {

public function __construct(
$message = "",
$code = 0,
\Throwable $previous = NULL
) {
parent::__construct($message, $code, $previous);

$this->transmitAnalytics();
}

/**
* Transmit anonymous data about Exception.
*/
protected function transmitAnalytics() {
// Create new BltAnalyticsData class.
}

}
4 changes: 1 addition & 3 deletions src/Robo/Commands/Fix/PhpCbfCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function phpcbfFileSet() {

$bin = $this->getConfigValue('composer.bin');
$command = "'$bin/phpcbf' --standard='{$this->standard}' '%s'";
$result = $this->executeCommandAgainstFilesets($filesets, $command);

return $result;
$this->executeCommandAgainstFilesets($filesets, $command);
}

}
Loading

0 comments on commit 519f402

Please sign in to comment.