forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
32eca2e
commit ba12a2a
Showing
5 changed files
with
188 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
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 "${@:-}" | ||
|
||
# |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
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 |