Skip to content

Commit ba12a2a

Browse files
committed
replace brew-cask.rb subcommand with bash shim
The `brew-cask` shim finds and executes `lib/brew-cask-cmd.rb`, but only if Ruby 2.0+ is found.
1 parent 32eca2e commit ba12a2a

File tree

5 files changed

+188
-46
lines changed

5 files changed

+188
-46
lines changed

bin/brew-cask

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/bin/bash
2+
#
3+
# brew-cask
4+
#
5+
# bash shim to invoke brew-cask-cmd.rb
6+
#
7+
8+
###
9+
### settings
10+
###
11+
12+
set -e
13+
set -o pipefail
14+
set +o histexpand
15+
set -o nounset
16+
shopt -s nocasematch
17+
shopt -s nullglob
18+
shopt -s dotglob
19+
20+
###
21+
### functions
22+
###
23+
24+
warn () {
25+
local message="$@"
26+
message="${message//\\t/$'\011'}"
27+
message="${message//\\n/$'\012'}"
28+
message="${message%"${message##*[![:space:]]}"}"
29+
printf "%s\n" "$message" 1>&2
30+
}
31+
32+
die () {
33+
warn "$@"
34+
exit 1
35+
}
36+
37+
resolve_dir () {
38+
local resolved="$(for dir; do cd "$dir"; done && /bin/pwd -P)"
39+
if [[ -d "$resolved" ]]; then
40+
printf "%s" "$resolved"
41+
else
42+
die "cannot resolve: '$@'"
43+
fi
44+
}
45+
46+
ensure_dir () {
47+
local dir="$1"
48+
local message
49+
shift
50+
51+
if [ "$#" -gt 0 ]; then
52+
message="$@"
53+
else
54+
message="brew-cask: no such directory: '$dir'"
55+
fi
56+
if ! [[ -d "$dir" ]]; then
57+
die "$message"
58+
fi
59+
}
60+
61+
ensure_file () {
62+
local file="$1"
63+
local message
64+
shift
65+
66+
if [ "$#" -gt 0 ]; then
67+
message="$@"
68+
else
69+
message="brew-cask: no such file: '$file'"
70+
fi
71+
if ! [[ -f "$file" ]]; then
72+
die "$message"
73+
fi
74+
}
75+
76+
ensure_executable () {
77+
local executable="$1"
78+
local message
79+
shift
80+
81+
if [ "$#" -gt 0 ]; then
82+
message="$@"
83+
else
84+
message="brew-cask: no such executable: '$executable'"
85+
fi
86+
if ! [[ -f "$executable" ]] || ! [[ -x "$executable" ]]; then
87+
die "$message"
88+
fi
89+
}
90+
91+
find_ruby_2_plus () {
92+
declare -a rubies
93+
local favorite_ruby="/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby"
94+
local version_str
95+
96+
if [[ -x "$favorite_ruby" ]]; then
97+
printf "%s" "$favorite_ruby"
98+
else
99+
IFS=$'\n' rubies=( $(/usr/bin/type -aP ruby) \
100+
"/usr/local/bin/ruby" \
101+
"$(brew --prefix 2>/dev/null)/bin/ruby" )
102+
for ruby in "${rubies[@]}"; do
103+
version_str="$("$ruby" --version 2>/dev/null)"
104+
if [[ "$version_str" =~ ^ruby.2 ]]; then
105+
printf "%s" "$ruby"
106+
break
107+
fi
108+
done
109+
fi
110+
}
111+
112+
###
113+
### main
114+
###
115+
116+
_brew_cask () {
117+
local script_dir
118+
local symlink_target
119+
local symlink_target_dir
120+
local brewcask_lib_dir
121+
local brewcask_command
122+
local interpreter
123+
declare -a ruby_opts
124+
125+
ruby_opts=( "-W0" "-EUTF-8:UTF-8" )
126+
127+
script_dir="$(resolve_dir "${0%/*}")"
128+
ensure_dir "$script_dir" \
129+
"brew-cask: could not resolve script directory"
130+
131+
# return "." in case we are not a link
132+
symlink_target="$(/usr/bin/readlink "$0" || echo ".")"
133+
134+
# redefine script_dir because we are likely to be a relative link
135+
symlink_target_dir="$(/usr/bin/dirname "$symlink_target")"
136+
script_dir="$(resolve_dir "$script_dir" "$symlink_target_dir")"
137+
ensure_dir "$script_dir" \
138+
"brew-cask: could not resolve script directory"
139+
140+
# The Homebrew install process replaces the lib element below with rubylib
141+
brewcask_lib_dir="$(resolve_dir "$script_dir"/../lib)"
142+
ensure_dir "$brewcask_lib_dir" \
143+
"brew-cask: could not resolve homebrew-cask library directory"
144+
145+
brewcask_command="$brewcask_lib_dir/brew-cask-cmd.rb"
146+
ensure_file "$brewcask_command" \
147+
"brew-cask: could not find brew-cask-cmd.rb in '$brewcask_lib_dir'"
148+
149+
interpreter="$(find_ruby_2_plus)"
150+
ensure_executable "$interpreter" \
151+
"brew-cask: could not find Ruby 2.0 or greater. Try 'brew install ruby'."
152+
153+
exec "$interpreter" "${ruby_opts[@]}" "$brewcask_command" "${@}"
154+
}
155+
156+
###
157+
### initialization
158+
###
159+
160+
unset GEM_HOME
161+
unset GEM_PATH
162+
163+
###
164+
### dispatch
165+
###
166+
167+
_brew_cask "${@:-}"
168+
169+
#

bin/brew-cask.rb

-43
This file was deleted.

brew-cask.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class BrewCask < Formula
1414
def install
1515
man1.install 'doc/man/brew-cask.1'
1616
prefix.install 'lib' => 'rubylib'
17-
inreplace 'bin/brew-cask.rb', '/lib', '/rubylib'
17+
inreplace 'bin/brew-cask', '/lib', '/rubylib'
1818

1919
prefix.install 'Casks', 'bin'
20-
(bin+'brew-cask.rb').chmod 0755
20+
(bin+'brew-cask').chmod 0755
2121
end
2222
end

doc/hacking.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ alternate version of the `brew-cask` subcommand, by invoking `brew cask`
125125
with fully-qualified paths, like this:
126126

127127
```bash
128-
$ 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
128+
$ 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
129129
```
130130

131131
#### Forcing a Specific Homebrew-cask Subcommand

lib/brew-cask-cmd.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -W0 -EUTF-8:UTF-8
2+
# encoding: UTF-8
3+
4+
require 'pathname'
5+
6+
# todo remove internal Homebrew dependencies and remove this line
7+
$LOAD_PATH.unshift(File.expand_path('../homebrew-fork/Library/Homebrew', Pathname.new(__FILE__).realpath))
8+
$LOAD_PATH.unshift(File.expand_path('..', Pathname.new(__FILE__).realpath))
9+
10+
# todo remove internal Homebrew dependencies and remove this line
11+
require 'global'
12+
13+
require 'cask'
14+
15+
Cask::CLI.process(ARGV)
16+
exit 0

0 commit comments

Comments
 (0)