Skip to content

Instantly share code, notes, and snippets.

@blech75
Last active March 19, 2024 00:50
Show Gist options
  • Save blech75/fd6c36c43057607507e2 to your computer and use it in GitHub Desktop.
Save blech75/fd6c36c43057607507e2 to your computer and use it in GitHub Desktop.
notify-send adapter

notify-send adapter for Mac OS X

This allows you to receive desktop notifications from grunt running inside of a local vagrant box.

How to install

Install grunt-notify and add to dev dependencies

This is done on the guest VM (within the vagrant box).

  • npm install --save-dev grunt-notify

Add config to Gruntfile.js

  • add notify_hooks block:
grunt.initConfig({
    notify_hooks: {
      options: {
        enabled: true,
        success: true
      }
    }
});
  • add grunt.loadNpmTasks('grunt-notify');
  • add grunt.task.run('notify_hooks');

Install vagrant-notify plugin

This is done on your host environment (Mac OS X).

  • vagrant plugin install vagrant-notify

Install terminal-notifier utility

This is done on your host environment (Mac OS X).

  • via homebrew: brew install terminal-notifier
  • via macports: sudo port install terminal-notifier

Install notify-send

This is done on your host environment (Mac OS X).

  • curl -sL http://bit.ly/1Ivuwr5 | bash

Note: That URL redirects to the install.sh script below.

Restart vagrant box

  • vagrant reload

Test it out

Now try running a grunt task.

  • grunt build

You should see an notification on your desktop.

#!/bin/bash
# installer for notify-send script and related files
#
# -JSB / 2015-06-10
# ----
# exit immediately if anything fails
set -o errexit # -e
SCRIPT_LOC="$HOME/bin"
SCRIPT_NAME="notify-send"
SCRIPT_PATH="${SCRIPT_LOC}/${SCRIPT_NAME}"
SCRIPT_DIR="$HOME/.${SCRIPT_NAME}"
SCRIPT_URL="https://gist.githubusercontent.com/blech75/fd6c36c43057607507e2/raw/d48a5738badd821713ee74dce8511483e4ac501e/notify-send"
SUCCESS_IMG="success.png"
FAILURE_IMG="failure.png"
SUCCESS_IMG_PATH="$SCRIPT_DIR/$SUCCESS_IMG"
FAILURE_IMG_PATH="$SCRIPT_DIR/$FAILURE_IMG"
SUCCESS_IMG_DATA="iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQMAAACXljzdAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABlBMVEUAgAD///8UPy9PAAAAAWJLR0QB/wIt3gAAAAd0SU1FB98GCBAoFUnuMCQAAAAcSURBVFjD7cGBAAAAAMOg+VNf4QBVAQAAAAB8BhRQAAEAnyMVAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE1LTA2LTA4VDE2OjQwOjIxLTA0OjAw72UJ+QAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNS0wNi0wOFQxNjo0MDoyMS0wNDowMJ44sUUAAAAASUVORK5CYII="
FAILURE_IMG_DATA="iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQMAAACXljzdAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABlBMVEX/AAD///9BHTQRAAAAAWJLR0QB/wIt3gAAAAd0SU1FB98GCBAoBs1QcfoAAAAcSURBVFjD7cGBAAAAAMOg+VNf4QBVAQAAAAB8BhRQAAEAnyMVAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE1LTA2LTA4VDE2OjQwOjA2LTA0OjAwaOcwCgAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNS0wNi0wOFQxNjo0MDowNi0wNDowMBm6iLYAAAAASUVORK5CYII="
if [[ -a "$SCRIPT_PATH" ]]; then
echo "You already have notify-send installed. Delete $SCRIPT_PATH if you want to download the latest version."
exit 1
else
# download notify-send
echo -n "> Fetching script..."
mkdir -p $SCRIPT_LOC
curl -sL $SCRIPT_URL > $SCRIPT_PATH && echo "saved to ${SCRIPT_PATH}."
chmod +x $SCRIPT_PATH
# create default icons
mkdir -p $SCRIPT_DIR
if [[ -a "$SUCCESS_IMG_PATH" ]]; then
echo "> Found existing 'success' icon. Leaving untouched." >&2
else
echo "$SUCCESS_IMG_DATA" | base64 -D -o $SUCCESS_IMG_PATH
echo "> Created 'success' icon ($SUCCESS_IMG_PATH). Replace with one of your choosing." >&2
fi
if [[ -a "$FAILURE_IMG_PATH" ]]; then
echo "> Found existing 'failure' icon. Leaving untouched." >&2
else
echo "$FAILURE_IMG_DATA" | base64 -D -o $FAILURE_IMG_PATH
echo "> Created 'failure' icon ($FAILURE_IMG_PATH). Replace with one of your choosing." >&2
fi
# detect SCRIPT_LOC in path
IFS=":"
FOUND_PATH=""
for p in $PATH; do
if [[ "$p" = "$SCRIPT_LOC" ]]; then
FOUND_PATH="true"
echo "> Found ${SCRIPT_LOC} in \$PATH. All set!" >&2
fi
done
echo ""
echo "Successfully installed notify-send."
# tell user to adjust $PATH if necessary
if [[ -z "$FOUND_PATH" ]]; then
echo "Now add ${SCRIPT_LOC} to \$PATH so that vagrant-notify can call it."
fi
fi
exit 0
#!/bin/bash
# notify-send adapter for terminal-notify (or growlnotify), as used by
# vagrant-notify <https://github.com/fgrehm/vagrant-notify#os-x>
#
# JSB / 2015-06-09
# ----
### BEGIN: boilerplate bash
# get full path to current script/file
# http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself#comment15185627_4774063
SCRIPT_PATH=$( cd $(dirname $0) ; pwd -P )
SCRIPT_NAME=`basename ${BASH_SOURCE[0]}`
# stop execution if anything fails and exit with non-zero status
set -o errexit # -e
trap "{ echo -e \"\nError: Something went wrong in $SCRIPT_NAME. ($*)\">&2; exit 1; }" ERR
# allow DEBUG env variable to trigger simple bash debugging when it is set to
# anything but 'false'
if [ ${DEBUG:=false} != 'false' ] ; then
# useful for debugging how exactly this script is called.
echo "$*"
echo ""
set -o verbose # -v
set -o xtrace # -x
fi
### END: boilerplate bash
# use terminal-notify for notifications (preferred)
# https://github.com/alloy/terminal-notifier
TERMINAL_NOTIFIER='terminal-notifier'
# if no terminal notifier, try growlnotify
# http://growl.info/downloads#generaldownloads
GROWL_NOTIFY='growlnotify'
# comes from grunt. used to detect success.
SUCCESS_MSG="Done, without errors."
# right now, just holds custom icons
ICON_DIR="$HOME/.notify-send"
# FIXME: allow overrriding filenames somehow. could be something other than
# PNG, for example.
ICON_SUCCESS="success.png"
ICON_FAILURE="failure.png"
# provide a prefix
context=""
# NOTE: grunt-notify thinks we're actually notify-send (due to vagrant-notify's
# use of notify-send) and therefore calls us with args intended for notify-send,
# so we have to jettison all the irrelevant args here.
#
# for more background, take a look at
# https://github.com/fgrehm/vagrant-notify/blob/master/files/notify-send.erb
# and the output of grunt -v to see what is actually passed to this script.
#
# FIXME: better way of detecting grunt-notify's call
if [[ "$1" = "-h" ]]; then
context="Grunt"
# first, grab icon path (the fourth arg passed in)
icon_path="$4"
# then shift args 7 positions over to clear out unused cruft.
shift 7
# grab title, which is now the first arg, and summarily shift args again
title="$1"
shift 1
# at last, the remaining args are the message
message="$*"
else
# remove '--'
shift 1
if [[ $# -gt 1 ]]; then
# grab title, which is now the first arg, and summarily shift args again
title="$1"
shift 1
fi
# at last, the remaining args are the message
message="$*"
fi
# whew. all done. moving on...
# set notification icon to represent pass/fail status
content_img="${ICON_DIR}/${ICON_FAILURE}"
if [[ "$message" = "$SUCCESS_MSG" ]]; then
content_img="${ICON_DIR}/${ICON_SUCCESS}"
fi
# add prefix to title if there's a context
if [[ "$context" ]]; then
title="${context}: ${title}"
fi
## call terminal-notifier if it exists in PATH
if [ `which $TERMINAL_NOTIFIER` ]; then
$TERMINAL_NOTIFIER \
-activate "com.apple.Terminal" \
-sender "com.apple.Terminal" \
-appIcon "file://${icon_path}" \
-title "Vagrant" \
`[[ "$context" = 'Grunt' ]] && echo -e "-contentImage \"file://${content_img}\""` \
-subtitle "${title}" \
-message "${message}"
## else call growlnotify if it exists in PATH
elif [ `which $GROWL_NOTIFY` ]; then
$GROWL_NOTIFY \
-n "Vagrant" \
-t "${title}" \
`[[ "$context" == "Grunt" ]] && echo -e "--image \"${content_img}\""` \
-d "com.apple.Terminal" \
-m "${message}"
else
# no tool? exit with nonzero status to signify failure.
echo "Error: No notifier tool."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment