Skip to content

Commit

Permalink
replace brew-cask.rb subcommand with bash shim
Browse files Browse the repository at this point in the history
The `brew-cask` shim finds and executes `lib/brew-cask-cmd.rb`,
but only if Ruby 2.0+ is found.
  • Loading branch information
rolandwalker committed Dec 16, 2014
1 parent 32eca2e commit ba12a2a
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 46 deletions.
169 changes: 169 additions & 0 deletions bin/brew-cask
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#!/bin/bash
#
# brew-cask
#
# bash shim to invoke brew-cask-cmd.rb
#

###
### settings
###

set -e
set -o pipefail
set +o histexpand
set -o nounset
shopt -s nocasematch
shopt -s nullglob
shopt -s dotglob

###
### functions
###

warn () {
local message="$@"
message="${message//\\t/$'\011'}"
message="${message//\\n/$'\012'}"
message="${message%"${message##*[![:space:]]}"}"
printf "%s\n" "$message" 1>&2
}

die () {
warn "$@"
exit 1
}

resolve_dir () {
local resolved="$(for dir; do cd "$dir"; done && /bin/pwd -P)"
if [[ -d "$resolved" ]]; then
printf "%s" "$resolved"
else
die "cannot resolve: '$@'"
fi
}

ensure_dir () {
local dir="$1"
local message
shift

if [ "$#" -gt 0 ]; then
message="$@"
else
message="brew-cask: no such directory: '$dir'"
fi
if ! [[ -d "$dir" ]]; then
die "$message"
fi
}

ensure_file () {
local file="$1"
local message
shift

if [ "$#" -gt 0 ]; then
message="$@"
else
message="brew-cask: no such file: '$file'"
fi
if ! [[ -f "$file" ]]; then
die "$message"
fi
}

ensure_executable () {
local executable="$1"
local message
shift

if [ "$#" -gt 0 ]; then
message="$@"
else
message="brew-cask: no such executable: '$executable'"
fi
if ! [[ -f "$executable" ]] || ! [[ -x "$executable" ]]; then
die "$message"
fi
}

find_ruby_2_plus () {
declare -a rubies
local favorite_ruby="/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby"
local version_str

if [[ -x "$favorite_ruby" ]]; then
printf "%s" "$favorite_ruby"
else
IFS=$'\n' rubies=( $(/usr/bin/type -aP ruby) \
"/usr/local/bin/ruby" \
"$(brew --prefix 2>/dev/null)/bin/ruby" )
for ruby in "${rubies[@]}"; do
version_str="$("$ruby" --version 2>/dev/null)"
if [[ "$version_str" =~ ^ruby.2 ]]; then
printf "%s" "$ruby"
break
fi
done
fi
}

###
### main
###

_brew_cask () {
local script_dir
local symlink_target
local symlink_target_dir
local brewcask_lib_dir
local brewcask_command
local interpreter
declare -a ruby_opts

ruby_opts=( "-W0" "-EUTF-8:UTF-8" )

script_dir="$(resolve_dir "${0%/*}")"
ensure_dir "$script_dir" \
"brew-cask: could not resolve script directory"

# return "." in case we are not a link
symlink_target="$(/usr/bin/readlink "$0" || echo ".")"

# redefine script_dir because we are likely to be a relative link
symlink_target_dir="$(/usr/bin/dirname "$symlink_target")"
script_dir="$(resolve_dir "$script_dir" "$symlink_target_dir")"
ensure_dir "$script_dir" \
"brew-cask: could not resolve script directory"

# The Homebrew install process replaces the lib element below with rubylib
brewcask_lib_dir="$(resolve_dir "$script_dir"/../lib)"
ensure_dir "$brewcask_lib_dir" \
"brew-cask: could not resolve homebrew-cask library directory"

brewcask_command="$brewcask_lib_dir/brew-cask-cmd.rb"
ensure_file "$brewcask_command" \
"brew-cask: could not find brew-cask-cmd.rb in '$brewcask_lib_dir'"

interpreter="$(find_ruby_2_plus)"
ensure_executable "$interpreter" \
"brew-cask: could not find Ruby 2.0 or greater. Try 'brew install ruby'."

exec "$interpreter" "${ruby_opts[@]}" "$brewcask_command" "${@}"
}

###
### initialization
###

unset GEM_HOME
unset GEM_PATH

###
### dispatch
###

_brew_cask "${@:-}"

#
43 changes: 0 additions & 43 deletions bin/brew-cask.rb

This file was deleted.

4 changes: 2 additions & 2 deletions brew-cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class BrewCask < Formula
def install
man1.install 'doc/man/brew-cask.1'
prefix.install 'lib' => 'rubylib'
inreplace 'bin/brew-cask.rb', '/lib', '/rubylib'
inreplace 'bin/brew-cask', '/lib', '/rubylib'

prefix.install 'Casks', 'bin'
(bin+'brew-cask.rb').chmod 0755
(bin+'brew-cask').chmod 0755
end
end
2 changes: 1 addition & 1 deletion doc/hacking.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ alternate version of the `brew-cask` subcommand, by invoking `brew cask`
with fully-qualified paths, like this:

```bash
$ HOMEBREW_BREW_FILE=/usr/local/bin/brew /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby /usr/local/Library/brew.rb /usr/local/bin/brew-cask.rb help
$ HOMEBREW_BREW_FILE=/usr/local/bin/brew /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby /usr/local/Library/brew.rb /usr/local/Library/Taps/caskroom/homebrew-cask/bin/brew-cask-cmd.rb help
```

#### Forcing a Specific Homebrew-cask Subcommand
Expand Down
16 changes: 16 additions & 0 deletions lib/brew-cask-cmd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -W0 -EUTF-8:UTF-8
# encoding: UTF-8

require 'pathname'

# todo remove internal Homebrew dependencies and remove this line
$LOAD_PATH.unshift(File.expand_path('../homebrew-fork/Library/Homebrew', Pathname.new(__FILE__).realpath))
$LOAD_PATH.unshift(File.expand_path('..', Pathname.new(__FILE__).realpath))

# todo remove internal Homebrew dependencies and remove this line
require 'global'

require 'cask'

Cask::CLI.process(ARGV)
exit 0

0 comments on commit ba12a2a

Please sign in to comment.