Commit f138f742 authored by Johannes Haseitl's avatar Johannes Haseitl
Browse files

Issue #2130239 by wizonesolutions, derhasi: Advanced options to control cache-clearing behavior?.

commit 749fb992a23d4f5295fa64e840391d4c3cd8077f
Author: Johannes Haseitl <johannes@undpaul.de>
Date:   Sat Mar 15 14:27:27 2014 +0100

    Updated changelog.

commit c7fdbe72770360717c74b67a390f79a34b1c5bf6
Author: Johannes Haseitl <johannes@undpaul.de>
Date:   Sat Mar 15 13:58:32 2014 +0100

    Rename no-cache to no-cache-clear

commit dcc9fc39108ed358eac78bd74a591094887e99c4
Author: Johannes Haseitl <johannes@undpaul.de>
Date:   Fri Mar 14 16:19:35 2014 +0100

    Replaced core commands for enable and disable with temporary master specifics

commit cf0adcaf51aae67f160ab52981f253bc4c15bd91
Author: Johannes Haseitl <johannes@undpaul.de>
Date:   Fri Mar 14 16:18:12 2014 +0100

    Added drush master-pm-disable with no-cache option.

commit ac032f740b32720c81b5eaa12b9ff4518a73860a
Author: Johannes Haseitl <johannes@undpaul.de>
Date:   Fri Mar 14 16:08:00 2014 +0100

    Added master-pm-enable command with --no-cache flag.
parent aae45633
Loading
Loading
Loading
Loading
+1 −1
Changes for CHANGELOG.txt: 1 added line, 1 removed line.
Original line number Diff line number Diff line

master 7.x-2.x-dev
==================
-
#2130239: added no-cache-clear option to 'master-execute'.

master 7.x-2.0-beta3 (2014-Jan-17)
==================================
+9 −3
Changes for master.drush.inc: 9 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Drush implementation for the master module.
 */

require_once 'master.drush.pm.inc';

/**
 * Implements hook_drush_command().
 */
@@ -35,6 +37,7 @@ function master_drush_command() {
        'skip-scope-validation' => 'Skip check on scope specific variable. By default scopes can only be used if there is at least an empty configuration set.',
        'no-disable' => 'Set if no modules shall be disabled.',
        'no-uninstall' => 'Set if no module shall be uninstalled.',
        'no-cache-clear' => 'Set, if flushing caches for should be avoided.',
      ),
      'examples' => array(
        'drush master-execute --scope=local' => 'For the local scope, it enables all master modules and requirements. Disables and uninstalls all redundant modules.',
@@ -97,7 +100,9 @@ function master_drush_command() {
        'drush dl $(drush master-absent --pipe)' => 'Tries to download missing modules from drupal.org - only works for module names matching the project name.',
      ),
    ),
  );
  )
  // Adds the core replacements.
  + _drush_master_pm_command();
}

/**
@@ -157,6 +162,7 @@ function drush_master_execute() {

  $no_disable = drush_get_option('no-disable');
  $no_uninstall = drush_get_option('no-uninstall');
  $no_cache_clear = drush_get_option('no-cache-clear');

  $master_modules = master_get_master_modules($scope);

@@ -178,7 +184,7 @@ function drush_master_execute() {
    drush_print(dt('The following modules will be enabled: !modules', array('!modules' => implode(', ', $status['missing']))));
    if (drush_confirm(dt('Do you really want to continue?'))) {
      foreach ($status['missing'] as $module) {
        drush_invoke_process(drush_sitealias_get_record('@self'), 'pm-enable', array($module));
        drush_invoke_process(drush_sitealias_get_record('@self'), 'master-pm-enable', array($module), array('no-cache-clear' => $no_cache_clear));
      }
    }
    else {
@@ -229,7 +235,7 @@ function drush_master_execute() {
    if (drush_confirm(dt('Do you really want to continue?'))) {
      foreach ($status['redundant'] as $module) {
        // First we disable, then we uninstall.
        drush_invoke_process(drush_sitealias_get_record('@self'), 'pm-disable', array($module));
        drush_invoke_process(drush_sitealias_get_record('@self'), 'master-pm-disable', array($module), array('no-cache-clear' => $no_cache_clear));

        // Uninstall directly to avoid uninstall to break if they need
        // another module for that to be enabled.

master.drush.pm.inc

0 → 100644
+249 −0
Changes for master.drush.pm.inc: 249 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains clones of drush core to disable cache flushing on enabling and
 * disabling modules.
 */

/**
 * Helper to encapsule the drush core clones.
 */
function _drush_master_pm_command() {
  return array(
    'master-pm-enable' => array(
      'description' => 'Enable one or more extensions (modules or themes).',
      'arguments' => array(
        'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to enable all matches.',
      ),
      'options' => array(
        'resolve-dependencies' => 'Attempt to download any missing dependencies. At the moment, only works when the module name is the same as the project name.',
        'skip' => 'Skip automatic downloading of libraries (c.f. devel).',
        'no-cache-clear' => 'Skip cache clear when enabling module',
      ),
      'drush dependencies' => array('pm'),
      // We hide the command, as it is intended for internal use in the first
      // place, only due to the missing --no-cache-clear flag in drush core.
      'hidden' => TRUE,
    ),
    'master-pm-disable' => array(
      'description' => 'Disable one or more extensions (modules or themes).',
      'arguments' => array(
        'extensions' => 'A list of modules or themes. You can use the * wildcard at the end of extension names to disable multiple matches.',
      ),
      'options' => array(
        'no-cache-clear' => 'Skip cache clear when disabling module',
      ),
      // Hide from interface, like master-pm-enable.
      'hidden' => TRUE,
    ),
  );
}


/**
 * Copy of the drush_pm_enable to call drush_master_module_enable() instead.
 *
 * @see drush_pm_enable()
 */
function drush_master_pm_enable() {
  // Get the data built during the validate phase
  $extension_info = drush_get_context('PM_ENABLE_EXTENSION_INFO');
  $modules = drush_get_context('PM_ENABLE_MODULES');
  $themes = drush_get_context('PM_ENABLE_THEMES');

  // Inform the user which extensions will finally be enabled.
  $extensions = array_merge($modules, $themes);
  if (empty($extensions)) {
    return drush_log(dt('There were no extensions that could be enabled.'), 'ok');
  }
  else {
    drush_print(dt('The following extensions will be enabled: !extensions', array('!extensions' => implode(', ', $extensions))));
    if(!drush_confirm(dt('Do you really want to continue?'))) {
      return drush_user_abort();
    }
  }

  // Enable themes.
  if (!empty($themes)) {
    drush_theme_enable($themes);
  }

  // Enable modules and pass dependency validation in form submit.
  // @note: this is the only place the code is different to pm-enable.
  if (!empty($modules)) {
    $no_cache_clear = drush_get_option('no-cache-clear', FALSE);
    drush_master_module_enable($modules, $no_cache_clear);
  }

  // Inform the user of final status.
  $result_extensions = drush_get_named_extensions_list($extensions);
  $problem_extensions = array();
  $role = drush_role_get_class();
  foreach ($result_extensions as $name => $extension) {
    if ($extension->status) {
      drush_log(dt('!extension was enabled successfully.', array('!extension' => $extension->name)), 'ok');
      $perms = $role->getModulePerms($extension->name);
      if (!empty($perms)) {
        drush_print(dt('!extension defines the following permissions: !perms', array('!extension' => $extension->name, '!perms' => implode(', ', $perms))));
      }
    }
    else {
      $problem_extensions[] = $extension->name;
    }
  }
  if (!empty($problem_extensions)) {
    return drush_set_error('DRUSH_PM_ENABLE_EXTENSION_ISSUE', dt('There was a problem enabling !extension.', array('!extension' => implode(',', $problem_extensions))));
  }
  // Return the list of extensions enabled
  return $extensions;
}

/**
 * Helper to enable some modules, with option to avoid cache flush.
 *
 * @param array $modules
 *   Array of module machine names.
 * @param bool $no_cache_clear
 *   Option to disable cache flush. Set to TRUE to avoid cache flushing.
 *
 * @see drush_module_enable().
 */
function drush_master_module_enable($modules, $no_cache_clear = FALSE) {
  module_enable($modules);

  if (empty($no_cache_clear)) {
    // Flush all caches.
    drupal_flush_all_caches();
  }
}

/**
 * Validation callback for master-pm-enable.
 */
function drush_master_pm_enable_validate() {
  $args = func_get_args();
  return call_user_func_array('drush_pm_enable_validate', $args);
}


/**
 * Command callback. Disable one or more extensions.
 */
function drush_master_pm_disable() {
  $args = pm_parse_arguments(func_get_args());

  $extension_info = drush_get_extensions();

  // classify $args in themes, modules or unknown.
  $modules = array();
  $themes = array();
  drush_pm_classify_extensions($args, $modules, $themes, $extension_info);
  $extensions = array_merge($modules, $themes);
  $unknown = array_diff($args, $extensions);

  // Discard and set an error for each unknown extension.
  foreach ($unknown as $name) {
    drush_log('DRUSH_PM_ENABLE_EXTENSION_NOT_FOUND', dt('!extension was not found and will not be disabled.', array('!extension' => $name)), 'warning');
  }

  // Discard already disabled extensions.
  foreach ($extensions as $name) {
    if (!$extension_info[$name]->status) {
      if ($extension_info[$name]->type == 'module') {
        unset($modules[$name]);
      }
      else {
        unset($themes[$name]);
      }
      drush_log(dt('!extension is already disabled.', array('!extension' => $name)), 'ok');
    }
  }

  // Discard default theme.
  if (!empty($themes)) {
    $default_theme = drush_theme_get_default();
    if (in_array($default_theme, $themes)) {
      unset($themes[$default_theme]);
      drush_log(dt('!theme is the default theme and can\'t be disabled.', array('!theme' => $default_theme)), 'ok');
    }
  }

  if (!empty($modules)) {
    // Add enabled dependents to the list of modules to disable.
    $dependents = drush_module_dependents($modules, $extension_info);
    $dependents = array_intersect($dependents, drush_module_list());
    $modules = array_merge($modules, $dependents);

    // Discard required modules.
    $required = drush_drupal_required_modules($extension_info);
    foreach ($required as $module) {
      if (isset($modules[$module])) {
        unset($modules[$module]);
        $info = $extension_info[$module]->info;
        // No message for hidden modules.
        if (!isset($info['hidden'])) {
          $explanation = !empty($info['explanation']) ? ' ' . dt('Reason: !explanation', array('!explanation' => strip_tags($info['explanation'])))  : '';
          drush_log(dt('!module is a required module and can\'t be disabled.', array('!module' => $module)) . $explanation, 'ok');
        }
      }
    }
  }

  // Inform the user which extensions will finally be disabled.
  $extensions = array_merge($modules, $themes);
  if (empty($extensions)) {
    return drush_log(dt('There were no extensions that could be disabled.'), 'ok');
  }
  else {
    drush_print(dt('The following extensions will be disabled: !extensions', array('!extensions' => implode(', ', $extensions))));
    if(!drush_confirm(dt('Do you really want to continue?'))) {
      return drush_user_abort();
    }
  }

  // Disable themes.
  if (!empty($themes)) {
    drush_theme_disable($themes);
  }

  // Disable modules and pass dependency validation in form submit.
  if (!empty($modules)) {
    $no_cache_clear = drush_get_option('no-cache-clear', FALSE);
    drush_master_module_disable($modules, $no_cache_clear);
  }

  // Inform the user of final status.
  $result_extensions = drush_get_named_extensions_list($extensions);
  $problem_extensions = array();
  foreach ($result_extensions as $name => $extension) {
    if (!$extension->status) {
      drush_log(dt('!extension was disabled successfully.', array('!extension' => $extension->name)), 'ok');
    }
    else {
      $problem_extensions[] = $extension->name;
    }
  }
  if (!empty($problem_extensions)) {
    return drush_set_error('DRUSH_PM_DISABLE_EXTENSION_ISSUE', dt('There was a problem disabling !extension.', array('!extension' => implode(',', $problem_extensions))));
  }
}

/**
 * Helper to disable some modules, with option to avoid cache flush.
 *
 * @param array $modules
 *   Array of module machine names.
 * @param bool $no_cache_clear
 *   Option to disable cache flush. Set to TRUE to avoid cache flushing.
 *
 * @see drush_module_disable().
 */
function drush_master_module_disable($modules, $no_cache_clear = FALSE) {
  module_disable($modules);

  if (empty($no_cache_clear)) {
    // Flush all caches.
    drupal_flush_all_caches();
  }
}