From b01ce411641c64d36688a58999c3644ab0daf4a7 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Fri, 25 Dec 2015 22:42:32 +0000 Subject: [PATCH] bin/brew: allow writing Homebrew commands in Bash. --- bin/brew | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/bin/brew b/bin/brew index 30d4f9c49b..5a4aef5fbd 100755 --- a/bin/brew +++ b/bin/brew @@ -1,7 +1,10 @@ -#!/bin/sh +#!/bin/bash chdir() { - cd "$@" >/dev/null + cd "$@" >/dev/null || { + echo "Error: failed to cd to " "$@" "!" >&2 + exit 1 + } } # Force UTF-8 to avoid encoding issues for users with broken locale settings. @@ -61,4 +64,30 @@ export HOMEBREW_REPOSITORY export HOMEBREW_LIBRARY export HOMEBREW_CELLAR -exec "$HOMEBREW_RUBY_PATH" -W0 "$BREW_LIBRARY_DIRECTORY/brew.rb" "$@" +for i in "$@" +do + if [[ "$1" = -v ]] + then + shift + set -- "$@" -v + fi + [[ "$i" =~ ^- ]] && continue + HOMEBREW_BASH_COMMAND="$HOMEBREW_LIBRARY/Homebrew/cmd/$i.sh" + break +done + +if [ -n "$HOMEBREW_BASH_COMMAND" ] && [ -x "$HOMEBREW_BASH_COMMAND" ] +then + # source rather than executing directly to ensure the entire file is read into + # memory before it is run. This makes running a Bash script behave more like + # a Ruby script and avoids hard-to-debug issues if the Bash script is updated + # at the same time as being run. + # + # Hide shellcheck complaint: + # shellcheck source=/dev/null + source "$HOMEBREW_BASH_COMMAND" + "$HOMEBREW_COMMAND" "$@" + exit $? +else + exec "$HOMEBREW_RUBY_PATH" -W0 "$HOMEBREW_LIBRARY/brew.rb" "$@" +fi