Skip to content

Commit 85826fa

Browse files
authored
Remove DRUSH_EXECUTION_COMPLETED and DRUSH_EXIT_CODE handling (#3780)
* Drush no longer cares about DRUSH_EXECUTION_COMPLETED and DRUSH_EXIT_CODE * Update test. * Bring back handling of exit() during termination * Remove drush_return_status(). * Bring back drush_return_status(). It changes exit code based on drush_get_error() Also mark a few context methods as deprecated * Use new CommandResult class * Use newly released Annotated Command.
1 parent fdaf813 commit 85826fa

File tree

12 files changed

+64
-90
lines changed

12 files changed

+64
-90
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"ext-dom": "*",
3535
"chi-teck/drupal-code-generator": "^1.27.0",
3636
"composer/semver": "^1.4",
37-
"consolidation/annotated-command": "^2.9.1",
37+
"consolidation/annotated-command": "^2.10.0",
3838
"consolidation/config": "^1.1.0",
3939
"consolidation/filter-via-dot-access-data": "^0.4",
4040
"consolidation/output-formatters": "^3.3.1",

includes/context.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ function drush_set_config_special_contexts(&$options) {
200200
*
201201
* @return
202202
* An associative array of the settings specified in the request context.
203+
*
204+
* @deprecated
203205
*/
204206
function drush_set_context($context, $value) {
205207
$cache =& drush_get_context($context);
@@ -235,6 +237,8 @@ function drush_set_context($context, $value) {
235237
* If context is not supplied, the entire context cache will be returned.
236238
* Otherwise only the requested context will be returned.
237239
* If the context does not exist yet, it will be initialized to an empty array.
240+
*
241+
* @deprecated
238242
*/
239243
function &drush_get_context($context = NULL, $default = NULL) {
240244
static $cache = [];
@@ -273,6 +277,8 @@ function drush_set_arguments($arguments) {
273277
* the array will not include the command name.
274278
*
275279
* @see drush_set_arguments()
280+
*
281+
* @deprecated
276282
*/
277283
function drush_get_arguments() {
278284
return drush_get_context('arguments');
@@ -294,6 +300,8 @@ function drush_set_command($command) {
294300

295301
/**
296302
* Return the command being executed.
303+
*
304+
* @deprecated()
297305
*/
298306
function drush_get_command() {
299307
return drush_get_context('command');
@@ -312,6 +320,8 @@ function drush_get_command() {
312320
* Optional. The value to return if the option has not been set
313321
* @param context
314322
* Optional. The context to check for the option. If this is set, only this context will be searched.
323+
*
324+
* @deprecated
315325
*/
316326
function drush_get_option($option, $default = NULL, $context = NULL) {
317327
// Uncomment when fumigating.

includes/environment.inc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ function drush_error_handler($errno, $message, $filename, $line) {
5555
}
5656
}
5757

58-
/**
59-
* Evalute the environment after an abnormal termination and
60-
* see if we can determine any configuration settings that the user might
61-
* want to adjust.
62-
*/
63-
function _drush_postmortem() {
64-
// Make sure that the memory limit has been bumped up from the minimum default value of 32M.
65-
$php_memory_limit = drush_memory_limit();
66-
if (($php_memory_limit > 0) && ($php_memory_limit <= 32*DRUSH_KILOBYTE*DRUSH_KILOBYTE)) {
67-
drush_set_error('DRUSH_MEMORY_LIMIT', dt('Your memory limit is set to !memory_limit; Drush needs as much memory to run as Drupal. !php_ini_msg', ['!memory_limit' => $php_memory_limit / (DRUSH_KILOBYTE*DRUSH_KILOBYTE) . 'M', '!php_ini_msg' => _drush_php_ini_loaded_file_message()]));
68-
}
69-
}
70-
7158
/**
7259
* Converts a Windows path (dir1\dir2\dir3) into a Unix path (dir1/dir2/dir3).
7360
* Also converts a cygwin "drive emulation" path (/cygdrive/c/dir1) into a

includes/preflight.inc

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use Drush\Drush;
9+
use Drush\Runtime\Runtime;
910

1011
/**
1112
* The main Drush function.
@@ -18,21 +19,6 @@ function drush_main() {
1819
}
1920

2021
/**
21-
* We set this context to let the shutdown function know we reached the end of drush_main().
22-
*
23-
* @see drush_main()
24-
*/
25-
function drush_postflight() {
26-
drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
27-
}
28-
29-
/**
30-
* Shutdown function for use while Drush and Drupal are bootstrapping and to return any
31-
* registered errors.
32-
*
33-
* The shutdown command checks whether certain options are set to reliably
34-
* detect and log some common Drupal initialization errors.
35-
*
3622
* If the command is being executed with the --backend option, the script
3723
* will return a json string containing the options and log information
3824
* used by the script.
@@ -46,21 +32,8 @@ function drush_shutdown() {
4632
return;
4733
}
4834

49-
// Mysteriously make $user available during sess_write(). Avoids a NOTICE.
50-
global $user;
51-
52-
if (!drush_get_context('DRUSH_EXECUTION_COMPLETED', FALSE) && !drush_get_context('DRUSH_USER_ABORT', FALSE)) {
53-
$php_error_message = '';
54-
if ($error = error_get_last()) {
55-
$php_error_message = "\n" . dt('Error: !message in !file, line !line', ['!message' => $error['message'], '!file' => $error['file'], '!line' => $error['line']]);
56-
}
57-
// We did not reach the end of the drush_main function,
58-
// this generally means somewhere in the code a call to exit(),
59-
// was made. We catch this, so that we can trigger an error in
60-
// those cases.
61-
drush_set_error("DRUSH_NOT_COMPLETED", dt("Drush command terminated abnormally due to an unrecoverable error.!message", ['!message' => $php_error_message]));
62-
// Attempt to give the user some advice about how to fix the problem
63-
_drush_postmortem();
35+
if (!Drush::config()->get(Runtime::DRUSH_RUNTIME_COMPLETED_NAMESPACE)) {
36+
throw new RuntimeException('Drush command terminated abnormally. Check for an exit() in your Drupal site.');
6437
}
6538

6639
if (Drush::backend()) {
@@ -108,6 +81,9 @@ function drush_coverage_shutdown() {
10881
}
10982
}
11083

84+
/**
85+
* @deprecated. This function will be removed in Drush 10. Throw an exception to indicate an error.
86+
*/
11187
function drush_return_status() {
11288
// If a specific exit code was set, then use it.
11389
$exit_code = drush_get_context('DRUSH_EXIT_CODE');

scenarios/phpunit4/composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@
3434
"ext-dom": "*",
3535
"chi-teck/drupal-code-generator": "^1.27.0",
3636
"composer/semver": "^1.4",
37-
"consolidation/annotated-command": "^2.9.1",
37+
"consolidation/annotated-command": "^2.10.0",
3838
"consolidation/config": "^1.1.0",
3939
"consolidation/filter-via-dot-access-data": "^0.4",
4040
"consolidation/output-formatters": "^3.3.1",
4141
"consolidation/robo": "^1.1.5",
42-
"consolidation/site-alias": "^1.1.6",
42+
"consolidation/site-alias": "^1.1.6|^2",
4343
"consolidation/site-process": "^0.1.13",
4444
"grasmash/yaml-expander": "^1.1.1",
4545
"league/container": "~2",
4646
"psr/log": "~1.0",
4747
"psy/psysh": "~0.6",
48-
"symfony/config": "^3.4",
4948
"symfony/console": "^3.4",
5049
"symfony/event-dispatcher": "^3.4",
51-
"symfony/finder": "^3.4",
50+
"symfony/finder": "^3.4 || ^4.0",
5251
"symfony/process": "^3.4",
5352
"symfony/var-dumper": "^3.4 || ^4.0",
5453
"symfony/yaml": "^3.4",

src/Commands/DrushCommands.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ abstract class DrushCommands implements IOAwareInterface, LoggerAwareInterface,
1818
const REQ=InputOption::VALUE_REQUIRED;
1919
const OPT=InputOption::VALUE_OPTIONAL;
2020

21+
// Common exit codes.
22+
const EXIT_SUCCESS = 0;
23+
const EXIT_FAILURE = 1;
24+
2125
use LoggerAwareTrait;
2226
use ConfigAwareTrait {
2327
// Move aside this method so we can replace. See https://stackoverflow.com/a/37687295.

src/Commands/core/UpdateDBCommands.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,14 @@ public function updatedb($options = ['cache-clear' => true, 'entity-updates' =>
6969
// Caches were just cleared in updateFinished callback.
7070
}
7171

72-
if (!$success) {
73-
drush_set_context('DRUSH_EXIT_CODE', DRUSH_FRAMEWORK_ERROR);
74-
}
75-
7672
$level = $success ? ConsoleLogLevel::SUCCESS : LogLevel::ERROR;
7773
$this->logger()->log($level, dt('Finished performing updates.'));
7874
} else {
7975
$this->logger()->success(dt('No pending updates.'));
76+
$success = true;
8077
}
78+
79+
return $success ? DRUSH_SUCCESS : DRUSH_FRAMEWORK_ERROR;
8180
}
8281

8382
/**

src/Commands/pm/SecurityUpdateCommands.php

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
namespace Drush\Commands\pm;
33

4-
use Composer\Semver\Comparator;
54
use Composer\Semver\Semver;
6-
use Consolidation\AnnotatedCommand\CommandData;
5+
use Consolidation\AnnotatedCommand\CommandResult;
76
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
87
use Drush\Commands\DrushCommands;
98
use Drush\Drush;
@@ -15,12 +14,6 @@
1514
*/
1615
class SecurityUpdateCommands extends DrushCommands
1716
{
18-
19-
/**
20-
* @var array
21-
*/
22-
protected $securityUpdates;
23-
2417
/**
2518
* Check Drupal Composer packages for pending security updates.
2619
*
@@ -45,37 +38,30 @@ class SecurityUpdateCommands extends DrushCommands
4538
*/
4639
public function security()
4740
{
48-
$this->securityUpdates = [];
4941
$security_advisories_composer_json = $this->fetchAdvisoryComposerJson();
5042
$composer_lock_data = $this->loadSiteComposerLock();
51-
$this->registerAllSecurityUpdates($composer_lock_data, $security_advisories_composer_json);
52-
if ($this->securityUpdates) {
53-
// @todo Modernize.
54-
drush_set_context('DRUSH_EXIT_CODE', DRUSH_FRAMEWORK_ERROR);
55-
$result = new RowsOfFields($this->securityUpdates);
56-
return $result;
43+
$updates = $this->calculateSecurityUpdates($composer_lock_data, $security_advisories_composer_json);
44+
if ($updates) {
45+
$this->suggestComposerCommand($updates);
46+
return CommandResult::dataWithExitCode(new RowsOfFields($updates), self::EXIT_FAILURE);
5747
} else {
5848
$this->logger()->success("<info>There are no outstanding security updates for Drupal projects.</info>");
5949
}
6050
}
6151

6252
/**
6353
* Emit suggested Composer command for security updates.
64-
*
65-
* @hook post-command pm:security
6654
*/
67-
public function suggestComposerCommand($result, CommandData $commandData)
55+
public function suggestComposerCommand($updates)
6856
{
69-
if (!empty($this->securityUpdates)) {
70-
$suggested_command = 'composer require ';
71-
foreach ($this->securityUpdates as $package) {
72-
$suggested_command .= $package['name'] . ' ';
73-
}
74-
$suggested_command .= '--update-with-dependencies';
75-
$this->logger()->warning("One or more of your dependencies has an outstanding security update. Please apply update(s) immediately.");
76-
$this->logger()->notice("Try running: <comment>$suggested_command</comment>");
77-
$this->logger()->notice("If that fails due to a conflict then you must update one or more root dependencies.");
57+
$suggested_command = 'composer require ';
58+
foreach ($updates as $package) {
59+
$suggested_command .= $package['name'] . ' ';
7860
}
61+
$suggested_command .= '--update-with-dependencies';
62+
$this->logger()->warning('One or more of your dependencies has an outstanding security update.');
63+
$this->logger()->notice("Try running: <comment>$suggested_command</comment>");
64+
$this->logger()->notice("If that fails due to a conflict then you must update one or more root dependencies.");
7965
}
8066

8167
/**
@@ -128,24 +114,29 @@ protected function loadSiteComposerLock()
128114
}
129115

130116
/**
131-
* Register all available security updates in $this->securityUpdates.
117+
* Return available security updates.
118+
*
132119
* @param array $composer_lock_data
133120
* The contents of the local Drupal application's composer.lock file.
134121
* @param array $security_advisories_composer_json
135122
* The composer.json array from drupal-security-advisories.
123+
*
124+
* @return array
136125
*/
137-
protected function registerAllSecurityUpdates($composer_lock_data, $security_advisories_composer_json)
126+
protected function calculateSecurityUpdates($composer_lock_data, $security_advisories_composer_json)
138127
{
128+
$updates = [];
139129
$both = array_merge($composer_lock_data['packages-dev'], $composer_lock_data['packages']);
140130
$conflict = $security_advisories_composer_json['conflict'];
141131
foreach ($both as $package) {
142132
$name = $package['name'];
143133
if (!empty($conflict[$name]) && Semver::satisfies($package['version'], $security_advisories_composer_json['conflict'][$name])) {
144-
$this->securityUpdates[$name] = [
134+
$updates[$name] = [
145135
'name' => $name,
146136
'version' => $package['version'],
147137
];
148138
}
149139
}
140+
return $updates;
150141
}
151142
}

src/Drupal/Commands/core/CliCommands.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drush\Psysh\DrushHelpCommand;
99
use Drupal\Component\Assertion\Handle;
1010
use Drush\Psysh\Shell;
11+
use Drush\Runtime\Runtime;
1112
use Psy\Configuration;
1213
use Psy\VersionUpdater\Checker;
1314
use Webmozart\PathUtil\Path;
@@ -67,7 +68,7 @@ public function cli(array $options = ['version-history' => false, 'cwd' => null]
6768
// PsySH will never return control to us, but our shutdown handler will still
6869
// run after the user presses ^D. Mark this command as completed to avoid a
6970
// spurious error message.
70-
drush_set_context('DRUSH_EXECUTION_COMPLETED', true);
71+
Runtime::setCompleted();
7172

7273
// Run the terminate event before the shell is run. Otherwise, if the shell
7374
// is forking processes (the default), any child processes will close the

src/Runtime/RedispatchHook.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,12 @@ protected function alterArgsForRedispatch($redispatchArgs)
116116
*/
117117
protected function exitEarly($exit_code)
118118
{
119-
Drush::logger()->log(LogLevel::DEBUG, 'Redispatch hook exit early');
119+
Drush::logger()->debug('Redispatch hook exit early');
120120

121-
// TODO: This is how Drush exits from redispatch commands today;
122-
// perhaps this could be somewhat improved, though.
123121
// Note that RemoteCommandProxy::execute() is expecting that
124122
// the redispatch() method will not return, so that will need
125123
// to be altered if this behavior is changed.
126-
drush_set_context('DRUSH_EXECUTION_COMPLETED', true);
124+
Runtime::setCompleted();
127125
exit($exit_code);
128126
}
129127
}

0 commit comments

Comments
 (0)