Skip to content

Instantly share code, notes, and snippets.

@chris-erickson
Last active November 7, 2015 21:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-erickson/bf0ce80d4af59cf80db2 to your computer and use it in GitHub Desktop.
Save chris-erickson/bf0ce80d4af59cf80db2 to your computer and use it in GitHub Desktop.
A helper to notify you when you have brews ready to update
#!/bin/bash
# This script runs the homebrew updater, then reports if any of your packages are out of date
# Generally, this should be run automatically using a launchctl task (see end of file for an example)
#
# In addition, you might want to change the notification style to be an "alert" so it hangs around a while.
# This can be done in System Preferences -> Notification Center (look for homebrew-notifier)
#
# Requires:
# - Homebrew
# - terminal-notifier (brew install terminal-notifier)
# Update this to your liking, based on System Preferences -> Sound -> Sound Effects (case sensitive)
declare -r NOTIFICATION_SOUND="Sosumi"
update_homebrew ()
{
# Check for updated recipes, don't do anything with the output
/usr/local/bin/brew update > /dev/null 2>&1
}
get_installed_package_updates ()
{
# Fetch the packages that are installed and out of date
local updates=`/usr/local/bin/brew outdated`
echo $updates
}
has_updates ()
{
# Check if the passed variable has anything in it
local var=$1
[[ -n $var ]]
}
post_notification ()
{
# Post a notification where the first argument is the message (outdated packages)
local updates=$1
local subtitle=$(echo "Homebrew updates available! 🍺")
/usr/local/bin/terminal-notifier \
-sound $NOTIFICATION_SOUND \
-message "$updates" \
-group homebrew_notifier \
-activate com.apple.Terminal \
-subtitle "$subtitle"
}
main ()
{
update_homebrew
local updates=$(get_installed_package_updates)
has_updates $updates && post_notification "$updates"
}
main
# # Example launchctl xml file
# 1. Replace the "ProgramArguments" array path string to match where you store this script
# 2. Edit the interval as needed (currently it runs every day at 9:00am), many guides and generators exist for this.
# 3. This XML file should be stored in ~/Library/LaunchAgents/ with a name that matches the label (optional)
# So the result file might be: ~/Library/LaunchAgents/local.homebrew-updater.plist
# 4. Then you need to load this into launchctl using:
# $ launchctl load -w ~/Library/LaunchAgents/local.homebrew-updater.plist
#
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
# <key>Label</key>
# <string>local.homebrew-update-notifier</string>
# <key>PATH</key>
# <string>/bin:/usr/bin:/usr/local/bin</string>
# <key>ProgramArguments</key>
# <array>
# <string>sh</string>
# <string>-c</string>
# <string>~/Desktop/brewupdatenotify.sh</string>
# </array>
# <key>StartCalendarInterval</key>
# <array>
# <dict>
# <key>Hour</key>
# <integer>9</integer>
# <key>Minute</key>
# <integer>00</integer>
# </dict>
# </array>
# </dict>
# </plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment