sync_enable.drush.inc

  1. 8.0.x examples/sync_enable.drush.inc
  2. 6.x examples/sync_enable.drush.inc
  3. 7.x examples/sync_enable.drush.inc
  4. 5.x examples/sync_enable.drush.inc
  5. master examples/sync_enable.drush.inc

Example "Sync enable" sql-sync command alter.

Sync_enable adds options to sql-sync to enable and disable modules after an sql-sync operation. One use case for this is to use Drush site aliases to automatically enable your development modules whenever you sync from your live site to your dev site. You may also add or remove permissions at the same time.

For example:

$aliases['dev'] = array (
  'root' => '/srv/www/drupal',
  'uri' => 'site.com',
  'target-command-specific' => array(
    'sql-sync'  => array(
      'enable'  => array('devel', 'hacked'),
      'disable' => array('securepages'),
      'permission' => array(
        'authenticated user' => array(
          'add' => array('access devel information', 'access environment indicator'),
          'remove' => 'change own password',
        ),
        'anonymous user' => array(
          'add' => 'access environment indicator',
        ),
      ),
    ),
  ),
);

To use this feature, copy the 'target-command-specific' item from the example alias above, place it in your development site aliases, and customize the development module list to suit. You must also copy the sync_enable.drush.inc file to a location where Drush will find it, such as $HOME/.drush. See `drush topic docs-commands` for more information.

To set variables on a development site:

Instead of calling variable_set and variable_delete in a post-sync hook, consider adding $conf variables to settings.php.

For example:

$conf['error_level'] = 2; error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); $conf['preprocess_css'] = 0; $conf['cache'] = 0; $conf['googleanalytics_account'] = '';

Functions

File

examples/sync_enable.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Example "Sync enable" sql-sync command alter.
  5. *
  6. * Sync_enable adds options to sql-sync to enable and disable
  7. * modules after an sql-sync operation. One use case for this
  8. * is to use Drush site aliases to automatically enable your
  9. * development modules whenever you sync from your live site to
  10. * your dev site. You may also add or remove permissions at
  11. * the same time.
  12. *
  13. * For example:
  14. *
  15. * @code
  16. * $aliases['dev'] = array (
  17. * 'root' => '/srv/www/drupal',
  18. * 'uri' => 'site.com',
  19. * 'target-command-specific' => array(
  20. * 'sql-sync' => array(
  21. * 'enable' => array('devel', 'hacked'),
  22. * 'disable' => array('securepages'),
  23. * 'permission' => array(
  24. * 'authenticated user' => array(
  25. * 'add' => array('access devel information', 'access environment indicator'),
  26. * 'remove' => 'change own password',
  27. * ),
  28. * 'anonymous user' => array(
  29. * 'add' => 'access environment indicator',
  30. * ),
  31. * ),
  32. * ),
  33. * ),
  34. * );
  35. * @endcode
  36. *
  37. * To use this feature, copy the 'target-command-specific'
  38. * item from the example alias above, place it in your development
  39. * site aliases, and customize the development module list
  40. * to suit. You must also copy the sync_enable.drush.inc
  41. * file to a location where Drush will find it, such as
  42. * $HOME/.drush. See `drush topic docs-commands` for more
  43. * information.
  44. *
  45. * To set variables on a development site:
  46. *
  47. * Instead of calling variable_set and variable_delete in a post-sync
  48. * hook, consider adding $conf variables to settings.php.
  49. *
  50. * For example:
  51. *
  52. * $conf['error_level'] = 2;
  53. * error_reporting(E_ALL);
  54. * ini_set('display_errors', TRUE);
  55. * ini_set('display_startup_errors', TRUE);
  56. * $conf['preprocess_css'] = 0;
  57. * $conf['cache'] = 0;
  58. * $conf['googleanalytics_account'] = '';
  59. */
  60. /**
  61. * Implements hook_drush_help_alter().
  62. *
  63. * When a hook extends a command with additional options, it must
  64. * implement help alter and declare the option(s). Doing so will add
  65. * the option to the help text for the modified command, and will also
  66. * allow the new option to be specified on the command line. Without
  67. * this, Drush will fail with an error when a user attempts to use
  68. * the option.
  69. */
  70. function sync_enable_drush_help_alter(&$command) {
  71. if ($command['command'] == 'sql-sync') {
  72. $command['options']['enable'] = "Enable the specified modules in the target database after the sync operation has completed.";
  73. $command['options']['disable'] = "Disable the specified modules in the target database after the sync operation has completed.";
  74. $command['options']['permission'] = "Add or remove permissions from a role in the target database after the sync operation has completed. The value of this option must be an array, so it may only be specified in a site alias record or drush configuration file. See `drush topic docs-example-sync-extension`.";
  75. }
  76. }
  77. /**
  78. * Implements drush_hook_post_COMMAND().
  79. *
  80. * The post hook is only called if the sql-sync operation completes
  81. * without an error. When called, we check to see if the user specified
  82. * any modules to enable/disable. If so, we will call pm-enable/pm-disable on
  83. * each module.
  84. */
  85. function drush_sync_enable_post_sql_sync($source = NULL, $destination = NULL) {
  86. $modules_to_enable = drush_get_option_list('enable');
  87. if (!empty($modules_to_enable)) {
  88. drush_log(dt("Enable !modules post-sql-sync", array('!modules' => implode(',', $modules_to_enable))), 'ok');
  89. drush_invoke_process($destination, 'pm-enable', $modules_to_enable, array('yes' => TRUE));
  90. }
  91. $modules_to_disable = drush_get_option_list('disable');
  92. if (!empty($modules_to_disable)) {
  93. drush_log(dt("Disable !modules post-sql-sync", array('!modules' => implode(',', $modules_to_disable))), 'ok');
  94. drush_invoke_process($destination, 'pm-disable', $modules_to_disable, array('yes' => TRUE));
  95. }
  96. $permissions_table = drush_get_option('permission');
  97. if (!empty($permissions_table)) {
  98. foreach ($permissions_table as $role_name => $actions) {
  99. if (array_key_exists('add', $actions)) {
  100. $permissions_to_add = is_array($actions['add']) ? $actions['add'] : explode(', ', $actions['add']);
  101. foreach ($permissions_to_add as $permission) {
  102. $values = drush_invoke_process($destination, 'role-add-perm', array($role_name, $permission), array(), array('integrate' => TRUE));
  103. }
  104. }
  105. if (array_key_exists('remove', $actions)) {
  106. $permissions_to_remove = is_array($actions['remove']) ? $actions['remove'] : explode(', ', $actions['remove']);
  107. foreach ($permissions_to_remove as $permission) {
  108. $values = drush_invoke_process($destination, 'role-remove-perm', array($role_name, $permission), array(), array('integrate' => TRUE));
  109. }
  110. }
  111. }
  112. }
  113. }