5

I want to create a drush command that reinstall some modules. I've done something like this:

  $modules_to_uninstall = array("my_module1", "my_module2", "my_module3", "my_module4", "my_module5");
  foreach($modules_to_uninstall as $name){
    $args = array_merge(array('pm-uninstall') ,array($name));
    call_user_func_array('drush_invoke', $args);
  }

but, when I run the command in drush, it always asks me if I want to uninstall that module.

The following modules will be uninstalled: my_module1
Do you really want to continue? (y/n): y
my_module1 was successfully uninstalled.                                                                                                                                                                     [ok]
The following modules will be uninstalled: my_module2
Do you really want to continue? (y/n): y
my_module2 was successfully uninstalled.

[ok] The following modules will be uninstalled: my_module2 Do you really want to continue? (y/n): y my_module3 was successfully uninstalled. ....

$modules_to_uninstall = array("-y my_module1", "my_module2", "my_module3", "my_module4", "my_module5");
  foreach($modules_to_uninstall as $name){
    $args = array_merge(array('pm-uninstall') ,array($name));
    call_user_func_array('drush_invoke', $args);
  }

I've try those but it's useless.

Some suggestion?

  • I use drush from command line. That was the point. just write down "drush reinstall-modules" and he will reinstall my lists of modules (an array for example) – vtemian Jul 27 '12 at 9:01
  • If you want to enable/disable a list of modules in one command why not write a Bash script(on linux) or batch script(on windows) that executes drush commands ??? – GoodSp33d Jul 27 '12 at 9:02
  • I'm not that familiar with drush's internals, but why don't you just use the -y option? – Letharion Jul 27 '12 at 9:34
7

Drush has a -y option, that accepts all questions with a yes. Use that.

3

For simple operations such as this, calling the drush script with the -y option, as suggested in the other answer, is the best option. If you have need to invoke Drush from php, the best API to use is drush_invoke_process.

$site = array('root' => '/path/to/drupal/root', 'uri' => 'mysite.com');
$modules_to_uninstall = array("my_module1", "my_module2", "my_module3", "my_module4", "my_module5");
$result = drush_invoke_process($site, 'pm-uninstall', $modules_to_uninstall, array('yes' => TRUE));

If you are already running the above code from within a bootstrapped site's environment (called via drush @site php-eval ... or drush @site php-script ...), then you may use '@self' for $site. Note that the --yes option is shown for illustrative purposes (how to pass options); --yes is the default mode for drush_invoke_process.

Using drush_invoke_process will allow you to run subcommands that do not prompt without auto-answering all questions in your script. By default, the code you call runs in 'integrate' mode, which means that the user will see all of the log messages, and error statuses from the subprocess will also be set in the calling process. To allow your script to handle errors itself, and make the subprocess run quietly, then disable integrate mode:

$result = drush_invoke_process($site, 'pm-uninstall', $modules_to_uninstall, array('yes' => TRUE), array('integrate' => FALSE));

Finally, if you do want your subprocess to prompt, you can either call drush_invoke, as you show in your question (runs with all of the options and environment of the calling script), or use drush_invoke_process in 'interactive' mode to isolate the environment (pass in new options) of the subprocess.

$result = drush_invoke_process($site, 'pm-uninstall', $modules_to_uninstall, array(), array('interactive' => TRUE));
0

For script purposes, you can set the context in your script to answer yes.

drush_set_context('DRUSH_AFFIRMATIVE', TRUE);

This would be like running your script with the -y option.

  • Will that make EVERYTHING thing on the drush commands answer yes? – Bruno Vincent Jan 15 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.