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

Functions

Namesort descending Description
drush_sync_enable_post_sql_sync Implement hook post sql sync.
sync_enable_drush_help_alter Implement hook help alter.

File

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