Last active
August 29, 2015 14:08
-
-
Save lavoiesl/eac60efde00c1df49c73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # @link https://gist.github.com/lavoiesl/eac60efde00c1df49c73 | |
| function usage() { | |
| echo "Homebrew manager" | |
| echo "Use dump-merge to save a list of installed brews to Dropbox" | |
| echo "Use install to install missing brews" | |
| echo "Usage: ${0} [--cask] [--directory directory] [--hostname hostname] dump|merge|dump-merge|diff|install" >&2 | |
| exit 255 | |
| } | |
| cask="" | |
| extension="list" | |
| directory="${HOME}/Dropbox/brew-merge" | |
| hostname="" | |
| cmd="" | |
| autoconfirm="" | |
| while [ $# -gt 0 ] | |
| do | |
| case "${1}" in | |
| --cask ) cask="cask"; extension="cask.list" ;; | |
| --directory ) if [ -d "${2}" ]; then directory="${2}"; shift; else echo "--directory argument must be a directory" >&2; exit 1; fi ;; | |
| --hostname ) if [ -n "${2}" ]; then hostname="${2}"; shift; else echo "--hostname argument must not be empty" >&2; exit 1; fi ;; | |
| -y ) autoconfirm="-y" ;; | |
| -* ) echo "$0: error - unrecognized option $1" >&2; exit 1;; | |
| * ) cmd="${1}" ;; | |
| esac | |
| shift | |
| done | |
| function ensure_directory() { | |
| local directory="${1}" | |
| if [ ! -d "$(dirname "${directory}")" ]; then | |
| echo "$(dirname "${directory}") does not exist" >&2 | |
| exit 1 | |
| fi | |
| [ -d "${directory}" ] || mkdir "${directory}" | |
| } | |
| function list_installed() { | |
| if [[ "${cask}" == "cask" ]]; then | |
| brew cask list -1 | |
| else | |
| brew leaves -1 | |
| fi | |
| } | |
| function diff_installed() { | |
| local hostname="${1}" | |
| if [ ! -f "${directory}/${hostname}.${extension}" ]; then | |
| echo "You must run '${0} dump' first." >&2; | |
| exit 255; | |
| fi | |
| list_installed | diff "${directory}/${hostname}.${extension}" - | grep -E '^(?:<|>) ' | |
| } | |
| function prompt_value() { | |
| local prompt="$1 " | |
| local default="$2" | |
| [[ -n "$default" ]] && prompt="$prompt($default) " | |
| read -p "$prompt" reply | |
| [[ -z "$reply" ]] && reply="$default" | |
| if [[ -z "$reply" ]]; then | |
| prompt_value "$prompt" "$default" | |
| exit | |
| fi | |
| echo "$reply" | |
| } | |
| function confirm() { | |
| local prompt="$1" | |
| if [[ -n "$autoconfirm" ]]; then | |
| echo "$prompt (Y/n) y" | |
| else | |
| read -p "$prompt (Y/n) " reply | |
| fi | |
| [[ $reply != "n" ]] | |
| return $? | |
| } | |
| case "${cmd}" in | |
| dump ) | |
| hostname="${hostname:-$(hostname)}" | |
| ensure_directory "${directory}" | |
| count=$(list_installed | tee "${directory}/${hostname}.${extension}" | wc -l) | |
| echo Saved $count formulas to "${directory}/${hostname}.${extension}" | |
| ;; | |
| merge ) | |
| hostname="${hostname:-$(hostname)}" | |
| ensure_directory "${directory}" | |
| if [ ! -f "${directory}/${hostname}.${extension}" ]; then echo "You must run '${0} dump' first." >&2; exit 255; fi | |
| [ -f "${directory}/common.${extension}" ] || touch "${directory}/common.${extension}" | |
| count=$(cat "${directory}/common.${extension}" "${directory}/${hostname}.${extension}" | sort | uniq | tee "${directory}/common.${extension}" | wc -l) | |
| echo Saved $count formulas to "${directory}/common.${extension}" | |
| ;; | |
| dump-merge ) | |
| hostname="${hostname:-$(hostname)}" | |
| $0 --directory "${directory}" --hostname "${hostname}" dump | |
| $0 --directory "${directory}" --hostname "${hostname}" merge | |
| if brew cask 2>&1 >/dev/null; then | |
| $0 --cask --directory "${directory}" --hostname "${hostname}" dump | |
| $0 --cask --directory "${directory}" --hostname "${hostname}" merge | |
| fi | |
| ;; | |
| diff ) | |
| hostname="${hostname:-common}" | |
| diff_installed "${hostname}" | sort | |
| ;; | |
| install ) | |
| hostname="${hostname:-common}" | |
| diff="$(diff_installed "${hostname}")" | |
| missing=$(echo "${diff}" | grep "^< " | sed 's/^< //') | |
| extra=$(echo "${diff}" | grep "^> " | sed 's/^> //') | |
| if [[ -n "${extra}" ]]; then | |
| echo "You have brews that are not yet saved:" | |
| echo "${extra}" | |
| confirm "Continue?" || exit 0 | |
| fi | |
| if [[ -n "${missing}" ]]; then | |
| echo "Installing missing brews:" | |
| echo "${missing}" | xargs echo | |
| confirm "Continue?" || exit 0 | |
| echo "${missing}" | xargs brew $cask install | |
| else | |
| echo "Already up-to-date!" | |
| fi | |
| ;; | |
| *) usage | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment