4 * Add system notifications as a new drush option.
8 * @todo there are no hooks fired after a command errors out.
10 register_shutdown_function('drush_notify_shutdown');
13 * Implements hook_drush_help_alter().
15 function notify_drush_help_alter(&$command) {
16 if ($command['command'] == 'global-options') {
17 // Do not include these in options in standard help.
18 if ($command['#brief'] === FALSE) {
19 $command['options']['notify'] = array(
20 'description' => 'Use system notifications to signal command completion. If set to a number, commands that finish in fewer seconds will not trigger a notification.',
21 'example-value' => 60,
22 'never-propagate' => TRUE,
24 $command['options']['notify-audio'] = array(
25 'description' => 'Trigger an audio alert to signal command completion. If set to a number, commands that finish in fewer seconds will not trigger a notification.',
26 'example-value' => 60,
27 'never-propagate' => TRUE,
29 $command['sub-options']['notify']['notify-cmd'] = array(
30 'description' => 'Specify the shell command to trigger the notification.',
31 'never-propagate' => TRUE,
33 $command['sub-options']['notify']['notify-cmd-audio'] = array(
34 'description' => 'Specify the shell command to trigger the audio notification.',
35 'never-propagate' => TRUE,
42 * Implements hook_drush_help().
44 function notify_drush_help($section) {
46 case 'notify:cache-clear':
47 return dt('Caches have been cleared.');
48 case 'notify:site-install:error':
49 return dt('Failed on site installation');
54 * Shutdown function to signal on errors.
56 function drush_notify_shutdown() {
57 $cmd = drush_get_command();
59 if (empty($cmd['command'])) {
63 // pm-download handles its own notification.
64 if ($cmd['command'] != 'pm-download' && drush_notify_allowed($cmd['command'])) {
65 $msg = dt("Command '!command' completed.", array('!command' => $cmd['command']));
66 drush_notify_send(drush_notify_command_message($cmd['command'], $msg));
69 if (drush_get_option('notify', FALSE) && drush_get_error()) {
70 // If the only error is that notify failed, do not try to notify again.
71 $log = drush_get_error_log();
72 if (count($log) == 1 && array_key_exists('NOTIFY_COMMAND_NOT_FOUND', $log)) {
76 // Send an alert that the command failed.
77 if (drush_notify_allowed($cmd['command'])) {
78 $msg = dt("Command '!command' failed.", array('!command' => $cmd['command']));
79 drush_notify_send(drush_notify_command_message($cmd['command'] . ':error', $msg));
85 * Determine the message to send on command completion.
87 * @param string $command
88 * Name of the Drush command for which we check message overrides.
89 * @param string $default
90 * (Default: NULL) Default message to use if there are not notification message overrides.
93 * Message to use for notification.
95 function drush_notify_command_message($command, $default = NULL) {
96 if ($msg = drush_command_invoke_all('drush_help', 'notify:' . $command)) {
97 $msg = implode("\n", $msg);
100 $msg = $default ? $default : $msg = $command . ': No news is good news.';
107 * Prepares and dispatches notifications to delivery mechanisms.
109 * You may avoid routing a message to secondary messaging mechanisms (e.g. audio),
110 * by direct use of the delivery functions.
113 * Message to send via notification.
115 function drush_notify_send($msg) {
116 drush_notify_send_text($msg);
117 if (drush_get_option('notify-audio', FALSE)) {
118 drush_notify_send_audio($msg);
123 * Send text-based system notification.
125 * This is the automatic, default behavior. It is intended for use with tools
126 * such as libnotify in Linux and Notification Center on OSX.
129 * Message text for delivery.
132 * TRUE on success, FALSE on failure
134 function drush_notify_send_text($msg) {
135 $override = drush_get_option('notify-cmd', FALSE);
137 if (!empty($override)) {
143 $cmd = 'terminal-notifier -message %s -title Drush';
144 $error_message = dt('terminal-notifier command failed. Please install it from https://github.com/alloy/terminal-notifier.');
148 $icon = drush_normalize_path(DRUSH_BASE_PATH . '/drush_logo-black.png');
149 $cmd = "notify-send %s -i $icon";
150 $error_message = dt('notify-send command failed. Please install it as per http://coderstalk.blogspot.com/2010/02/how-to-install-notify-send-in-ubuntu.html.');
155 if (!drush_shell_exec($cmd, $msg)) {
156 return drush_set_error('NOTIFY_COMMAND_NOT_FOUND', $error_message . ' ' . dt('Or you may specify an alternate command to run by specifying --notify-cmd=<my_command>'));
163 * Send an audio-based system notification.
165 * This function is only automatically invoked with the additional use of the
166 * --notify-audio flag or configuration state.
169 * Message for audio recital.
172 * TRUE on success, FALSE on failure
174 function drush_notify_send_audio($msg) {
175 $override = drush_get_option('notify-cmd-audio', FALSE);
177 if (!empty($override)) {
187 $cmd = drush_get_option('notify-cmd-audio', 'spd-say') . ' %s';
191 if (!drush_shell_exec($cmd, $msg)) {
192 return drush_set_error('NOTIFY_COMMAND_NOT_FOUND', dt('The third party notification utility failed.'));
197 * Identify if the given Drush request should trigger a notification.
200 * Name of the command.
205 function drush_notify_allowed($command) {
206 $notify = drush_get_option(array('notify', 'notify-audio'), FALSE);
207 $execution = time() - $_SERVER['REQUEST_TIME'];
209 return ($notify === TRUE ||
210 (is_numeric($notify) && $notify > 0 && $execution > $notify));